summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Cloke <tylercloke@gmail.com>2017-06-20 12:23:56 -0700
committerBryan McLellan <btm@loftninjas.org>2017-06-22 19:22:19 -0400
commit7a2a59fcdbf9a47d59b8f0b98c910384589c3ad2 (patch)
tree75cf3ef3df91b5d7b8dca7b341b6d6a488de536a
parent3deba4390e014032a1f8c132ab5109d83cc059af (diff)
downloadchef-btm/12-pf-expanded-run-list.tar.gz
Properly send run_list_expanded event in policy node casebtm/12-pf-expanded-run-list
Signed-off-by: Tyler Cloke <tylercloke@gmail.com>
-rw-r--r--lib/chef/policy_builder/policyfile.rb28
-rw-r--r--spec/unit/policy_builder/policyfile_spec.rb50
2 files changed, 77 insertions, 1 deletions
diff --git a/lib/chef/policy_builder/policyfile.rb b/lib/chef/policy_builder/policyfile.rb
index f0009eac6c..3c9cb7024c 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 307bd45c18..cb7f070746 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) { {} }