diff options
author | Tyler Cloke <tylercloke@gmail.com> | 2017-06-20 12:23:56 -0700 |
---|---|---|
committer | Tyler Cloke <tylercloke@gmail.com> | 2017-06-20 16:42:16 -0700 |
commit | 7102108e89e6045323dded023a36f2e3672fd43b (patch) | |
tree | 7bdf8d6f4be06898b056149becbbb8678756902c | |
parent | 5c4dbb8b34f55e2e253f988cc958c8b0e78ccea5 (diff) | |
download | chef-7102108e89e6045323dded023a36f2e3672fd43b.tar.gz |
Properly send run_list_expanded event in policy node casetc/fix-expanded-run-list-reporting-for-policyfiles
Signed-off-by: Tyler Cloke <tylercloke@gmail.com>
-rw-r--r-- | lib/chef/policy_builder/policyfile.rb | 28 | ||||
-rw-r--r-- | spec/unit/policy_builder/policyfile_spec.rb | 50 |
2 files changed, 77 insertions, 1 deletions
diff --git a/lib/chef/policy_builder/policyfile.rb b/lib/chef/policy_builder/policyfile.rb index f84e1dc68e..545d36cb0e 100644 --- a/lib/chef/policy_builder/policyfile.rb +++ b/lib/chef/policy_builder/policyfile.rb @@ -51,7 +51,32 @@ class Chef class PolicyfileError < StandardError; end - RunListExpansionIsh = Struct.new(:recipes, :roles) + RunListExpansionIsh = Struct.new(:recipes, :roles) do + # Implementing the parts of the RunListExpansion + # interface we need to properly send this through to + # events.run_list_expanded as it is expecting a RunListExpansion + # object. + def to_hash + # It looks like version only gets populated in the expanded_run_list when + # using a little used feature of roles to version lock cookbooks, so + # version is not reliable in here anyway (places like Automate UI are + # not getting version out of here. + # + # Skipped will always be false as it can only be true when two expanded + # roles contain the same recipe. + expanded_run_list = recipes.map do |r| + { type: "recipe", name: r, skipped: false, version: nil } + end + data_collector_hash = {} + data_collector_hash[:id] = "_policy_node" + data_collector_hash[:run_list] = expanded_run_list + data_collector_hash + end + + def to_json(*opts) + to_hash.to_json(*opts) + end + end attr_reader :events attr_reader :node @@ -137,6 +162,7 @@ class Chef Chef::Log.info("Run List expands to [#{run_list_with_versions_for_display.join(', ')}]") events.node_load_completed(node, run_list_with_versions_for_display, Chef::Config) + events.run_list_expanded(run_list_expansion_ish) node rescue Exception => e diff --git a/spec/unit/policy_builder/policyfile_spec.rb b/spec/unit/policy_builder/policyfile_spec.rb index c9086c2f63..466d0f970b 100644 --- a/spec/unit/policy_builder/policyfile_spec.rb +++ b/spec/unit/policy_builder/policyfile_spec.rb @@ -330,6 +330,56 @@ describe Chef::PolicyBuilder::Policyfile do end + describe "#build_node" do + + let(:node) do + node = Chef::Node.new + node.name(node_name) + node + end + + before do + allow(policy_builder).to receive(:node).and_return(node) + end + + context "when the run is successful" do + let(:run_list) do + ["recipe[test::default]", + "recipe[test::other]"] + end + + let(:version_hash) do + { + "version" => "0.1.0", + "identifier" => "012345678", + } + end + + let(:run_list_for_data_collector) do + { + :id => "_policy_node", + :run_list => [ + { :type => "recipe", :name => "test::default", :skipped => false, :version => nil }, + { :type => "recipe", :name => "test::other", :skipped => false, :version => nil }, + ], + } + end + + before do + allow(policy_builder).to receive(:run_list) + .and_return(run_list) + allow(policy_builder).to receive(:cookbook_lock_for) + .and_return(version_hash) + end + + it "sends the run_list_expanded event" do + policy_builder.build_node + expect(policy_builder.run_list_expansion_ish.to_hash) + .to eq(run_list_for_data_collector) + end + end + end + describe "building the node object" do let(:extra_chef_config) { {} } |