Top Level Namespace
- Includes:
- RspecPuppetFacts
Constant Summary collapse
- FACTS_CACHE =
Generating facts is slow - this memoizes the facts between multiple classes. Marshalling is used to get unique instances which helps when tests overrides facts.
{}
Instance Method Summary collapse
- #add_facts_for_metadata(metadata) ⇒ Object
-
#add_mocked_facts! ⇒ Object
Add mocked facts based on the metadata present in the module.
- #add_stdlib_facts ⇒ Object
-
#apply_overrides!(facts, overrides, enforce_strings) ⇒ Object
A private helper to override_facts.
- #normalize_module_name(name) ⇒ Object
- #on_supported_os(opts = {}) ⇒ Object
-
#override_facts(base_facts, **overrides) ⇒ Object
Override facts.
-
#suggest_facter_version ⇒ Object
getting the correct facter version is tricky.
Instance Method Details
#add_facts_for_metadata(metadata) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/voxpupuli/test/facts.rb', line 48 def () return unless && ['dependencies'] ['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 |
#add_mocked_facts! ⇒ Object
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.
44 45 46 |
# File 'lib/voxpupuli/test/facts.rb', line 44 def add_mocked_facts! (RspecPuppetFacts.) end |
#add_stdlib_facts ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/voxpupuli/test/facts.rb', line 66 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 |
#apply_overrides!(facts, overrides, enforce_strings) ⇒ Object
A private helper to override_facts
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/voxpupuli/test/facts.rb', line 25 def apply_overrides!(facts, overrides, enforce_strings) overrides.each do |key, value| # Nested facts are strings key = key.to_s if enforce_strings if value.is_a?(Hash) facts[key] = {} unless facts.key?(key) apply_overrides!(facts[key], value, true) else facts[key] = value end end end |
#normalize_module_name(name) ⇒ Object
61 62 63 64 |
# File 'lib/voxpupuli/test/facts.rb', line 61 def normalize_module_name(name) return unless name name.sub('-', '/') end |
#on_supported_os(opts = {}) ⇒ Object
34 35 36 37 |
# File 'lib/voxpupuli/test/spec_helper.rb', line 34 def on_supported_os(opts = {}) result = FACTS_CACHE[opts.to_s] ||= super(opts) Marshal.load(Marshal.dump(result)) end |
#override_facts(base_facts, **overrides) ⇒ Object
Override facts
This doesn’t use deep_merge because that’s highly unpredictable. It can merge nested hashes in place, modifying the original. It’s also unable to override true to false.
A deep copy is obtained by using Marshal so it can be modified in place. Then it recursively overrides values. If the result is a hash, it’s recursed into.
A typical example:
let(:facts) do
override_facts(super(), os: {'selinux' => {'enabled' => false}})
end
18 19 20 21 22 |
# File 'lib/voxpupuli/test/facts.rb', line 18 def override_facts(base_facts, **overrides) facts = Marshal.load(Marshal.dump(base_facts)) apply_overrides!(facts, overrides, false) facts end |
#suggest_facter_version ⇒ Object
getting the correct facter version is tricky. We use facterdb as a source to mock facts see github.com/camptocamp/facterdb people might provide a specific facter version. In that case we use it. Otherwise we need to match the correct facter version to the used puppet version. as of 2019-10-31, puppet 5 ships facter 3.11 and puppet 6 ships facter 3.14 puppet.com/docs/puppet/5.5/about_agent.html
The environment variable ‘PUPPET_VERSION` is available in our travis environment, but we cannot rely on it if somebody runs the tests locally. For that case we should fallback the the puppet gem version.
10 11 12 13 14 15 16 |
# File 'lib/voxpupuli/test/spec_helper.rb', line 10 def suggest_facter_version return ENV['FACTERDB_FACTS_VERSION'] if ENV['FACTERDB_FACTS_VERSION'] require 'bundler' puppet_version = ENV['PUPPET_VERSION'] ? ENV['PUPPET_VERSION'] : Gem.loaded_specs['puppet'].version.to_s Gem::Dependency.new('', puppet_version).match?('', '5') ? '3.11.0' : '3.14.0' end |