From f5950f1e5ef8e594a859886c31c6c3cec0257bc7 Mon Sep 17 00:00:00 2001 From: Nikita Mathur Date: Tue, 22 Mar 2022 17:05:29 +0530 Subject: [PATCH] Oracle session resource fix when empty results and zero rows Signed-off-by: Nikita Mathur --- lib/inspec/resources/oracledb_session.rb | 10 ++++-- test/fixtures/cmd/oracle-empty-result | 1 + test/unit/resources/oracledb_session_test.rb | 34 ++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/cmd/oracle-empty-result diff --git a/lib/inspec/resources/oracledb_session.rb b/lib/inspec/resources/oracledb_session.rb index 910b9afad..ed0db6692 100644 --- a/lib/inspec/resources/oracledb_session.rb +++ b/lib/inspec/resources/oracledb_session.rb @@ -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 diff --git a/test/fixtures/cmd/oracle-empty-result b/test/fixtures/cmd/oracle-empty-result new file mode 100644 index 000000000..3cc762b55 --- /dev/null +++ b/test/fixtures/cmd/oracle-empty-result @@ -0,0 +1 @@ +"" \ No newline at end of file diff --git a/test/unit/resources/oracledb_session_test.rb b/test/unit/resources/oracledb_session_test.rb index 19b337663..d1f6146c9 100644 --- a/test/unit/resources/oracledb_session_test.rb +++ b/test/unit/resources/oracledb_session_test.rb @@ -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")