Skip to content

Commit d9959f3

Browse files
authored
Merge branch 'habitat_pkg_plan_linux' into nikhil/hab-path-changes
2 parents 654703b + cd601aa commit d9959f3

File tree

9 files changed

+346
-1
lines changed

9 files changed

+346
-1
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ gem "logger", "< 1.6" # 1.6 causes errors with mixlib-log < 3.1.1
66
gem "chefspec"
77
group :test do
88
gem "rake"
9-
gem "rspec", "~> 3.8"
9+
gem "rspec", "=3.12.0"
1010
gem "rspec-expectations", "~> 3.8"
1111
gem "rspec-mocks", "~> 3.8"
1212
gem "cookstyle"

habitat/plan.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ do_install() {
9494
appbundler $CACHE_PATH $pkg_prefix/bin $gem
9595
done
9696
)
97+
sed -i '1s|^.*|#!'$(pkg_path_for $_chef_client_ruby)/bin/ruby'|' ${pkg_prefix}/vendor/bin/rspec
98+
9799
}
98100

99101
do_after() {

lib/chef-cli/builtin_commands.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,6 @@
5757

5858
c.builtin "describe-cookbook", :DescribeCookbook, require_path: "chef-cli/command/describe_cookbook",
5959
desc: "Prints cookbook checksum information used for cookbook identifier"
60+
c.builtin "license", :License, require_path: "chef-cli/command/license",
61+
desc: "Create & install a new license on the system or view installed license(s)."
6062
end

lib/chef-cli/command/license.rb

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright:: Chef Software Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require_relative "base"
20+
require "chef-cli/licensing/base"
21+
require_relative "../configurable"
22+
23+
module ChefCLI
24+
module Command
25+
26+
# This class will manage the license command in the chef-cli
27+
class License < Base
28+
29+
include Configurable
30+
31+
MAIN_COMMAND_HELP = <<~HELP.freeze
32+
Usage: #{ChefCLI::Dist::EXEC} license [SUBCOMMAND]
33+
34+
`#{ChefCLI::Dist::EXEC} license` command will validate the existing license
35+
or will help you interactively generate new free/trial license and activate the
36+
commercial license the chef team has sent you through email.
37+
HELP
38+
39+
SUB_COMMANDS = [
40+
{ name: "list", description: "List details of the license(s) installed on the system." },
41+
{ name: "add", description: "Create & install a Free/ Trial license or install a Commercial license on the system." },
42+
].freeze
43+
44+
option :chef_license_key,
45+
long: "--chef-license-key LICENSE",
46+
description: "New license key to accept and store in the system"
47+
48+
attr_accessor :ui
49+
50+
def self.banner
51+
<<~BANNER
52+
#{MAIN_COMMAND_HELP}
53+
Subcommands:
54+
#{SUB_COMMANDS.map do |c|
55+
" #{c[:name].ljust(7)}#{c[:description]}"
56+
end.join("\n") }
57+
58+
Options:
59+
BANNER
60+
end
61+
62+
def initialize
63+
super
64+
65+
@ui = UI.new
66+
end
67+
68+
def run(params)
69+
config_license_debug if debug?
70+
remaining_args = parse_options(params)
71+
return 1 unless validate_params!(remaining_args)
72+
73+
if remaining_args.blank?
74+
ChefCLI::Licensing::Base.validate
75+
else
76+
ChefCLI::Licensing::Base.send(remaining_args[0])
77+
end
78+
end
79+
80+
def debug?
81+
!!config[:debug]
82+
end
83+
84+
def validate_params!(args)
85+
if args.length > 1
86+
ui.err("Too many arguments")
87+
return false
88+
end
89+
90+
valid_subcommands = SUB_COMMANDS.collect { |c| c[:name] }
91+
args.each do |arg|
92+
next if valid_subcommands.include?(arg)
93+
94+
ui.err("Invalid option: #{arg}")
95+
return false
96+
end
97+
98+
true
99+
end
100+
101+
private
102+
103+
def config_license_debug
104+
ChefLicensing.output = ui
105+
end
106+
end
107+
end
108+
end

lib/chef-cli/licensing/base.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright:: Chef Software Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require "chef-licensing"
20+
require_relative "config"
21+
22+
module ChefCLI
23+
module Licensing
24+
class Base
25+
class << self
26+
def validate
27+
ChefLicensing.fetch_and_persist.each do |license_key|
28+
puts "License Key: #{license_key}"
29+
end
30+
end
31+
32+
def list
33+
ChefLicensing.list_license_keys_info
34+
end
35+
36+
def add
37+
ChefLicensing.add_license
38+
end
39+
end
40+
end
41+
end
42+
end

lib/chef-cli/licensing/config.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright:: Chef Software Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require "chef-licensing"
20+
21+
ChefLicensing.configure do |config|
22+
config.chef_product_name = "workstation"
23+
config.chef_entitlement_id = "x6f3bc76-a94f-4b6c-bc97-4b7ed2b045c0"
24+
config.chef_executable_name = "chef"
25+
# config.license_server_url = "https://services.chef.io/licensing"
26+
config.license_server_url = "https://licensing-acceptance.chef.co/License"
27+
end

spec/unit/cli_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ def mock_shell_out(exitstatus, stdout, stderr)
8989

9090
commands_map.builtin "example", :TestCommand, require_path: "unit/fixtures/command/cli_test_command",
9191
desc: "Example subcommand for testing"
92+
93+
allow(ChefCLI::Licensing::Base).to receive(:validate).and_return(true)
9294
end
9395

9496
context "given no arguments or options" do

spec/unit/command/license_spec.rb

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#
2+
# Copyright:: Chef Software Inc.
3+
# License:: Apache License, Version 2.0
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
require "spec_helper"
19+
require "chef-cli/command/license"
20+
require "shared/command_with_ui_object"
21+
require "chef-cli/licensing/base"
22+
23+
describe ChefCLI::Command::License do
24+
it_behaves_like "a command with a UI object"
25+
26+
let(:params) { [] }
27+
let(:ui) { TestHelpers::TestUI.new }
28+
29+
before do
30+
# Disable the access of local licenses
31+
allow_any_instance_of(ChefLicensing::LicenseKeyFetcher).to receive(:fetch_license_key_from_arg).and_return([])
32+
allow_any_instance_of(ChefLicensing::LicenseKeyFetcher).to receive(:fetch_license_key_from_env).and_return([])
33+
allow_any_instance_of(ChefLicensing::LicensingService::Local).to receive(:detected?).and_return(false)
34+
end
35+
36+
let(:command) do
37+
c = described_class.new
38+
c.validate_params!(params)
39+
c.ui = ui
40+
c
41+
end
42+
43+
it "disables debug by default" do
44+
expect(command.debug?).to be(false)
45+
end
46+
47+
context "invalid parameters passed" do
48+
let(:multiple_params) { %w{add list} }
49+
let(:invalid_command) { %w{not_a_subcommand} }
50+
51+
it "should fail with errors when multiple subcommands passed" do
52+
expect(command.run(multiple_params)).to eq(1)
53+
end
54+
55+
it "should fail for invalid argument" do
56+
expect(command.run(invalid_command)).to eq(1)
57+
end
58+
end
59+
60+
context "license command" do
61+
context "when pre-accepted license exists" do
62+
let(:license_keys) { %w{tsmc-abcd} }
63+
64+
before(:each) do
65+
allow(ChefLicensing).to receive(:fetch_and_persist).and_return(license_keys)
66+
end
67+
68+
it "should be successful" do
69+
expect { command.run(params) }.not_to raise_exception
70+
end
71+
72+
it "should return the correct license key" do
73+
expect(command.run(params)).to eq(license_keys)
74+
end
75+
end
76+
77+
context "when no licenses are accepted previously" do
78+
let(:new_key) { ["tsmc-123456789"] }
79+
before(:each) do
80+
ChefLicensing.configure do |config|
81+
config.license_server_url = "https://license.test"
82+
config.chef_product_name = "chef"
83+
config.chef_entitlement_id = "chef-entitled-id"
84+
config.chef_executable_name = "chef"
85+
end
86+
87+
# Disable the active license check
88+
allow_any_instance_of(ChefLicensing::LicenseKeyFetcher).to receive(:licenses_active?).and_return(false)
89+
# Disable the UI engine
90+
allow_any_instance_of(ChefLicensing::LicenseKeyFetcher).to receive(:append_extra_info_to_tui_engine)
91+
# Disable the API call to fetch the license type
92+
allow_any_instance_of(ChefLicensing::LicenseKeyFetcher).to receive(:get_license_type).and_return("free")
93+
# Disable the overwriting to the license.yml file
94+
allow_any_instance_of(ChefLicensing::LicenseKeyFetcher::File).to receive(:persist)
95+
96+
# Mocks the user prompt to enter the license
97+
allow_any_instance_of(ChefLicensing::LicenseKeyFetcher::Prompt).to receive(:fetch).and_return(new_key)
98+
allow(ChefLicensing).to receive(:fetch_and_persist).and_return(new_key)
99+
end
100+
101+
it "should create and stores the new license" do
102+
expect { command.run(params) }.not_to raise_exception
103+
end
104+
105+
it "should be same as the user entered license" do
106+
expect(command.run(params)).to include(new_key.first)
107+
end
108+
end
109+
end
110+
111+
context "chef license list command" do
112+
let(:params) { %w{list} }
113+
let(:license_key) { "tsmn-123123" }
114+
115+
before do
116+
command.ui = ui
117+
end
118+
119+
context "when no licenses are accepted" do
120+
before do
121+
allow_any_instance_of(ChefLicensing::ListLicenseKeys).to receive(:fetch_license_keys).and_return([])
122+
allow_any_instance_of(ChefLicensing::ListLicenseKeys).to receive(:fetch_licenses_metadata).and_return([])
123+
end
124+
125+
it "should return the correct error message" do
126+
expect(command.run(params)).to eq([])
127+
end
128+
end
129+
130+
context "when there is a valid license" do
131+
before do
132+
allow(ChefLicensing).to receive(:list_license_keys_info).and_return(license_key)
133+
end
134+
135+
it "should print the license details" do
136+
expect(command.run(params)).to eq(license_key)
137+
end
138+
end
139+
end
140+
141+
context "chef license add command" do
142+
let(:params) { %w{add} }
143+
let(:license_key) { ["tsmn-123123"] }
144+
145+
before do
146+
# Disable the API call to fetch the license type
147+
allow_any_instance_of(ChefLicensing::LicenseKeyFetcher).to receive(:get_license_type).and_return("free")
148+
# Disable the overwriting to the license.yml file
149+
allow_any_instance_of(ChefLicensing::LicenseKeyFetcher::File).to receive(:persist)
150+
# Mocks the user prompt to enter the license
151+
allow_any_instance_of(ChefLicensing::LicenseKeyFetcher::Prompt).to receive(:fetch).and_return(license_key)
152+
end
153+
154+
it "should not raise any errors" do
155+
expect { command.run(params) }.not_to raise_exception
156+
end
157+
158+
it "should create and store the new license" do
159+
expect(command.run(params)).to include(license_key.first)
160+
end
161+
end
162+
end

spec/unit/fixtures/local_path_cookbooks/noignore-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/chefignore

Whitespace-only changes.

0 commit comments

Comments
 (0)