2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/resource"
|
|
|
|
require "inspec/resources/postgres_session"
|
2020-06-29 22:43:10 +00:00
|
|
|
require "inspec/resources/command"
|
2017-07-03 06:10:27 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "Inspec::Resources::PostgresSession" do
|
|
|
|
it "verify postgres_session create_psql_cmd with a basic query" do
|
2020-08-11 14:34:13 +00:00
|
|
|
resource = load_resource("postgres_session", "myuser", "mypass", "127.0.0.1", 5432)
|
2021-06-25 08:29:21 +00:00
|
|
|
_(resource.send(:create_psql_cmd, "SELECT * FROM STUDENTS;", ["testdb"])).must_equal "psql -d postgresql://myuser:mypass@127.0.0.1:5432/testdb -A -t -w -c SELECT\\ \\*\\ FROM\\ STUDENTS\\;"
|
2017-07-03 06:10:27 +00:00
|
|
|
end
|
2019-06-11 22:24:35 +00:00
|
|
|
it "verify postgres_session escaped_query with a complex query" do
|
2020-08-11 14:34:13 +00:00
|
|
|
resource = load_resource("postgres_session", "myuser", "mypass", "127.0.0.1", 5432)
|
2021-06-25 08:29:21 +00:00
|
|
|
_(resource.send(:create_psql_cmd, "SELECT current_setting('client_min_messages')", ["testdb"])).must_equal "psql -d postgresql://myuser:mypass@127.0.0.1:5432/testdb -A -t -w -c SELECT\\ current_setting\\(\\'client_min_messages\\'\\)"
|
2017-07-03 06:10:27 +00:00
|
|
|
end
|
2020-06-29 22:43:10 +00:00
|
|
|
it "verify postgres_session redacts output" do
|
2021-06-25 08:29:21 +00:00
|
|
|
cmd = %q{psql -d postgresql://myuser:mypass@127.0.0.1:5432/testdb -A -t -w -c "SELECT current_setting('client_min_messages')"}
|
|
|
|
options = { redact_regex: %r{(:\/\/[a-z]*:).*(@)} }
|
2020-06-29 22:43:10 +00:00
|
|
|
resource = load_resource("command", cmd, options)
|
|
|
|
|
2021-06-25 08:29:21 +00:00
|
|
|
expected_to_s = %q{Command: `psql -d postgresql://myuser:REDACTED@127.0.0.1:5432/testdb -A -t -w -c "SELECT current_setting('client_min_messages')"`}
|
2020-06-29 22:43:10 +00:00
|
|
|
_(resource.to_s).must_equal(expected_to_s)
|
|
|
|
end
|
2020-08-11 14:34:13 +00:00
|
|
|
it "verify postgres_session works with empty port value" do
|
|
|
|
resource = load_resource("postgres_session", "myuser", "mypass", "127.0.0.1")
|
2021-06-25 08:29:21 +00:00
|
|
|
_(resource.send(:create_psql_cmd, "SELECT * FROM STUDENTS;", ["testdb"])).must_equal "psql -d postgresql://myuser:mypass@127.0.0.1:5432/testdb -A -t -w -c SELECT\\ \\*\\ FROM\\ STUDENTS\\;"
|
2020-08-11 14:34:13 +00:00
|
|
|
end
|
|
|
|
it "verify postgres_session works with empty host and port value" do
|
|
|
|
resource = load_resource("postgres_session", "myuser", "mypass")
|
2021-06-25 08:29:21 +00:00
|
|
|
_(resource.send(:create_psql_cmd, "SELECT * FROM STUDENTS;", ["testdb"])).must_equal "psql -d postgresql://myuser:mypass@localhost:5432/testdb -A -t -w -c SELECT\\ \\*\\ FROM\\ STUDENTS\\;"
|
|
|
|
end
|
|
|
|
it "fails when no user, password" do
|
|
|
|
resource = load_resource("postgres_session", nil, nil, "localhost", 5432)
|
|
|
|
_(resource.resource_failed?).must_equal true
|
|
|
|
_(resource.resource_exception_message).must_equal "Can't run PostgreSQL SQL checks without authentication."
|
2020-08-11 14:34:13 +00:00
|
|
|
end
|
2021-06-03 13:41:25 +00:00
|
|
|
it "fails when no connection established" do
|
|
|
|
resource = load_resource("postgres_session", "postgres", "postgres", "localhost", 5432)
|
2021-08-17 10:59:49 +00:00
|
|
|
_(proc { resource.send(:query, "Select 5;", ["mydatabase"]) }).must_raise Inspec::Exceptions::ResourceFailed
|
2021-06-03 13:41:25 +00:00
|
|
|
end
|
2021-09-16 07:36:05 +00:00
|
|
|
|
|
|
|
it "verify postgres_session create_psql_cmd in socket connection" do
|
|
|
|
resource = load_resource("postgres_session", "myuser", "mypass", "127.0.0.1", 5432, "/var/run/postgresql")
|
|
|
|
_(resource.send(:create_psql_cmd, "SELECT * FROM STUDENTS;", ["testdb"])).must_equal "psql -d postgresql://myuser:mypass@/testdb?host=/var/run/postgresql -A -t -w -c SELECT\\ \\*\\ FROM\\ STUDENTS\\;"
|
|
|
|
end
|
2017-07-03 06:10:27 +00:00
|
|
|
end
|