From ad06b3700f0aa004e6d5b7752da61810aa043aae Mon Sep 17 00:00:00 2001 From: Serdar Sutay Date: Thu, 6 Nov 2014 11:53:37 -0800 Subject: Make sure that :distro is evaluated later than :bootstrap_template and :template_file in order for knife cloud plugins to work corectly with Chef 12. --- lib/chef/config.rb | 2 +- lib/chef/knife/bootstrap.rb | 15 ++++++++------- spec/unit/knife/bootstrap_spec.rb | 28 +++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/chef/config.rb b/lib/chef/config.rb index dc04026acb..2dca41791f 100644 --- a/lib/chef/config.rb +++ b/lib/chef/config.rb @@ -491,7 +491,7 @@ class Chef default :ssh_gateway, nil default :bootstrap_version, nil default :bootstrap_proxy, nil - default :bootstrap_template, "chef-full" + default :bootstrap_template, nil default :secret, nil default :secret_file, nil default :identity_file, nil diff --git a/lib/chef/knife/bootstrap.rb b/lib/chef/knife/bootstrap.rb index f159c9105b..19a329199d 100644 --- a/lib/chef/knife/bootstrap.rb +++ b/lib/chef/knife/bootstrap.rb @@ -194,13 +194,15 @@ class Chef :description => "Verify the SSL cert for HTTPS requests to the Chef server API.", :boolean => true + def default_bootstrap_template + "chef-full" + end + def bootstrap_template - # For some reason knife.merge_configs doesn't pick up the default values from - # Chef::Config[:knife][:bootstrap_template] unless Chef::Config[:knife][:bootstrap_template] - # is forced to pick up the values before calling merge_configs. - # We therefore have Chef::Config[:knife][:bootstrap_template] to pick up the defaults - # if no option is specified. - config[:bootstrap_template] || config[:distro] || config[:template_file] || Chef::Config[:knife][:bootstrap_template] + # The order here is important. We want to check if we have the new Chef 12 option is set first. + # Knife cloud plugins unfortunately all set a default option for the :distro so it should be at + # the end. + config[:bootstrap_template] || config[:template_file] || config[:distro] || default_bootstrap_template end def find_template @@ -210,7 +212,6 @@ class Chef if File.exists?(template) Chef::Log.debug("Using the specified bootstrap template: #{File.dirname(template)}") return template - end # Otherwise search the template directories until we find the right one diff --git a/spec/unit/knife/bootstrap_spec.rb b/spec/unit/knife/bootstrap_spec.rb index 4de2b4531d..01752875fc 100644 --- a/spec/unit/knife/bootstrap_spec.rb +++ b/spec/unit/knife/bootstrap_spec.rb @@ -29,7 +29,7 @@ describe Chef::Knife::Bootstrap do Chef::Log.logger = Logger.new(StringIO.new) Chef::Config[:knife][:bootstrap_template] = bootstrap_template unless bootstrap_template.nil? - k = Chef::Knife::Bootstrap.new + k = Chef::Knife::Bootstrap.new(bootstrap_cli_options) k.merge_configs allow(k.ui).to receive(:stderr).and_return(stderr) @@ -41,11 +41,37 @@ describe Chef::Knife::Bootstrap do let(:bootstrap_template) { nil } + let(:bootstrap_cli_options) { [ ] } + it "should use chef-full as default template" do expect(knife.bootstrap_template).to be_a_kind_of(String) expect(File.basename(knife.bootstrap_template)).to eq("chef-full") end + context "with :distro and :bootstrap_template cli options" do + let(:bootstrap_cli_options) { [ "--bootstrap-template", "my-template", "--distro", "other-template" ] } + + it "should select bootstrap template" do + File.basename(knife.bootstrap_template).should eq("my-template") + end + end + + context "with :distro and :template_file cli options" do + let(:bootstrap_cli_options) { [ "--distro", "my-template", "--template-file", "other-template" ] } + + it "should select bootstrap template" do + File.basename(knife.bootstrap_template).should eq("other-template") + end + end + + context "with :bootstrap_template and :template_file cli options" do + let(:bootstrap_cli_options) { [ "--bootstrap-template", "my-template", "--template-file", "other-template" ] } + + it "should select bootstrap template" do + File.basename(knife.bootstrap_template).should eq("my-template") + end + end + context "when finding templates" do context "when :bootstrap_template config is set to a file" do context "that doesn't exist" do -- cgit v1.2.1 From da9aae004db44718ac8eb5027f74d36f7917ebf2 Mon Sep 17 00:00:00 2001 From: Serdar Sutay Date: Fri, 7 Nov 2014 16:12:33 -0800 Subject: Rspec 3 compat. --- spec/unit/knife/bootstrap_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/unit/knife/bootstrap_spec.rb b/spec/unit/knife/bootstrap_spec.rb index 01752875fc..27731ea9f6 100644 --- a/spec/unit/knife/bootstrap_spec.rb +++ b/spec/unit/knife/bootstrap_spec.rb @@ -52,7 +52,7 @@ describe Chef::Knife::Bootstrap do let(:bootstrap_cli_options) { [ "--bootstrap-template", "my-template", "--distro", "other-template" ] } it "should select bootstrap template" do - File.basename(knife.bootstrap_template).should eq("my-template") + expect(File.basename(knife.bootstrap_template)).to eq("my-template") end end @@ -60,7 +60,7 @@ describe Chef::Knife::Bootstrap do let(:bootstrap_cli_options) { [ "--distro", "my-template", "--template-file", "other-template" ] } it "should select bootstrap template" do - File.basename(knife.bootstrap_template).should eq("other-template") + expect(File.basename(knife.bootstrap_template)).to eq("other-template") end end @@ -68,7 +68,7 @@ describe Chef::Knife::Bootstrap do let(:bootstrap_cli_options) { [ "--bootstrap-template", "my-template", "--template-file", "other-template" ] } it "should select bootstrap template" do - File.basename(knife.bootstrap_template).should eq("my-template") + expect(File.basename(knife.bootstrap_template)).to eq("my-template") end end -- cgit v1.2.1