Skip to content

Commit 7cf8f64

Browse files
author
Kevin Paulisse
committed
Add a warning if the computed hiera data directory is not found
1 parent 858b68b commit 7cf8f64

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ def install_hiera_config(logger, options)
208208
end
209209
rexp2 = Regexp.new('%{(::)?environment}')
210210
obj[key.to_sym][:datadir].sub!(rexp2, 'production')
211+
212+
# Make sure the dirctory exists. If not, log a warning. This is *probably* a setup error, but we don't
213+
# want it to be fatal in case (for example) someone is doing an octocatalog-diff to verify moving this
214+
# directory around or even setting up Hiera for the very first time.
215+
unless File.directory?(obj[key.to_sym][:datadir])
216+
message = "WARNING: Hiera datadir for #{key} doesn't seem to exist at #{obj[key.to_sym][:datadir]}"
217+
logger.warn message
218+
end
211219
end
212220

213221
# Write properly formatted hiera config file into temporary directory

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,14 @@
192192
hiera_config: OctocatalogDiff::Spec.fixture_path('repos/default/config/hiera.yaml'),
193193
hiera_path_strip: '/var/lib/puppet'
194194
)
195-
logger, _logger_str = OctocatalogDiff::Spec.setup_logger
195+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
196196
testobj = OctocatalogDiff::CatalogUtil::BuildDir.new(options, logger)
197197
hiera_yaml = File.join(testobj.tempdir, 'hiera.yaml')
198198
expect(File.file?(hiera_yaml)).to eq(true)
199199
hiera_cfg = YAML.load_file(hiera_yaml)
200200
expect(hiera_cfg[:backends]).to eq(['yaml'])
201201
expect(hiera_cfg[:yaml]).to eq(datadir: File.join(testobj.tempdir, 'environments', 'production', 'hieradata'))
202+
expect(logger_str.string).not_to match(/Hiera datadir for yaml doesn't seem to exist/)
202203
end
203204
end
204205

@@ -213,6 +214,23 @@
213214
end.to raise_error(ArgumentError, /Called install_hiera_config with a Symbol argument/)
214215
end
215216
end
217+
218+
context 'warning if directory not found' do
219+
it 'should warn about not finding the hiera data directory' do
220+
options = default_options.merge(
221+
hiera_config: OctocatalogDiff::Spec.fixture_path('repos/default/config/hiera.yaml'),
222+
hiera_path: 'aksdfjlkfjk'
223+
)
224+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
225+
testobj = OctocatalogDiff::CatalogUtil::BuildDir.new(options, logger)
226+
hiera_yaml = File.join(testobj.tempdir, 'hiera.yaml')
227+
expect(File.file?(hiera_yaml)).to eq(true)
228+
hiera_cfg = YAML.load_file(hiera_yaml)
229+
expect(hiera_cfg[:backends]).to eq(['yaml'])
230+
expect(hiera_cfg[:yaml]).to eq(datadir: File.join(testobj.tempdir, 'environments', 'production', 'aksdfjlkfjk'))
231+
expect(logger_str.string).to match(%r{WARNING: Hiera datadir for yaml.+/environments/production/aksdfjlkfjk})
232+
end
233+
end
216234
end
217235

218236
describe '#install_fact_file' do

0 commit comments

Comments
 (0)