diff options
Diffstat (limited to 'spec/unit/application_spec.rb')
-rw-r--r-- | spec/unit/application_spec.rb | 131 |
1 files changed, 51 insertions, 80 deletions
diff --git a/spec/unit/application_spec.rb b/spec/unit/application_spec.rb index f3df8c14cd..1606c82531 100644 --- a/spec/unit/application_spec.rb +++ b/spec/unit/application_spec.rb @@ -20,15 +20,17 @@ require 'spec_helper' describe Chef::Application do before do - @original_conf = Chef::Config.configuration + @original_argv = ARGV.dup + ARGV.clear Chef::Log.logger = Logger.new(StringIO.new) @app = Chef::Application.new Dir.stub!(:chdir).and_return(0) @app.stub!(:reconfigure) + Chef::Log.init(STDERR) end after do - Chef::Config.configuration.replace(@original_conf) + ARGV.replace(@original_argv) end describe "reconfigure" do @@ -94,28 +96,30 @@ describe Chef::Application do end 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 + let(:config_content) { "rspec_ran('true')" } + let(:config_location) { "/etc/chef/default.rb" } - @app.config[:config_file] = "/etc/chef/default.rb" + let(:config_location_pathname) do + p = Pathname.new(config_location) + p.stub(:realpath).and_return(config_location) + p end - after do - @config_file.unlink + before do + @app.config[:config_file] = config_location + Pathname.stub(:new).with(config_location).and_return(config_location_pathname) + File.should_receive(:read). + with(config_location). + and_return(config_content) 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) + Chef::Config.should_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) + #File.should_receive(:open).with("/etc/chef/default.rb").and_yield(@config_file) @app.configure_chef Chef::Config.rspec_ran.should == "true" end @@ -127,9 +131,9 @@ describe Chef::Application do @app.config[:config_file] = nil end - it "should raise a fatal" do + it "should emit a warning" do Chef::Config.should_not_receive(:from_file).with("/etc/chef/default.rb") - Chef::Application.should_receive(:fatal!) + Chef::Log.should_receive(:warn).with("No config file found or specified on command line, using command line options.") @app.configure_chef end end @@ -143,36 +147,6 @@ describe Chef::Application do @app.configure_chef 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 - - - @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 describe "when configuring the logger" do @@ -189,17 +163,8 @@ describe Chef::Application do @app.configure_logging 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 - - context "and log_level is :auto" do - before do - Chef::Config[:log_level] = :auto - end - - context "and STDOUT is to a tty" do + shared_examples_for "log_level_is_auto" do + context "when STDOUT is to a tty" do before do STDOUT.stub!(:tty?).and_return(true) end @@ -209,7 +174,7 @@ describe Chef::Application do Chef::Log.level.should == :warn end - context "and force_logger is configured" do + context "when force_logger is configured" do before do Chef::Config[:force_logger] = true end @@ -221,7 +186,7 @@ describe Chef::Application do end end - context "and STDOUT is not to a tty" do + context "when STDOUT is not to a tty" do before do STDOUT.stub!(:tty?).and_return(false) end @@ -231,7 +196,7 @@ describe Chef::Application do Chef::Log.level.should == :info end - context "and force_formatter is configured" do + context "when force_formatter is configured" do before do Chef::Config[:force_formatter] = true end @@ -241,7 +206,18 @@ describe Chef::Application do end 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 + it_behaves_like "log_level_is_auto" end end @@ -293,34 +269,29 @@ describe Chef::Application do end end - describe "configuration errors" do - before do - Process.stub!(:exit).and_return(true) - end - - def raises_informative_fatals_on_configure_chef - config_file_regexp = Regexp.new @app.config[:config_file] - Chef::Log.should_receive(:fatal).with(config_file_regexp).and_return(true) - @app.configure_chef - end - - def warns_informatively_on_configure_chef + context "when the config file is not available" 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.should_receive(:warn).any_number_of_times.and_return(true) @app.configure_chef end + end - it "should warn for bad config file path" do - @app.config[:config_file] = "/tmp/non-existing-dir/file" - warns_informatively_on_configure_chef + describe "configuration errors" do + before do + Process.should_receive(:exit) end - it "should raise informative fatals for bad config file url" do - non_existing_url = "http://its-stubbed.com/foo.rb" - @app.config[:config_file] = non_existing_url - Chef::REST.any_instance.stub(:fetch).with(non_existing_url).and_raise(SocketError) - raises_informative_fatals_on_configure_chef + def raises_informative_fatals_on_configure_chef + config_file_regexp = Regexp.new @app.config[:config_file] + Chef::Log.should_receive(:fatal). + with(/Configuration error/) + Chef::Log.should_receive(:fatal). + with(config_file_regexp). + at_least(1).times + @app.configure_chef end describe "when config file exists but contains errors" do |