diff options
author | Jay Mundrawala <jdmundrawala@gmail.com> | 2016-02-08 12:07:33 -0800 |
---|---|---|
committer | Jay Mundrawala <jdmundrawala@gmail.com> | 2016-02-24 21:24:14 -0800 |
commit | 352c98ee1f50cf3d18f341e48b5fe3214e5afda5 (patch) | |
tree | 6542ad560f1f2f1006c5c4838d6bd897ab21efbc /chef-config | |
parent | 9cfe82b9af36c92457598f857cba5671a1e24ef2 (diff) | |
download | chef-352c98ee1f50cf3d18f341e48b5fe3214e5afda5.tar.gz |
Allow the workstation config to load a conf.d directory
This will behave similarly to the client.d directory.
The top-level ruby files will be loaded in sorted order.
Diffstat (limited to 'chef-config')
-rw-r--r-- | chef-config/lib/chef-config/config.rb | 6 | ||||
-rw-r--r-- | chef-config/lib/chef-config/workstation_config_loader.rb | 35 | ||||
-rw-r--r-- | chef-config/spec/unit/workstation_config_loader_spec.rb | 81 |
3 files changed, 113 insertions, 9 deletions
diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb index b162cacd89..2187787c5e 100644 --- a/chef-config/lib/chef-config/config.rb +++ b/chef-config/lib/chef-config/config.rb @@ -490,9 +490,13 @@ module ChefConfig # HTTP file servers. default(:trusted_certs_dir) { PathHelper.join(config_dir, "trusted_certs") } - # A directory that contains additional configuration scripts to load + # A directory that contains additional configuration scripts to load for chef-client default(:client_d_dir) { PathHelper.join(config_dir, "client.d") } + # A directory that contains additional configuration scripts to load for + # the workstation config + default(:conf_d_dir) { PathHelper.join(config_dir, "conf.d") } + # Where should chef-solo download recipes from? default :recipe_url, nil diff --git a/chef-config/lib/chef-config/workstation_config_loader.rb b/chef-config/lib/chef-config/workstation_config_loader.rb index 34ba6d6537..aabfe7235b 100644 --- a/chef-config/lib/chef-config/workstation_config_loader.rb +++ b/chef-config/lib/chef-config/workstation_config_loader.rb @@ -62,15 +62,17 @@ module ChefConfig def load # Ignore it if there's no explicit_config_file and can't find one at a # default path. - return false if config_location.nil? + if !config_location.nil? + if explicit_config_file && !path_exists?(config_location) + raise ChefConfig::ConfigurationError, "Specified config file #{config_location} does not exist" + end - if explicit_config_file && !path_exists?(config_location) - raise ChefConfig::ConfigurationError, "Specified config file #{config_location} does not exist" + # Have to set Config.config_file b/c other config is derived from it. + Config.config_file = config_location + read_config(IO.read(config_location), config_location) end - # Have to set Config.config_file b/c other config is derived from it. - Config.config_file = config_location - read_config(IO.read(config_location), config_location) + load_conf_d_directory end # (Private API, public for test purposes) @@ -124,6 +126,27 @@ module ChefConfig end end + def load_conf_d_directory + conf_d_files.sort.map do |conf| + read_config(IO.read(conf), conf) + end + end + + def conf_d_files + @conf_d_files ||= + begin + entries = if Config[:conf_d_dir] + Dir.glob(File.join(PathHelper.escape_glob( + Config[:conf_d_dir]), "*.rb")) + else + [] + end + entries.select do |entry| + File.file?(entry) + end + end + end + def working_directory a = if ChefConfig.windows? env["CD"] diff --git a/chef-config/spec/unit/workstation_config_loader_spec.rb b/chef-config/spec/unit/workstation_config_loader_spec.rb index df53f87de9..cae9ce4304 100644 --- a/chef-config/spec/unit/workstation_config_loader_spec.rb +++ b/chef-config/spec/unit/workstation_config_loader_spec.rb @@ -35,6 +35,12 @@ RSpec.describe ChefConfig::WorkstationConfigLoader do end end + before do + # We set this to nil so that a dev workstation will + # not interfere with the tests. + ChefConfig::Config[:conf_d_dir] = nil + end + # Test methods that do I/O or reference external state which are stubbed out # elsewhere. describe "external dependencies" do @@ -215,7 +221,8 @@ RSpec.describe ChefConfig::WorkstationConfigLoader do it "skips loading" do expect(config_loader.config_location).to be(nil) - expect(config_loader.load).to be(false) + expect(config_loader).not_to receive(:read_config) + config_loader.load end end @@ -254,7 +261,8 @@ RSpec.describe ChefConfig::WorkstationConfigLoader do let(:config_content) { "config_file_evaluated(true)" } it "loads the config" do - expect(config_loader.load).to be(true) + expect(config_loader).to receive(:read_config).and_call_original + config_loader.load expect(ChefConfig::Config.config_file_evaluated).to be(true) end @@ -286,4 +294,73 @@ RSpec.describe ChefConfig::WorkstationConfigLoader do end + describe "when loading config.d" do + context "when the conf.d directory exists" do + let(:config_content) { "" } + + let(:tempdir) { Dir.mktmpdir("chef-workstation-test") } + + let!(:confd_file) do + Tempfile.new(["Chef-WorkstationConfigLoader-rspec-test", ".rb"], tempdir).tap do |t| + t.print(config_content) + t.close + end + end + + before do + ChefConfig::Config[:conf_d_dir] = tempdir + allow(config_loader).to receive(:path_exists?).with( + an_instance_of(String)).and_return(false) + end + + after do + FileUtils.remove_entry_secure tempdir + end + + context "and is valid" do + let(:config_content) { "config_d_file_evaluated(true)" } + + it "loads the config" do + expect(config_loader).to receive(:read_config).and_call_original + config_loader.load + expect(ChefConfig::Config.config_d_file_evaluated).to be(true) + end + end + + context "and has a syntax error" do + let(:config_content) { "{{{{{:{{" } + + it "raises a ConfigurationError" do + expect { config_loader.load }.to raise_error(ChefConfig::ConfigurationError) + end + end + + context "has a non rb file" do + let(:sytax_error_content) { "{{{{{:{{" } + let(:config_content) { "config_d_file_evaluated(true)" } + + let!(:not_confd_file) do + Tempfile.new(["Chef-WorkstationConfigLoader-rspec-test", ".foorb"], tempdir).tap do |t| + t.print(sytax_error_content) + t.close + end + end + + it "does not load the non rb file" do + expect { config_loader.load }.not_to raise_error + expect(ChefConfig::Config.config_d_file_evaluated).to be(true) + end + end + end + + context "when the conf.d directory does not exist" do + before do + ChefConfig::Config[:conf_d_dir] = "/nope/nope/nope/nope/notdoingit" + end + + it "does not load anything" do + expect(config_loader).not_to receive(:read_config) + end + end + end end |