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 #38
  • Loading branch information
alexjfisher committed Mar 15, 2023
1 parent 612d8af commit 074363e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rspec-puppet/facter_impl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ def initialize
end

def value(fact_name)
@facts[fact_name.to_s]
begin
@facts.dig(*fact_name.to_s.split('.'))
rescue TypeError
nil
end
end

def clear
Expand Down
24 changes: 24 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,28 @@
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

it 'returns nil if trying to lookup into a string' do
expect(facter_impl.value('os.name.foo')).to eq(nil)
end
end
end

describe '#to_hash' do
Expand Down

0 comments on commit 074363e

Please sign in to comment.