summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortyler-ball <tyleraball@gmail.com>2014-12-12 11:47:12 -0800
committertyler-ball <tyleraball@gmail.com>2014-12-16 17:59:56 -0800
commitbac1de423f35c6c6edf761d04217250f312302ff (patch)
tree00d9d6676da7fd0f0c63f7a4d58e9aad34988cfd
parentf0e75a337daa62608b9e816ef8bdc2335bf2b033 (diff)
downloadchef-bac1de423f35c6c6edf761d04217250f312302ff.tar.gz
Adding unit test coverage and updating per github review
Conflicts: spec/unit/recipe_spec.rb spec/unit/resource_spec.rb spec/unit/run_context_spec.rb Resolving cherry pick commit merge issues
-rw-r--r--lib/chef/resource.rb2
-rw-r--r--lib/chef/resource/resource_notification.rb (renamed from lib/chef/resource_notification.rb)18
-rw-r--r--spec/unit/recipe_spec.rb1
-rw-r--r--spec/unit/resource/resource_notification_spec.rb170
-rw-r--r--spec/unit/resource_spec.rb187
-rw-r--r--spec/unit/run_context_spec.rb110
6 files changed, 263 insertions, 225 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index 2e2bd2c84e..17f109242f 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -30,7 +30,7 @@ require 'chef/resource_collection'
require 'chef/node_map'
require 'chef/node'
require 'chef/platform'
-require 'chef/resource_notification'
+require 'chef/resource/resource_notification'
require 'chef/mixin/deprecation'
require 'chef/mixin/descendants_tracker'
diff --git a/lib/chef/resource_notification.rb b/lib/chef/resource/resource_notification.rb
index c02c268709..a27ed961c7 100644
--- a/lib/chef/resource_notification.rb
+++ b/lib/chef/resource/resource_notification.rb
@@ -1,3 +1,21 @@
+#
+# 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 'chef/resource'
class Chef
diff --git a/spec/unit/recipe_spec.rb b/spec/unit/recipe_spec.rb
index 1b7506d965..e1a42362ef 100644
--- a/spec/unit/recipe_spec.rb
+++ b/spec/unit/recipe_spec.rb
@@ -154,6 +154,7 @@ describe Chef::Recipe do
expect(zm_resource.recipe_name).to eq("test")
expect(zm_resource.cookbook_name).to eq("hjk")
expect(zm_resource.source_line).to include(__FILE__)
+ expect(zm_resource.declared_type).to eq(: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..7f6b124d4d
--- /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
+ expect(@notification.resource).to eq(:service_apache)
+ end
+
+ it "has an action to take on the service" do
+ expect(@notification.action).to eq(:restart)
+ end
+
+ it "has a notifying resource" do
+ expect(@notification.notifying_resource).to eq(: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)
+ expect(@notification.duplicates?(other)).to be_truthy
+ end
+
+ it "is not a duplicate of another notification if the actions differ" do
+ other = Chef::Resource::Notification.new(:service_apache, :enable, :install_apache)
+ expect(@notification.duplicates?(other)).to be_falsey
+ 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)
+ expect(@notification.duplicates?(other)).to be_falsey
+ end
+
+ it "raises an ArgumentError if you try to check a non-ducktype object for duplication" do
+ expect {@notification.duplicates?(:not_a_notification)}.to 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)
+ expect(@notification.resource).to eq(@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)
+ expect(@notification.resource).to eq(@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)
+ expect(@notification.notifying_resource).to eq(@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)
+ expect(@notification.resource).to eq(@keyboard_cat)
+ expect(@notification.notifying_resource).to eq(@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
+ expect {@notification.resolve_resource_reference(@resource_collection)}.to 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
+ expect {@notification.resolve_resource_reference(@resource_collection)}.to 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
+ expect {@notification.resolve_resource_reference(@resource_collection)}.to 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
+ expect {@notification.resolve_resource_reference(@resource_collection)}.to 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
+ expect {@notification.resolve_resource_reference(@resource_collection)}.to 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
+ expect {@notification.resolve_resource_reference(@resource_collection)}.to 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 335956009f..da14d20208 100644
--- a/spec/unit/resource_spec.rb
+++ b/spec/unit/resource_spec.rb
@@ -346,10 +346,10 @@ describe Chef::Resource do
it "should convert to a hash" do
hash = @resource.to_hash
expected_keys = [ :allowed_actions, :params, :provider, :updated,
- :updated_by_last_action, :before, :supports,
- :noop, :ignore_failure, :name, :source_line,
- :action, :retries, :retry_delay, :elapsed_time,
- :default_guard_interpreter, :guard_interpreter, :sensitive ]
+ :updated_by_last_action, :before, :supports,
+ :noop, :ignore_failure, :name, :source_line,
+ :action, :retries, :retry_delay, :elapsed_time,
+ :default_guard_interpreter, :guard_interpreter, :sensitive ]
expect(hash.keys - expected_keys).to eq([])
expect(expected_keys - hash.keys).to eq([])
expect(hash[:name]).to eql("funk")
@@ -671,8 +671,8 @@ describe Chef::Resource do
snitch_var1 = snitch_var2 = 0
@runner = Chef::Runner.new(@run_context)
Chef::Platform.set(
- :resource => :cat,
- :provider => Chef::Provider::SnakeOil
+ :resource => :cat,
+ :provider => Chef::Provider::SnakeOil
)
@resource1.only_if { snitch_var1 = 1 }
@@ -704,22 +704,22 @@ describe Chef::Resource do
it 'adds mappings for a single platform' do
expect(Chef::Resource.node_map).to receive(:set).with(
- :dinobot, Chef::Resource::Klz, { platform: ['autobots'] }
- )
+ :dinobot, Chef::Resource::Klz, { platform: ['autobots'] }
+ )
klz.provides :dinobot, platform: ['autobots']
end
it 'adds mappings for multiple platforms' do
expect(Chef::Resource.node_map).to receive(:set).with(
- :energy, Chef::Resource::Klz, { platform: ['autobots', 'decepticons']}
- )
+ :energy, Chef::Resource::Klz, { platform: ['autobots', 'decepticons']}
+ )
klz.provides :energy, platform: ['autobots', 'decepticons']
end
it 'adds mappings for all platforms' do
expect(Chef::Resource.node_map).to receive(:set).with(
- :tape_deck, Chef::Resource::Klz, {}
- )
+ :tape_deck, Chef::Resource::Klz, {}
+ )
klz.provides :tape_deck
end
@@ -824,8 +824,8 @@ describe Chef::Resource do
describe "resource sensitive attribute" do
before(:each) do
- @resource_file = Chef::Resource::File.new("/nonexistent/CHEF-5098/file", @run_context)
- @action = :create
+ @resource_file = Chef::Resource::File.new("/nonexistent/CHEF-5098/file", @run_context)
+ @action = :create
end
def compiled_resource_data(resource, action, err)
@@ -842,167 +842,16 @@ describe Chef::Resource do
it "when set to false should show compiled resource for failed resource" do
expect { @resource_file.run_action(@action) }.to raise_error { |err|
- expect(compiled_resource_data(@resource_file, @action, err)).to match 'path "/nonexistent/CHEF-5098/file"'
- }
+ expect(compiled_resource_data(@resource_file, @action, err)).to match 'path "/nonexistent/CHEF-5098/file"'
+ }
end
it "when set to true should show compiled resource for failed resource" do
@resource_file.sensitive true
expect { @resource_file.run_action(@action) }.to raise_error { |err|
- expect(compiled_resource_data(@resource_file, @action, err)).to eql("suppressed sensitive resource output")
- }
+ expect(compiled_resource_data(@resource_file, @action, err)).to eql("suppressed sensitive resource output")
+ }
end
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
- expect(@notification.resource).to eq(:service_apache)
- end
-
- it "has an action to take on the service" do
- expect(@notification.action).to eq(:restart)
- end
-
- it "has a notifying resource" do
- expect(@notification.notifying_resource).to eq(: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)
- expect(@notification.duplicates?(other)).to be_truthy
- end
-
- it "is not a duplicate of another notification if the actions differ" do
- other = Chef::Resource::Notification.new(:service_apache, :enable, :install_apache)
- expect(@notification.duplicates?(other)).to be_falsey
- 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)
- expect(@notification.duplicates?(other)).to be_falsey
- end
-
- it "raises an ArgumentError if you try to check a non-ducktype object for duplication" do
- expect {@notification.duplicates?(:not_a_notification)}.to 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)
- expect(@notification.resource).to eq(@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)
- expect(@notification.resource).to eq(@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)
- expect(@notification.notifying_resource).to eq(@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)
- expect(@notification.resource).to eq(@keyboard_cat)
- expect(@notification.notifying_resource).to eq(@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
- expect {@notification.resolve_resource_reference(@resource_collection)}.to 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
- expect {@notification.resolve_resource_reference(@resource_collection)}.to 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
- expect {@notification.resolve_resource_reference(@resource_collection)}.to 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
- expect {@notification.resolve_resource_reference(@resource_collection)}.to 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
- expect {@notification.resolve_resource_reference(@resource_collection)}.to 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
- expect {@notification.resolve_resource_reference(@resource_collection)}.to 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 cc112f332a..ba4f2ede68 100644
--- a/spec/unit/run_context_spec.rb
+++ b/spec/unit/run_context_spec.rb
@@ -21,19 +21,24 @@
require 'spec_helper'
require 'support/lib/library_load_order'
-
describe Chef::RunContext do
+ 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
+ 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) }
+
before(:each) do
@original_log_level = Chef::Log.level
Chef::Log.level = :debug
- @chef_repo_path = File.expand_path(File.join(CHEF_SPEC_DATA, "run_context", "cookbooks"))
- 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
after(:each) do
@@ -41,11 +46,11 @@ describe Chef::RunContext do
end
it "has a cookbook collection" do
- expect(@run_context.cookbook_collection).to eq(@cookbook_collection)
+ expect(run_context.cookbook_collection).to eq(cookbook_collection)
end
it "has a node" do
- expect(@run_context.node).to eq(@node)
+ expect(run_context.node).to eq(node)
end
describe "loading cookbooks for a run list" do
@@ -57,44 +62,44 @@ describe Chef::RunContext do
Chef::Provider.send(:remove_const, :TestProvider)
end
- @node.run_list << "test" << "test::one" << "test::two"
- expect(@node).to receive(:loaded_recipe).with(:test, "default")
- expect(@node).to receive(:loaded_recipe).with(:test, "one")
- expect(@node).to receive(:loaded_recipe).with(:test, "two")
- @run_context.load(@node.run_list.expand('_default'))
+ node.run_list << "test" << "test::one" << "test::two"
+ expect(node).to receive(:loaded_recipe).with(:test, "default")
+ expect(node).to receive(:loaded_recipe).with(:test, "one")
+ expect(node).to 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
- expect(@run_context.definitions).to have_key(:new_cat)
- expect(@run_context.definitions).to have_key(:new_badger)
- expect(@run_context.definitions).to have_key(:new_dog)
+ expect(run_context.definitions).to have_key(:new_cat)
+ expect(run_context.definitions).to have_key(:new_badger)
+ expect(run_context.definitions).to have_key(:new_dog)
end
it "should load all the recipes specified for this node" do
- expect(@run_context.resource_collection[0].to_s).to eq("cat[einstein]")
- expect(@run_context.resource_collection[1].to_s).to eq("cat[loulou]")
- expect(@run_context.resource_collection[2].to_s).to eq("cat[birthday]")
- expect(@run_context.resource_collection[3].to_s).to eq("cat[peanut]")
- expect(@run_context.resource_collection[4].to_s).to eq("cat[fat peanut]")
+ expect(run_context.resource_collection[0].to_s).to eq("cat[einstein]")
+ expect(run_context.resource_collection[1].to_s).to eq("cat[loulou]")
+ expect(run_context.resource_collection[2].to_s).to eq("cat[birthday]")
+ expect(run_context.resource_collection[3].to_s).to eq("cat[peanut]")
+ expect(run_context.resource_collection[4].to_s).to eq("cat[fat peanut]")
end
it "loads all the attribute files in the cookbook collection" do
- expect(@run_context.loaded_fully_qualified_attribute?("test", "george")).to be_truthy
- expect(@node[:george]).to eq("washington")
+ expect(run_context.loaded_fully_qualified_attribute?("test", "george")).to be_truthy
+ expect(node[:george]).to eq("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.
- expect(@node).not_to receive(:from_file)
- @node.include_attribute("test::george")
+ expect(node).not_to 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
- expect(@node).to receive(:loaded_recipe).with(:ancient, "aliens")
+ expect(node).to receive(:loaded_recipe).with(:ancient, "aliens")
expect do
- @run_context.include_recipe("ancient::aliens")
+ run_context.include_recipe("ancient::aliens")
# In CHEF-5120, this becomes a Chef::Exceptions::MissingCookbookDependency error:
end.to raise_error(Chef::Exceptions::CookbookNotFound)
end
@@ -102,39 +107,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
- expect(@run_context).to have_template_in_cookbook("openldap", "test.erb")
- expect(@run_context).not_to have_template_in_cookbook("openldap", "missing.erb")
+ expect(run_context).to have_template_in_cookbook("openldap", "test.erb")
+ expect(run_context).not_to 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
- expect(@run_context).to have_cookbook_file_in_cookbook("java", "java.response")
- expect(@run_context).not_to have_cookbook_file_in_cookbook("java", "missing.txt")
+ expect(run_context).to have_cookbook_file_in_cookbook("java", "java.response")
+ expect(run_context).not_to 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
@@ -145,13 +145,13 @@ 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_truthy
+ run_context.request_reboot(expected)
+ expect(run_context.reboot_info).to eq(expected)
+ expect(run_context.reboot_requested?).to be_truthy
- @run_context.cancel_reboot
- expect(@run_context.reboot_info).to eq({})
- expect(@run_context.reboot_requested?).to be_falsey
+ run_context.cancel_reboot
+ expect(run_context.reboot_info).to eq({})
+ expect(run_context.reboot_requested?).to be_falsey
end
end
end