Skip to content

Commit

Permalink
Add mocked custom facts based on metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ekohl committed Dec 14, 2020
1 parent 429ea7b commit 2081fa4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
63 changes: 63 additions & 0 deletions lib/voxpupuli/test/facts.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'rspec-puppet-facts'
include RspecPuppetFacts

# Override facts
#
# This doesn't use deep_merge because that's highly unpredictable. It can merge
Expand Down Expand Up @@ -32,3 +35,63 @@ def apply_overrides!(facts, overrides, enforce_strings)
end
end
end

# Add mocked facts based on the metadata present in the module
#
# This means that for some module there are hardcoded mocks, such as stdlib.
# When stdlib is present in metadata.json, facts like service_provider are
# mocked and return the correct value according to the OS facts.
def add_mocked_facts!
add_facts_for_metadata(RspecPuppetFacts.metadata)
end

def add_facts_for_metadata(metadata)
return unless metadata && metadata['dependencies']

metadata['dependencies'].each do |dependency|
case normalize_module_name(dependency['name'])
when 'camptocamp/systemd'
add_custom_fact :systemd, ->(os, facts) { facts[:service_provider] == 'systemd' }
when 'puppetlabs/stdlib'
add_stdlib_facts
end
end
end

def normalize_module_name(name)
return unless name
name.sub('-', '/')
end

def add_stdlib_facts
add_custom_fact :puppet_environmentpath, '/etc/puppetlabs/code/environments'
add_custom_fact :puppet_vardir, '/opt/puppetlabs/puppet/cache'
add_custom_fact :root_home, '/root'

# Rough conversion of grepping in the puppet source:
# grep defaultfor lib/puppet/provider/service/*.rb
add_custom_fact :service_provider, ->(os, facts) do
case facts[:osfamily].downcase
when 'archlinux'
'systemd'
when 'darwin'
'launchd'
when 'debian'
'systemd'
when 'freebsd'
'freebsd'
when 'gentoo'
'openrc'
when 'openbsd'
'openbsd'
when 'redhat'
facts[:operatingsystemrelease].to_i >= 7 ? 'systemd' : 'redhat'
when 'suse'
facts[:operatingsystemmajrelease].to_i >= 12 ? 'systemd' : 'redhat'
when 'windows'
'windows'
else
'init'
end
end
end
2 changes: 0 additions & 2 deletions lib/voxpupuli/test/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ def suggest_facter_version

require 'voxpupuli/test/facts'
require 'puppetlabs_spec_helper/module_spec_helper'
require 'rspec-puppet-facts'
include RspecPuppetFacts

# Generating facts is slow - this memoizes the facts between multiple classes.
# Marshalling is used to get unique instances which helps when tests overrides
Expand Down

0 comments on commit 2081fa4

Please sign in to comment.