Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Adding license acceptance support to ChefDK #2074

Merged
merged 2 commits into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .expeditor/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,6 @@ subscriptions:
- workload: ruby_gem_published:stove-*
actions:
- bash:.expeditor/update_dep.sh
- workload: ruby_gem_published:license-acceptance-*
actions:
- bash:.expeditor/update_dep.sh
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ PATH
cookbook-omnifetch (~> 0.5)
diff-lcs (~> 1.0)
ffi-yajl (>= 1.0, < 3.0)
license-acceptance (~> 1.0, >= 1.0.11)
minitar (~> 0.6)
mixlib-cli (>= 1.7, < 3.0)
mixlib-shellout (>= 2.0, < 4.0)
Expand Down
2 changes: 1 addition & 1 deletion bin/chef
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ Kernel.trap(:INT) { print("\n"); exit 1 }
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
require "chef-dk/cli"

ChefDK::CLI.new(ARGV.clone).run
ChefDK::CLI.new(ARGV.clone).run(enforce_license: true)
1 change: 1 addition & 0 deletions chef-dk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Gem::Specification.new do |gem|
gem.add_dependency "cookbook-omnifetch", "~> 0.5"
gem.add_dependency "diff-lcs", "~> 1.0"
gem.add_dependency "paint", "~> 1.0"
gem.add_dependency "license-acceptance", "~> 1.0", ">= 1.0.11"

%w{rspec-core rspec-expectations rspec-mocks}.each do |dev_gem|
gem.add_development_dependency dev_gem, "~> 3.0"
Expand Down
4 changes: 2 additions & 2 deletions lib/chef-dk/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def initialize(argv)
super() # mixlib-cli #initialize doesn't allow arguments
end

def run
def run(enforce_license: false)
sanity_check!

subcommand_name, *subcommand_params = argv
Expand All @@ -70,7 +70,7 @@ def run
handle_options
elsif have_command?(subcommand_name)
subcommand = instantiate_subcommand(subcommand_name)
exit_code = subcommand.run_with_default_options(subcommand_params)
exit_code = subcommand.run_with_default_options(enforce_license, subcommand_params)
exit normalized_exit_code(exit_code)
else
err "Unknown command `#{subcommand_name}'."
Expand Down
10 changes: 9 additions & 1 deletion lib/chef-dk/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
require "chef-dk/helpers"
require "chef-dk/version"
require "chef/exceptions"
require "license_acceptance/acceptor"
require "license_acceptance/cli_flags/mixlib_cli"

module ChefDK
module Command
class Base
include Mixlib::CLI
include ChefDK::Helpers
include LicenseAcceptance::CLIFlags::MixlibCLI

option :help,
short: "-h",
Expand All @@ -47,14 +50,15 @@ def initialize
# In order to control this behavior, make sure the default options are
# handled here.
#
def run_with_default_options(params = [ ])
def run_with_default_options(enforce_license, params = [ ])
if needs_help?(params)
msg(opt_parser.to_s)
0
elsif needs_version?(params)
msg("Chef Development Kit Version: #{ChefDK::VERSION}")
0
else
check_license_acceptance if enforce_license
run(params)
end
rescue Chef::Exceptions::ConfigurationError => e
Expand All @@ -74,6 +78,10 @@ def needs_version?(params)
params.include?("-v") || params.include?("--version")
end

def check_license_acceptance
LicenseAcceptance::Acceptor.check_and_persist!("chef-dk", ChefDK::VERSION.to_s)
end

end
end
end
25 changes: 24 additions & 1 deletion spec/unit/command/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def run(params)
let(:stderr_io) { StringIO.new }
let(:stdout_io) { StringIO.new }
let(:command_instance) { TestCommand.new() }
let(:enforce_license) { false }

def stdout
stdout_io.string
Expand All @@ -57,7 +58,7 @@ def stderr
end

def run_command(options)
command_instance.run_with_default_options(options)
command_instance.run_with_default_options(enforce_license, options)
end

it "should print the banner for -h" do
Expand Down Expand Up @@ -95,6 +96,26 @@ def run_command(options)
expect(stdout).to eq("thanks for passing me true\n")
end

describe "when enforce_license is true" do
tyler-ball marked this conversation as resolved.
Show resolved Hide resolved
let(:enforce_license) { true }

it "calls the license acceptance library" do
expect(LicenseAcceptance::Acceptor).to receive(:check_and_persist!).with("chef-dk", ChefDK::VERSION.to_s)
run_command([])
expect(stdout).to eq("thanks for passing me \n")
end
end

describe "when enforce_license is false" do
let(:enforce_license) { false }

it "does not call the license acceptance library" do
expect(LicenseAcceptance::Acceptor).to_not receive(:check_and_persist!)
run_command([])
expect(stdout).to eq("thanks for passing me \n")
end
end

describe "when given invalid options" do

it "prints the help banner and exits gracefully" do
Expand All @@ -105,6 +126,7 @@ def run_command(options)
expected = <<~E
use me please
-a, --arg ARG An option with a required argument
--chef-license ACCEPTANCE Accept the license for this product and any contained products ('accept', 'accept-no-persist', or 'accept-silent')
-h, --help Show this message
-u, --user If the user exists
-v, --version Show chef version
Expand All @@ -125,6 +147,7 @@ def run_command(options)
expected = <<~E
use me please
-a, --arg ARG An option with a required argument
--chef-license ACCEPTANCE Accept the license for this product and any contained products ('accept', 'accept-no-persist', or 'accept-silent')
-h, --help Show this message
-u, --user If the user exists
-v, --version Show chef version
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/command/env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
end

def run_command
command_instance.run_with_default_options(command_options)
command_instance.run_with_default_options(false, command_options)
end

it "has a usage banner" do
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/command/exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
let(:command_options) { [] }

def run_command
command_instance.run_with_default_options(command_options)
command_instance.run_with_default_options(false, command_options)
end

it "has a usage banner" do
Expand Down