Skip to content

Commit 679ad68

Browse files
committed
Add regexp support to builddir
1 parent 9fdf3ca commit 679ad68

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,12 @@ def install_fact_file(logger, options)
163163

164164
if options[:fact_override].is_a?(Array)
165165
options[:fact_override].each do |override|
166-
old_value = facts.fact(override.key)
167-
facts.override(override.key, override.value)
168-
logger.debug("Override #{override.key} from #{old_value.inspect} to #{override.value.inspect}")
166+
keys = override.key.is_a?(Regexp) ? facts.matching(override.key) : [override.key]
167+
keys.each do |key|
168+
old_value = facts.fact(key)
169+
facts.override(key, override.value)
170+
logger.debug("Override #{key} from #{old_value.inspect} to #{override.value.inspect}")
171+
end
169172
end
170173
end
171174

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,35 @@
616616
expect(factobj['values']['fizz']).to eq('buzz')
617617
expect(factobj['values']['jsontest']).to eq('foo' => 'bar')
618618
end
619+
620+
it 'should handle regular expressions in fact overrides' do
621+
overrides_raw = %w(/dd+/=10.30.50.70 fizz=buzz jsontest=(json){"foo":"bar"} /asdflk/=true)
622+
overrides = overrides_raw.map { |x| OctocatalogDiff::API::V1::Override.create_from_input(x) }
623+
options = {
624+
basedir: OctocatalogDiff::Spec.fixture_path('repos/default'),
625+
fact_file: OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml'),
626+
fact_override: overrides,
627+
facts_terminus: 'yaml',
628+
node: 'rspec-node.github.net'
629+
}
630+
logger, _logger_str = OctocatalogDiff::Spec.setup_logger
631+
testobj = OctocatalogDiff::CatalogUtil::BuildDir.new(options, logger)
632+
fact_file = File.join(testobj.tempdir, 'var/yaml/facts/rspec-node.github.net.yaml')
633+
expect(File.file?(fact_file)).to eq(true)
634+
635+
yaml_content = File.read(fact_file).split(/\n/)
636+
expect(yaml_content[0]).to eq('--- !ruby/object:Puppet::Node::Facts')
637+
638+
yaml_content[0] = '---' # To avoid need for puppet gem
639+
factobj = YAML.load(yaml_content.join("\n"))
640+
expect(factobj).to be_a_kind_of(Hash)
641+
expect(factobj['name']).to eq('rspec-node.github.net')
642+
expect(factobj['values']).to be_a_kind_of(Hash)
643+
expect(factobj['values']['clientcert']).to eq('rspec-node.github.net')
644+
expect(factobj['values']['ipaddress']).to eq('10.30.50.70')
645+
expect(factobj['values']['fizz']).to eq('buzz')
646+
expect(factobj['values']['jsontest']).to eq('foo' => 'bar')
647+
end
619648
end
620649

621650
context 'with invalid options' do

0 commit comments

Comments
 (0)