Merge pull request #5943 from inspec/cw/backport-oracle-empty-query-fix-inspec-4

Backport Oracle session resource fix when empty results and zero rows
This commit is contained in:
Clinton Wolfe 2022-03-23 14:03:23 -04:00 committed by GitHub
commit 814259f3a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 3 deletions

View file

@ -61,9 +61,13 @@ module Inspec::Resources
raise Inspec::Exceptions::ResourceFailed, "Oracle query with errors: #{out}"
else
begin
DatabaseHelper::SQLQueryResult.new(inspec_cmd, parse_csv_result(inspec_cmd.stdout))
rescue
raise Inspec::Exceptions::ResourceFailed, "Oracle query with errors: #{out}"
unless inspec_cmd.stdout.empty?
DatabaseHelper::SQLQueryResult.new(inspec_cmd, parse_csv_result(inspec_cmd.stdout))
else
inspec_cmd.stdout
end
rescue Exception => ex
raise Inspec::Exceptions::ResourceFailed, "Oracle query with exception: #{ex}"
end
end
end

1
test/fixtures/cmd/oracle-empty-result vendored Normal file
View file

@ -0,0 +1 @@
""

View file

@ -54,6 +54,23 @@ describe "Inspec::Resources::OracledbSession" do
_(query.row(1).column("value").value).must_equal "ORCL"
end
it "sqlplus Linux with empty output" do
resource = quick_resource(:oracledb_session, :linux, as_os_user: "OSUSER", as_db_role: "DBA", host: "localhost", service: "ORCL", port: 1527, sqlplus_bin: "/bin/sqlplus") do |cmd|
cmd.strip!
case cmd
when "su - OSUSER -c \"echo 'oracle_query_string'; env ORACLE_SID=ORCL /bin/sqlplus -S / as DBA <<'EOC'\nSET PAGESIZE 32000\nSET FEEDBACK OFF\nSET UNDERLINE OFF\nSELECT NAME AS VALUE FROM v\\$database;\nEXIT\nEOC\"" then
stdout_file "test/fixtures/cmd/oracle-empty-result"
else
raise cmd.inspect
end
end
_(resource.resource_skipped?).must_equal false
query = resource.query("SELECT NAME AS VALUE FROM v$database;")
_(query.size).must_equal 0
_(query.row(1).column("value").value).must_equal ""
end
it "sqlplus Windows" do
resource = quick_resource(:oracledb_session, :windows, user: "USER", password: "password", host: "localhost", service: "ORCL", port: 1527, sqlplus_bin: "C:/sqlplus.exe") do |cmd|
cmd.strip!
@ -88,6 +105,23 @@ describe "Inspec::Resources::OracledbSession" do
_(query.row(1).column("value").value).must_equal "ORCL"
end
it "sqlplus Windows with empty output" do
resource = quick_resource(:oracledb_session, :windows, user: "USER", password: "password", host: "localhost", service: "ORCL", port: 1527, sqlplus_bin: "C:/sqlplus.exe") do |cmd|
cmd.strip!
case cmd
when "@'\nSET PAGESIZE 32000\nSET FEEDBACK OFF\nSET UNDERLINE OFF\nSELECT NAME AS VALUE FROM v$database;\nEXIT\n'@ | C:/sqlplus.exe -S USER/password@localhost:1527/ORCL" then
stdout_file "test/fixtures/cmd/oracle-empty-result"
else
raise cmd.inspect
end
end
_(resource.resource_skipped?).must_equal false
query = resource.query("SELECT NAME AS VALUE FROM v$database;")
_(query.size).must_equal 0
_(query.row(1).column("value").value).must_equal ""
end
it "skipped when sqlplus Windows as_os_user" do
resource = quick_resource(:oracledb_session, :windows, user: "USER", password: "password", host: "localhost", service: "ORCL", port: 1527, sqlplus_bin: "C:/sqlplus.exe", as_os_user: "Administrator")