diff options
Diffstat (limited to 'spec/unit/application_spec.rb')
-rw-r--r-- | spec/unit/application_spec.rb | 183 |
1 files changed, 109 insertions, 74 deletions
diff --git a/spec/unit/application_spec.rb b/spec/unit/application_spec.rb index b7b69c6993..f5a2c72aa0 100644 --- a/spec/unit/application_spec.rb +++ b/spec/unit/application_spec.rb @@ -24,9 +24,9 @@ describe Chef::Application do ARGV.clear Chef::Log.logger = Logger.new(StringIO.new) @app = Chef::Application.new - @app.stub(:trap) - Dir.stub(:chdir).and_return(0) - @app.stub(:reconfigure) + allow(@app).to receive(:trap) + allow(Dir).to receive(:chdir).and_return(0) + allow(@app).to receive(:reconfigure) Chef::Log.init(STDERR) end @@ -37,23 +37,28 @@ describe Chef::Application do describe "reconfigure" do before do @app = Chef::Application.new - @app.stub(:configure_chef).and_return(true) - @app.stub(:configure_logging).and_return(true) - @app.stub(:configure_proxy_environment_variables).and_return(true) + allow(@app).to receive(:configure_chef).and_return(true) + allow(@app).to receive(:configure_logging).and_return(true) + allow(@app).to receive(:configure_proxy_environment_variables).and_return(true) end it "should configure chef" do - @app.should_receive(:configure_chef).and_return(true) + expect(@app).to receive(:configure_chef).and_return(true) @app.reconfigure end it "should configure logging" do - @app.should_receive(:configure_logging).and_return(true) + expect(@app).to receive(:configure_logging).and_return(true) @app.reconfigure end it "should configure environment variables" do - @app.should_receive(:configure_proxy_environment_variables).and_return(true) + expect(@app).to receive(:configure_proxy_environment_variables).and_return(true) + @app.reconfigure + end + + it 'should not receive set_specific_recipes' do + expect(@app).to_not receive(:set_specific_recipes) @app.reconfigure end end @@ -65,24 +70,24 @@ describe Chef::Application do 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) + allow(@app).to receive(:setup_application).and_return(true) + allow(@app).to receive(:run_application).and_return(true) + allow(@app).to receive(:configure_chef).and_return(true) + allow(@app).to receive(:configure_logging).and_return(true) end it "should reconfigure the application before running" do - @app.should_receive(:reconfigure).and_return(true) + expect(@app).to 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) + expect(@app).to receive(:setup_application).and_return(true) @app.run end it "should run the actual application" do - @app.should_receive(:run_application).and_return(true) + expect(@app).to receive(:run_application).and_return(true) @app.run end end @@ -91,15 +96,15 @@ describe Chef::Application do describe "configure_chef" do before do # Silence warnings when no config file exists - Chef::Log.stub(:warn) + allow(Chef::Log).to receive(:warn) @app = Chef::Application.new #Chef::Config.stub(:merge!).and_return(true) - @app.stub(:parse_options).and_return(true) + allow(@app).to receive(:parse_options).and_return(true) end it "should parse the commandline options" do - @app.should_receive(:parse_options).and_return(true) + expect(@app).to receive(:parse_options).and_return(true) @app.config[:config_file] = "/etc/chef/default.rb" #have a config file set, to prevent triggering error block @app.configure_chef end @@ -110,7 +115,7 @@ describe Chef::Application do let(:config_location_pathname) do p = Pathname.new(config_location) - p.stub(:realpath).and_return(config_location) + allow(p).to receive(:realpath).and_return(config_location) p end @@ -119,21 +124,21 @@ describe Chef::Application do # force let binding to get evaluated or else we stub Pathname.new before we try to use it. config_location_pathname - Pathname.stub(:new).with(config_location).and_return(config_location_pathname) - File.should_receive(:read). + allow(Pathname).to receive(:new).with(config_location).and_return(config_location_pathname) + expect(File).to receive(:read). with(config_location). and_return(config_content) end it "should configure chef::config from a file" do - Chef::Config.should_receive(:from_string).with(config_content, config_location) + expect(Chef::Config).to receive(:from_string).with(config_content, config_location) @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" + expect(Chef::Config.rspec_ran).to eq("true") end end @@ -144,8 +149,8 @@ describe Chef::Application do end it "should emit a warning" do - Chef::Config.should_not_receive(:from_file).with("/etc/chef/default.rb") - Chef::Log.should_receive(:warn).with("No config file found or specified on command line, using command line options.") + expect(Chef::Config).not_to receive(:from_file).with("/etc/chef/default.rb") + expect(Chef::Log).to receive(:warn).with("No config file found or specified on command line, using command line options.") @app.configure_chef end end @@ -155,7 +160,7 @@ describe Chef::Application do @app.config[:config_file] = "/etc/chef/notfound" end it "should use the passed in command line options and defaults" do - Chef::Config.should_receive(:merge!) + expect(Chef::Config).to receive(:merge!) @app.configure_chef end end @@ -164,33 +169,33 @@ describe Chef::Application do describe "when configuring the logger" do before do @app = Chef::Application.new - Chef::Log.stub(:init) + allow(Chef::Log).to receive(:init) end it "should initialise the chef logger" do - Chef::Log.stub(:level=) + allow(Chef::Log).to receive(:level=) @monologger = double("Monologger") - MonoLogger.should_receive(:new).with(Chef::Config[:log_location]).and_return(@monologger) - Chef::Log.should_receive(:init).with(@monologger) + expect(MonoLogger).to receive(:new).with(Chef::Config[:log_location]).and_return(@monologger) + expect(Chef::Log).to receive(:init).with(@monologger) @app.configure_logging end it "should raise fatals if log location is invalid" do Chef::Config[:log_location] = "/tmp/non-existing-dir/logfile" - Chef::Log.should_receive(:fatal).at_least(:once) - Process.should_receive(:exit) + expect(Chef::Log).to receive(:fatal).at_least(:once) + expect(Process).to receive(:exit) @app.configure_logging end shared_examples_for "log_level_is_auto" do context "when STDOUT is to a tty" do before do - STDOUT.stub(:tty?).and_return(true) + allow(STDOUT).to receive(:tty?).and_return(true) end it "configures the log level to :warn" do @app.configure_logging - Chef::Log.level.should == :warn + expect(Chef::Log.level).to eq(:warn) end context "when force_logger is configured" do @@ -200,19 +205,19 @@ describe Chef::Application do it "configures the log level to info" do @app.configure_logging - Chef::Log.level.should == :info + expect(Chef::Log.level).to eq(:info) end end end context "when STDOUT is not to a tty" do before do - STDOUT.stub(:tty?).and_return(false) + allow(STDOUT).to receive(:tty?).and_return(false) end it "configures the log level to :info" do @app.configure_logging - Chef::Log.level.should == :info + expect(Chef::Log.level).to eq(:info) end context "when force_formatter is configured" do @@ -221,7 +226,7 @@ describe Chef::Application do end it "sets the log level to :warn" do @app.configure_logging - Chef::Log.level.should == :warn + expect(Chef::Log.level).to eq(:warn) end end end @@ -242,10 +247,10 @@ describe Chef::Application do describe "when configuring environment variables" do def configure_proxy_environment_variables_stubs - @app.stub(:configure_http_proxy).and_return(true) - @app.stub(:configure_https_proxy).and_return(true) - @app.stub(:configure_ftp_proxy).and_return(true) - @app.stub(:configure_no_proxy).and_return(true) + allow(@app).to receive(:configure_http_proxy).and_return(true) + allow(@app).to receive(:configure_https_proxy).and_return(true) + allow(@app).to receive(:configure_ftp_proxy).and_return(true) + allow(@app).to receive(:configure_no_proxy).and_return(true) end shared_examples_for "setting ENV['http_proxy']" do @@ -255,12 +260,12 @@ describe Chef::Application do it "should set ENV['http_proxy']" do @app.configure_proxy_environment_variables - @env['http_proxy'].should == "#{scheme}://#{address}:#{port}" + expect(@env['http_proxy']).to eq("#{scheme}://#{address}:#{port}") end it "should set ENV['HTTP_PROXY']" do @app.configure_proxy_environment_variables - @env['HTTP_PROXY'].should == "#{scheme}://#{address}:#{port}" + expect(@env['HTTP_PROXY']).to eq("#{scheme}://#{address}:#{port}") end describe "when Chef::Config[:http_proxy_user] is set" do @@ -270,8 +275,8 @@ describe Chef::Application do it "should set ENV['http_proxy'] with the username" do @app.configure_proxy_environment_variables - @env['http_proxy'].should == "#{scheme}://username@#{address}:#{port}" - @env['HTTP_PROXY'].should == "#{scheme}://username@#{address}:#{port}" + expect(@env['http_proxy']).to eq("#{scheme}://username@#{address}:#{port}") + expect(@env['HTTP_PROXY']).to eq("#{scheme}://username@#{address}:#{port}") end context "when :http_proxy_user contains '@' and/or ':'" do @@ -281,8 +286,8 @@ describe Chef::Application do it "should set ENV['http_proxy'] with the escaped username" do @app.configure_proxy_environment_variables - @env['http_proxy'].should == "#{scheme}://my%3Ausern%40me@#{address}:#{port}" - @env['HTTP_PROXY'].should == "#{scheme}://my%3Ausern%40me@#{address}:#{port}" + expect(@env['http_proxy']).to eq("#{scheme}://my%3Ausern%40me@#{address}:#{port}") + expect(@env['HTTP_PROXY']).to eq("#{scheme}://my%3Ausern%40me@#{address}:#{port}") end end @@ -293,8 +298,8 @@ describe Chef::Application do it "should set ENV['http_proxy'] with the password" do @app.configure_proxy_environment_variables - @env['http_proxy'].should == "#{scheme}://username:password@#{address}:#{port}" - @env['HTTP_PROXY'].should == "#{scheme}://username:password@#{address}:#{port}" + expect(@env['http_proxy']).to eq("#{scheme}://username:password@#{address}:#{port}") + expect(@env['HTTP_PROXY']).to eq("#{scheme}://username:password@#{address}:#{port}") end context "when :http_proxy_pass contains '@' and/or ':'" do @@ -304,8 +309,8 @@ describe Chef::Application do it "should set ENV['http_proxy'] with the escaped password" do @app.configure_proxy_environment_variables - @env['http_proxy'].should == "#{scheme}://username:%3AP%40ssword101@#{address}:#{port}" - @env['HTTP_PROXY'].should == "#{scheme}://username:%3AP%40ssword101@#{address}:#{port}" + expect(@env['http_proxy']).to eq("#{scheme}://username:%3AP%40ssword101@#{address}:#{port}") + expect(@env['HTTP_PROXY']).to eq("#{scheme}://username:%3AP%40ssword101@#{address}:#{port}") end end end @@ -319,8 +324,8 @@ describe Chef::Application do it "should set ENV['http_proxy']" do @app.configure_proxy_environment_variables - @env['http_proxy'].should == "#{scheme}://#{address}:#{port}" - @env['HTTP_PROXY'].should == "#{scheme}://#{address}:#{port}" + expect(@env['http_proxy']).to eq("#{scheme}://#{address}:#{port}") + expect(@env['HTTP_PROXY']).to eq("#{scheme}://#{address}:#{port}") end end end @@ -328,11 +333,11 @@ describe Chef::Application do describe "when configuring ENV['http_proxy']" do before do @env = {} - @app.stub(:env).and_return(@env) + allow(@app).to receive(:env).and_return(@env) - @app.stub(:configure_https_proxy).and_return(true) - @app.stub(:configure_ftp_proxy).and_return(true) - @app.stub(:configure_no_proxy).and_return(true) + allow(@app).to receive(:configure_https_proxy).and_return(true) + allow(@app).to receive(:configure_ftp_proxy).and_return(true) + allow(@app).to receive(:configure_no_proxy).and_return(true) end describe "when Chef::Config[:http_proxy] is not set" do @@ -342,7 +347,7 @@ describe Chef::Application do it "should not set ENV['http_proxy']" do @app.configure_proxy_environment_variables - @env.should == {} + expect(@env).to eq({}) end end @@ -401,7 +406,7 @@ describe Chef::Application do it "should set ENV['http_proxy']" do @app.configure_proxy_environment_variables - @env['http_proxy'].should == Chef::Config[:http_proxy] + expect(@env['http_proxy']).to eq(Chef::Config[:http_proxy]) end end @@ -417,26 +422,26 @@ describe Chef::Application do describe "class method: fatal!" do before do - STDERR.stub(:puts).with("FATAL: blah").and_return(true) - Chef::Log.stub(:fatal).and_return(true) - Process.stub(:exit).and_return(true) + allow(STDERR).to receive(:puts).with("FATAL: blah").and_return(true) + allow(Chef::Log).to receive(:fatal).and_return(true) + allow(Process).to receive(: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) + expect(Chef::Log).to receive(:fatal).with("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) + expect(Process).to 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) + expect(Process).to receive(:exit).with(-1).and_return(true) Chef::Application.fatal! "blah" end end @@ -449,7 +454,7 @@ describe Chef::Application do end it "should raise an error" do - lambda { @app.setup_application }.should raise_error(Chef::Exceptions::Application) + expect { @app.setup_application }.to raise_error(Chef::Exceptions::Application) end end @@ -459,7 +464,7 @@ describe Chef::Application do end it "should raise an error" do - lambda { @app.run_application }.should raise_error(Chef::Exceptions::Application) + expect { @app.run_application }.to raise_error(Chef::Exceptions::Application) end end @@ -467,22 +472,52 @@ describe Chef::Application do it "should warn for bad config file path" do @app.config[:config_file] = "/tmp/non-existing-dir/file" config_file_regexp = Regexp.new @app.config[:config_file] - Chef::Log.should_receive(:warn).at_least(:once).with(config_file_regexp).and_return(true) - Chef::Log.stub(:warn).and_return(true) + expect(Chef::Log).to receive(:warn).at_least(:once).with(config_file_regexp).and_return(true) + allow(Chef::Log).to receive(:warn).and_return(true) @app.configure_chef end end + describe 'run_chef_client' do + context 'with an application' do + let(:app) { Chef::Application.new } + + context 'when called with an invalid argument' do + before do + allow(app).to receive(:fork_chef_client).and_return(true) + allow(app).to receive(:run_with_graceful_exit_option).and_return(true) + end + + it 'should raise an argument error detailing the problem' do + specific_recipes_regexp = Regexp.new 'received non-Array like specific_recipes argument' + expect { app.run_chef_client(nil) }.to raise_error(ArgumentError, specific_recipes_regexp) + end + end + + context 'when called with an Array-like argument (#size)' do + before do + allow(app).to receive(:fork_chef_client).and_return(true) + allow(app).to receive(:run_with_graceful_exit_option).and_return(true) + end + + it 'should be cool' do + expect { app.run_chef_client([]) }.not_to raise_error + end + end + end + + end + describe "configuration errors" do before do - Process.should_receive(:exit) + expect(Process).to receive(:exit) end def raises_informative_fatals_on_configure_chef config_file_regexp = Regexp.new @app.config[:config_file] - Chef::Log.should_receive(:fatal). + expect(Chef::Log).to receive(:fatal). with(/Configuration error/) - Chef::Log.should_receive(:fatal). + expect(Chef::Log).to receive(:fatal). with(config_file_regexp). at_least(1).times @app.configure_chef |