require 'support/shared/integration/integration_helper' require 'chef/mixin/shell_out' require 'tiny_server' require 'tmpdir' def recipes_filename File.join(CHEF_SPEC_DATA, 'recipes.tgz') end def start_tiny_server(server_opts={}) recipes_size = File::Stat.new(recipes_filename).size @server = TinyServer::Manager.new(server_opts) @server.start @api = TinyServer::API.instance @api.clear # # trivial endpoints # # just a normal file # (expected_content should be uncompressed) @api.get("/recipes.tgz", 200) { File.open(recipes_filename, "rb") do |f| f.read end } end def stop_tiny_server @server.stop @server = @api = nil end describe "chef-client" do include 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' --minimal-ohai" } when_the_repository "has a cookbook with a no-op recipe" do before { file 'cookbooks/x/recipes/default.rb', '' } it "should complete with success" do file 'config/client.rb', < chef_dir) result.error! end it "should complete successfully with --no-listen" 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' --disable-config", :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' --disable-config", :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' --disable-config", :cwd => File.expand_path('..', path_to(''))) expect(result.exitstatus).to eq(1) end end context 'and a config file under .chef/knife.rb' do before { 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'", :cwd => path_to('')) # FATAL: Configuration error NoMethodError: undefined method `xxx' for nil:NilClass expect(result.stdout).to include("xxx") end end it "should complete with success" do file 'config/client.rb', < chef_dir) result.error! end context 'and a private key' do before 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! expect(IO.read(path_to('tempfile.txt'))).to eq('1') expect(IO.read(path_to('tempfile2.txt'))).to eq('2') end it "should run recipes specified as relative paths directly on the command line" do file 'config/client.rb', < path_to('')) result.error! expect(IO.read(path_to('tempfile.txt'))).to eq('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! expect(IO.read(path_to('tempfile.txt'))).to eq('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 not print SSL warnings when running in local-mode" do file 'config/client.rb', < chef_dir) expect(result.stdout).not_to include("SSL validation of HTTPS requests is disabled.") 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) expect(result.stdout).not_to include("Overridden Run List") expect(result.stdout).to include("Run List is [recipe[x::default]]") #puts result.stdout result.error! end end when_the_repository "has a cookbook with only an audit recipe" do before do file 'config/client.rb', < chef_dir) expect(result.error?).to be_falsey expect(result.stdout).to include("Successfully executed all `control_group` blocks and contained examples") end it "should exit with a non-zero code when there is an audit failure" do file 'cookbooks/audit_test/recipes/fail.rb', <<-RECIPE control_group "control group without top level control" do it "should fail" do expect(2 - 2).to eq(1) end end RECIPE result = shell_out("#{chef_client} -c \"#{path_to('config/client.rb')}\" -o 'audit_test::fail'", :cwd => chef_dir) expect(result.error?).to be_truthy expect(result.stdout).to include("Failure/Error: expect(2 - 2).to eq(1)") end end context "when using recipe-url" do before(:all) do start_tiny_server end after(:all) do stop_tiny_server end let(:tmp_dir) { Dir.mktmpdir("recipe-url") } it "should complete with success when passed -z and --recipe-url" do file 'config/client.rb', < tmp_dir) result.error! end it 'should fail when passed --recipe-url and not passed -z' do result = shell_out("#{chef_client} --recipe-url=http://localhost:9000/recipes.tgz", :cwd => tmp_dir) expect(result.exitstatus).to eq(1) end end end