diff options
author | Daniel DeLeo <dan@opscode.com> | 2010-11-05 15:32:11 -0700 |
---|---|---|
committer | Daniel DeLeo <dan@opscode.com> | 2010-11-05 15:32:11 -0700 |
commit | bd32a4e17573b5821a3dd5f5137cd6808f0306e2 (patch) | |
tree | 11bc99f3442f8507c4c1911fb35483f39b573db9 | |
parent | a9c67f23b8921bd05d954810d5069930562b83a0 (diff) | |
download | chef-bd32a4e17573b5821a3dd5f5137cd6808f0306e2.tar.gz |
make context explicit in specs so they run correctly in any order
and also fix a bug in shell out's environment variable setting
Conflicts:
chef/spec/unit/application/knife_spec.rb
-rw-r--r-- | chef/lib/chef/knife.rb | 5 | ||||
-rw-r--r-- | chef/lib/chef/shell_out.rb | 2 | ||||
-rw-r--r-- | chef/spec/spec_helper.rb | 2 | ||||
-rw-r--r-- | chef/spec/unit/application/client_spec.rb | 2 | ||||
-rw-r--r-- | chef/spec/unit/application/knife_spec.rb | 10 | ||||
-rw-r--r-- | chef/spec/unit/application_spec.rb | 343 | ||||
-rw-r--r-- | chef/spec/unit/checksum_spec.rb | 2 | ||||
-rw-r--r-- | chef/spec/unit/client_spec.rb | 2 | ||||
-rw-r--r-- | chef/spec/unit/cookbook/syntax_check_spec.rb | 2 | ||||
-rw-r--r-- | chef/spec/unit/knife/bootstrap_spec.rb | 2 | ||||
-rw-r--r-- | chef/spec/unit/knife/client_bulk_delete_spec.rb | 2 | ||||
-rw-r--r-- | chef/spec/unit/knife/configure_spec.rb | 2 | ||||
-rw-r--r-- | chef/spec/unit/knife/cookbook_bulk_delete_spec.rb | 2 | ||||
-rw-r--r-- | chef/spec/unit/knife/node_bulk_delete_spec.rb | 2 | ||||
-rw-r--r-- | chef/spec/unit/knife_spec.rb | 17 | ||||
-rw-r--r-- | chef/spec/unit/lwrp_spec.rb | 229 | ||||
-rw-r--r-- | chef/spec/unit/mixin/deprecation_spec.rb | 2 |
17 files changed, 361 insertions, 267 deletions
diff --git a/chef/lib/chef/knife.rb b/chef/lib/chef/knife.rb index 9e4c2f46b8..1351b77326 100644 --- a/chef/lib/chef/knife.rb +++ b/chef/lib/chef/knife.rb @@ -38,6 +38,11 @@ class Chef puts msg end + def self.reset_subcommands! + @@subcommands = {} + @subcommands_by_category = nil + end + def self.inherited(subclass) unless subclass.unnamed? subcommands[subclass.snake_case_name] = subclass diff --git a/chef/lib/chef/shell_out.rb b/chef/lib/chef/shell_out.rb index fad6d0f704..ba69b5093d 100644 --- a/chef/lib/chef/shell_out.rb +++ b/chef/lib/chef/shell_out.rb @@ -257,7 +257,7 @@ class Chef self.valid_exit_codes = Array(setting) when 'environment', 'env' # passing :environment => nil means don't set any new ENV vars - setting.nil? ? @environment = {} : @environment.merge!(setting) + @environment = setting.nil? ? {} : @environment.dup.merge!(setting) else raise Chef::Exceptions::InvalidCommandOption, "option '#{option.inspect}' is not a valid option for #{self.class.name}" end diff --git a/chef/spec/spec_helper.rb b/chef/spec/spec_helper.rb index b26396933d..c0e05cf7f0 100644 --- a/chef/spec/spec_helper.rb +++ b/chef/spec/spec_helper.rb @@ -25,6 +25,8 @@ end require 'rubygems' $:.unshift(File.join(File.dirname(__FILE__), "..", "lib")) + +$:.unshift(File.expand_path("../lib", __FILE__)) $:.unshift(File.dirname(__FILE__)) require 'chef' diff --git a/chef/spec/unit/application/client_spec.rb b/chef/spec/unit/application/client_spec.rb index 843dea355f..e92b2458ef 100644 --- a/chef/spec/unit/application/client_spec.rb +++ b/chef/spec/unit/application/client_spec.rb @@ -28,6 +28,8 @@ describe Chef::Application::Client, "reconfigure" do Chef::Config[:json_attribs] = nil Chef::Config[:interval] = 10 Chef::Config[:splay] = nil + + Chef::Config[:once] = false end after do diff --git a/chef/spec/unit/application/knife_spec.rb b/chef/spec/unit/application/knife_spec.rb index a89446e010..ab7c7019e3 100644 --- a/chef/spec/unit/application/knife_spec.rb +++ b/chef/spec/unit/application/knife_spec.rb @@ -20,6 +20,13 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_hel require "#{CHEF_SPEC_DATA}/knife_subcommand/test_yourself" describe Chef::Application::Knife do + before(:all) do + class NoopKnifeCommand < Chef::Knife + def run + end + end + end + before(:each) do @knife = Chef::Application::Knife.new @knife.stub!(:puts) @@ -41,12 +48,13 @@ describe Chef::Application::Knife do end it "should run a sub command with the applications command line option prototype" do - with_argv(*%w{test yourself with some args}) do + with_argv(*%w{noop knife command with some args}) do knife = mock(Chef::Knife, :null_object => true) Chef::Knife.should_receive(:run).with(ARGV, @knife.options).and_return(knife) @knife.should_receive(:exit).with(0) @knife.run end end + end end diff --git a/chef/spec/unit/application_spec.rb b/chef/spec/unit/application_spec.rb index 30d9c445dc..3319cfe500 100644 --- a/chef/spec/unit/application_spec.rb +++ b/chef/spec/unit/application_spec.rb @@ -17,218 +17,225 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper")) -describe Chef::Application, "initialize" do +describe Chef::Application do before do - @app = Chef::Application.new - Dir.stub!(:chdir).and_return(0) - end - - it "should create an instance of Chef::Application" do - @app.should be_kind_of(Chef::Application) + Chef::Log.logger = Logger.new(StringIO.new) end -end - -describe Chef::Application, "reconfigure" do - before do - @app = Chef::Application.new - @app.stub!(:configure_chef).and_return(true) - @app.stub!(:configure_logging).and_return(true) - end - - it "should configure chef" do - @app.should_receive(:configure_chef).and_return(true) - @app.reconfigure - end - - it "should configure logging" do - @app.should_receive(:configure_logging).and_return(true) - @app.reconfigure - end - -end - -describe Chef::Application do - before do - @app = Chef::Application.new + describe "initialize" do + before do + @app = Chef::Application.new + Dir.stub!(:chdir).and_return(0) + end + + it "should create an instance of Chef::Application" do + @app.should be_kind_of(Chef::Application) + end end - - describe "run" do + + + describe "reconfigure" do before do - @app.stub!(:setup_application).and_return(true) - @app.stub!(:run_application).and_return(true) + @app = Chef::Application.new @app.stub!(:configure_chef).and_return(true) @app.stub!(:configure_logging).and_return(true) end - - it "should reconfigure the application before running" do - @app.should_receive(:reconfigure).and_return(true) - @app.run - end - - it "should setup the application before running it" do - @app.should_receive(:setup_application).and_return(true) - @app.run + + it "should configure chef" do + @app.should_receive(:configure_chef).and_return(true) + @app.reconfigure end - - it "should run the actual application" do - @app.should_receive(:run_application).and_return(true) - @app.run + + it "should configure logging" do + @app.should_receive(:configure_logging).and_return(true) + @app.reconfigure end - end -end -describe Chef::Application, "configure_chef" do - before do - @app = Chef::Application.new - #Chef::Config.stub!(:merge!).and_return(true) - @app.stub!(:parse_options).and_return(true) - end - - it "should parse the commandline options" do - @app.should_receive(:parse_options).and_return(true) - @app.configure_chef end - describe "when a config_file is present" do + describe Chef::Application do before do - Chef::Config.configuration.delete('rspec_ran') - - @config_file = Tempfile.new("rspec-chef-config") - @config_file.puts("rspec_ran('true')") - @config_file.close - - @app.config[:config_file] = "/etc/chef/default.rb" + @app = Chef::Application.new end - after do - @config_file.unlink + describe "run" do + before do + @app.stub!(:setup_application).and_return(true) + @app.stub!(:run_application).and_return(true) + @app.stub!(:configure_chef).and_return(true) + @app.stub!(:configure_logging).and_return(true) + end + + it "should reconfigure the application before running" do + @app.should_receive(:reconfigure).and_return(true) + @app.run + end + + it "should setup the application before running it" do + @app.should_receive(:setup_application).and_return(true) + @app.run + end + + it "should run the actual application" do + @app.should_receive(:run_application).and_return(true) + @app.run + end end + end - it "should configure chef::config from a file" do - File.should_receive(:open).with("/etc/chef/default.rb").and_yield(@config_file) - Chef::Config.should_receive(:from_file).with(@config_file.path) - @app.configure_chef + describe "configure_chef" do + before do + @app = Chef::Application.new + #Chef::Config.stub!(:merge!).and_return(true) + @app.stub!(:parse_options).and_return(true) end - it "should merge the local config hash into chef::config" do - File.should_receive(:open).with("/etc/chef/default.rb").and_yield(@config_file) + it "should parse the commandline options" do + @app.should_receive(:parse_options).and_return(true) @app.configure_chef - Chef::Config.rspec_ran.should == "true" end - end - - describe "when there is no config_file defined" do - before do - @app.config[:config_file] = nil + describe "when a config_file is present" do + before do + Chef::Config.configuration.delete('rspec_ran') + + @config_file = Tempfile.new("rspec-chef-config") + @config_file.puts("rspec_ran('true')") + @config_file.close + + @app.config[:config_file] = "/etc/chef/default.rb" + end + + after do + @config_file.unlink + end + + it "should configure chef::config from a file" do + File.should_receive(:open).with("/etc/chef/default.rb").and_yield(@config_file) + Chef::Config.should_receive(:from_file).with(@config_file.path) + @app.configure_chef + end + + it "should merge the local config hash into chef::config" do + File.should_receive(:open).with("/etc/chef/default.rb").and_yield(@config_file) + @app.configure_chef + Chef::Config.rspec_ran.should == "true" + end + end - - it "should configure chef::config from a file" do - Chef::Config.should_not_receive(:from_file).with("/etc/chef/default.rb") - @app.configure_chef + + describe "when there is no config_file defined" do + before do + @app.config[:config_file] = nil + end + + it "should configure chef::config from a file" do + Chef::Config.should_not_receive(:from_file).with("/etc/chef/default.rb") + @app.configure_chef + end end - end - - describe "when the config_file is an URL" do - before do - Chef::Config.configuration.delete('rspec_ran') - - @app.config[:config_file] = "http://example.com/foo.rb" - @config_file = Tempfile.new("rspec-chef-config") - @config_file.puts("rspec_ran('true')") - @config_file.close + describe "when the config_file is an URL" do + before do + Chef::Config.configuration.delete('rspec_ran') + @app.config[:config_file] = "http://example.com/foo.rb" - @cf = mock("cf") - #@cf.stub!(:path).and_return("/tmp/some/path") - #@cf.stub!(:nil?).and_return(false) - @rest = mock("rest") - #@rest.stub!(:get_rest).and_return(@rest) - #@rest.stub!(:open).and_yield(@cf) - Chef::REST.stub!(:new).and_return(@rest) - end + @config_file = Tempfile.new("rspec-chef-config") + @config_file.puts("rspec_ran('true')") + @config_file.close - after {@config_file.unlink} - it "should configure chef::config from an URL" do - Chef::REST.should_receive(:new).with("", nil, nil).at_least(1).times.and_return(@rest) - @rest.should_receive(:fetch).with("http://example.com/foo.rb").and_yield(@config_file) - @app.configure_chef - Chef::Config.rspec_ran.should == "true" + @cf = mock("cf") + #@cf.stub!(:path).and_return("/tmp/some/path") + #@cf.stub!(:nil?).and_return(false) + @rest = mock("rest") + #@rest.stub!(:get_rest).and_return(@rest) + #@rest.stub!(:open).and_yield(@cf) + Chef::REST.stub!(:new).and_return(@rest) + end + + after {@config_file.unlink} + + it "should configure chef::config from an URL" do + Chef::REST.should_receive(:new).with("", nil, nil).at_least(1).times.and_return(@rest) + @rest.should_receive(:fetch).with("http://example.com/foo.rb").and_yield(@config_file) + @app.configure_chef + Chef::Config.rspec_ran.should == "true" + end end end -end -describe Chef::Application, "configure_logging" do - before do - @app = Chef::Application.new - Chef::Log.stub!(:init) - Chef::Log.stub!(:level=) - end - - it "should initialise the chef logger" do - Chef::Log.should_receive(:init).with(Chef::Config[:log_location]).and_return(true) - @app.configure_logging - end + describe "configure_logging" do + before do + @app = Chef::Application.new + Chef::Log.stub!(:init) + Chef::Log.stub!(:level=) + end - it "should initialise the chef logger level" do - Chef::Log.should_receive(:level=).with(Chef::Config[:log_level]).and_return(true) - @app.configure_logging - end + it "should initialise the chef logger" do + Chef::Log.should_receive(:init).with(Chef::Config[:log_location]).and_return(true) + @app.configure_logging + end -end + it "should initialise the chef logger level" do + Chef::Log.should_receive(:level=).with(Chef::Config[:log_level]).and_return(true) + @app.configure_logging + end -describe Chef::Application, "class method: fatal!" do - before do - STDERR.stub!(:puts).with("FATAL: blah").and_return(true) - Chef::Log.stub!(:fatal).with("blah").and_return(true) - Process.stub!(:exit).and_return(true) - end - - it "should log an error message to the logger" do - Chef::Log.should_receive(:fatal).with("blah").and_return(true) - Chef::Application.fatal! "blah" end - it "should log an error message on STDERR" do - STDERR.should_receive(:puts).with("FATAL: blah").and_return(true) - Chef::Application.fatal! "blah" - end - - describe "when an exit code is supplied" do - it "should exit with the given exit code" do - Process.should_receive(:exit).with(-100).and_return(true) - Chef::Application.fatal! "blah", -100 + describe "class method: fatal!" do + before do + STDERR.stub!(:puts).with("FATAL: blah").and_return(true) + Chef::Log.stub!(:fatal).with("blah").and_return(true) + Process.stub!(:exit).and_return(true) end - end - - describe "when an exit code is not supplied" do - it "should exit with the default exit code" do - Process.should_receive(:exit).with(-1).and_return(true) + + it "should log an error message to the logger" do + Chef::Log.should_receive(:fatal).with("blah").and_return(true) Chef::Application.fatal! "blah" end - end - -end -describe Chef::Application, "setup_application" do - before do - @app = Chef::Application.new - end - - it "should raise an error" do - lambda { @app.setup_application }.should raise_error(Chef::Exceptions::Application) + it "should log an error message on STDERR" do + STDERR.should_receive(:puts).with("FATAL: blah").and_return(true) + Chef::Application.fatal! "blah" + end + + describe "when an exit code is supplied" do + it "should exit with the given exit code" do + Process.should_receive(:exit).with(-100).and_return(true) + Chef::Application.fatal! "blah", -100 + end + end + + describe "when an exit code is not supplied" do + it "should exit with the default exit code" do + Process.should_receive(:exit).with(-1).and_return(true) + Chef::Application.fatal! "blah" + end + end + end -end -describe Chef::Application, "run_application" do - before do - @app = Chef::Application.new + describe "setup_application" do + before do + @app = Chef::Application.new + end + + it "should raise an error" do + lambda { @app.setup_application }.should raise_error(Chef::Exceptions::Application) + end end - - it "should raise an error" do - lambda { @app.run_application }.should raise_error(Chef::Exceptions::Application) + + describe "run_application" do + before do + @app = Chef::Application.new + end + + it "should raise an error" do + lambda { @app.run_application }.should raise_error(Chef::Exceptions::Application) + end end + end diff --git a/chef/spec/unit/checksum_spec.rb b/chef/spec/unit/checksum_spec.rb index 4fa5f36f9e..d2740dae64 100644 --- a/chef/spec/unit/checksum_spec.rb +++ b/chef/spec/unit/checksum_spec.rb @@ -22,6 +22,8 @@ require 'chef/checksum' describe Chef::Checksum do before do + Chef::Log.logger = Logger.new(StringIO.new) + @now = Time.now Time.stub!(:now).and_return(@now) diff --git a/chef/spec/unit/client_spec.rb b/chef/spec/unit/client_spec.rb index 40dc49ffca..8ae171dd37 100644 --- a/chef/spec/unit/client_spec.rb +++ b/chef/spec/unit/client_spec.rb @@ -25,6 +25,8 @@ require 'chef/rest' describe Chef::Client do before do + Chef::Log.logger = Logger.new(StringIO.new) + # Node/Ohai data @hostname = "hostname" @fqdn = "hostname.example.org" diff --git a/chef/spec/unit/cookbook/syntax_check_spec.rb b/chef/spec/unit/cookbook/syntax_check_spec.rb index 35cded7c3e..770afff452 100644 --- a/chef/spec/unit/cookbook/syntax_check_spec.rb +++ b/chef/spec/unit/cookbook/syntax_check_spec.rb @@ -83,6 +83,8 @@ require "chef/cookbook/syntax_check" describe Chef::Cookbook::SyntaxCheck do before do + Chef::Log.logger = Logger.new(StringIO.new) + @cookbook_path = File.join(CHEF_SPEC_DATA, 'cookbooks', 'openldap') @attr_files = %w{default.rb smokey.rb}.map { |f| File.join(@cookbook_path, 'attributes', f) } diff --git a/chef/spec/unit/knife/bootstrap_spec.rb b/chef/spec/unit/knife/bootstrap_spec.rb index ab6a1af330..3260e67dc1 100644 --- a/chef/spec/unit/knife/bootstrap_spec.rb +++ b/chef/spec/unit/knife/bootstrap_spec.rb @@ -22,6 +22,8 @@ require 'net/ssh' describe Chef::Knife::Bootstrap do before(:each) do + Chef::Log.logger = Logger.new(StringIO.new) + @knife = Chef::Knife::Bootstrap.new @knife.config[:template_file] = File.expand_path(File.join(CHEF_SPEC_DATA, "bootstrap", "test.erb")) end diff --git a/chef/spec/unit/knife/client_bulk_delete_spec.rb b/chef/spec/unit/knife/client_bulk_delete_spec.rb index 609c6a6ada..10cb73ab9e 100644 --- a/chef/spec/unit/knife/client_bulk_delete_spec.rb +++ b/chef/spec/unit/knife/client_bulk_delete_spec.rb @@ -20,6 +20,8 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_hel describe Chef::Knife::ClientBulkDelete do before(:each) do + Chef::Log.logger = Logger.new(StringIO.new) + Chef::Config[:node_name] = "webmonkey.example.com" @knife = Chef::Knife::ClientBulkDelete.new @knife.config = { diff --git a/chef/spec/unit/knife/configure_spec.rb b/chef/spec/unit/knife/configure_spec.rb index 56fc9e8d05..801e3670f4 100644 --- a/chef/spec/unit/knife/configure_spec.rb +++ b/chef/spec/unit/knife/configure_spec.rb @@ -2,6 +2,8 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') describe Chef::Knife::Configure do before do + Chef::Log.logger = Logger.new(StringIO.new) + Chef::Config[:node_name] = "webmonkey.example.com" @knife = Chef::Knife::Configure.new @rest_client = mock("null rest client", :post_rest => { :result => :true }) diff --git a/chef/spec/unit/knife/cookbook_bulk_delete_spec.rb b/chef/spec/unit/knife/cookbook_bulk_delete_spec.rb index a85b53f0f4..49cdc14cd0 100644 --- a/chef/spec/unit/knife/cookbook_bulk_delete_spec.rb +++ b/chef/spec/unit/knife/cookbook_bulk_delete_spec.rb @@ -20,6 +20,8 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_hel describe Chef::Knife::CookbookBulkDelete do before(:each) do + Chef::Log.logger = Logger.new(StringIO.new) + Chef::Config[:node_name] = "webmonkey.example.com" @knife = Chef::Knife::CookbookBulkDelete.new @knife.config = {:print_after => nil} diff --git a/chef/spec/unit/knife/node_bulk_delete_spec.rb b/chef/spec/unit/knife/node_bulk_delete_spec.rb index e7984f0794..59d387a9df 100644 --- a/chef/spec/unit/knife/node_bulk_delete_spec.rb +++ b/chef/spec/unit/knife/node_bulk_delete_spec.rb @@ -20,6 +20,8 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_hel describe Chef::Knife::NodeBulkDelete do before(:each) do + Chef::Log.logger = Logger.new(StringIO.new) + Chef::Config[:node_name] = "webmonkey.example.com" @knife = Chef::Knife::NodeBulkDelete.new @knife.config = { diff --git a/chef/spec/unit/knife_spec.rb b/chef/spec/unit/knife_spec.rb index 770a1f7e44..dedf929042 100644 --- a/chef/spec/unit/knife_spec.rb +++ b/chef/spec/unit/knife_spec.rb @@ -24,6 +24,8 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper")) describe Chef::Knife do before(:each) do + Chef::Log.logger = Logger.new(StringIO.new) + Chef::Config[:node_name] = "webmonkey.example.com" @knife = Chef::Knife.new @knife.stub!(:puts) @@ -40,6 +42,8 @@ describe Chef::Knife do describe "after loading a subcommand" do before do + Chef::Knife.reset_subcommands! + if KnifeSpecs.const_defined?(:TestNameMapping) KnifeSpecs.send(:remove_const, :TestNameMapping) end @@ -76,12 +80,18 @@ describe Chef::Knife do describe "after loading all subcommands" do before do + Chef::Knife.reset_subcommands! Chef::Knife.load_commands end it "references a subcommand class by its snake cased name" do - Chef::Knife.subcommands.should have_key("node_show") - Chef::Knife.subcommands["node_show"].should == Chef::Knife::NodeShow + class SuperAwesomeCommand < Chef::Knife + end + + Chef::Knife.load_commands + + Chef::Knife.subcommands.should have_key("super_awesome_command") + Chef::Knife.subcommands["super_awesome_command"].should == SuperAwesomeCommand end it "lists all of the commands" do @@ -219,7 +229,8 @@ describe Chef::Knife do describe "confirm" do before(:each) do @question = "monkeys rule" - Kernel.stub!(:print).and_return(true) + @stdout = StringIO.new + @knife.stub(:stdout).and_return(@stdout) STDIN.stub!(:readline).and_return("y") end diff --git a/chef/spec/unit/lwrp_spec.rb b/chef/spec/unit/lwrp_spec.rb index 72a8af2265..738c8dac2a 100644 --- a/chef/spec/unit/lwrp_spec.rb +++ b/chef/spec/unit/lwrp_spec.rb @@ -44,114 +44,155 @@ describe "override logging" do end -describe "Light-weight Chef::Resource" do - - it "should load the resource into a properly-named class" do - Chef::Resource.const_get("LwrpFoo").should be_kind_of(Class) - end - - it "should set resource_name" do - Chef::Resource::LwrpFoo.new("blah").resource_name.should eql(:lwrp_foo) - end - - it "should add the specified actions to the allowed_actions array" do - Chef::Resource::LwrpFoo.new("blah").allowed_actions.should include(:pass_buck, :twiddle_thumbs) - end - - it "should create a method for each attribute" do - Chef::Resource::LwrpFoo.new("blah").methods.map{ |m| m.to_sym}.should include(:monkey) +describe "LWRP" do + before do + @original_VERBOSE = $VERBOSE + $VERBOSE = nil end - it "should build attribute methods that respect validation rules" do - lambda { Chef::Resource::LwrpFoo.new("blah").monkey(42) }.should raise_error(ArgumentError) + after do + $VERBOSE = @original_VERBOSE end - -end -describe "Light-weight Chef::Provider" do - before(:each) do - node = Chef::Node.new - node.platform(:ubuntu) - node.platform_version('8.10') - @run_context = Chef::RunContext.new(node, Chef::CookbookCollection.new({})) + describe "Light-weight Chef::Resource" do - @runner = Chef::Runner.new(@run_context) - end - - - it "should load the provider into a properly-named class" do - Chef::Provider.const_get("LwrpBuckPasser").should be_kind_of(Class) - end - - it "should create a method for each attribute" do - new_resource = mock("new resource", :null_object=>true) - Chef::Provider::LwrpBuckPasser.new(nil, new_resource).methods.map{|m|m.to_sym}.should include(:action_pass_buck) - Chef::Provider::LwrpThumbTwiddler.new(nil, new_resource).methods.map{|m|m.to_sym}.should include(:action_twiddle_thumbs) - end + before do - it "should insert resources embedded in the provider into the middle of the resource collection" do - injector = Chef::Resource::LwrpFoo.new("morpheus", @run_context) - injector.action(:pass_buck) - injector.provider(:lwrp_buck_passer) - dummy = Chef::Resource::ZenMaster.new("keanu reeves", @run_context) - dummy.provider(Chef::Provider::Easy) - @run_context.resource_collection.insert(injector) - @run_context.resource_collection.insert(dummy) + Dir[File.expand_path(File.join(File.dirname(__FILE__), "..", "data", "lwrp", "resources", "*"))].each do |file| + Chef::Resource.build_from_file("lwrp", file) + end - Chef::Runner.new(@run_context).converge - - @run_context.resource_collection[0].should eql(injector) - @run_context.resource_collection[1].name.should eql(:prepared_thumbs) - @run_context.resource_collection[2].name.should eql(:twiddled_thumbs) - @run_context.resource_collection[3].should eql(dummy) - end - - it "should insert embedded resources from multiple providers, including from the last position, properly into the resource collection" do - injector = Chef::Resource::LwrpFoo.new("morpheus", @run_context) - injector.action(:pass_buck) - injector.provider(:lwrp_buck_passer) - injector2 = Chef::Resource::LwrpBar.new("tank", @run_context) - injector2.action(:pass_buck) - injector2.provider(:lwrp_buck_passer_2) - dummy = Chef::Resource::ZenMaster.new("keanu reeves", @run_context) - dummy.provider(Chef::Provider::Easy) - - @run_context.resource_collection.insert(injector) - @run_context.resource_collection.insert(dummy) - @run_context.resource_collection.insert(injector2) - - Chef::Runner.new(@run_context).converge - - @run_context.resource_collection[0].should eql(injector) - @run_context.resource_collection[1].name.should eql(:prepared_thumbs) - @run_context.resource_collection[2].name.should eql(:twiddled_thumbs) - @run_context.resource_collection[3].should eql(dummy) - @run_context.resource_collection[4].should eql(injector2) - @run_context.resource_collection[5].name.should eql(:prepared_eyes) - @run_context.resource_collection[6].name.should eql(:dried_paint_watched) - end + Dir[File.expand_path(File.join(File.dirname(__FILE__), "..", "data", "lwrp_override", "resources", "*"))].each do |file| + Chef::Resource.build_from_file("lwrp", file) + end - it "should properly handle a new_resource reference" do - resource = Chef::Resource::LwrpFoo.new("morpheus") - resource.monkey("bob") - resource.provider(:lwrp_monkey_name_printer) + end + + it "should load the resource into a properly-named class" do + Chef::Resource.const_get("LwrpFoo").should be_kind_of(Class) + end - provider = @runner.build_provider(resource) - provider.action_twiddle_thumbs + it "should set resource_name" do + Chef::Resource::LwrpFoo.new("blah").resource_name.should eql(:lwrp_foo) + end + + it "should add the specified actions to the allowed_actions array" do + Chef::Resource::LwrpFoo.new("blah").allowed_actions.should include(:pass_buck, :twiddle_thumbs) + end + + it "should create a method for each attribute" do + Chef::Resource::LwrpFoo.new("blah").methods.map{ |m| m.to_sym}.should include(:monkey) + end + + it "should build attribute methods that respect validation rules" do + lambda { Chef::Resource::LwrpFoo.new("blah").monkey(42) }.should raise_error(ArgumentError) + end - provider.monkey_name.should == "my monkey's name is 'bob'" end - it "should properly handle an embedded Resource accessing the enclosing Provider's scope" do + describe "Light-weight Chef::Provider" do + before(:each) do + Dir[File.expand_path(File.join(File.dirname(__FILE__), "..", "data", "lwrp", "resources", "*"))].each do |file| + Chef::Resource.build_from_file("lwrp", file) + end - resource = Chef::Resource::LwrpFoo.new("morpheus") - resource.monkey("bob") - resource.provider(:lwrp_embedded_resource_accesses_providers_scope) - - provider = @runner.build_provider(resource) - provider.action_twiddle_thumbs + Dir[File.expand_path(File.join(File.dirname(__FILE__), "..", "data", "lwrp_override", "resources", "*"))].each do |file| + Chef::Resource.build_from_file("lwrp", file) + end + + Dir[File.expand_path(File.join(File.dirname(__FILE__), "..", "data", "lwrp", "providers", "*"))].each do |file| + Chef::Provider.build_from_file("lwrp", file) + end + + Dir[File.expand_path(File.join(File.dirname(__FILE__), "..", "data", "lwrp_override", "providers", "*"))].each do |file| + Chef::Provider.build_from_file("lwrp", file) + end + + + node = Chef::Node.new + node.platform(:ubuntu) + node.platform_version('8.10') + @run_context = Chef::RunContext.new(node, Chef::CookbookCollection.new({})) + + @runner = Chef::Runner.new(@run_context) + end + + + it "should load the provider into a properly-named class" do + Chef::Provider.const_get("LwrpBuckPasser").should be_kind_of(Class) + end + + it "should create a method for each attribute" do + new_resource = mock("new resource", :null_object=>true) + Chef::Provider::LwrpBuckPasser.new(nil, new_resource).methods.map{|m|m.to_sym}.should include(:action_pass_buck) + Chef::Provider::LwrpThumbTwiddler.new(nil, new_resource).methods.map{|m|m.to_sym}.should include(:action_twiddle_thumbs) + end + + it "should insert resources embedded in the provider into the middle of the resource collection" do + injector = Chef::Resource::LwrpFoo.new("morpheus", @run_context) + injector.action(:pass_buck) + injector.provider(:lwrp_buck_passer) + dummy = Chef::Resource::ZenMaster.new("keanu reeves", @run_context) + dummy.provider(Chef::Provider::Easy) + @run_context.resource_collection.insert(injector) + @run_context.resource_collection.insert(dummy) + + Chef::Runner.new(@run_context).converge + + @run_context.resource_collection[0].should eql(injector) + @run_context.resource_collection[1].name.should eql(:prepared_thumbs) + @run_context.resource_collection[2].name.should eql(:twiddled_thumbs) + @run_context.resource_collection[3].should eql(dummy) + end + + it "should insert embedded resources from multiple providers, including from the last position, properly into the resource collection" do + injector = Chef::Resource::LwrpFoo.new("morpheus", @run_context) + injector.action(:pass_buck) + injector.provider(:lwrp_buck_passer) + injector2 = Chef::Resource::LwrpBar.new("tank", @run_context) + injector2.action(:pass_buck) + injector2.provider(:lwrp_buck_passer_2) + dummy = Chef::Resource::ZenMaster.new("keanu reeves", @run_context) + dummy.provider(Chef::Provider::Easy) + + @run_context.resource_collection.insert(injector) + @run_context.resource_collection.insert(dummy) + @run_context.resource_collection.insert(injector2) + + Chef::Runner.new(@run_context).converge + + @run_context.resource_collection[0].should eql(injector) + @run_context.resource_collection[1].name.should eql(:prepared_thumbs) + @run_context.resource_collection[2].name.should eql(:twiddled_thumbs) + @run_context.resource_collection[3].should eql(dummy) + @run_context.resource_collection[4].should eql(injector2) + @run_context.resource_collection[5].name.should eql(:prepared_eyes) + @run_context.resource_collection[6].name.should eql(:dried_paint_watched) + end + + it "should properly handle a new_resource reference" do + resource = Chef::Resource::LwrpFoo.new("morpheus") + resource.monkey("bob") + resource.provider(:lwrp_monkey_name_printer) + + provider = @runner.build_provider(resource) + provider.action_twiddle_thumbs + + provider.monkey_name.should == "my monkey's name is 'bob'" + end + + it "should properly handle an embedded Resource accessing the enclosing Provider's scope" do + + resource = Chef::Resource::LwrpFoo.new("morpheus") + resource.monkey("bob") + resource.provider(:lwrp_embedded_resource_accesses_providers_scope) + + provider = @runner.build_provider(resource) + provider.action_twiddle_thumbs + + provider.enclosed_resource.monkey.should == 'bob, the monkey' + end - provider.enclosed_resource.monkey.should == 'bob, the monkey' end end diff --git a/chef/spec/unit/mixin/deprecation_spec.rb b/chef/spec/unit/mixin/deprecation_spec.rb index 7b1e0eb218..af0b27ed22 100644 --- a/chef/spec/unit/mixin/deprecation_spec.rb +++ b/chef/spec/unit/mixin/deprecation_spec.rb @@ -21,6 +21,8 @@ require 'chef/mixin/deprecation' describe Chef::Mixin::Deprecation::DeprecatedInstanceVariable do before do + Chef::Log.logger = Logger.new(StringIO.new) + @deprecated_ivar = Chef::Mixin::Deprecation::DeprecatedInstanceVariable.new('value', 'an_ivar') end |