|
| 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 |
0 commit comments