diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2014-12-03 11:29:38 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2014-12-03 11:35:43 -0800 |
commit | 5002cbbee1135195d8dcb04f5509ab98cfb9169a (patch) | |
tree | 6ee0ea53cc51d7fb1ce11130f92a90bb98a28646 | |
parent | a00ed4b198ea25e14d6b7fe861b401a32352e9fa (diff) | |
download | chef-5002cbbee1135195d8dcb04f5509ab98cfb9169a.tar.gz |
Fix knife plugin path searching
Unreleased Chef 12.0.0 bug caught in testing where only the searching
upwards from PWD to find .chef in order to get the plugin path for knife
was busted. The chef_config_dir was being used before load_config was
being called, so it was nil, which broke finding knife plugins. This
was fixed by adding some lazy initialization of the config_loader object
itself and the chef_config_dir.
The reset_config_loader! is added entirely to reset the global state for unit
testing. This whole class is a bit horrible and needs to have all its
global state removed from it, class methods removed, class ivars and the
class-variable-in-a-class-method-for-inheritance-lolwut needs to be removed.
Unfortunately, that requires some delicate surgery because Chef::Knife
gets used as a public API, and is beyond the scope of gettting Chef 12
shipped.
-rw-r--r-- | lib/chef/knife.rb | 14 | ||||
-rw-r--r-- | lib/chef/workstation_config_loader.rb | 2 | ||||
-rw-r--r-- | spec/unit/application/knife_spec.rb | 8 | ||||
-rw-r--r-- | spec/unit/knife_spec.rb | 5 |
4 files changed, 23 insertions, 6 deletions
diff --git a/lib/chef/knife.rb b/lib/chef/knife.rb index c000ca4d0a..3f234d7ce3 100644 --- a/lib/chef/knife.rb +++ b/lib/chef/knife.rb @@ -72,6 +72,11 @@ class Chef ui.msg(msg) end + def self.reset_config_loader! + @@chef_config_dir = nil + @config_loader = nil + end + def self.reset_subcommands! @@subcommands = {} @subcommands_by_category = nil @@ -162,12 +167,15 @@ class Chef # Shared with subclasses @@chef_config_dir = nil + def self.config_loader + @config_loader ||= WorkstationConfigLoader.new(nil, Chef::Log) + end + def self.load_config(explicit_config_file) - config_loader = WorkstationConfigLoader.new(explicit_config_file, Chef::Log) + config_loader.explicit_config_file = explicit_config_file config_loader.load ui.warn("No knife configuration file found") if config_loader.no_config_found? - @@chef_config_dir = config_loader.chef_config_dir config_loader rescue Exceptions::ConfigurationError => e @@ -176,7 +184,7 @@ class Chef end def self.chef_config_dir - @@chef_config_dir + @@chef_config_dir ||= config_loader.chef_config_dir end # Run knife for the given +args+ (ARGV), adding +options+ to the list of diff --git a/lib/chef/workstation_config_loader.rb b/lib/chef/workstation_config_loader.rb index 6715d4eec2..dd02ad9a66 100644 --- a/lib/chef/workstation_config_loader.rb +++ b/lib/chef/workstation_config_loader.rb @@ -25,7 +25,7 @@ class Chef class WorkstationConfigLoader # Path to a config file requested by user, (e.g., via command line option). Can be nil - attr_reader :explicit_config_file + attr_accessor :explicit_config_file # TODO: initialize this with a logger for Chef and Knife def initialize(explicit_config_file, logger=nil) diff --git a/spec/unit/application/knife_spec.rb b/spec/unit/application/knife_spec.rb index 806a596a61..3c215eac7f 100644 --- a/spec/unit/application/knife_spec.rb +++ b/spec/unit/application/knife_spec.rb @@ -33,6 +33,11 @@ describe Chef::Application::Knife do end end + after(:each) do + # reset some really nasty global state + NoopKnifeCommand.reset_config_loader! + end + before(:each) do # Prevent code from getting loaded on every test invocation. allow(Chef::Knife).to receive(:load_commands) @@ -109,7 +114,6 @@ describe Chef::Application::Knife do end expect(Chef::Config[:client_key]).to eq(full_path) end - end describe "with environment configuration" do @@ -168,6 +172,6 @@ describe Chef::Application::Knife do @knife.run end end - end + end diff --git a/spec/unit/knife_spec.rb b/spec/unit/knife_spec.rb index 49fa4a85ca..8f652e58c7 100644 --- a/spec/unit/knife_spec.rb +++ b/spec/unit/knife_spec.rb @@ -44,6 +44,10 @@ describe Chef::Knife do @stderr = StringIO.new end + after(:each) do + Chef::Knife.reset_config_loader! + end + describe "after loading a subcommand" do before do Chef::Knife.reset_subcommands! @@ -268,6 +272,7 @@ describe Chef::Knife do @knife.config[:verbosity] = 1 @knife.config[:config_file] = fake_config config_loader = double("Chef::WorkstationConfigLoader", :load => true, :no_config_found? => false, :chef_config_dir => "/etc/chef", :config_location => fake_config) + allow(config_loader).to receive(:explicit_config_file=).with(fake_config).and_return(fake_config) allow(Chef::WorkstationConfigLoader).to receive(:new).and_return(config_loader) end |