Skip to content

Commit

Permalink
Support dot-notation when retrieving facts in facter_impl
Browse files Browse the repository at this point in the history
In Puppet 8, core providers are being confined using facts fetched using
'dot-notation'. We need to support this style of lookup in our stub
implementation.

For example in `lib/puppet/provider/service/init.rb`

```ruby
confine :true => begin
  os = Puppet.runtime[:facter].value(:operatingsystem).downcase
  # ...
```

was updated to

```ruby
confine :true => begin
  os = Puppet.runtime[:facter].value('os.name').downcase
  # ...
```

See
puppetlabs/puppet@82cef23 for Puppet 8 change.

Relates to puppetlabs#38
  • Loading branch information
alexjfisher committed Mar 15, 2023
1 parent 612d8af commit 10992f0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/rspec-puppet/facter_impl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ def initialize
end

def value(fact_name)
@facts[fact_name.to_s]
fact_name.to_s.split('.').reduce(@facts) do |sub_hash, key|
if sub_hash.is_a?(Hash) && sub_hash.key?(key)
sub_hash[key]
else
return nil
end
end
end

def clear
Expand Down
20 changes: 20 additions & 0 deletions spec/unit/facter_impl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'int_fact' => 3,
'true_fact' => true,
'false_fact' => false,
'os' => { 'name' => 'my_os', 'release' => { 'major' => '42' } }
}
end

Expand All @@ -19,6 +20,7 @@
facter_impl.add(:int_fact) { setcode { 3 } }
facter_impl.add(:true_fact) { setcode { true } }
facter_impl.add(:false_fact) { setcode { false } }
facter_impl.add(:os) { setcode { { 'name' => 'my_os', 'release' => { 'major' => '42' }} } }
end

describe 'noop methods' do
Expand Down Expand Up @@ -49,6 +51,24 @@
it 'retrieves a fact of type FalseClass' do
expect(facter_impl.value(:false_fact)).to eq(false)
end

context 'when using dot-notation' do
it 'retrieves a child fact using dot-notation' do
expect(facter_impl.value('os.name')).to eq('my_os')
end

it 'retrieves a hash child fact using dot-notation' do
expect(facter_impl.value('os.release')).to eq({ 'major' => '42' })
end

it 'retrieves a deeply nested child fact using dot-notation' do
expect(facter_impl.value('os.release.major')).to eq('42')
end

it 'returns nil if a child fact is missing' do
expect(facter_impl.value('os.release.unknown_subkey')).to eq(nil)
end
end
end

describe '#to_hash' do
Expand Down

0 comments on commit 10992f0

Please sign in to comment.