diff options
Diffstat (limited to 'spec/unit/application')
-rw-r--r-- | spec/unit/application/apply_spec.rb | 30 | ||||
-rw-r--r-- | spec/unit/application/client_spec.rb | 68 | ||||
-rw-r--r-- | spec/unit/application/knife_spec.rb | 62 | ||||
-rw-r--r-- | spec/unit/application/solo_spec.rb | 56 |
4 files changed, 108 insertions, 108 deletions
diff --git a/spec/unit/application/apply_spec.rb b/spec/unit/application/apply_spec.rb index e29c038340..5a6366281f 100644 --- a/spec/unit/application/apply_spec.rb +++ b/spec/unit/application/apply_spec.rb @@ -21,7 +21,7 @@ describe Chef::Application::Apply do before do @app = Chef::Application::Apply.new - @app.stub(:configure_logging).and_return(true) + allow(@app).to receive(:configure_logging).and_return(true) @recipe_text = "package 'nyancat'" Chef::Config[:solo] = true end @@ -29,7 +29,7 @@ describe Chef::Application::Apply do describe "configuring the application" do it "should set solo mode to true" do @app.reconfigure - Chef::Config[:solo].should be_true + expect(Chef::Config[:solo]).to be_truthy end end describe "read_recipe_file" do @@ -37,30 +37,30 @@ describe Chef::Application::Apply do @recipe_file_name = "foo.rb" @recipe_path = File.expand_path(@recipe_file_name) @recipe_file = double("Tempfile (mock)", :read => @recipe_text) - @app.stub(:open).with(@recipe_path).and_return(@recipe_file) - File.stub(:exist?).with(@recipe_path).and_return(true) - Chef::Application.stub(:fatal!).and_return(true) + allow(@app).to receive(:open).with(@recipe_path).and_return(@recipe_file) + allow(File).to receive(:exist?).with(@recipe_path).and_return(true) + allow(Chef::Application).to receive(:fatal!).and_return(true) end it "should read text properly" do - @app.read_recipe_file(@recipe_file_name)[0].should == @recipe_text + expect(@app.read_recipe_file(@recipe_file_name)[0]).to eq(@recipe_text) end it "should return a file_handle" do - @app.read_recipe_file(@recipe_file_name)[1].should be_instance_of(RSpec::Mocks::Mock) + expect(@app.read_recipe_file(@recipe_file_name)[1]).to be_instance_of(RSpec::Mocks::Double) end describe "when recipe is nil" do it "should raise a fatal with the missing filename message" do - Chef::Application.should_receive(:fatal!).with("No recipe file was provided", 1) + expect(Chef::Application).to receive(:fatal!).with("No recipe file was provided", 1) @app.read_recipe_file(nil) end end describe "when recipe doesn't exist" do before do - File.stub(:exist?).with(@recipe_path).and_return(false) + allow(File).to receive(:exist?).with(@recipe_path).and_return(false) end it "should raise a fatal with the file doesn't exist message" do - Chef::Application.should_receive(:fatal!).with(/^No file exists at/, 1) + expect(Chef::Application).to receive(:fatal!).with(/^No file exists at/, 1) @app.read_recipe_file(@recipe_file_name) end end @@ -72,13 +72,13 @@ describe Chef::Application::Apply do @recipe_fh = @app.instance_variable_get(:@recipe_fh) end it "should open a tempfile" do - @recipe_fh.path.should match(/.*recipe-temporary-file.*/) + expect(@recipe_fh.path).to match(/.*recipe-temporary-file.*/) end it "should write recipe text to the tempfile" do - @recipe_fh.read.should == @recipe_text + expect(@recipe_fh.read).to eq(@recipe_text) end it "should save the filename for later use" do - @recipe_fh.path.should == @app.instance_variable_get(:@recipe_filename) + expect(@recipe_fh.path).to eq(@app.instance_variable_get(:@recipe_filename)) end end describe "recipe_file_arg" do @@ -86,8 +86,8 @@ describe Chef::Application::Apply do ARGV.clear end it "should exit and log message" do - Chef::Log.should_receive(:debug).with(/^No recipe file provided/) - lambda { @app.run }.should raise_error(SystemExit) { |e| e.status.should == 1 } + expect(Chef::Log).to receive(:debug).with(/^No recipe file provided/) + expect { @app.run }.to raise_error(SystemExit) { |e| expect(e.status).to eq(1) } end end diff --git a/spec/unit/application/client_spec.rb b/spec/unit/application/client_spec.rb index ee91e67256..38e051e269 100644 --- a/spec/unit/application/client_spec.rb +++ b/spec/unit/application/client_spec.rb @@ -19,16 +19,16 @@ require 'spec_helper' describe Chef::Application::Client, "reconfigure" do before do - Kernel.stub(:trap).and_return(:ok) + allow(Kernel).to receive(:trap).and_return(:ok) @original_argv = ARGV.dup ARGV.clear @app = Chef::Application::Client.new - @app.stub(:trap) - @app.stub(:configure_opt_parser).and_return(true) - @app.stub(:configure_chef).and_return(true) - @app.stub(:configure_logging).and_return(true) + allow(@app).to receive(:trap) + allow(@app).to receive(:configure_opt_parser).and_return(true) + allow(@app).to receive(:configure_chef).and_return(true) + allow(@app).to receive(:configure_logging).and_return(true) @app.cli_arguments = [] Chef::Config[:interval] = 10 @@ -53,7 +53,7 @@ describe Chef::Application::Client, "reconfigure" do end it "should terminate with message" do - Chef::Application.should_receive(:fatal!).with( + expect(Chef::Application).to receive(:fatal!).with( "Unforked chef-client interval runs are disabled in Chef 12. Configuration settings: interval = 600 seconds @@ -71,7 +71,7 @@ Enable chef-client interval runs by setting `:client_fork = true` in your config it "should reconfigure chef-client" do @app.reconfigure - Chef::Config[:interval].should be_nil + expect(Chef::Config[:interval]).to be_nil end end end @@ -84,7 +84,7 @@ Enable chef-client interval runs by setting `:client_fork = true` in your config it "should set the interval to 1800" do @app.reconfigure - Chef::Config.interval.should == 1800 + expect(Chef::Config.interval).to eq(1800) end end @@ -98,12 +98,12 @@ Enable chef-client interval runs by setting `:client_fork = true` in your config it "ignores the splay" do @app.reconfigure - Chef::Config.splay.should be_nil + expect(Chef::Config.splay).to be_nil end it "forces the interval to nil" do @app.reconfigure - Chef::Config.interval.should be_nil + expect(Chef::Config.interval).to be_nil end end @@ -116,13 +116,13 @@ Enable chef-client interval runs by setting `:client_fork = true` in your config before do Chef::Config[:json_attribs] = json_source - Chef::ConfigFetcher.should_receive(:new).with(json_source). + expect(Chef::ConfigFetcher).to receive(:new).with(json_source). and_return(config_fetcher) end it "reads the JSON attributes from the specified source" do @app.reconfigure - @app.chef_client_json.should == json_attribs + expect(@app.chef_client_json).to eq(json_attribs) end end end @@ -131,13 +131,13 @@ describe Chef::Application::Client, "setup_application" do before do @app = Chef::Application::Client.new # this is all stuff the reconfigure method needs - @app.stub(:configure_opt_parser).and_return(true) - @app.stub(:configure_chef).and_return(true) - @app.stub(:configure_logging).and_return(true) + allow(@app).to receive(:configure_opt_parser).and_return(true) + allow(@app).to receive(:configure_chef).and_return(true) + allow(@app).to receive(:configure_logging).and_return(true) end it "should change privileges" do - Chef::Daemon.should_receive(:change_privilege).and_return(true) + expect(Chef::Daemon).to receive(:change_privilege).and_return(true) @app.setup_application end after do @@ -159,9 +159,9 @@ describe Chef::Application::Client, "configure_chef" do it "should set the colored output to false by default on windows and true otherwise" do if windows? - Chef::Config[:color].should be_false + expect(Chef::Config[:color]).to be_falsey else - Chef::Config[:color].should be_true + expect(Chef::Config[:color]).to be_truthy end end end @@ -177,8 +177,8 @@ describe Chef::Application::Client, "run_application", :unix_only do @pipe = IO.pipe @client = Chef::Client.new - Chef::Client.stub(:new).and_return(@client) - @client.stub(:run) do + allow(Chef::Client).to receive(:new).and_return(@client) + allow(@client).to receive(:run) do @pipe[1].puts 'started' sleep 1 @pipe[1].puts 'finished' @@ -189,7 +189,7 @@ describe Chef::Application::Client, "run_application", :unix_only do context "when converging in forked process" do before do Chef::Config[:daemonize] = true - Chef::Daemon.stub(:daemonize).and_return(true) + allow(Chef::Daemon).to receive(:daemonize).and_return(true) end it "should exit hard with exitstatus 3" do @@ -198,19 +198,19 @@ describe Chef::Application::Client, "run_application", :unix_only do end Process.kill("TERM", pid) _pid, result = Process.waitpid2(pid) - result.exitstatus.should == 3 + expect(result.exitstatus).to eq(3) end it "should allow child to finish converging" do pid = fork do @app.run_application end - @pipe[0].gets.should == "started\n" + expect(@pipe[0].gets).to eq("started\n") Process.kill("TERM", pid) Process.wait sleep 1 # Make sure we give the converging child process enough time to finish - IO.select([@pipe[0]], nil, nil, 0).should_not be_nil - @pipe[0].gets.should == "finished\n" + expect(IO.select([@pipe[0]], nil, nil, 0)).not_to be_nil + expect(@pipe[0].gets).to eq("finished\n") end end @@ -224,12 +224,12 @@ describe Chef::Application::Client, "run_application", :unix_only do pid = fork do @app.run_application end - @pipe[0].gets.should == "started\n" + expect(@pipe[0].gets).to eq("started\n") Process.kill("TERM", pid) _pid, result = Process.waitpid2(pid) - result.exitstatus.should == 0 - IO.select([@pipe[0]], nil, nil, 0).should_not be_nil - @pipe[0].gets.should == "finished\n" + expect(result.exitstatus).to eq(0) + expect(IO.select([@pipe[0]], nil, nil, 0)).not_to be_nil + expect(@pipe[0].gets).to eq("finished\n") end it "should exit hard when sent before converge" do @@ -239,7 +239,7 @@ describe Chef::Application::Client, "run_application", :unix_only do end Process.kill("TERM", pid) _pid, result = Process.waitpid2(pid) - result.exitstatus.should == 3 + expect(result.exitstatus).to eq(3) end end end @@ -255,7 +255,7 @@ describe Chef::Application::Client, "run_application", :unix_only do # Chef::Log.init($stderr) # Chef::Log.level = :debug - @app.stub(:run_chef_client) do + allow(@app).to receive(:run_chef_client) do run_count += 1 if run_count > 3 @@ -277,7 +277,7 @@ describe Chef::Application::Client, "run_application", :unix_only do # We have to do it this way because the main loop of # Chef::Application::Client swallows most exceptions, and we need to be # able to expose our expectation failures to the parent process in the test. - @app.stub(:interval_sleep) do |arg| + allow(@app).to receive(:interval_sleep) do |arg| number_of_sleep_calls += 1 if number_of_sleep_calls > 1 exit 127 @@ -286,12 +286,12 @@ describe Chef::Application::Client, "run_application", :unix_only do end it "shouldn't sleep when sent USR1" do - @app.stub(:interval_sleep).with(0).and_call_original + allow(@app).to receive(:interval_sleep).with(0).and_call_original pid = fork do @app.run_application end _pid, result = Process.waitpid2(pid) - result.exitstatus.should == 0 + expect(result.exitstatus).to eq(0) end end end diff --git a/spec/unit/application/knife_spec.rb b/spec/unit/application/knife_spec.rb index 0933d7e178..806a596a61 100644 --- a/spec/unit/application/knife_spec.rb +++ b/spec/unit/application/knife_spec.rb @@ -35,66 +35,66 @@ describe Chef::Application::Knife do before(:each) do # Prevent code from getting loaded on every test invocation. - Chef::Knife.stub(:load_commands) + allow(Chef::Knife).to receive(:load_commands) @knife = Chef::Application::Knife.new - @knife.stub(:puts) - @knife.stub(:trap) - Chef::Knife.stub(:list_commands) + allow(@knife).to receive(:puts) + allow(@knife).to receive(:trap) + allow(Chef::Knife).to receive(:list_commands) end it "should exit 1 and print the options if no arguments are given at all" do with_argv([]) do - lambda { @knife.run }.should raise_error(SystemExit) { |e| e.status.should == 1 } + expect { @knife.run }.to raise_error(SystemExit) { |e| expect(e.status).to eq(1) } end end it "should exit 2 if run without a sub command" do with_argv("--user", "adam") do - Chef::Log.should_receive(:error).with(/you need to pass a sub\-command/i) - lambda { @knife.run }.should raise_error(SystemExit) { |e| e.status.should == 2 } + expect(Chef::Log).to receive(:error).with(/you need to pass a sub\-command/i) + expect { @knife.run }.to raise_error(SystemExit) { |e| expect(e.status).to eq(2) } end end it "should run a sub command with the applications command line option prototype" do with_argv(*%w{noop knife command with some args}) do knife = double(Chef::Knife) - Chef::Knife.should_receive(:run).with(ARGV, @knife.options).and_return(knife) - @knife.should_receive(:exit).with(0) + expect(Chef::Knife).to receive(:run).with(ARGV, @knife.options).and_return(knife) + expect(@knife).to receive(:exit).with(0) @knife.run end end it "should set the colored output to false by default on windows and true otherwise" do with_argv(*%w{noop knife command}) do - @knife.should_receive(:exit).with(0) + expect(@knife).to receive(:exit).with(0) @knife.run end if windows? - Chef::Config[:color].should be_false + expect(Chef::Config[:color]).to be_falsey else - Chef::Config[:color].should be_true + expect(Chef::Config[:color]).to be_truthy end end describe "when given a path to the client key" do it "expands a relative path relative to the CWD" do relative_path = '.chef/client.pem' - Dir.stub(:pwd).and_return(CHEF_SPEC_DATA) + allow(Dir).to receive(:pwd).and_return(CHEF_SPEC_DATA) with_argv(*%W{noop knife command -k #{relative_path}}) do - @knife.should_receive(:exit).with(0) + expect(@knife).to receive(:exit).with(0) @knife.run end - Chef::Config[:client_key].should == File.join(CHEF_SPEC_DATA, relative_path) + expect(Chef::Config[:client_key]).to eq(File.join(CHEF_SPEC_DATA, relative_path)) end it "expands a ~/home/path to the correct full path" do home_path = '~/.chef/client.pem' with_argv(*%W{noop knife command -k #{home_path}}) do - @knife.should_receive(:exit).with(0) + expect(@knife).to receive(:exit).with(0) @knife.run end - Chef::Config[:client_key].should == File.join(ENV['HOME'], '.chef/client.pem').gsub((File::ALT_SEPARATOR || '\\'), File::SEPARATOR) + expect(Chef::Config[:client_key]).to eq(File.join(ENV['HOME'], '.chef/client.pem').gsub((File::ALT_SEPARATOR || '\\'), File::SEPARATOR)) end it "does not expand a full path" do @@ -104,10 +104,10 @@ describe Chef::Application::Knife do '/etc/chef/client.pem' end with_argv(*%W{noop knife command -k #{full_path}}) do - @knife.should_receive(:exit).with(0) + expect(@knife).to receive(:exit).with(0) @knife.run end - Chef::Config[:client_key].should == full_path + expect(Chef::Config[:client_key]).to eq(full_path) end end @@ -119,52 +119,52 @@ describe Chef::Application::Knife do it "should default to no environment" do with_argv(*%w{noop knife command}) do - @knife.should_receive(:exit).with(0) + expect(@knife).to receive(:exit).with(0) @knife.run end - Chef::Config[:environment].should == nil + expect(Chef::Config[:environment]).to eq(nil) end it "should load the environment from the config file" do config_file = File.join(CHEF_SPEC_DATA,"environment-config.rb") with_argv(*%W{noop knife command -c #{config_file}}) do - @knife.should_receive(:exit).with(0) + expect(@knife).to receive(:exit).with(0) @knife.run end - Chef::Config[:environment].should == 'production' + expect(Chef::Config[:environment]).to eq('production') end it "should load the environment from the CLI options" do with_argv(*%W{noop knife command -E development}) do - @knife.should_receive(:exit).with(0) + expect(@knife).to receive(:exit).with(0) @knife.run end - Chef::Config[:environment].should == 'development' + expect(Chef::Config[:environment]).to eq('development') end it "should override the config file environment with the CLI environment" do config_file = File.join(CHEF_SPEC_DATA,"environment-config.rb") with_argv(*%W{noop knife command -c #{config_file} -E override}) do - @knife.should_receive(:exit).with(0) + expect(@knife).to receive(:exit).with(0) @knife.run end - Chef::Config[:environment].should == 'override' + expect(Chef::Config[:environment]).to eq('override') end it "should override the config file environment with the CLI environment regardless of order" do config_file = File.join(CHEF_SPEC_DATA,"environment-config.rb") with_argv(*%W{noop knife command -E override -c #{config_file}}) do - @knife.should_receive(:exit).with(0) + expect(@knife).to receive(:exit).with(0) @knife.run end - Chef::Config[:environment].should == 'override' + expect(Chef::Config[:environment]).to eq('override') end it "should run a sub command with the applications command line option prototype" do with_argv(*%w{noop knife command with some args}) do knife = double(Chef::Knife) - Chef::Knife.should_receive(:run).with(ARGV, @knife.options).and_return(knife) - @knife.should_receive(:exit).with(0) + expect(Chef::Knife).to receive(:run).with(ARGV, @knife.options).and_return(knife) + expect(@knife).to receive(:exit).with(0) @knife.run end end diff --git a/spec/unit/application/solo_spec.rb b/spec/unit/application/solo_spec.rb index e29fcc9367..26d7d34caa 100644 --- a/spec/unit/application/solo_spec.rb +++ b/spec/unit/application/solo_spec.rb @@ -19,12 +19,12 @@ require 'spec_helper' describe Chef::Application::Solo do before do - Kernel.stub(:trap).and_return(:ok) + allow(Kernel).to receive(:trap).and_return(:ok) @app = Chef::Application::Solo.new - @app.stub(:configure_opt_parser).and_return(true) - @app.stub(:configure_chef).and_return(true) - @app.stub(:configure_logging).and_return(true) - @app.stub(:trap) + allow(@app).to receive(:configure_opt_parser).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(:trap) Chef::Config[:recipe_url] = false Chef::Config[:json_attribs] = false Chef::Config[:solo] = true @@ -33,7 +33,7 @@ describe Chef::Application::Solo do describe "configuring the application" do it "should set solo mode to true" do @app.reconfigure - Chef::Config[:solo].should be_true + expect(Chef::Config[:solo]).to be_truthy end describe "when configured to not fork the client process" do @@ -50,7 +50,7 @@ describe Chef::Application::Solo do end it "should terminate with message" do - Chef::Application.should_receive(:fatal!).with( + expect(Chef::Application).to receive(:fatal!).with( "Unforked chef-client interval runs are disabled in Chef 12. Configuration settings: interval = 600 seconds @@ -69,7 +69,7 @@ Enable chef-client interval runs by setting `:client_fork = true` in your config it "should set the interval to 1800" do Chef::Config[:interval] = nil @app.reconfigure - Chef::Config[:interval].should == 1800 + expect(Chef::Config[:interval]).to eq(1800) end end @@ -80,13 +80,13 @@ Enable chef-client interval runs by setting `:client_fork = true` in your config before do Chef::Config[:json_attribs] = json_source - Chef::ConfigFetcher.should_receive(:new).with(json_source). + expect(Chef::ConfigFetcher).to receive(:new).with(json_source). and_return(config_fetcher) end it "reads the JSON attributes from the specified source" do @app.reconfigure - @app.chef_client_json.should == json_attribs + expect(@app.chef_client_json).to eq(json_attribs) end end @@ -94,33 +94,33 @@ Enable chef-client interval runs by setting `:client_fork = true` in your config before do Chef::Config[:cookbook_path] = "#{Dir.tmpdir}/chef-solo/cookbooks" Chef::Config[:recipe_url] = "http://junglist.gen.nz/recipes.tgz" - FileUtils.stub(:mkdir_p).and_return(true) + allow(FileUtils).to receive(:mkdir_p).and_return(true) @tarfile = StringIO.new("remote_tarball_content") - @app.stub(:open).with("http://junglist.gen.nz/recipes.tgz").and_yield(@tarfile) + allow(@app).to receive(:open).with("http://junglist.gen.nz/recipes.tgz").and_yield(@tarfile) @target_file = StringIO.new - File.stub(:open).with("#{Dir.tmpdir}/chef-solo/recipes.tgz", "wb").and_yield(@target_file) + allow(File).to receive(:open).with("#{Dir.tmpdir}/chef-solo/recipes.tgz", "wb").and_yield(@target_file) - Chef::Mixin::Command.stub(:run_command).and_return(true) + allow(Chef::Mixin::Command).to receive(:run_command).and_return(true) end it "should create the recipes path based on the parent of the cookbook path" do - FileUtils.should_receive(:mkdir_p).with("#{Dir.tmpdir}/chef-solo").and_return(true) + expect(FileUtils).to receive(:mkdir_p).with("#{Dir.tmpdir}/chef-solo").and_return(true) @app.reconfigure end it "should download the recipes" do - @app.should_receive(:open).with("http://junglist.gen.nz/recipes.tgz").and_yield(@tarfile) + expect(@app).to receive(:open).with("http://junglist.gen.nz/recipes.tgz").and_yield(@tarfile) @app.reconfigure end it "should write the recipes to the target path" do @app.reconfigure - @target_file.string.should == "remote_tarball_content" + expect(@target_file.string).to eq("remote_tarball_content") end it "should untar the target file to the parent of the cookbook path" do - Chef::Mixin::Command.should_receive(:run_command).with({:command => "tar zxvf #{Dir.tmpdir}/chef-solo/recipes.tgz -C #{Dir.tmpdir}/chef-solo"}).and_return(true) + expect(Chef::Mixin::Command).to receive(:run_command).with({:command => "tar zxvf #{Dir.tmpdir}/chef-solo/recipes.tgz -C #{Dir.tmpdir}/chef-solo"}).and_return(true) @app.reconfigure end end @@ -135,13 +135,13 @@ Enable chef-client interval runs by setting `:client_fork = true` in your config Chef::Config[:json_attribs] = json_source Chef::Config[:recipe_url] = "http://icanhas.cheezburger.com/lolcats" Chef::Config[:cookbook_path] = "#{Dir.tmpdir}/chef-solo/cookbooks" - FileUtils.stub(:mkdir_p).and_return(true) - Chef::Mixin::Command.stub(:run_command).and_return(true) + allow(FileUtils).to receive(:mkdir_p).and_return(true) + allow(Chef::Mixin::Command).to receive(:run_command).and_return(true) end it "should fetch the recipe_url first" do - @app.should_receive(:fetch_recipe_tarball).ordered - Chef::ConfigFetcher.should_receive(:new).ordered.and_return(config_fetcher) + expect(@app).to receive(:fetch_recipe_tarball).ordered + expect(Chef::ConfigFetcher).to receive(:new).ordered.and_return(config_fetcher) @app.reconfigure end end @@ -150,18 +150,18 @@ Enable chef-client interval runs by setting `:client_fork = true` in your config before do Chef::Config[:solo] = true - Chef::Daemon.stub(:change_privilege) + allow(Chef::Daemon).to receive(:change_privilege) @chef_client = double("Chef::Client") - Chef::Client.stub(:new).and_return(@chef_client) + allow(Chef::Client).to receive(:new).and_return(@chef_client) @app = Chef::Application::Solo.new # this is all stuff the reconfigure method needs - @app.stub(:configure_opt_parser).and_return(true) - @app.stub(:configure_chef).and_return(true) - @app.stub(:configure_logging).and_return(true) + allow(@app).to receive(:configure_opt_parser).and_return(true) + allow(@app).to receive(:configure_chef).and_return(true) + allow(@app).to receive(:configure_logging).and_return(true) end it "should change privileges" do - Chef::Daemon.should_receive(:change_privilege).and_return(true) + expect(Chef::Daemon).to receive(:change_privilege).and_return(true) @app.setup_application end end |