require 'support/shared/integration/integration_helper' require 'chef/mixin/shell_out' describe "chef-client" do extend IntegrationSupport include Chef::Mixin::ShellOut let(:chef_dir) { File.join(File.dirname(__FILE__), "..", "..", "..", "bin") } # Invoke `chef-client` as `ruby PATH/TO/chef-client`. This ensures the # following constraints are satisfied: # * Windows: windows can only run batch scripts as bare executables. Rubygems # creates batch wrappers for installed gems, but we don't have batch wrappers # in the source tree. # * Other `chef-client` in PATH: A common case is running the tests on a # machine that has omnibus chef installed. In that case we need to ensure # we're running `chef-client` from the source tree and not the external one. # cf. CHEF-4914 let(:chef_client) { "ruby #{chef_dir}/chef-client" } when_the_repository "has a cookbook with a no-op recipe" do file 'cookbooks/x/recipes/default.rb', '' it "should complete with success" do file 'config/client.rb', < chef_dir) result.error! end context 'and no config file' do it 'should complete with success when cwd is just above cookbooks and paths are not specified' do result = shell_out("#{chef_client} -z -o 'x::default' --config-file-jail \"#{path_to('')}\"", :cwd => path_to('')) result.error! end it 'should complete with success when cwd is below cookbooks and paths are not specified' do result = shell_out("#{chef_client} -z -o 'x::default' --config-file-jail \"#{path_to('')}\"", :cwd => path_to('cookbooks/x')) result.error! end it 'should fail when cwd is below high above and paths are not specified' do result = shell_out("#{chef_client} -z -o 'x::default' --config-file-jail \"#{path_to('')}\"", :cwd => File.expand_path('..', path_to(''))) result.exitstatus.should == 1 end end context 'and a config file under .chef/knife.rb' do file '.chef/knife.rb', 'xxx.xxx' it 'should load .chef/knife.rb when -z is specified' do result = shell_out("#{chef_client} -z -o 'x::default' --config-file-jail \"#{path_to('')}\"", :cwd => path_to('')) # FATAL: Configuration error NoMethodError: undefined method `xxx' for nil:NilClass result.stdout.should include("xxx") end it 'fails to load .chef/knife.rb when -z is specified and --config-file-jail does not include the .chef/knife.rb' do result = shell_out("#{chef_client} -z -o 'x::default' --config-file-jail \"#{path_to('roles')}\"", :cwd => path_to('')) result.error! end end it "should complete with success" do file 'config/client.rb', < chef_dir) result.error! end context 'and a private key' do file 'mykey.pem', < chef_dir) result.error! end it "should run recipes specified directly on the command line" do file 'config/client.rb', < chef_dir) result.error! IO.read(path_to('tempfile.txt')).should == '1' IO.read(path_to('tempfile2.txt')).should == '2' end it "should run recipes specified as relative paths directly on the command line" do file 'config/client.rb', < path_to('')) result.error! IO.read(path_to('tempfile.txt')).should == '1' end it "should run recipes specified directly on the command line AFTER recipes in the run list" do file 'config/client.rb', < path_to('')) result.error! IO.read(path_to('tempfile.txt')).should == '1' end end it "should complete with success when passed the -z flag" do file 'config/client.rb', < chef_dir) result.error! end it "should complete with success when passed the --local-mode flag" do file 'config/client.rb', < chef_dir) result.error! end it "should complete with success when passed -z and --chef-zero-port" do file 'config/client.rb', < chef_dir) result.error! end it "should complete with success when setting the run list with -r" do file 'config/client.rb', < chef_dir) result.stdout.should_not include("Overridden Run List") result.stdout.should include("Run List is [recipe[x::default]]") #puts result.stdout result.error! end end end