diff options
author | danielsdeleo <dan@chef.io> | 2015-09-09 18:17:48 -0700 |
---|---|---|
committer | danielsdeleo <dan@chef.io> | 2015-09-17 14:29:49 -0700 |
commit | 01be9b1e91e515eb76de43a1e448e5476bd56574 (patch) | |
tree | 0d63197047ef25996a909f86c69e2aa178d02db0 | |
parent | a59b1992cabd063bdcf1494a1a3d0edf48ffa0d5 (diff) | |
download | chef-01be9b1e91e515eb76de43a1e448e5476bd56574.tar.gz |
Add policy_name and policy_group attrs to Node class
-rw-r--r-- | lib/chef/node.rb | 49 | ||||
-rw-r--r-- | spec/unit/node_spec.rb | 72 |
2 files changed, 121 insertions, 0 deletions
diff --git a/lib/chef/node.rb b/lib/chef/node.rb index 22c7d5bd8e..65ed21a442 100644 --- a/lib/chef/node.rb +++ b/lib/chef/node.rb @@ -63,6 +63,8 @@ class Chef include Chef::Mixin::ParamsValidate + NULL_ARG = Object.new + # Create a new Chef::Node object. def initialize(chef_server_rest: nil) @chef_server_rest = chef_server_rest @@ -72,6 +74,9 @@ class Chef @primary_runlist = Chef::RunList.new @override_runlist = Chef::RunList.new + @policy_name = nil + @policy_group = nil + @attributes = Chef::Node::Attribute.new({}, {}, {}, {}) @run_state = {} @@ -132,6 +137,50 @@ class Chef alias :environment :chef_environment + # The `policy_name` for this node. Setting this to a non-nil value will + # enable policyfile mode when `chef-client` is run. If set in the config + # file or in node json, running `chef-client` will update this value. + # + # @see Chef::PolicyBuilder::Dynamic + # @see Chef::PolicyBuilder::Policyfile + # + # @param arg [String] the new policy_name value + # @return [String] the current policy_name, or the one you just set + def policy_name(arg=NULL_ARG) + return @policy_name if arg.equal?(NULL_ARG) + validate({policy_name: arg}, { policy_name: { kind_of: [ String, NilClass ], regex: /^[\-:.[:alnum:]_]+$/ } }) + @policy_name = arg + end + + # A "non-DSL-style" setter for `policy_name` + # + # @see #policy_name + def policy_name=(policy_name) + policy_name(policy_name) + end + + # The `policy_group` for this node. Setting this to a non-nil value will + # enable policyfile mode when `chef-client` is run. If set in the config + # file or in node json, running `chef-client` will update this value. + # + # @see Chef::PolicyBuilder::Dynamic + # @see Chef::PolicyBuilder::Policyfile + # + # @param arg [String] the new policy_group value + # @return [String] the current policy_group, or the one you just set + def policy_group(arg=NULL_ARG) + return @policy_group if arg.equal?(NULL_ARG) + validate({policy_group: arg}, { policy_group: { kind_of: [ String, NilClass ], regex: /^[\-:.[:alnum:]_]+$/ } }) + @policy_group = arg + end + + # A "non-DSL-style" setter for `policy_group` + # + # @see #policy_group + def policy_group=(policy_group) + policy_group(policy_group) + end + def attributes @attributes end diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb index b7752eb734..39a4e829c9 100644 --- a/spec/unit/node_spec.rb +++ b/spec/unit/node_spec.rb @@ -127,6 +127,78 @@ describe Chef::Node do end end + describe "policy_name" do + + it "defaults to nil" do + expect(node.policy_name).to be_nil + end + + it "sets policy_name with a regular setter" do + node.policy_name = "example-policy" + expect(node.policy_name).to eq("example-policy") + end + + it "allows policy_name with every valid character" do + expect { node.policy_name = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqurstuvwxyz0123456789-_:.' }.to_not raise_error + end + + it "sets policy_name when given an argument" do + node.policy_name("example-policy") + expect(node.policy_name).to eq("example-policy") + end + + it "sets policy_name to nil when given nil" do + node.policy_name = "example-policy" + node.policy_name = nil + expect(node.policy_name).to be_nil + end + + it "disallows non-strings" do + expect { node.policy_name(Hash.new) }.to raise_error(Chef::Exceptions::ValidationFailed) + expect { node.policy_name(42) }.to raise_error(Chef::Exceptions::ValidationFailed) + end + + it "cannot be blank" do + expect { node.policy_name("")}.to raise_error(Chef::Exceptions::ValidationFailed) + end + end + + describe "policy_group" do + + it "defaults to nil" do + expect(node.policy_group).to be_nil + end + + it "sets policy_group with a regular setter" do + node.policy_group = "staging" + expect(node.policy_group).to eq("staging") + end + + it "allows policy_group with every valid character" do + expect { node.policy_group = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqurstuvwxyz0123456789-_:.' }.to_not raise_error + end + + it "sets an environment with chef_environment(something)" do + node.policy_group("staging") + expect(node.policy_group).to eq("staging") + end + + it "sets policy_group to nil when given nil" do + node.policy_group = "staging" + node.policy_group = nil + expect(node.policy_group).to be_nil + end + + it "disallows non-strings" do + expect { node.policy_group(Hash.new) }.to raise_error(Chef::Exceptions::ValidationFailed) + expect { node.policy_group(42) }.to raise_error(Chef::Exceptions::ValidationFailed) + end + + it "cannot be blank" do + expect { node.policy_group("")}.to raise_error(Chef::Exceptions::ValidationFailed) + end + end + describe "attributes" do it "should have attributes" do expect(node.attribute).to be_a_kind_of(Hash) |