summaryrefslogtreecommitdiff
path: root/spec/unit/data_collector_spec.rb
diff options
context:
space:
mode:
authorJon Cowie <jonlives@gmail.com>2018-02-20 10:13:10 +0000
committerJon Cowie <jonlives@gmail.com>2018-02-20 10:24:08 +0000
commit5cf64952220d84bbf1a5e0711caa06841b6b4293 (patch)
tree7adf41bbd02531b25842bdea59acc0603faa8aa7 /spec/unit/data_collector_spec.rb
parentc4522e3359dc64e10575f20ebef08f81109aaa48 (diff)
downloadchef-5cf64952220d84bbf1a5e0711caa06841b6b4293.tar.gz
Add output_locations functionality to data collector
This commit implements the recent changes to RFC-077, to support multiple output locations in the data collector. Signed-off-by: Jon Cowie <jonlives@gmail.com>
Diffstat (limited to 'spec/unit/data_collector_spec.rb')
-rw-r--r--spec/unit/data_collector_spec.rb143
1 files changed, 142 insertions, 1 deletions
diff --git a/spec/unit/data_collector_spec.rb b/spec/unit/data_collector_spec.rb
index f3f7ffb30f..1189e89411 100644
--- a/spec/unit/data_collector_spec.rb
+++ b/spec/unit/data_collector_spec.rb
@@ -25,9 +25,10 @@ require "chef/resource_builder"
describe Chef::DataCollector do
describe ".register_reporter?" do
- context "when no data collector URL is configured" do
+ context "when no data collector URL or output locations are configured" do
it "returns false" do
Chef::Config[:data_collector][:server_url] = nil
+ Chef::Config[:data_collector][:output_locations] = nil
expect(Chef::DataCollector.register_reporter?).to be_falsey
end
end
@@ -134,6 +135,109 @@ describe Chef::DataCollector do
end
end
+
+ context "when output_locations are configured" do
+ before do
+ Chef::Config[:data_collector][:output_locations] = ["http://data_collector", "/tmp/data_collector.json"]
+ end
+
+ context "when operating in why_run mode" do
+ it "returns false" do
+ Chef::Config[:why_run] = true
+ expect(Chef::DataCollector.register_reporter?).to be_falsey
+ end
+ end
+
+ context "when not operating in why_run mode" do
+
+ before do
+ Chef::Config[:why_run] = false
+ Chef::Config[:data_collector][:token] = token
+ end
+
+ context "when a token is configured" do
+
+ let(:token) { "supersecrettoken" }
+
+ context "when report is enabled for current mode" do
+ it "returns true" do
+ allow(Chef::DataCollector).to receive(:reporter_enabled_for_current_mode?).and_return(true)
+ expect(Chef::DataCollector.register_reporter?).to be_truthy
+ end
+ end
+
+ context "when report is disabled for current mode" do
+ it "returns false" do
+ allow(Chef::DataCollector).to receive(:reporter_enabled_for_current_mode?).and_return(false)
+ expect(Chef::DataCollector.register_reporter?).to be_falsey
+ end
+ end
+
+ end
+
+ # `Chef::Config[:data_collector][:server_url]` defaults to a URL
+ # relative to the `chef_server_url`, so we use configuration of the
+ # token to infer whether a solo/local mode user intends for data
+ # collection to be enabled.
+ context "when a token is not configured" do
+
+ let(:token) { nil }
+
+ context "when report is enabled for current mode" do
+
+ before do
+ allow(Chef::DataCollector).to receive(:reporter_enabled_for_current_mode?).and_return(true)
+ end
+
+ context "when the current mode is solo" do
+
+ before do
+ Chef::Config[:solo] = true
+ end
+
+ it "returns true" do
+ expect(Chef::DataCollector.register_reporter?).to be(true)
+ end
+
+ end
+
+ context "when the current mode is local mode" do
+
+ before do
+ Chef::Config[:local_mode] = true
+ end
+
+ it "returns false" do
+ expect(Chef::DataCollector.register_reporter?).to be(true)
+ end
+ end
+
+ context "when the current mode is client mode" do
+
+ before do
+ Chef::Config[:local_mode] = false
+ Chef::Config[:solo] = false
+ end
+
+ it "returns true" do
+ expect(Chef::DataCollector.register_reporter?).to be_truthy
+ end
+
+ end
+
+ end
+
+ context "when report is disabled for current mode" do
+ it "returns false" do
+ allow(Chef::DataCollector).to receive(:reporter_enabled_for_current_mode?).and_return(false)
+ expect(Chef::DataCollector.register_reporter?).to be_falsey
+ end
+ end
+
+ end
+
+ end
+ end
end
describe ".reporter_enabled_for_current_mode?" do
@@ -656,6 +760,13 @@ describe Chef::DataCollector::Reporter do
end
end
+ context "when server_url is omitted but output_locations is specified" do
+ it "raises an exception" do
+ Chef::Config[:data_collector][:output_locations] = ["http://data_collector", "/tmp/data_collector.json"]
+ expect { reporter.send(:validate_data_collector_server_url!) }.not_to raise_error(Chef::Exceptions::ConfigurationError)
+ end
+ end
+
context "when server_url is not empty" do
context "when server_url is an invalid URI" do
it "raises an exception" do
@@ -683,6 +794,36 @@ describe Chef::DataCollector::Reporter do
end
end
+ describe "#validate_data_collector_output_locations!" do
+ context "when output_locations is empty" do
+ it "raises an exception" do
+ Chef::Config[:data_collector][:output_locations] = {}
+ expect { reporter.send(:validate_data_collector_output_locations!) }.to raise_error(Chef::Exceptions::ConfigurationError)
+ end
+ end
+
+ context "when valid output_locations are provided" do
+ it "does not raise an exception" do
+ Chef::Config[:data_collector][:output_locations] = { :urls => ["http://data_collector"], :files => ["/tmp/data_collection.json"] }
+ expect { reporter.send(:validate_data_collector_output_locations!) }.not_to raise_error(Chef::Exceptions::ConfigurationError)
+ end
+ end
+
+ context "when output_locations contains an invalid file path" do
+ it "raises an exception" do
+ Chef::Config[:data_collector][:output_locations] = { :urls => ["http://data_collector"], :files => ["/data_collector.json"] }
+ expect { reporter.send(:validate_data_collector_output_locations!) }.to raise_error(Chef::Exceptions::ConfigurationError)
+ end
+ end
+
+ context "when output_locations contains an invalid URI" do
+ it "raises an exception" do
+ Chef::Config[:data_collector][:output_locations] = { :urls => ["this is not a url"], :files => ["/tmp/data_collection.json"] }
+ expect { reporter.send(:validate_data_collector_output_locations!) }.to raise_error(Chef::Exceptions::ConfigurationError)
+ end
+ end
+ end
+
describe "#detect_unprocessed_resources" do
context "when resources do not override core methods" do
it "adds resource reports for any resources that have not yet been processed" do