diff options
author | danielsdeleo <dan@chef.io> | 2016-01-27 09:21:53 -0800 |
---|---|---|
committer | danielsdeleo <dan@chef.io> | 2016-01-27 12:38:54 -0800 |
commit | 036236309fad39da04b05a50bdde02f8d56102d3 (patch) | |
tree | 7bb5ee45504cce99025a297ab11c9beb303cdb70 | |
parent | 05d98b354ab995fefd9ef6bc6d3e19bb5f960ad5 (diff) | |
download | chef-036236309fad39da04b05a50bdde02f8d56102d3.tar.gz |
Reorganize application tests to catch fatals
-rw-r--r-- | spec/unit/application_spec.rb | 304 |
1 files changed, 158 insertions, 146 deletions
diff --git a/spec/unit/application_spec.rb b/spec/unit/application_spec.rb index d66cc26927..99c5133f70 100644 --- a/spec/unit/application_spec.rb +++ b/spec/unit/application_spec.rb @@ -34,222 +34,234 @@ describe Chef::Application do ARGV.replace(@original_argv) end - describe "reconfigure" do - before do - @app = Chef::Application.new - allow(@app).to receive(:configure_chef).and_return(true) - allow(@app).to receive(:configure_logging).and_return(true) - end - - it "should configure chef" do - expect(@app).to receive(:configure_chef).and_return(true) - @app.reconfigure - end - - it "should configure logging" do - expect(@app).to receive(:configure_logging).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 + context "when there are no configuration errors" do - describe Chef::Application do before do - @app = Chef::Application.new + expect(Chef::Log).to_not receive(:fatal) + expect(Chef::Application).to_not receive(:fatal!) end - describe "run" do + describe "reconfigure" do before do - allow(@app).to receive(:setup_application).and_return(true) - allow(@app).to receive(:run_application).and_return(true) + @app = Chef::Application.new 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 - expect(@app).to receive(:reconfigure).and_return(true) - @app.run + it "should configure chef" do + expect(@app).to receive(:configure_chef).and_return(true) + @app.reconfigure end - it "should setup the application before running it" do - expect(@app).to receive(:setup_application).and_return(true) - @app.run + it "should configure logging" do + expect(@app).to receive(:configure_logging).and_return(true) + @app.reconfigure end - it "should run the actual application" do - expect(@app).to receive(:run_application).and_return(true) - @app.run + it "should not receive set_specific_recipes" do + expect(@app).to_not receive(:set_specific_recipes) + @app.reconfigure end end - end - describe "configure_chef" do - before do - # Silence warnings when no config file exists - allow(Chef::Log).to receive(:warn) + describe Chef::Application do + before do + @app = Chef::Application.new + end - @app = Chef::Application.new - #Chef::Config.stub(:merge!).and_return(true) - allow(@app).to receive(:parse_options).and_return(true) - expect(Chef::Config).to receive(:export_proxies).and_return(true) - end + describe "run" do + before do + 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 parse the commandline options" do - 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 + it "should reconfigure the application before running" do + expect(@app).to receive(:reconfigure).and_return(true) + @app.run + end - describe "when a config_file is present" do - let(:config_content) { "rspec_ran('true')" } - let(:config_location) { "/etc/chef/default.rb" } + it "should setup the application before running it" do + expect(@app).to receive(:setup_application).and_return(true) + @app.run + end - let(:config_location_pathname) do - p = Pathname.new(config_location) - allow(p).to receive(:realpath).and_return(config_location) - p + it "should run the actual application" do + expect(@app).to receive(:run_application).and_return(true) + @app.run + end end + end + describe "configure_chef" do before do - @app.config[:config_file] = config_location - - # force let binding to get evaluated or else we stub Pathname.new before we try to use it. - config_location_pathname - 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 + # Silence warnings when no config file exists + allow(Chef::Log).to receive(:warn) - it "should configure chef::config from a file" do - expect(Chef::Config).to receive(:from_string).with(config_content, config_location) - @app.configure_chef + @app = Chef::Application.new + allow(@app).to receive(:parse_options).and_return(true) + expect(Chef::Config).to receive(:export_proxies).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 + 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 - expect(Chef::Config.rspec_ran).to eq("true") end - context "when openssl fips" do + describe "when a config_file is present" do + let(:config_content) { "rspec_ran('true')" } + let(:config_location) { "/etc/chef/default.rb" } + + let(:config_location_pathname) do + p = Pathname.new(config_location) + allow(p).to receive(:realpath).and_return(config_location) + p + end + before do - allow(Chef::Config).to receive(:fips).and_return(true) + @app.config[:config_file] = config_location + + # force let binding to get evaluated or else we stub Pathname.new before we try to use it. + config_location_pathname + 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 "sets openssl in fips mode" do - expect(OpenSSL).to receive(:'fips_mode=').with(true) + it "should configure chef::config from a file" do + 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 + expect(Chef::Config.rspec_ran).to eq("true") + end + + context "when openssl fips" do + before do + allow(Chef::Config).to receive(:fips).and_return(true) + end + + it "sets openssl in fips mode" do + expect(OpenSSL).to receive(:'fips_mode=').with(true) + @app.configure_chef + end + end end - end - describe "when there is no config_file defined" do - before do - @app.config[:config_file] = nil + describe "when there is no config_file defined" do + before do + @app.config[:config_file] = nil + end + + it "should emit a warning" do + 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 - it "should emit a warning" do - 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 + describe "when the config file is set and not found" do + before do + @app.config[:config_file] = "/etc/chef/notfound" + end + it "should use the passed in command line options and defaults" do + expect(Chef::Config).to receive(:merge!) + @app.configure_chef + end end end - describe "when the config file is set and not found" do + describe "when configuring the logger" do before do - @app.config[:config_file] = "/etc/chef/notfound" + @app = Chef::Application.new + allow(Chef::Log).to receive(:init) end - it "should use the passed in command line options and defaults" do - expect(Chef::Config).to receive(:merge!) - @app.configure_chef + + it "should initialise the chef logger" do + allow(Chef::Log).to receive(:level=) + @monologger = double("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 - end - end - describe "when configuring the logger" do - before do - @app = Chef::Application.new - allow(Chef::Log).to receive(:init) - end + shared_examples_for "log_level_is_auto" do + context "when STDOUT is to a tty" do + before do + allow(STDOUT).to receive(:tty?).and_return(true) + end - it "should initialise the chef logger" do - allow(Chef::Log).to receive(:level=) - @monologger = double("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 "configures the log level to :warn" do + @app.configure_logging + expect(Chef::Log.level).to eq(:warn) + end - it "should raise fatals if log location is invalid" do - Chef::Config[:log_location] = "/tmp/non-existing-dir/logfile" - expect(Chef::Log).to receive(:fatal).at_least(:once) - expect(Process).to receive(:exit) - @app.configure_logging - end + context "when force_logger is configured" do + before do + Chef::Config[:force_logger] = true + end - shared_examples_for "log_level_is_auto" do - context "when STDOUT is to a tty" do - before do - allow(STDOUT).to receive(:tty?).and_return(true) - end - - it "configures the log level to :warn" do - @app.configure_logging - expect(Chef::Log.level).to eq(:warn) + it "configures the log level to info" do + @app.configure_logging + expect(Chef::Log.level).to eq(:info) + end + end end - context "when force_logger is configured" do + context "when STDOUT is not to a tty" do before do - Chef::Config[:force_logger] = true + allow(STDOUT).to receive(:tty?).and_return(false) end - it "configures the log level to info" do + it "configures the log level to :info" do @app.configure_logging expect(Chef::Log.level).to eq(:info) end + + context "when force_formatter is configured" do + before do + Chef::Config[:force_formatter] = true + end + it "sets the log level to :warn" do + @app.configure_logging + expect(Chef::Log.level).to eq(:warn) + end + end end end - context "when STDOUT is not to a tty" do - before do - allow(STDOUT).to receive(:tty?).and_return(false) - end + context "when log_level is not set" do + it_behaves_like "log_level_is_auto" + end - it "configures the log level to :info" do - @app.configure_logging - expect(Chef::Log.level).to eq(:info) + context "when log_level is :auto" do + before do + Chef::Config[:log_level] = :auto end - context "when force_formatter is configured" do - before do - Chef::Config[:force_formatter] = true - end - it "sets the log level to :warn" do - @app.configure_logging - expect(Chef::Log.level).to eq(:warn) - end - end + it_behaves_like "log_level_is_auto" end end + end - context "when log_level is not set" do - it_behaves_like "log_level_is_auto" - end - context "when log_level is :auto" do - before do - Chef::Config[:log_level] = :auto - end + context "with an invalid log location" do - it_behaves_like "log_level_is_auto" + it "logs a fatal error and exits" do + Chef::Config[:log_location] = "/tmp/non-existing-dir/logfile" + expect(Chef::Log).to receive(:fatal).at_least(:once) + expect(Process).to receive(:exit) + @app.configure_logging end end + describe "class method: fatal!" do before do allow(STDERR).to receive(:puts).with("FATAL: blah").and_return(true) |