mirror of
https://github.com/inspec/inspec
synced 2024-11-10 07:04:15 +00:00
* Added fatal message check for postgres_session:query method * Added empty new line for the new fixture --------- Signed-off-by: Gokulakrishnan KS <Gokulakrishnan.KS@progress.com> Co-authored-by: Gokulakrishnan KS <Gokulakrishnan.KS@progress.com>
This commit is contained in:
parent
46bd4d4098
commit
0faaeb121a
3 changed files with 16 additions and 1 deletions
|
@ -55,7 +55,7 @@ module Inspec::Resources
|
||||||
psql_cmd = create_psql_cmd(query, db)
|
psql_cmd = create_psql_cmd(query, db)
|
||||||
cmd = inspec.command(psql_cmd, redact_regex: %r{(:\/\/[a-z]*:).*(@)})
|
cmd = inspec.command(psql_cmd, redact_regex: %r{(:\/\/[a-z]*:).*(@)})
|
||||||
out = cmd.stdout + "\n" + cmd.stderr
|
out = cmd.stdout + "\n" + cmd.stderr
|
||||||
if cmd.exit_status != 0 && ( out =~ /could not connect to/ || out =~ /password authentication failed/ ) && out.downcase =~ /error:/
|
if cmd.exit_status != 0 && ( out =~ /could not connect to/ || out =~ /password authentication failed/ ) && (out.downcase =~ /error:/ || out.downcase =~ /fatal:/)
|
||||||
raise Inspec::Exceptions::ResourceFailed, "PostgreSQL connection error: #{out}"
|
raise Inspec::Exceptions::ResourceFailed, "PostgreSQL connection error: #{out}"
|
||||||
elsif cmd.exit_status != 0 && out.downcase =~ /error:/
|
elsif cmd.exit_status != 0 && out.downcase =~ /error:/
|
||||||
Lines.new(out, "PostgreSQL query with error: #{query}", cmd.exit_status)
|
Lines.new(out, "PostgreSQL query with error: #{query}", cmd.exit_status)
|
||||||
|
|
1
test/fixtures/cmd/psql-password-authentication-fatal-error
vendored
Normal file
1
test/fixtures/cmd/psql-password-authentication-fatal-error
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
psql: FATAL: password authentication failed for user "postgres"\n
|
|
@ -74,6 +74,20 @@ describe "Inspec::Resources::PostgresSession" do
|
||||||
_(ex.message).must_include("PostgreSQL connection error")
|
_(ex.message).must_include("PostgreSQL connection error")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "fails when no password authentication fails with fatal message" do
|
||||||
|
resource = quick_resource(:postgres_session, :linux, "postgres", "wrongpassword", "localhost", 5432) do |cmd, opts|
|
||||||
|
cmd.strip!
|
||||||
|
case cmd
|
||||||
|
when ("psql -d postgresql://postgres:wrongpassword@localhost:5432/mydatabase -A -t -w -c Select\\ 5\\;") then
|
||||||
|
result(nil, "test/fixtures/cmd/psql-password-authentication-fatal-error", 1)
|
||||||
|
else
|
||||||
|
raise cmd.inspect
|
||||||
|
end
|
||||||
|
end
|
||||||
|
ex = assert_raises(Inspec::Exceptions::ResourceFailed) { resource.query("Select 5;", ["mydatabase"]) }
|
||||||
|
_(ex.message).must_include("PostgreSQL connection error")
|
||||||
|
end
|
||||||
|
|
||||||
it "returns stderr as output if there is error in the query." do
|
it "returns stderr as output if there is error in the query." do
|
||||||
resource = quick_resource(:postgres_session, :linux, "postgres", "postgres", "localhost", 5432) do |cmd, opts|
|
resource = quick_resource(:postgres_session, :linux, "postgres", "postgres", "localhost", 5432) do |cmd, opts|
|
||||||
cmd.strip!
|
cmd.strip!
|
||||||
|
|
Loading…
Reference in a new issue