From ca603ccfa8f2f9fde10038f21e06e7dd69790b8b Mon Sep 17 00:00:00 2001 From: Jon Morrow Date: Thu, 10 May 2018 21:01:43 -0700 Subject: [PATCH] SHACK-191 Basic Automate 2 Reporting This changes adds config values for data_collector url and token. If set in the Chef Workstation config these values will be written on the target node so it will report run details into Automate 2. --- .../lib/chef-cli/action/converge_target.rb | 11 ++++ components/chef-cli/lib/chef-cli/config.rb | 4 ++ .../spec/unit/action/converge_target_spec.rb | 56 +++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/components/chef-cli/lib/chef-cli/action/converge_target.rb b/components/chef-cli/lib/chef-cli/action/converge_target.rb index 61cf99574..d4f5680aa 100644 --- a/components/chef-cli/lib/chef-cli/action/converge_target.rb +++ b/components/chef-cli/lib/chef-cli/action/converge_target.rb @@ -60,6 +60,17 @@ def create_remote_config(dir) exception_handlers << reporter EOM + # Maybe add data collector endpoint. + dc = ChefCLI::Config.data_collector + if !dc.url.nil? && !dc.token.nil? + workstation_rb << <<~EOM + data_collector.server_url "#{dc.url}" + data_collector.token "#{dc.token}" + data_collector.mode :solo + data_collector.organization "Chef Workstation" + EOM + end + begin config_file = Tempfile.new config_file.write(workstation_rb) diff --git a/components/chef-cli/lib/chef-cli/config.rb b/components/chef-cli/lib/chef-cli/config.rb index d7f00c7d4..132a56583 100644 --- a/components/chef-cli/lib/chef-cli/config.rb +++ b/components/chef-cli/lib/chef-cli/config.rb @@ -103,5 +103,9 @@ def reset default(:cookbook_repo_paths, ChefConfig::Config[:cookbook_path]) end + config_context :data_collector do + default :url, nil + default :token, nil + end end end diff --git a/components/chef-cli/spec/unit/action/converge_target_spec.rb b/components/chef-cli/spec/unit/action/converge_target_spec.rb index 41d584b51..4f40f7c7a 100644 --- a/components/chef-cli/spec/unit/action/converge_target_spec.rb +++ b/components/chef-cli/spec/unit/action/converge_target_spec.rb @@ -67,6 +67,62 @@ # ensure the tempfile is deleted locally expect(local_tempfile.closed?).to eq(true) end + + describe "when data_collector is set in config" do + before do + ChefCLI::Config.data_collector.url = "dc.url" + ChefCLI::Config.data_collector.token = "dc.token" + end + + it "creates a config file with data collector config values" do + expect(Tempfile).to receive(:new).and_return(local_tempfile) + expect(local_tempfile).to receive(:write).with(<<~EOM + local_mode true + color false + cache_path "\#{ENV['APPDATA']}/chef-workstation" + chef_repo_path "\#{ENV['APPDATA']}/chef-workstation" + require_relative "reporter" + reporter = ChefCLI::Reporter.new + report_handlers << reporter + exception_handlers << reporter + data_collector.server_url "dc.url" + data_collector.token "dc.token" + data_collector.mode :solo + data_collector.organization "Chef Workstation" + EOM + ) + expect(target_host).to receive(:upload_file).with(local_tempfile.path, remote_config) + expect(action.create_remote_config(remote_folder)).to eq(remote_config) + # ensure the tempfile is deleted locally + expect(local_tempfile.closed?).to eq(true) + end + end + + describe "when data_collector is not set" do + before do + ChefCLI::Config.data_collector.url = nil + ChefCLI::Config.data_collector.token = nil + end + + it "creates a config file without data collector config values" do + expect(Tempfile).to receive(:new).and_return(local_tempfile) + expect(local_tempfile).to receive(:write).with(<<~EOM + local_mode true + color false + cache_path "\#{ENV['APPDATA']}/chef-workstation" + chef_repo_path "\#{ENV['APPDATA']}/chef-workstation" + require_relative "reporter" + reporter = ChefCLI::Reporter.new + report_handlers << reporter + exception_handlers << reporter + EOM + ) + expect(target_host).to receive(:upload_file).with(local_tempfile.path, remote_config) + expect(action.create_remote_config(remote_folder)).to eq(remote_config) + # ensure the tempfile is deleted locally + expect(local_tempfile.closed?).to eq(true) + end + end end describe "#create_remote_handler" do