summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@chef.io>2015-09-09 18:33:15 -0700
committerdanielsdeleo <dan@chef.io>2015-09-17 14:29:49 -0700
commit69c7fa5f63e01c64f8ccc198ddc00c836c24914e (patch)
treee5a1277fbfd682d6c722609c7614236ebe12798f
parent01be9b1e91e515eb76de43a1e448e5476bd56574 (diff)
downloadchef-69c7fa5f63e01c64f8ccc198ddc00c836c24914e.tar.gz
Add Dynamic PolicyBuilder to switch on policyfile mode
Now we need to switch PolicyBuilder implementations based on several factors instead of just a single config settings, including content of the node fetched from the Chef Server.
-rw-r--r--lib/chef/policy_builder.rb1
-rw-r--r--lib/chef/policy_builder/dynamic.rb136
-rw-r--r--spec/unit/policy_builder/dynamic_spec.rb211
3 files changed, 348 insertions, 0 deletions
diff --git a/lib/chef/policy_builder.rb b/lib/chef/policy_builder.rb
index 136b2853b0..036d8bc051 100644
--- a/lib/chef/policy_builder.rb
+++ b/lib/chef/policy_builder.rb
@@ -18,6 +18,7 @@
require 'chef/policy_builder/expand_node_object'
require 'chef/policy_builder/policyfile'
+require 'chef/policy_builder/dynamic'
class Chef
diff --git a/lib/chef/policy_builder/dynamic.rb b/lib/chef/policy_builder/dynamic.rb
new file mode 100644
index 0000000000..e976ddd87f
--- /dev/null
+++ b/lib/chef/policy_builder/dynamic.rb
@@ -0,0 +1,136 @@
+#
+# Author:: Daniel DeLeo (<dan@chef.io>)
+# Copyright:: Copyright 2015 Chef Software, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'chef/log'
+require 'chef/rest'
+require 'chef/run_context'
+require 'chef/config'
+require 'chef/node'
+
+class Chef
+ module PolicyBuilder
+
+ # PolicyBuilder that selects either a Policyfile or non-Policyfile
+ # implementation based on the content of the node object.
+ class Dynamic
+
+ attr_reader :node
+ attr_reader :node_name
+ attr_reader :ohai_data
+ attr_reader :json_attribs
+ attr_reader :override_runlist
+ attr_reader :events
+
+ def initialize(node_name, ohai_data, json_attribs, override_runlist, events)
+ @implementation = nil
+
+ @node_name = node_name
+ @ohai_data = ohai_data
+ @json_attribs = json_attribs
+ @override_runlist = override_runlist
+ @events = events
+
+ @node = nil
+ end
+
+ ## PolicyBuilder API ##
+
+ # Loads the node state from the server, then picks the correct
+ # implementation class based on the node and json_attribs.
+ def load_node
+ events.node_load_start(node_name, config)
+ Chef::Log.debug("Building node object for #{node_name}")
+
+ node = Chef::Node.find_or_create(node_name)
+ select_implementation(node)
+ implementation.finish_load_node(node)
+ node
+ rescue Exception => e
+ events.node_load_failed(node_name, e, config)
+ raise
+ end
+
+ ## Delegated Methods ##
+
+ def original_runlist
+ implementation.original_runlist
+ end
+
+ def run_context
+ implementation.run_context
+ end
+
+ def run_list_expansion
+ implementation.run_list_expansion
+ end
+
+ def build_node
+ implementation.build_node
+ end
+
+ def setup_run_context(specific_recipes=nil)
+ implementation.setup_run_context(specific_recipes)
+ end
+
+ def expand_run_list
+ implementation.expand_run_list
+ end
+
+ def sync_cookbooks
+ implementation.sync_cookbooks
+ end
+
+ def temporary_policy?
+ implementation.temporary_policy?
+ end
+
+ ## Internal Public API ##
+
+ def implementation
+ @implementation
+ end
+
+ def select_implementation(node)
+ if policyfile_set_in_config? || policyfile_attribs_in_node_json? || node_has_policyfile_attrs?(node)
+ @implementation = Policyfile.new(node_name, ohai_data, json_attribs, override_runlist, events)
+ else
+ @implementation = ExpandNodeObject.new(node_name, ohai_data, json_attribs, override_runlist, events)
+ end
+ end
+
+ def config
+ Chef::Config
+ end
+
+ private
+
+ def node_has_policyfile_attrs?(node)
+ node.policy_name || node.policy_group
+ end
+
+ def policyfile_attribs_in_node_json?
+ json_attribs.key?("policy_name") || json_attribs.key?("policy_group")
+ end
+
+ def policyfile_set_in_config?
+ config[:use_policyfile] || config[:policy_name] || config[:policy_group]
+ end
+
+ end
+ end
+end
diff --git a/spec/unit/policy_builder/dynamic_spec.rb b/spec/unit/policy_builder/dynamic_spec.rb
new file mode 100644
index 0000000000..74a0272f42
--- /dev/null
+++ b/spec/unit/policy_builder/dynamic_spec.rb
@@ -0,0 +1,211 @@
+#
+# Author:: Daniel DeLeo (<dan@getchef.com>)
+# Copyright:: Copyright 2014 Chef Software, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'spec_helper'
+require 'chef/policy_builder'
+
+describe Chef::PolicyBuilder::Dynamic do
+
+ let(:node_name) { "joe_node" }
+ let(:ohai_data) { {"platform" => "ubuntu", "platform_version" => "13.04", "fqdn" => "joenode.example.com"} }
+ let(:json_attribs) { {"custom_attr" => "custom_attr_value"} }
+ let(:override_runlist) { nil }
+ let(:events) { Chef::EventDispatch::Dispatcher.new }
+
+ let(:err_namespace) { Chef::PolicyBuilder::Policyfile }
+
+ let(:base_node) do
+ node = Chef::Node.new
+ node.name(node_name)
+ node
+ end
+
+ let(:node) { base_node }
+
+ subject(:policy_builder) { Chef::PolicyBuilder::Dynamic.new(node_name, ohai_data, json_attribs, override_runlist, events) }
+
+ describe "loading policy data" do
+
+ describe "delegating PolicyBuilder API to the correct implementation" do
+
+ let(:implementation) { instance_double("Chef::PolicyBuilder::Policyfile") }
+
+ before do
+ allow(policy_builder).to receive(:implementation).and_return(implementation)
+ end
+
+ # Dynamic should load_node, figure out the correct backend, then forward
+ # messages to it after. That behavior is tested below.
+ it "responds to #load_node" do
+ expect(policy_builder).to respond_to(:load_node)
+ end
+
+ it "forwards #original_runlist" do
+ expect(implementation).to receive(:original_runlist)
+ policy_builder.original_runlist
+ end
+
+ it "forwards #run_context" do
+ expect(implementation).to receive(:run_context)
+ policy_builder.run_context
+ end
+
+ it "forwards #run_list_expansion" do
+ expect(implementation).to receive(:run_list_expansion)
+ policy_builder.run_list_expansion
+ end
+
+ it "forwards #build_node to the implementation object" do
+ expect(implementation).to receive(:build_node)
+ policy_builder.build_node
+ end
+
+ it "forwards #setup_run_context to the implementation object" do
+ expect(implementation).to receive(:setup_run_context)
+ policy_builder.setup_run_context
+
+ arg = Object.new
+
+ expect(implementation).to receive(:setup_run_context).with(arg)
+ policy_builder.setup_run_context(arg)
+ end
+
+ it "forwards #expand_run_list to the implementation object" do
+ expect(implementation).to receive(:expand_run_list)
+ policy_builder.expand_run_list
+ end
+
+ it "forwards #sync_cookbooks to the implementation object" do
+ expect(implementation).to receive(:sync_cookbooks)
+ policy_builder.sync_cookbooks
+ end
+
+ it "forwards #temporary_policy? to the implementation object" do
+ expect(implementation).to receive(:temporary_policy?)
+ policy_builder.temporary_policy?
+ end
+
+ end
+
+ describe "selecting a backend implementation" do
+
+ let(:implementation) do
+ policy_builder.select_implementation(node)
+ policy_builder.implementation
+ end
+
+ context "when no policyfile attributes are present on the node" do
+
+ context "and json_attribs are not given" do
+
+ let(:json_attribs) { {} }
+
+ it "uses the ExpandNodeObject implementation" do
+ expect(implementation).to be_a(Chef::PolicyBuilder::ExpandNodeObject)
+ end
+
+ end
+
+ context "and no policyfile attributes are present in json_attribs" do
+
+ let(:json_attribs) { {"foo" => "bar"} }
+
+ it "uses the ExpandNodeObject implementation" do
+ expect(implementation).to be_a(Chef::PolicyBuilder::ExpandNodeObject)
+ end
+
+ end
+
+ context "and :use_policyfile is set in Chef::Config" do
+
+ before do
+ Chef::Config[:use_policyfile] = true
+ end
+
+ it "uses the Policyfile implementation" do
+ expect(implementation).to be_a(Chef::PolicyBuilder::Policyfile)
+ end
+
+ end
+
+ context "and policy_name and policy_group are set on Chef::Config" do
+
+ before do
+ Chef::Config[:policy_name] = "example-policy"
+ Chef::Config[:policy_group] = "testing"
+ end
+
+ it "uses the Policyfile implementation" do
+ expect(implementation).to be_a(Chef::PolicyBuilder::Policyfile)
+ end
+
+ end
+
+ context "and policyfile attributes are present in json_attribs" do
+
+ let(:json_attribs) { {"policy_name" => "example-policy", "policy_group" => "testing"} }
+
+ it "uses the Policyfile implementation" do
+ expect(implementation).to be_a(Chef::PolicyBuilder::Policyfile)
+ end
+
+ end
+
+ end
+
+ context "when policyfile attributes are present on the node" do
+
+ let(:node) do
+ base_node.policy_name = "example-policy"
+ base_node.policy_group = "staging"
+ base_node
+ end
+
+ it "uses the Policyfile implementation" do
+ expect(implementation).to be_a(Chef::PolicyBuilder::Policyfile)
+ end
+
+ end
+
+ end
+
+ describe "loading a node" do
+
+ let(:implementation) { instance_double("Chef::PolicyBuilder::Policyfile") }
+
+ before do
+ allow(policy_builder).to receive(:implementation).and_return(implementation)
+
+ expect(Chef::Node).to receive(:find_or_create).with(node_name).and_return(node)
+ expect(policy_builder).to receive(:select_implementation).with(node)
+ expect(implementation).to receive(:finish_load_node).with(node)
+ end
+
+ context "when successful" do
+
+ it "selects the backend implementation and continues node loading", :pending do
+ policy_builder.load_node
+ end
+
+ end
+
+ end
+
+ end
+
+end