Top Level Namespace

Includes:
RspecPuppetFacts

Instance Method Summary collapse

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', 'puppet/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_factsObject



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
98
# 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
    os = RSpec.configuration.facterdb_string_keys ? facts['os'] : facts[:os]
    case os['family'].downcase
    when 'archlinux'
      'systemd'
    when 'darwin'
      'launchd'
    when 'debian'
      'systemd'
    when 'freebsd'
      'freebsd'
    when 'gentoo'
      'openrc'
    when 'openbsd'
      'openbsd'
    when 'redhat'
      os['release']['major'].to_i >= 7 ? 'systemd' : 'redhat'
    when 'suse'
      os['release']['major'].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

#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