summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPete Higgins <pete@peterhiggins.org>2020-12-11 12:22:23 -0800
committerPete Higgins <pete@peterhiggins.org>2020-12-11 12:44:08 -0800
commitce2d4b36cc532732d2631c905f8c7206b496458c (patch)
treead98b9778bd081987bc7943724fd380a6ace82cf
parent2fc41ca52d229f24f3872c68211c13a0a7033c8d (diff)
downloadchef-ce2d4b36cc532732d2631c905f8c7206b496458c.tar.gz
Refactor to separate reporter creation from report sending.
Signed-off-by: Pete Higgins <pete@peterhiggins.org>
-rw-r--r--lib/chef/compliance/reporter/chef_server_automate.rb2
-rw-r--r--lib/chef/compliance/runner.rb26
-rw-r--r--spec/unit/compliance/runner_spec.rb45
3 files changed, 42 insertions, 31 deletions
diff --git a/lib/chef/compliance/reporter/chef_server_automate.rb b/lib/chef/compliance/reporter/chef_server_automate.rb
index be59a4cf69..f0eba27816 100644
--- a/lib/chef/compliance/reporter/chef_server_automate.rb
+++ b/lib/chef/compliance/reporter/chef_server_automate.rb
@@ -7,6 +7,8 @@ class Chef
# Used to send inspec reports to Chef Automate server via Chef Server
#
class ChefServerAutomate < Chef::Compliance::Reporter::Automate
+ attr_reader :url
+
def initialize(opts)
@entity_uuid = opts[:entity_uuid]
@run_id = opts[:run_id]
diff --git a/lib/chef/compliance/runner.rb b/lib/chef/compliance/runner.rb
index 828cce9094..789ff74419 100644
--- a/lib/chef/compliance/runner.rb
+++ b/lib/chef/compliance/runner.rb
@@ -201,14 +201,20 @@ class Chef
}
end
- def send_report(reporter, report)
- logger.info "Reporting to #{reporter}"
+ def send_report(reporter_type, report)
+ logger.info "Reporting to #{reporter_type}"
+ reporter = reporter(reporter_type)
+
+ reporter.send_report(report) if reporter
+ end
+
+ def reporter(reporter_type)
insecure = node["audit"]["insecure"]
run_time_limit = node["audit"]["run_time_limit"]
control_results_limit = node["audit"]["control_results_limit"]
- case reporter
+ case reporter_type
when "chef-automate"
opts = {
entity_uuid: node["chef_guid"],
@@ -218,7 +224,7 @@ class Chef
run_time_limit: run_time_limit,
control_results_limit: control_results_limit,
}
- Chef::Compliance::Reporter::Automate.new(opts).send_report(report)
+ Chef::Compliance::Reporter::Automate.new(opts)
when "chef-server-automate"
url = chef_server_automate_url
if url
@@ -231,18 +237,20 @@ class Chef
run_time_limit: run_time_limit,
control_results_limit: control_results_limit,
}
- Chef::Compliance::Reporter::ChefServerAutomate.new(opts).send_report(report)
+ Chef::Compliance::Reporter::ChefServerAutomate.new(opts)
else
- logger.warn "Unable to determine #{ChefUtils::Dist::Server::PRODUCT} url required by #{Inspec::Dist::PRODUCT_NAME} report collector '#{reporter}'. Skipping..."
+ logger.warn "Unable to determine #{ChefUtils::Dist::Server::PRODUCT} url required by #{Inspec::Dist::PRODUCT_NAME} report collector 'chef-server-automate'. Skipping..."
+ nil
end
when "json-file"
path = node["audit"]["json_file"]["location"]
logger.info "Writing compliance report to #{path}"
- Chef::Compliance::Reporter::JsonFile.new(file: path).send_report(report)
+ Chef::Compliance::Reporter::JsonFile.new(file: path)
when "audit-enforcer"
- Chef::Compliance::Reporter::ComplianceEnforcer.new.send_report(report)
+ Chef::Compliance::Reporter::ComplianceEnforcer.new
else
- logger.warn "#{reporter} is not a supported #{Inspec::Dist::PRODUCT_NAME} report collector"
+ logger.warn "'#{reporter_type}' is not a supported #{Inspec::Dist::PRODUCT_NAME} report collector"
+ nil
end
end
diff --git a/spec/unit/compliance/runner_spec.rb b/spec/unit/compliance/runner_spec.rb
index ddc9bc3c4e..fbd57f6be9 100644
--- a/spec/unit/compliance/runner_spec.rb
+++ b/spec/unit/compliance/runner_spec.rb
@@ -111,36 +111,37 @@ describe Chef::Compliance::Runner do
end
end
- describe "#send_report" do
- before do
- Chef::Config[:chef_server_url] = "https://chef_config_url.example.com/my_org"
- end
-
- it "uses the correct URL when 'server' attribute is set for chef-server-automate reporter" do
- node.normal["audit"]["server"] = "https://server_attribute_url.example.com/application/sub_application"
- report = { fake_report: true }
+ describe "#reporter" do
+ context "chef-server-automate reporter" do
+ it "uses the correct URL when 'server' attribute is set" do
+ Chef::Config[:chef_server_url] = "https://chef_config_url.example.com/my_org"
+ node.normal["audit"]["server"] = "https://server_attribute_url.example.com/application/sub_application"
- reporter = double(:chef_server_automate_reporter)
- expect(reporter).to receive(:send_report).with(report)
+ reporter = runner.reporter("chef-server-automate")
- expected_opts = hash_including(url: URI("https://server_attribute_url.example.com/application/sub_application/organizations/my_org/data-collector"))
+ expect(reporter).to be_kind_of(Chef::Compliance::Reporter::ChefServerAutomate)
+ expect(reporter.url).to eq(URI("https://server_attribute_url.example.com/application/sub_application/organizations/my_org/data-collector"))
+ end
- expect(Chef::Compliance::Reporter::ChefServerAutomate).to receive(:new).with(expected_opts).and_return(reporter)
+ it "falls back to chef_server_url for URL when 'server' attribute is not set" do
+ Chef::Config[:chef_server_url] = "https://chef_config_url.example.com/my_org"
- runner.send_report("chef-server-automate", report)
- end
+ reporter = runner.reporter("chef-server-automate")
- it "falls back to chef_server_url for URL when 'server' attribute is not set for chef-server-automate reporter" do
- report = { fake_report: true }
+ expect(reporter).to be_kind_of(Chef::Compliance::Reporter::ChefServerAutomate)
+ expect(reporter.url).to eq(URI("https://chef_config_url.example.com/organizations/my_org/data-collector"))
+ end
- reporter = double(:chef_server_automate_reporter)
- expect(reporter).to receive(:send_report).with(report)
-
- expected_opts = hash_including(url: URI("https://chef_config_url.example.com/organizations/my_org/data-collector"))
+ xit "returns nil with no 'server' attribute or chef_server_url configured" do
+ Chef::Config[:chef_server_url] = nil
+ expect(runner.reporter("chef-server-automate")).to be_nil
+ end
+ end
- expect(Chef::Compliance::Reporter::ChefServerAutomate).to receive(:new).with(expected_opts).and_return(reporter)
+ it "returns nil for unexpected reporter value" do
+ expect(logger).to receive(:warn).with("'tacos' is not a supported Chef InSpec report collector")
- runner.send_report("chef-server-automate", report)
+ expect(runner.reporter("tacos")).to be_nil
end
end
end