diff options
-rw-r--r-- | lib/chef/policy_builder/expand_node_object.rb | 30 | ||||
-rw-r--r-- | spec/unit/policy_builder/expand_node_object_spec.rb | 29 |
2 files changed, 59 insertions, 0 deletions
diff --git a/lib/chef/policy_builder/expand_node_object.rb b/lib/chef/policy_builder/expand_node_object.rb index 26b98afa52..933960a429 100644 --- a/lib/chef/policy_builder/expand_node_object.rb +++ b/lib/chef/policy_builder/expand_node_object.rb @@ -33,6 +33,9 @@ class Chef # expands the run_list on a node object and then queries the chef-server # to find the correct set of cookbooks, given version constraints of the # node's environment. + # + # Note that this class should only be used via PolicyBuilder::Dynamic and + # not instantiated directly. class ExpandNodeObject attr_reader :events @@ -94,6 +97,33 @@ class Chef run_context end + # DEPRECATED: As of Chef 12.5, chef selects either policyfile mode or + # "expand node" mode dynamically, based on the content of the node + # object, first boot JSON, and config. This happens in + # PolicyBuilder::Dynamic, which selects the implementation during + # #load_node and then delegates to either ExpandNodeObject or Policyfile + # implementations as appropriate. Tools authors should update their code + # to create a PolicyBuilder::Dynamc policy builder and allow it to select + # the proper implementation. + def load_node + Chef.log_deprecation("ExpandNodeObject#load_node is deprecated. Please use Chef::PolicyBuilder::Dynamic instead of using ExpandNodeObject directly") + + events.node_load_start(node_name, config) + Chef::Log.debug("Building node object for #{node_name}") + + @node = + if Chef::Config[:solo] + Chef::Node.build(node_name) + else + Chef::Node.find_or_create(node_name) + end + finish_load_node(node) + node + rescue Exception => e + events.node_load_failed(node_name, e, config) + raise + end + def finish_load_node(node) @node = node end diff --git a/spec/unit/policy_builder/expand_node_object_spec.rb b/spec/unit/policy_builder/expand_node_object_spec.rb index 815926ffb7..306d677108 100644 --- a/spec/unit/policy_builder/expand_node_object_spec.rb +++ b/spec/unit/policy_builder/expand_node_object_spec.rb @@ -34,6 +34,14 @@ describe Chef::PolicyBuilder::ExpandNodeObject do expect(policy_builder).to respond_to(:node) end + it "implements a load_node method for backwards compatibility until Chef 13" do + expect(policy_builder).to respond_to(:load_node) + end + + it "has removed the deprecated #load_node method", :chef_gte_13_only do + expect(policy_builder).to_not respond_to(:load_node) + end + it "implements a finish_load_node method" do expect(policy_builder).to respond_to(:finish_load_node) end @@ -98,6 +106,27 @@ describe Chef::PolicyBuilder::ExpandNodeObject do end + context "deprecated #load_node method" do + + let(:node) do + node = Chef::Node.new + node.name(node_name) + node.run_list(["recipe[a::default]", "recipe[b::server]"]) + node + end + + before do + Chef::Config[:treat_deprecation_warnings_as_errors] = false + expect(Chef::Node).to receive(:find_or_create).with(node_name).and_return(node) + policy_builder.load_node + end + + it "loads the node" do + expect(policy_builder.node).to eq(node) + end + + end + context "once the node has been loaded" do let(:node) do node = Chef::Node.new |