diff options
author | tyler-ball <tyleraball@gmail.com> | 2014-12-12 11:47:12 -0800 |
---|---|---|
committer | tyler-ball <tyleraball@gmail.com> | 2014-12-15 11:03:13 -0800 |
commit | 5f44d328dfa9031e9213ad433da1bcddb77081d0 (patch) | |
tree | 523b02d087fee619cfa9d734338dac4d4dbf2ce8 /spec/unit | |
parent | e7f1e437451de4c9fef893ce67171e966d828949 (diff) | |
download | chef-5f44d328dfa9031e9213ad433da1bcddb77081d0.tar.gz |
Adding unit test coverage and updating per github review
Diffstat (limited to 'spec/unit')
-rw-r--r-- | spec/unit/recipe_spec.rb | 1 | ||||
-rw-r--r-- | spec/unit/resource/resource_notification_spec.rb | 170 | ||||
-rw-r--r-- | spec/unit/resource_spec.rb | 151 | ||||
-rw-r--r-- | spec/unit/run_context_spec.rb | 149 |
4 files changed, 265 insertions, 206 deletions
diff --git a/spec/unit/recipe_spec.rb b/spec/unit/recipe_spec.rb index 0e660dc0cc..a9f37098a2 100644 --- a/spec/unit/recipe_spec.rb +++ b/spec/unit/recipe_spec.rb @@ -154,6 +154,7 @@ describe Chef::Recipe do zm_resource.recipe_name.should == "test" zm_resource.cookbook_name.should == "hjk" zm_resource.source_line.should include(__FILE__) + zm_resource.declared_type.should == :zen_master end it "does not add the resource to the resource collection" do diff --git a/spec/unit/resource/resource_notification_spec.rb b/spec/unit/resource/resource_notification_spec.rb new file mode 100644 index 0000000000..8d8eeb4503 --- /dev/null +++ b/spec/unit/resource/resource_notification_spec.rb @@ -0,0 +1,170 @@ +# +# Author:: Tyler Ball (<tball@chef.io>) +# Copyright:: Copyright (c) 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/resource/resource_notification' + +describe Chef::Resource::Notification do + before do + @notification = Chef::Resource::Notification.new(:service_apache, :restart, :template_httpd_conf) + end + + it "has a resource to be notified" do + @notification.resource.should == :service_apache + end + + it "has an action to take on the service" do + @notification.action.should == :restart + end + + it "has a notifying resource" do + @notification.notifying_resource.should == :template_httpd_conf + end + + it "is a duplicate of another notification with the same target resource and action" do + other = Chef::Resource::Notification.new(:service_apache, :restart, :sync_web_app_code) + @notification.duplicates?(other).should be_true + end + + it "is not a duplicate of another notification if the actions differ" do + other = Chef::Resource::Notification.new(:service_apache, :enable, :install_apache) + @notification.duplicates?(other).should be_false + end + + it "is not a duplicate of another notification if the target resources differ" do + other = Chef::Resource::Notification.new(:service_sshd, :restart, :template_httpd_conf) + @notification.duplicates?(other).should be_false + end + + it "raises an ArgumentError if you try to check a non-ducktype object for duplication" do + lambda {@notification.duplicates?(:not_a_notification)}.should raise_error(ArgumentError) + end + + it "takes no action to resolve a resource reference that doesn't need to be resolved" do + @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") + @notification.resource = @keyboard_cat + @long_cat = Chef::Resource::Cat.new("long_cat") + @notification.notifying_resource = @long_cat + @resource_collection = Chef::ResourceCollection.new + # would raise an error since the resource is not in the collection + @notification.resolve_resource_reference(@resource_collection) + @notification.resource.should == @keyboard_cat + end + + it "resolves a lazy reference to a resource" do + @notification.resource = {:cat => "keyboard_cat"} + @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") + @resource_collection = Chef::ResourceCollection.new + @resource_collection << @keyboard_cat + @long_cat = Chef::Resource::Cat.new("long_cat") + @notification.notifying_resource = @long_cat + @notification.resolve_resource_reference(@resource_collection) + @notification.resource.should == @keyboard_cat + end + + it "resolves a lazy reference to its notifying resource" do + @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") + @notification.resource = @keyboard_cat + @notification.notifying_resource = {:cat => "long_cat"} + @long_cat = Chef::Resource::Cat.new("long_cat") + @resource_collection = Chef::ResourceCollection.new + @resource_collection << @long_cat + @notification.resolve_resource_reference(@resource_collection) + @notification.notifying_resource.should == @long_cat + end + + it "resolves lazy references to both its resource and its notifying resource" do + @notification.resource = {:cat => "keyboard_cat"} + @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") + @resource_collection = Chef::ResourceCollection.new + @resource_collection << @keyboard_cat + @notification.notifying_resource = {:cat => "long_cat"} + @long_cat = Chef::Resource::Cat.new("long_cat") + @resource_collection << @long_cat + @notification.resolve_resource_reference(@resource_collection) + @notification.resource.should == @keyboard_cat + @notification.notifying_resource.should == @long_cat + end + + it "raises a RuntimeError if you try to reference multiple resources" do + @notification.resource = {:cat => ["keyboard_cat", "cheez_cat"]} + @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") + @cheez_cat = Chef::Resource::Cat.new("cheez_cat") + @resource_collection = Chef::ResourceCollection.new + @resource_collection << @keyboard_cat + @resource_collection << @cheez_cat + @long_cat = Chef::Resource::Cat.new("long_cat") + @notification.notifying_resource = @long_cat + lambda {@notification.resolve_resource_reference(@resource_collection)}.should raise_error(RuntimeError) + end + + it "raises a RuntimeError if you try to reference multiple notifying resources" do + @notification.notifying_resource = {:cat => ["long_cat", "cheez_cat"]} + @long_cat = Chef::Resource::Cat.new("long_cat") + @cheez_cat = Chef::Resource::Cat.new("cheez_cat") + @resource_collection = Chef::ResourceCollection.new + @resource_collection << @long_cat + @resource_collection << @cheez_cat + @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") + @notification.resource = @keyboard_cat + lambda {@notification.resolve_resource_reference(@resource_collection)}.should raise_error(RuntimeError) + end + + it "raises a RuntimeError if it can't find a resource in the resource collection when resolving a lazy reference" do + @notification.resource = {:cat => "keyboard_cat"} + @cheez_cat = Chef::Resource::Cat.new("cheez_cat") + @resource_collection = Chef::ResourceCollection.new + @resource_collection << @cheez_cat + @long_cat = Chef::Resource::Cat.new("long_cat") + @notification.notifying_resource = @long_cat + lambda {@notification.resolve_resource_reference(@resource_collection)}.should raise_error(RuntimeError) + end + + it "raises a RuntimeError if it can't find a notifying resource in the resource collection when resolving a lazy reference" do + @notification.notifying_resource = {:cat => "long_cat"} + @cheez_cat = Chef::Resource::Cat.new("cheez_cat") + @resource_collection = Chef::ResourceCollection.new + @resource_collection << @cheez_cat + @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") + @notification.resource = @keyboard_cat + lambda {@notification.resolve_resource_reference(@resource_collection)}.should raise_error(RuntimeError) + end + + it "raises an ArgumentError if improper syntax is used in the lazy reference to its resource" do + @notification.resource = "cat => keyboard_cat" + @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") + @resource_collection = Chef::ResourceCollection.new + @resource_collection << @keyboard_cat + @long_cat = Chef::Resource::Cat.new("long_cat") + @notification.notifying_resource = @long_cat + lambda {@notification.resolve_resource_reference(@resource_collection)}.should raise_error(ArgumentError) + end + + it "raises an ArgumentError if improper syntax is used in the lazy reference to its notifying resource" do + @notification.notifying_resource = "cat => long_cat" + @long_cat = Chef::Resource::Cat.new("long_cat") + @resource_collection = Chef::ResourceCollection.new + @resource_collection << @long_cat + @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") + @notification.resource = @keyboard_cat + lambda {@notification.resolve_resource_reference(@resource_collection)}.should raise_error(ArgumentError) + end + + # Create test to resolve lazy references to both notifying resource and dest. resource + # Create tests to check proper error raising + +end diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb index 2163cf181e..1a4ee6b0fe 100644 --- a/spec/unit/resource_spec.rb +++ b/spec/unit/resource_spec.rb @@ -855,154 +855,3 @@ describe Chef::Resource do end end - -describe Chef::Resource::Notification do - before do - @notification = Chef::Resource::Notification.new(:service_apache, :restart, :template_httpd_conf) - end - - it "has a resource to be notified" do - @notification.resource.should == :service_apache - end - - it "has an action to take on the service" do - @notification.action.should == :restart - end - - it "has a notifying resource" do - @notification.notifying_resource.should == :template_httpd_conf - end - - it "is a duplicate of another notification with the same target resource and action" do - other = Chef::Resource::Notification.new(:service_apache, :restart, :sync_web_app_code) - @notification.duplicates?(other).should be_true - end - - it "is not a duplicate of another notification if the actions differ" do - other = Chef::Resource::Notification.new(:service_apache, :enable, :install_apache) - @notification.duplicates?(other).should be_false - end - - it "is not a duplicate of another notification if the target resources differ" do - other = Chef::Resource::Notification.new(:service_sshd, :restart, :template_httpd_conf) - @notification.duplicates?(other).should be_false - end - - it "raises an ArgumentError if you try to check a non-ducktype object for duplication" do - lambda {@notification.duplicates?(:not_a_notification)}.should raise_error(ArgumentError) - end - - it "takes no action to resolve a resource reference that doesn't need to be resolved" do - @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") - @notification.resource = @keyboard_cat - @long_cat = Chef::Resource::Cat.new("long_cat") - @notification.notifying_resource = @long_cat - @resource_collection = Chef::ResourceCollection.new - # would raise an error since the resource is not in the collection - @notification.resolve_resource_reference(@resource_collection) - @notification.resource.should == @keyboard_cat - end - - it "resolves a lazy reference to a resource" do - @notification.resource = {:cat => "keyboard_cat"} - @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") - @resource_collection = Chef::ResourceCollection.new - @resource_collection << @keyboard_cat - @long_cat = Chef::Resource::Cat.new("long_cat") - @notification.notifying_resource = @long_cat - @notification.resolve_resource_reference(@resource_collection) - @notification.resource.should == @keyboard_cat - end - - it "resolves a lazy reference to its notifying resource" do - @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") - @notification.resource = @keyboard_cat - @notification.notifying_resource = {:cat => "long_cat"} - @long_cat = Chef::Resource::Cat.new("long_cat") - @resource_collection = Chef::ResourceCollection.new - @resource_collection << @long_cat - @notification.resolve_resource_reference(@resource_collection) - @notification.notifying_resource.should == @long_cat - end - - it "resolves lazy references to both its resource and its notifying resource" do - @notification.resource = {:cat => "keyboard_cat"} - @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") - @resource_collection = Chef::ResourceCollection.new - @resource_collection << @keyboard_cat - @notification.notifying_resource = {:cat => "long_cat"} - @long_cat = Chef::Resource::Cat.new("long_cat") - @resource_collection << @long_cat - @notification.resolve_resource_reference(@resource_collection) - @notification.resource.should == @keyboard_cat - @notification.notifying_resource.should == @long_cat - end - - it "raises a RuntimeError if you try to reference multiple resources" do - @notification.resource = {:cat => ["keyboard_cat", "cheez_cat"]} - @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") - @cheez_cat = Chef::Resource::Cat.new("cheez_cat") - @resource_collection = Chef::ResourceCollection.new - @resource_collection << @keyboard_cat - @resource_collection << @cheez_cat - @long_cat = Chef::Resource::Cat.new("long_cat") - @notification.notifying_resource = @long_cat - lambda {@notification.resolve_resource_reference(@resource_collection)}.should raise_error(RuntimeError) - end - - it "raises a RuntimeError if you try to reference multiple notifying resources" do - @notification.notifying_resource = {:cat => ["long_cat", "cheez_cat"]} - @long_cat = Chef::Resource::Cat.new("long_cat") - @cheez_cat = Chef::Resource::Cat.new("cheez_cat") - @resource_collection = Chef::ResourceCollection.new - @resource_collection << @long_cat - @resource_collection << @cheez_cat - @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") - @notification.resource = @keyboard_cat - lambda {@notification.resolve_resource_reference(@resource_collection)}.should raise_error(RuntimeError) - end - - it "raises a RuntimeError if it can't find a resource in the resource collection when resolving a lazy reference" do - @notification.resource = {:cat => "keyboard_cat"} - @cheez_cat = Chef::Resource::Cat.new("cheez_cat") - @resource_collection = Chef::ResourceCollection.new - @resource_collection << @cheez_cat - @long_cat = Chef::Resource::Cat.new("long_cat") - @notification.notifying_resource = @long_cat - lambda {@notification.resolve_resource_reference(@resource_collection)}.should raise_error(RuntimeError) - end - - it "raises a RuntimeError if it can't find a notifying resource in the resource collection when resolving a lazy reference" do - @notification.notifying_resource = {:cat => "long_cat"} - @cheez_cat = Chef::Resource::Cat.new("cheez_cat") - @resource_collection = Chef::ResourceCollection.new - @resource_collection << @cheez_cat - @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") - @notification.resource = @keyboard_cat - lambda {@notification.resolve_resource_reference(@resource_collection)}.should raise_error(RuntimeError) - end - - it "raises an ArgumentError if improper syntax is used in the lazy reference to its resource" do - @notification.resource = "cat => keyboard_cat" - @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") - @resource_collection = Chef::ResourceCollection.new - @resource_collection << @keyboard_cat - @long_cat = Chef::Resource::Cat.new("long_cat") - @notification.notifying_resource = @long_cat - lambda {@notification.resolve_resource_reference(@resource_collection)}.should raise_error(ArgumentError) - end - - it "raises an ArgumentError if improper syntax is used in the lazy reference to its notifying resource" do - @notification.notifying_resource = "cat => long_cat" - @long_cat = Chef::Resource::Cat.new("long_cat") - @resource_collection = Chef::ResourceCollection.new - @resource_collection << @long_cat - @keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") - @notification.resource = @keyboard_cat - lambda {@notification.resolve_resource_reference(@resource_collection)}.should raise_error(ArgumentError) - end - - # Create test to resolve lazy references to both notifying resource and dest. resource - # Create tests to check proper error raising - -end diff --git a/spec/unit/run_context_spec.rb b/spec/unit/run_context_spec.rb index 21ece2abaa..ca0c4dd354 100644 --- a/spec/unit/run_context_spec.rb +++ b/spec/unit/run_context_spec.rb @@ -24,23 +24,26 @@ require 'support/lib/library_load_order' Chef::Log.level = :debug describe Chef::RunContext do - before(:each) do - @chef_repo_path = File.expand_path(File.join(CHEF_SPEC_DATA, "run_context", "cookbooks")) - cl = Chef::CookbookLoader.new(@chef_repo_path) + let(:chef_repo_path) { File.expand_path(File.join(CHEF_SPEC_DATA, "run_context", "cookbooks")) } + let(:cookbook_collection) { + cl = Chef::CookbookLoader.new(chef_repo_path) cl.load_cookbooks - @cookbook_collection = Chef::CookbookCollection.new(cl) - @node = Chef::Node.new - @node.run_list << "test" << "test::one" << "test::two" - @events = Chef::EventDispatch::Dispatcher.new - @run_context = Chef::RunContext.new(@node, @cookbook_collection, @events) - end + Chef::CookbookCollection.new(cl) + } + let(:node) { + node = Chef::Node.new + node.run_list << "test" << "test::one" << "test::two" + node + } + let(:events) { Chef::EventDispatch::Dispatcher.new } + let(:run_context) { Chef::RunContext.new(node, cookbook_collection, events) } it "has a cookbook collection" do - @run_context.cookbook_collection.should == @cookbook_collection + run_context.cookbook_collection.should == cookbook_collection end it "has a node" do - @run_context.node.should == @node + run_context.node.should == node end describe "loading cookbooks for a run list" do @@ -52,44 +55,44 @@ describe Chef::RunContext do Chef::Provider.send(:remove_const, :TestProvider) end - @node.run_list << "test" << "test::one" << "test::two" - @node.should_receive(:loaded_recipe).with(:test, "default") - @node.should_receive(:loaded_recipe).with(:test, "one") - @node.should_receive(:loaded_recipe).with(:test, "two") - @run_context.load(@node.run_list.expand('_default')) + node.run_list << "test" << "test::one" << "test::two" + node.should_receive(:loaded_recipe).with(:test, "default") + node.should_receive(:loaded_recipe).with(:test, "one") + node.should_receive(:loaded_recipe).with(:test, "two") + run_context.load(node.run_list.expand('_default')) end it "should load all the definitions in the cookbooks for this node" do - @run_context.definitions.should have_key(:new_cat) - @run_context.definitions.should have_key(:new_badger) - @run_context.definitions.should have_key(:new_dog) + run_context.definitions.should have_key(:new_cat) + run_context.definitions.should have_key(:new_badger) + run_context.definitions.should have_key(:new_dog) end it "should load all the recipes specified for this node" do - @run_context.resource_collection[0].to_s.should == "cat[einstein]" - @run_context.resource_collection[1].to_s.should == "cat[loulou]" - @run_context.resource_collection[2].to_s.should == "cat[birthday]" - @run_context.resource_collection[3].to_s.should == "cat[peanut]" - @run_context.resource_collection[4].to_s.should == "cat[fat peanut]" + run_context.resource_collection[0].to_s.should == "cat[einstein]" + run_context.resource_collection[1].to_s.should == "cat[loulou]" + run_context.resource_collection[2].to_s.should == "cat[birthday]" + run_context.resource_collection[3].to_s.should == "cat[peanut]" + run_context.resource_collection[4].to_s.should == "cat[fat peanut]" end it "loads all the attribute files in the cookbook collection" do - @run_context.loaded_fully_qualified_attribute?("test", "george").should be_true - @node[:george].should == "washington" + run_context.loaded_fully_qualified_attribute?("test", "george").should be_true + node[:george].should == "washington" end it "registers attributes files as loaded so they won't be reloaded" do # This test unfortunately is pretty tightly intertwined with the # implementation of how nodes load attribute files, but is the only # convenient way to test this behavior. - @node.should_not_receive(:from_file) - @node.include_attribute("test::george") + node.should_not_receive(:from_file) + node.include_attribute("test::george") end it "raises an error when attempting to include_recipe from a cookbook not reachable by run list or dependencies" do - @node.should_receive(:loaded_recipe).with(:ancient, "aliens") + node.should_receive(:loaded_recipe).with(:ancient, "aliens") lambda do - @run_context.include_recipe("ancient::aliens") + run_context.include_recipe("ancient::aliens") # In CHEF-5120, this becomes a Chef::Exceptions::MissingCookbookDependency error: end.should raise_error(Chef::Exceptions::CookbookNotFound) end @@ -97,39 +100,34 @@ describe Chef::RunContext do end describe "querying the contents of cookbooks" do - before do - @chef_repo_path = File.expand_path(File.join(CHEF_SPEC_DATA, "cookbooks")) - cl = Chef::CookbookLoader.new(@chef_repo_path) - cl.load_cookbooks - @cookbook_collection = Chef::CookbookCollection.new(cl) - @node = Chef::Node.new - @node.set[:platform] = "ubuntu" - @node.set[:platform_version] = "13.04" - @node.name("testing") - @events = Chef::EventDispatch::Dispatcher.new - @run_context = Chef::RunContext.new(@node, @cookbook_collection, @events) - end - + let(:chef_repo_path) { File.expand_path(File.join(CHEF_SPEC_DATA, "cookbooks")) } + let(:node) { + node = Chef::Node.new + node.set[:platform] = "ubuntu" + node.set[:platform_version] = "13.04" + node.name("testing") + node + } it "queries whether a given cookbook has a specific template" do - @run_context.should have_template_in_cookbook("openldap", "test.erb") - @run_context.should_not have_template_in_cookbook("openldap", "missing.erb") + run_context.should have_template_in_cookbook("openldap", "test.erb") + run_context.should_not have_template_in_cookbook("openldap", "missing.erb") end it "errors when querying for a template in a not-available cookbook" do expect do - @run_context.has_template_in_cookbook?("no-such-cookbook", "foo.erb") + run_context.has_template_in_cookbook?("no-such-cookbook", "foo.erb") end.to raise_error(Chef::Exceptions::CookbookNotFound) end it "queries whether a given cookbook has a specific cookbook_file" do - @run_context.should have_cookbook_file_in_cookbook("java", "java.response") - @run_context.should_not have_cookbook_file_in_cookbook("java", "missing.txt") + run_context.should have_cookbook_file_in_cookbook("java", "java.response") + run_context.should_not have_cookbook_file_in_cookbook("java", "missing.txt") end it "errors when querying for a cookbook_file in a not-available cookbook" do expect do - @run_context.has_cookbook_file_in_cookbook?("no-such-cookbook", "foo.txt") + run_context.has_cookbook_file_in_cookbook?("no-such-cookbook", "foo.txt") end.to raise_error(Chef::Exceptions::CookbookNotFound) end end @@ -140,13 +138,54 @@ describe Chef::RunContext do end it "stores and deletes the reboot request" do - @run_context.request_reboot(expected) - expect(@run_context.reboot_info).to eq(expected) - expect(@run_context.reboot_requested?).to be_true + run_context.request_reboot(expected) + expect(run_context.reboot_info).to eq(expected) + expect(run_context.reboot_requested?).to be_true + + run_context.cancel_reboot + expect(run_context.reboot_info).to eq({}) + expect(run_context.reboot_requested?).to be_false + end + end + + describe "notifications" do + let(:notification) { Chef::Resource::Notification.new(nil, nil, notifying_resource) } + + shared_context "notifying resource is a Chef::Resource" do + let(:notifying_resource) { Chef::Resource.new("gerbil") } + + it "should be keyed off the resource name" do + run_context.send(setter, notification) + expect(run_context.send(getter, notifying_resource)).to eq([notification]) + end + end + + shared_context "notifying resource is a subclass of Chef::Resource" do + let(:declared_type) { :alpaca } + let(:notifying_resource) { + r = Class.new(Chef::Resource).new("guinea pig") + r.declared_type = declared_type + r + } + + it "should be keyed off the resource declared key" do + run_context.send(setter, notification) + expect(run_context.send(getter, notifying_resource)).to eq([notification]) + end + end + + describe "of the immediate kind" do + let(:setter) { :notifies_immediately } + let(:getter) { :immediate_notifications } + include_context "notifying resource is a Chef::Resource" + include_context "notifying resource is a subclass of Chef::Resource" + end - @run_context.cancel_reboot - expect(@run_context.reboot_info).to eq({}) - expect(@run_context.reboot_requested?).to be_false + describe "of the delayed kind" do + let(:setter) { :notifies_delayed } + let(:getter) { :delayed_notifications } + include_context "notifying resource is a Chef::Resource" + include_context "notifying resource is a subclass of Chef::Resource" end end end |