Skip to content

Commit 721921f

Browse files
author
Kevin Paulisse
committed
Make hiera_path actually do what it's supposed to
1 parent 983ab26 commit 721921f

2 files changed

Lines changed: 38 additions & 18 deletions

File tree

lib/octocatalog-diff/catalog-util/builddir.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class BuildDir
2727
# :enc [String] ENC script file (can be relative or absolute path)
2828
# :pe_enc_url [String] ENC URL (for Puppet Enterprise node classification service)
2929
# :hiera_config [String] hiera configuration file (relative to base directory)
30+
# :hiera_path [String] relative path to hiera data files (mutually exclusive with :hiera_path_strip)
3031
# :hiera_path_strip [String] string to strip off the beginning of :datadir
3132
# :puppetdb_ssl_ca [String] Path to SSL CA certificate
3233
# :puppetdb_ssl_client_key [String] String representation of SSL client key
@@ -52,9 +53,8 @@ def initialize(options = {}, logger = nil)
5253
install_puppetdb_conf(logger, options[:puppetdb_url], options[:puppetdb_server_url_timeout])
5354
install_routes_yaml(logger)
5455
end
55-
unless options[:hiera_config].nil?
56-
install_hiera_config(logger, options[:hiera_config], options[:hiera_path_strip])
57-
end
56+
install_hiera_config(logger, options) unless options[:hiera_config].nil?
57+
5858
@fact_file = install_fact_file(logger, options) if @facts_terminus == 'yaml'
5959
@enc = install_enc(logger) unless options[:enc].nil? && options[:pe_enc_url].nil?
6060
install_ssl(logger, options) if options[:puppetdb_ssl_ca] || options[:puppetdb_ssl_client_cert]
@@ -179,10 +179,10 @@ def install_enc(logger)
179179
end
180180

181181
# Install hiera config file
182-
# @param hiera_config [String] Path to file, relative to checkout
183-
# @param hiera_path_strip [String] Prefix to strip off when munging file
184-
def install_hiera_config(logger, hiera_config, hiera_path_strip)
182+
# @param options [Hash] Options hash
183+
def install_hiera_config(logger, options)
185184
# Validate hiera config file
185+
hiera_config = options[:hiera_config]
186186
unless hiera_config.is_a?(String)
187187
raise ArgumentError, "Called install_hiera_config with a #{hiera_config.class} argument"
188188
end
@@ -199,10 +199,12 @@ def install_hiera_config(logger, hiera_config, hiera_path_strip)
199199
obj = YAML.load_file(file_src)
200200
%w(yaml json).each do |key|
201201
next unless obj.key?(key.to_sym)
202-
next if obj[key.to_sym][:datadir].nil?
203-
unless hiera_path_strip.nil?
204-
rexp1 = Regexp.new('^' + hiera_path_strip)
202+
if options[:hiera_path_strip].is_a?(String)
203+
next if obj[key.to_sym][:datadir].nil?
204+
rexp1 = Regexp.new('^' + options[:hiera_path_strip])
205205
obj[key.to_sym][:datadir].sub!(rexp1, @tempdir)
206+
elsif options[:hiera_path]
207+
obj[key.to_sym][:datadir] = File.join(@tempdir, 'environments', 'production', options[:hiera_path])
206208
end
207209
rexp2 = Regexp.new('%{(::)?environment}')
208210
obj[key.to_sym][:datadir].sub!(rexp2, 'production')

spec/octocatalog-diff/tests/catalog-util/builddir_spec.rb

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,35 @@
170170
end
171171
end
172172

173-
context 'with invalid options' do
174-
it 'should raise argument error if called with non-string argument' do
175-
options = {
176-
basedir: OctocatalogDiff::Spec.fixture_path('repos/default'),
177-
fact_file: OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml'),
178-
node: 'rspec-node.github.net'
179-
}
173+
context 'with hiera_path specified' do
174+
it 'should install the hiera configuration file' do
175+
options = default_options.merge(
176+
hiera_config: OctocatalogDiff::Spec.fixture_path('repos/default/config/hiera.yaml'),
177+
hiera_path: 'hieradata'
178+
)
180179
logger, _logger_str = OctocatalogDiff::Spec.setup_logger
181-
r = Regexp.new('Called install_hiera_config with a Symbol argument')
182180
testobj = OctocatalogDiff::CatalogUtil::BuildDir.new(options, logger)
183-
expect { testobj.send(:install_hiera_config, logger, :chicken, nil) }.to raise_error(ArgumentError, r)
181+
hiera_yaml = File.join(testobj.tempdir, 'hiera.yaml')
182+
expect(File.file?(hiera_yaml)).to eq(true)
183+
hiera_cfg = YAML.load_file(hiera_yaml)
184+
expect(hiera_cfg[:backends]).to eq(['yaml'])
185+
expect(hiera_cfg[:yaml]).to eq(datadir: File.join(testobj.tempdir, 'environments', 'production', 'hieradata'))
186+
end
187+
end
188+
189+
context 'with hiera_path_strip specified' do
190+
it 'should install the hiera configuration file' do
191+
options = default_options.merge(
192+
hiera_config: OctocatalogDiff::Spec.fixture_path('repos/default/config/hiera.yaml'),
193+
hiera_path_strip: '/var/lib/puppet'
194+
)
195+
logger, _logger_str = OctocatalogDiff::Spec.setup_logger
196+
testobj = OctocatalogDiff::CatalogUtil::BuildDir.new(options, logger)
197+
hiera_yaml = File.join(testobj.tempdir, 'hiera.yaml')
198+
expect(File.file?(hiera_yaml)).to eq(true)
199+
hiera_cfg = YAML.load_file(hiera_yaml)
200+
expect(hiera_cfg[:backends]).to eq(['yaml'])
201+
expect(hiera_cfg[:yaml]).to eq(datadir: File.join(testobj.tempdir, 'environments', 'production', 'hieradata'))
184202
end
185203
end
186204
end

0 commit comments

Comments
 (0)