diff --git a/chef-test-kitchen-enterprise.gemspec b/chef-test-kitchen-enterprise.gemspec index 5f7b91daf..115c16d76 100644 --- a/chef-test-kitchen-enterprise.gemspec +++ b/chef-test-kitchen-enterprise.gemspec @@ -38,4 +38,5 @@ Gem::Specification.new do |gem| # TK is not under Chef EULA gem.add_dependency "license-acceptance", ">= 1.0.11", "< 3.0" # pinning until we can confirm 3+ works gem.add_dependency "chef-licensing", "~> 1.0" + gem.add_dependency "berkshelf", "~> 8.0" # for managing berks cookbooks end diff --git a/habitat/plan.sh b/habitat/plan.sh index ef7e232b8..c25014ef9 100644 --- a/habitat/plan.sh +++ b/habitat/plan.sh @@ -62,9 +62,7 @@ do_install() { build_line "Setting GEM_PATH=$GEM_HOME" export GEM_PATH="$GEM_HOME" gem install chef-test-kitchen-enterprise-*.gem --no-document - gem install chef-cli wrap_ruby_kitchen - wrap_ruby_chef_cli set_runtime_env "GEM_PATH" "${pkg_prefix}/vendor" } @@ -74,12 +72,6 @@ wrap_ruby_kitchen() { wrap_bin_with_ruby "$bin" "$real_bin" } -wrap_ruby_chef_cli() { - local bin="$pkg_prefix/bin/chef-cli" - local real_bin="$GEM_HOME/bin/chef-cli" - wrap_bin_with_ruby "$bin" "$real_bin" -} - wrap_bin_with_ruby() { local bin="$1" local real_bin="$2" diff --git a/lib/kitchen/provisioner/chef/policyfile.rb b/lib/kitchen/provisioner/chef/policyfile.rb index 743fea636..4b40b7934 100644 --- a/lib/kitchen/provisioner/chef/policyfile.rb +++ b/lib/kitchen/provisioner/chef/policyfile.rb @@ -150,16 +150,29 @@ def escape_path(path) # @api private # @returns [String] def cli_path - @cli_path ||= which("chef-cli") || which("chef") || no_cli_found_error + @cli_path ||= which("chef-cli") || hab_chef_cli || no_cli_found_error + end + + # If the habitat package for chef-cli is installed and not binlinked, + # return the hab pkg exec command to run chef-cli. + def hab_chef_cli + "hab pkg exec chef/chef-cli chef-cli" if hab_pkg_installed?("chef/chef-cli") + end + + # Check whether a habitat package is installed or not + def hab_pkg_installed?(pkg) + if which("hab") + `hab pkg list #{pkg} 2>/dev/null`.include?(pkg) + else + false + end end # @api private def no_cli_found_error - @logger.fatal("The `chef` or `chef-cli` executables cannot be found in your " \ - "PATH. Ensure you have installed Chef Workstation " \ - "from https://www.chef.io/downloads/ and that your PATH " \ - "setting includes the path to the `chef` or `chef-cli` commands.") - raise UserError, "Could not find the chef or chef-cli executables in your PATH." + @logger.fatal("The `chef-cli` executable or the `chef/chef-cli` Habitat package cannot be found in your PATH. " \ + "Ensure that you have installed the Chef Development Kit Enterprise Habitat package.") + raise UserError, "Could not find the chef-cli executables or the chef/chef-cli hab package." end end end diff --git a/spec/kitchen/provisioner/chef/policyfile_spec.rb b/spec/kitchen/provisioner/chef/policyfile_spec.rb index 5590b3db9..c24cc124a 100644 --- a/spec/kitchen/provisioner/chef/policyfile_spec.rb +++ b/spec/kitchen/provisioner/chef/policyfile_spec.rb @@ -48,10 +48,10 @@ describe "#resolve" do subject { described_object.resolve } - describe "on Unix with chef" do + describe "on Unix with chef-cli hab pkg" do before do described_object.expects(:which).with("chef-cli").returns(false) - described_object.expects(:which).with("chef").returns("chef") + described_object.expects(:hab_pkg_installed?).with("chef/chef-cli").returns(true) end let(:os) { "linux-gnu" } @@ -61,7 +61,7 @@ let(:path) { "/tmp/kitchen/cookbooks" } let(:license) { "accept" } it do - described_object.expects(:run_command).with("chef export /home/user/cookbook/Policyfile.rb /tmp/kitchen/cookbooks --force --chef-license accept") + described_object.expects(:run_command).with("hab pkg exec chef/chef-cli chef-cli export /home/user/cookbook/Policyfile.rb /tmp/kitchen/cookbooks --force --chef-license accept") subject end end @@ -71,7 +71,7 @@ let(:path) { "/tmp/kitchen/cookbooks" } let(:license) { "accept-silent" } it do - described_object.expects(:run_command).with("chef export /home/jenkins/My\\ Chef\\ Cookbook/workspace/current/Policyfile.rb /tmp/kitchen/cookbooks --force --chef-license accept-silent") + described_object.expects(:run_command).with("hab pkg exec chef/chef-cli chef-cli export /home/jenkins/My\\ Chef\\ Cookbook/workspace/current/Policyfile.rb /tmp/kitchen/cookbooks --force --chef-license accept-silent") subject end end @@ -81,7 +81,7 @@ let(:path) { "/tmp/kitchen/cookbooks" } let(:license) { "accept-no-persist" } it do - described_object.expects(:run_command).with("chef export /home/user/cookbook/Policyfile.rb /tmp/kitchen/cookbooks --force --chef-license accept-no-persist") + described_object.expects(:run_command).with("hab pkg exec chef/chef-cli chef-cli export /home/user/cookbook/Policyfile.rb /tmp/kitchen/cookbooks --force --chef-license accept-no-persist") subject end end @@ -125,10 +125,10 @@ end end - describe "on Windows with chef" do + describe "on Windows with chef-cli hab pkg" do before do described_object.expects(:which).with("chef-cli").returns(false) - described_object.expects(:which).with("chef").returns("chef") + described_object.expects(:hab_pkg_installed?).with("chef/chef-cli").returns(true) end let(:os) { "mswin" } @@ -138,7 +138,7 @@ let(:path) { 'C:\\Temp\\kitchen\\cookbooks' } let(:license) { "accept" } it do - described_object.expects(:run_command).with('chef export C:\\cookbook\\Policyfile.rb C:\\Temp\\kitchen\\cookbooks --force --chef-license accept') + described_object.expects(:run_command).with('hab pkg exec chef/chef-cli chef-cli export C:\\cookbook\\Policyfile.rb C:\\Temp\\kitchen\\cookbooks --force --chef-license accept') subject end end @@ -148,7 +148,7 @@ let(:path) { 'C:\\Temp\\kitchen\\cookbooks' } let(:license) { "accept-silent" } it do - described_object.expects(:run_command).with('chef export "C:\\\\Program\\ Files\\\\Jenkins\\\\My\\ Chef\\ Cookbook\\\\workspace\\\\current\\\\Policyfile.rb" C:\\Temp\\kitchen\\cookbooks --force --chef-license accept-silent') + described_object.expects(:run_command).with('hab pkg exec chef/chef-cli chef-cli export "C:\\\\Program\\ Files\\\\Jenkins\\\\My\\ Chef\\ Cookbook\\\\workspace\\\\current\\\\Policyfile.rb" C:\\Temp\\kitchen\\cookbooks --force --chef-license accept-silent') subject end end @@ -158,7 +158,7 @@ let(:path) { 'C:\\Temp\\kitchen\\cookbooks' } let(:license) { "accept-no-persist" } it do - described_object.expects(:run_command).with('chef export "C:\\\\Program\\ Files\\\\Jenkins\\\\My\\ Chef\\ Cookbook\\\\workspace\\\\current\\\\Policyfile.rb" C:\\Temp\\kitchen\\cookbooks --force --chef-license accept-no-persist') + described_object.expects(:run_command).with('hab pkg exec chef/chef-cli chef-cli export "C:\\\\Program\\ Files\\\\Jenkins\\\\My\\ Chef\\ Cookbook\\\\workspace\\\\current\\\\Policyfile.rb" C:\\Temp\\kitchen\\cookbooks --force --chef-license accept-no-persist') subject end end @@ -205,7 +205,7 @@ describe "failure to resolve paths" do before do described_object.expects(:which).with("chef-cli").returns(false) - described_object.expects(:which).with("chef").returns(false) + described_object.expects(:hab_pkg_installed?).with("chef/chef-cli").returns(false) end let(:os) { "linux-gnu" } @@ -292,10 +292,10 @@ end end - describe "on Unix with chef" do + describe "on Unix with chef-cli hab pkg" do before do described_object.expects(:which).with("chef-cli").returns(false) - described_object.expects(:which).with("chef").returns("chef") + described_object.expects(:hab_pkg_installed?).with("chef/chef-cli").returns(true) end let(:os) { "linux-gnu" } @@ -304,7 +304,7 @@ let(:policyfile) { "/home/user/cookbook/Policyfile.rb" } let(:license) { "accept" } it do - described_object.expects(:run_command).with("chef install /home/user/cookbook/Policyfile.rb --chef-license accept") + described_object.expects(:run_command).with("hab pkg exec chef/chef-cli chef-cli install /home/user/cookbook/Policyfile.rb --chef-license accept") subject end end @@ -313,7 +313,7 @@ let(:policyfile) { "/home/jenkins/My Chef Cookbook/workspace/current/Policyfile.rb" } let(:license) { "accept-silent" } it do - described_object.expects(:run_command).with('chef install /home/jenkins/My\\ Chef\\ Cookbook/workspace/current/Policyfile.rb --chef-license accept-silent') + described_object.expects(:run_command).with('hab pkg exec chef/chef-cli chef-cli install /home/jenkins/My\\ Chef\\ Cookbook/workspace/current/Policyfile.rb --chef-license accept-silent') subject end end @@ -322,16 +322,16 @@ let(:policyfile) { "/home/jenkins/My Chef Cookbook/workspace/current/Policyfile.rb" } let(:license) { "accept-no-persist" } it do - described_object.expects(:run_command).with('chef install /home/jenkins/My\\ Chef\\ Cookbook/workspace/current/Policyfile.rb --chef-license accept-no-persist') + described_object.expects(:run_command).with('hab pkg exec chef/chef-cli chef-cli install /home/jenkins/My\\ Chef\\ Cookbook/workspace/current/Policyfile.rb --chef-license accept-no-persist') subject end end end - describe "on Windows with chef" do + describe "on Windows with chef-cli hab pkg" do before do described_object.expects(:which).with("chef-cli").returns(false) - described_object.expects(:which).with("chef").returns("chef") + described_object.expects(:hab_pkg_installed?).with("chef/chef-cli").returns(true) end let(:os) { "mswin" } @@ -340,7 +340,7 @@ let(:policyfile) { 'C:\\cookbook\\Policyfile.rb' } let(:license) { "accept" } it do - described_object.expects(:run_command).with('chef install C:\\cookbook\\Policyfile.rb --chef-license accept') + described_object.expects(:run_command).with('hab pkg exec chef/chef-cli chef-cli install C:\\cookbook\\Policyfile.rb --chef-license accept') subject end end @@ -349,7 +349,7 @@ let(:policyfile) { 'C:\\Program Files\\Jenkins\\My Chef Cookbook\\workspace\\current\\Policyfile.rb' } let(:license) { "accept-silent" } it do - described_object.expects(:run_command).with('chef install "C:\\\\Program\\ Files\\\\Jenkins\\\\My\\ Chef\\ Cookbook\\\\workspace\\\\current\\\\Policyfile.rb" --chef-license accept-silent') + described_object.expects(:run_command).with('hab pkg exec chef/chef-cli chef-cli install "C:\\\\Program\\ Files\\\\Jenkins\\\\My\\ Chef\\ Cookbook\\\\workspace\\\\current\\\\Policyfile.rb" --chef-license accept-silent') subject end end @@ -358,7 +358,7 @@ let(:policyfile) { 'C:\\Program Files\\Jenkins\\My Chef Cookbook\\workspace\\current\\Policyfile.rb' } let(:license) { "accept-no-persist" } it do - described_object.expects(:run_command).with('chef install "C:\\\\Program\\ Files\\\\Jenkins\\\\My\\ Chef\\ Cookbook\\\\workspace\\\\current\\\\Policyfile.rb" --chef-license accept-no-persist') + described_object.expects(:run_command).with('hab pkg exec chef/chef-cli chef-cli install "C:\\\\Program\\ Files\\\\Jenkins\\\\My\\ Chef\\ Cookbook\\\\workspace\\\\current\\\\Policyfile.rb" --chef-license accept-no-persist') subject end end