Skip to content

Commit

Permalink
Merge pull request #36 from chef/CHEF-18897-missing-berkshelf-gem
Browse files Browse the repository at this point in the history
[CHEF-18897] Add the berkshelf gem and use the chef-cli from the Chef-DKE
  • Loading branch information
ashiqueps authored Feb 25, 2025
2 parents 939b8e4 + 63a8f7c commit 36561ad
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
1 change: 1 addition & 0 deletions chef-test-kitchen-enterprise.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 0 additions & 8 deletions habitat/plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand All @@ -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"
Expand Down
25 changes: 19 additions & 6 deletions lib/kitchen/provisioner/chef/policyfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 21 additions & 21 deletions spec/kitchen/provisioner/chef/policyfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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" }
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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" }
Expand Down Expand Up @@ -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" }
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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" }
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 36561ad

Please sign in to comment.