summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVivek Singh <vivek.singh@msystechnologies.com>2019-07-29 15:20:29 +0530
committerVivek Singh <vivek.singh@msystechnologies.com>2019-07-29 15:20:29 +0530
commit5b5794730119956e32d7b7de34704c926d0875f5 (patch)
treed94391cecf203756182c75e1fc0698d63e54f53a
parentcf743e1152c54cf616a5266025cd278306f1b0db (diff)
downloadchef-5b5794730119956e32d7b7de34704c926d0875f5.tar.gz
Raise an error on Chef 14 when trying to bootstrap Chef 15
Signed-off-by: Vivek Singh <vivek.singh@msystechnologies.com>
-rw-r--r--lib/chef/knife/bootstrap.rb32
-rw-r--r--spec/unit/knife/bootstrap_spec.rb86
2 files changed, 118 insertions, 0 deletions
diff --git a/lib/chef/knife/bootstrap.rb b/lib/chef/knife/bootstrap.rb
index 5f2a0df00a..9af7ff5b48 100644
--- a/lib/chef/knife/bootstrap.rb
+++ b/lib/chef/knife/bootstrap.rb
@@ -28,6 +28,7 @@ class Chef
class Bootstrap < Knife
include DataBagSecretOptions
+ CHEF_15 ||= 15
attr_accessor :client_builder
attr_accessor :chef_vault_handler
@@ -366,6 +367,7 @@ class Chef
validate_name_args!
validate_options!
+ validate_bootstrap_version_options!
$stdout.sync = true
@@ -432,6 +434,22 @@ class Chef
true
end
+ # Ensure options are valid by checking bootstrap version values.
+ #
+ # The method call will cause the program to exit(1) if:
+ # * Bootstrap version is greater than or equal to 15 for chef 14 or lower
+ # * Pre-release options is passed for chef 14 or lower
+ #
+ # @return [TrueClass] If options are valid.
+ def validate_bootstrap_version_options!
+ if target_node_gt_15?
+ ui.error("You must use Chef 15 or later to bootstrap Chef 15 nodes")
+ exit 1
+ end
+
+ true
+ end
+
# setup a Chef::Knife::Ssh object using the passed config options
#
# @return Chef::Knife::Ssh
@@ -496,6 +514,20 @@ class Chef
(!!config[:policy_name] ^ config[:policy_group])
end
+ # True if the bootstrap version is greater than or equal to 15 or latest.
+ def bootstrap_version_gt_15?
+ !!config[:bootstrap_version] &&
+ (config[:bootstrap_version] == "latest" ||
+ config[:bootstrap_version].split(".").first.to_i >= CHEF_15)
+ end
+
+ # Consider only if current chef version less than 15.
+ # True if both bootstrap node version greater than or equal to 15 or pre-release is set.
+ def target_node_gt_15?
+ if Chef::VERSION.split(".").first.to_i < CHEF_15
+ config[:prerelease] || bootstrap_version_gt_15?
+ end
+ end
end
end
end
diff --git a/spec/unit/knife/bootstrap_spec.rb b/spec/unit/knife/bootstrap_spec.rb
index 76b9d49a8c..e4e89b2db3 100644
--- a/spec/unit/knife/bootstrap_spec.rb
+++ b/spec/unit/knife/bootstrap_spec.rb
@@ -633,6 +633,92 @@ describe Chef::Knife::Bootstrap do
end
+ describe "#validate_bootstrap_version_options!" do
+ describe "chef-14 or lower" do
+ before do
+ stub_const("Chef::VERSION", "14.0")
+ end
+
+ context "when bootstrap version not given" do
+ it "passes the validation" do
+ knife.config[:bootstrap_version] = nil
+ expect { knife.validate_bootstrap_version_options! }.to_not raise_error
+ end
+ end
+
+ context "when bootstrap version is given" do
+ it "passes the validation for chef 14.12.0 bootstrap version" do
+ knife.config[:bootstrap_version] = "14.12.0"
+ expect { knife.validate_bootstrap_version_options! }.to_not raise_error
+ end
+
+ it "passes the validation for chef 13.12.0 bootstrap version" do
+ knife.config[:bootstrap_version] = "13.12.0"
+ expect { knife.validate_bootstrap_version_options! }.to_not raise_error
+ end
+
+ it "raise an error for latest bootstrap version" do
+ knife.config[:bootstrap_version] = "latest"
+ expect { knife.validate_bootstrap_version_options! }.to raise_error(SystemExit)
+ end
+
+ it "raise an error for chef 15.0+ bootstrap version" do
+ knife.config[:bootstrap_version] = "15.0.1"
+ expect { knife.validate_bootstrap_version_options! }.to raise_error(SystemExit)
+ end
+
+ it "raise an error for chef 15.1+ bootstrap version" do
+ knife.config[:bootstrap_version] = "15.1"
+ expect { knife.validate_bootstrap_version_options! }.to raise_error(SystemExit)
+ end
+ end
+
+ context "when prerelease option is given" do
+ it "raise an error" do
+ knife.config[:prerelease] = true
+ expect { knife.validate_bootstrap_version_options! }.to raise_error(SystemExit)
+ end
+ end
+ end
+
+ describe "chef-15+" do
+ before do
+ stub_const("Chef::VERSION", "15.0")
+ end
+
+ context "when bootstrap version not given" do
+ it "passes the validation" do
+ knife.config[:bootstrap_version] = nil
+ expect { knife.validate_bootstrap_version_options! }.to_not raise_error
+ end
+ end
+
+ context "when bootstrap version is given" do
+ it "passes the validation for chef 14.12.0 bootstrap version" do
+ knife.config[:bootstrap_version] = "14.12.0"
+ expect { knife.validate_bootstrap_version_options! }.to_not raise_error
+ end
+
+ it "passes the validation for chef 15.2 bootstrap version" do
+ knife.config[:bootstrap_version] = "15.2"
+ expect { knife.validate_bootstrap_version_options! }.to_not raise_error
+ end
+
+ it "passes the validation for chef 16.2 bootstrap version" do
+ knife.config[:bootstrap_version] = "16.2"
+ expect { knife.validate_bootstrap_version_options! }.to_not raise_error
+ end
+ end
+
+ context "when prerelease option is given" do
+ it "passes the validation" do
+ knife.config[:prerelease] = true
+ expect { knife.validate_bootstrap_version_options! }.to_not raise_error
+ end
+ end
+ end
+ end
+
describe "when configuring the underlying knife ssh command" do
context "from the command line" do
let(:knife_ssh) do