diff options
Diffstat (limited to 'spec/unit/node_spec.rb')
-rw-r--r-- | spec/unit/node_spec.rb | 121 |
1 files changed, 49 insertions, 72 deletions
diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb index 234955cc27..109e4ceb36 100644 --- a/spec/unit/node_spec.rb +++ b/spec/unit/node_spec.rb @@ -20,7 +20,6 @@ require "spec_helper" require "ostruct" describe Chef::Node do - let(:node) { Chef::Node.new() } let(:platform_introspector) { node } @@ -32,7 +31,7 @@ describe Chef::Node do end it "should validate the name of the node" do - expect{Chef::Node.build("solo node")}.to raise_error(Chef::Exceptions::ValidationFailed) + expect { Chef::Node.build("solo node") }.to raise_error(Chef::Exceptions::ValidationFailed) end it "should be sortable" do @@ -107,11 +106,11 @@ describe Chef::Node do end it "cannot be blank" do - expect { node.name("")}.to raise_error(Chef::Exceptions::ValidationFailed) + expect { node.name("") }.to raise_error(Chef::Exceptions::ValidationFailed) end it "should not accept name doesn't match /^[\-[:alnum:]_:.]+$/" do - expect { node.name("space in it")}.to raise_error(Chef::Exceptions::ValidationFailed) + expect { node.name("space in it") }.to raise_error(Chef::Exceptions::ValidationFailed) end end @@ -131,12 +130,11 @@ describe Chef::Node do end it "cannot be blank" do - expect { node.chef_environment("")}.to raise_error(Chef::Exceptions::ValidationFailed) + expect { node.chef_environment("") }.to raise_error(Chef::Exceptions::ValidationFailed) end end describe "policy_name" do - it "defaults to nil" do expect(node.policy_name).to be_nil end @@ -167,12 +165,11 @@ describe Chef::Node do end it "cannot be blank" do - expect { node.policy_name("")}.to raise_error(Chef::Exceptions::ValidationFailed) + 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 @@ -203,7 +200,7 @@ describe Chef::Node do end it "cannot be blank" do - expect { node.policy_group("")}.to raise_error(Chef::Exceptions::ValidationFailed) + expect { node.policy_group("") }.to raise_error(Chef::Exceptions::ValidationFailed) end end @@ -223,7 +220,7 @@ describe Chef::Node do end it "does not allow you to set an attribute via node[]=" do - expect { node["secret"] = "shush" }.to raise_error(Chef::Exceptions::ImmutableAttributeModification) + expect { node["secret"] = "shush" }.to raise_error(Chef::Exceptions::ImmutableAttributeModification) end it "should allow you to query whether an attribute exists with attribute?" do @@ -239,7 +236,7 @@ describe Chef::Node do end it "does not allow you to set an attribute via method_missing" do - expect { node.sunshine = "is bright"}.to raise_error(Chef::Exceptions::ImmutableAttributeModification) + expect { node.sunshine = "is bright" }.to raise_error(Chef::Exceptions::ImmutableAttributeModification) end it "should allow you get get an attribute via method_missing" do @@ -389,7 +386,7 @@ describe Chef::Node do it "deletes nested things correctly" do node.default["mysql"]["client"]["client_setting"] = "foo" - expect( node.rm("mysql", "server") ).to eql( {"port" => 3456} ) + expect( node.rm("mysql", "server") ).to eql( { "port" => 3456 } ) expect( node["mysql"] ).to eql( { "client" => { "client_setting" => "foo" } } ) end @@ -398,7 +395,7 @@ describe Chef::Node do end it "can delete the entire tree" do - expect( node.rm("mysql") ).to eql({"server"=>{"port"=>3456}}) + expect( node.rm("mysql") ).to eql({ "server" => { "port" => 3456 } }) end end @@ -614,8 +611,8 @@ describe Chef::Node do } expect( node["mysql"]["server"] ).to eql({ - "data_dir" => "/my_raid_volume/lib/mysql" - },) + "data_dir" => "/my_raid_volume/lib/mysql" + },) end it "removes all values from the precedence level when setting" do @@ -630,8 +627,8 @@ describe Chef::Node do expect( node["mysql"]["server"]["port"] ).to be_nil expect( node["mysql"]["server"]["data_dir"] ).to eql("/my_raid_volume/lib/mysql") expect( node["mysql"]["server"] ).to eql({ - "data_dir" => "/my_raid_volume/lib/mysql" - },) + "data_dir" => "/my_raid_volume/lib/mysql" + },) end it "higher precedence levels are not removed" do @@ -647,9 +644,9 @@ describe Chef::Node do expect( node["mysql"]["server"]["port"] ).to be_nil expect( node["mysql"]["server"]["data_dir"] ).to eql("/my_raid_volume/lib/mysql") expect( node["mysql"]["server"] ).to eql({ - "service_name" => "fancypants-sql", - "data_dir" => "/my_raid_volume/lib/mysql", - },) + "service_name" => "fancypants-sql", + "data_dir" => "/my_raid_volume/lib/mysql", + },) end it "will autovivify" do @@ -672,9 +669,9 @@ describe Chef::Node do expect( node["mysql"]["server"]["port"] ).to be_nil expect( node["mysql"]["server"]["data_dir"] ).to eql("/my_raid_volume/lib/mysql") expect( node["mysql"]["server"] ).to eql({ - "service_name" => "fancypants-sql", - "data_dir" => "/my_raid_volume/lib/mysql", - },) + "service_name" => "fancypants-sql", + "data_dir" => "/my_raid_volume/lib/mysql", + },) end it "when overwriting a non-hash/array" do @@ -692,8 +689,8 @@ describe Chef::Node do "data_dir" => "/my_raid_volume/lib/mysql" } expect( node["mysql"]["server"] ).to eql({ - "data_dir" => "/my_raid_volume/lib/mysql" - },) + "data_dir" => "/my_raid_volume/lib/mysql" + },) end end @@ -736,7 +733,7 @@ describe Chef::Node do node.default.sunshine = "is bright" node.default.canada = "is a nice place" seen_attributes = Hash.new - node.each_attribute do |a,v| + node.each_attribute do |a, v| seen_attributes[a] = v end expect(seen_attributes).to have_key("sunshine") @@ -747,14 +744,13 @@ describe Chef::Node do end describe "consuming json" do - before do - @ohai_data = {:platform => "foo", :platform_version => "bar"} + @ohai_data = { :platform => "foo", :platform_version => "bar" } end it "consumes the run list portion of a collection of attributes and returns the remainder" do - attrs = {"run_list" => [ "role[base]", "recipe[chef::server]" ], "foo" => "bar"} - expect(node.consume_run_list(attrs)).to eq({"foo" => "bar"}) + attrs = { "run_list" => [ "role[base]", "recipe[chef::server]" ], "foo" => "bar" } + expect(node.consume_run_list(attrs)).to eq({ "foo" => "bar" }) expect(node.run_list).to eq([ "role[base]", "recipe[chef::server]" ]) end @@ -774,7 +770,7 @@ describe Chef::Node do it "should not add duplicate recipes from the json attributes" do node.run_list << "one" node.consume_run_list "recipes" => [ "one", "two", "three" ] - expect(node.run_list).to eq([ "one", "two", "three" ]) + expect(node.run_list).to eq([ "one", "two", "three" ]) end it "doesn't change the run list if no run_list is specified in the json" do @@ -788,7 +784,7 @@ describe Chef::Node do end it "should add json attributes to the node" do - node.consume_external_attrs(@ohai_data, {"one" => "two", "three" => "four"}) + node.consume_external_attrs(@ohai_data, { "one" => "two", "three" => "four" }) expect(node.one).to eql("two") expect(node.three).to eql("four") end @@ -823,25 +819,24 @@ describe Chef::Node do end it "deep merges attributes instead of overwriting them" do - node.consume_external_attrs(@ohai_data, "one" => {"two" => {"three" => "four"}}) - expect(node.one.to_hash).to eq({"two" => {"three" => "four"}}) - node.consume_external_attrs(@ohai_data, "one" => {"abc" => "123"}) - node.consume_external_attrs(@ohai_data, "one" => {"two" => {"foo" => "bar"}}) - expect(node.one.to_hash).to eq({"two" => {"three" => "four", "foo" => "bar"}, "abc" => "123"}) + node.consume_external_attrs(@ohai_data, "one" => { "two" => { "three" => "four" } }) + expect(node.one.to_hash).to eq({ "two" => { "three" => "four" } }) + node.consume_external_attrs(@ohai_data, "one" => { "abc" => "123" }) + node.consume_external_attrs(@ohai_data, "one" => { "two" => { "foo" => "bar" } }) + expect(node.one.to_hash).to eq({ "two" => { "three" => "four", "foo" => "bar" }, "abc" => "123" }) end it "gives attributes from JSON priority when deep merging" do - node.consume_external_attrs(@ohai_data, "one" => {"two" => {"three" => "four"}}) - expect(node.one.to_hash).to eq({"two" => {"three" => "four"}}) - node.consume_external_attrs(@ohai_data, "one" => {"two" => {"three" => "forty-two"}}) - expect(node.one.to_hash).to eq({"two" => {"three" => "forty-two"}}) + node.consume_external_attrs(@ohai_data, "one" => { "two" => { "three" => "four" } }) + expect(node.one.to_hash).to eq({ "two" => { "three" => "four" } }) + node.consume_external_attrs(@ohai_data, "one" => { "two" => { "three" => "forty-two" } }) + expect(node.one.to_hash).to eq({ "two" => { "three" => "forty-two" } }) end - end describe "preparing for a chef client run" do before do - @ohai_data = {:platform => "foobuntu", :platform_version => "23.42"} + @ohai_data = { :platform => "foobuntu", :platform_version => "23.42" } end it "sets its platform according to platform detection" do @@ -851,18 +846,17 @@ describe Chef::Node do end it "consumes the run list from provided json attributes" do - node.consume_external_attrs(@ohai_data, {"run_list" => ["recipe[unicorn]"]}) + node.consume_external_attrs(@ohai_data, { "run_list" => ["recipe[unicorn]"] }) expect(node.run_list).to eq(["recipe[unicorn]"]) end it "saves non-runlist json attrs for later" do expansion = Chef::RunList::RunListExpansion.new("_default", []) allow(node.run_list).to receive(:expand).and_return(expansion) - node.consume_external_attrs(@ohai_data, {"foo" => "bar"}) + node.consume_external_attrs(@ohai_data, { "foo" => "bar" }) node.expand! - expect(node.normal_attrs).to eq({"foo" => "bar", "tags" => []}) + expect(node.normal_attrs).to eq({ "foo" => "bar", "tags" => [] }) end - end describe "when expanding its run list and merging attributes" do @@ -885,7 +879,7 @@ describe Chef::Node do end it "sets the 'roles' automatic attribute to the expanded role list" do - @expansion.instance_variable_set(:@applied_roles, {"arf" => nil, "countersnark" => nil}) + @expansion.instance_variable_set(:@applied_roles, { "arf" => nil, "countersnark" => nil }) node.expand! expect(node.automatic_attrs[:roles].sort).to eq(["arf", "countersnark"]) end @@ -978,19 +972,18 @@ describe Chef::Node do it "leaves normal level attributes untouched" do expect(node[:foo]).to eq("normal") end - end describe "when merging environment attributes" do before do node.chef_environment = "rspec" @expansion = Chef::RunList::RunListExpansion.new("rspec", []) - @expansion.default_attrs.replace({:default => "from role", :d_role => "role only"}) - @expansion.override_attrs.replace({:override => "from role", :o_role => "role only"}) + @expansion.default_attrs.replace({ :default => "from role", :d_role => "role only" }) + @expansion.override_attrs.replace({ :override => "from role", :o_role => "role only" }) @environment = Chef::Environment.new - @environment.default_attributes = {:default => "from env", :d_env => "env only" } - @environment.override_attributes = {:override => "from env", :o_env => "env only"} + @environment.default_attributes = { :default => "from env", :d_env => "env only" } + @environment.override_attributes = { :override => "from env", :o_env => "env only" } allow(Chef::Environment).to receive(:load).and_return(@environment) node.apply_expansion_attributes(@expansion) end @@ -1173,8 +1166,8 @@ describe Chef::Node do it "should serialize valid json with a run list", :json => true do #This test came about because activesupport mucks with Chef json serialization #Test should pass with and without Activesupport - node.run_list << {"type" => "role", "name" => "Cthulu"} - node.run_list << {"type" => "role", "name" => "Hastur"} + node.run_list << { "type" => "role", "name" => "Cthulu" } + node.run_list << { "type" => "role", "name" => "Hastur" } json = Chef::JSONCompat.to_json(node) expect(json).to match(/\"run_list\":\[\"role\[Cthulu\]\",\"role\[Hastur\]\"\]/) end @@ -1211,14 +1204,13 @@ describe Chef::Node do expect(serialized_node).to be_a_kind_of(Chef::Node) expect(serialized_node.name).to eql(node.name) expect(serialized_node.chef_environment).to eql(node.chef_environment) - node.each_attribute do |k,v| + node.each_attribute do |k, v| expect(serialized_node[k]).to eql(v) end expect(serialized_node.run_list).to eq(node.run_list) end context "when policyfile attributes are not present" do - it "does not have a policy_name key in the json" do expect(node.for_json.keys).to_not include("policy_name") end @@ -1229,7 +1221,6 @@ describe Chef::Node do end context "when policyfile attributes are present" do - before do node.policy_name = "my-application" node.policy_group = "staging" @@ -1251,7 +1242,6 @@ describe Chef::Node do expect(round_tripped_node.policy_name).to eq("my-application") expect(round_tripped_node.policy_group).to eq("staging") end - end include_examples "to_json equivalent to Chef::JSONCompat.to_json" do @@ -1457,7 +1447,6 @@ describe Chef::Node do end context "when policyfile attributes are present" do - before do node.name("example-node") node.policy_name = "my-application" @@ -1465,7 +1454,6 @@ describe Chef::Node do end context "and the server supports policyfile attributes in node JSON" do - it "creates the object normally" do expect(@rest).to receive(:post).with("nodes", node.for_json) node.create @@ -1479,7 +1467,6 @@ describe Chef::Node do # Chef Server before 12.3 context "and the Chef Server does not support policyfile attributes in node JSON" do - let(:response_body) { %q[{"error":["Invalid key policy_name in request body"]}] } let(:response) do @@ -1501,11 +1488,9 @@ describe Chef::Node do j.delete("policy_name") j.delete("policy_group") end - end context "on Chef Client 13 and later" do - # Though we normally attempt to provide compatibility with chef # server one major version back, policyfiles were beta when we # added the policyfile attributes to the node JSON, therefore @@ -1514,21 +1499,17 @@ describe Chef::Node do it "lets the 400 pass through", :chef_gte_13_only do expect { node.save }.to raise_error(http_exception) end - end context "when the node exists" do - it "falls back to saving without policyfile attributes" do expect(@rest).to receive(:put).with("nodes/example-node", node.for_json).and_raise(http_exception) expect(@rest).to receive(:put).with("nodes/example-node", trimmed_node).and_return(@node) expect { node.save }.to_not raise_error end - end context "when the node doesn't exist" do - let(:response_404) do Net::HTTPResponse.send(:response_class, "404").new("1.0", "404", "Not Found") end @@ -1554,12 +1535,8 @@ describe Chef::Node do node.create end end - end - end - end end - end |