Top Level Namespace

Includes:
RspecPuppetFacts

Instance Method Summary collapse

Instance Method Details

#add_facts_for_metadata(metadata) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/voxpupuli/test/facts.rb', line 50

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

  ['dependencies'].each do |dependency|
    case normalize_module_name(dependency['name'])
    when 'camptocamp/systemd', 'puppet/systemd'
      if RSpec.configuration.facterdb_string_keys
        add_custom_fact 'systemd', ->(_os, facts) { facts['service_provider'] == 'systemd' }
      else
        add_custom_fact :systemd, ->(_os, facts) { facts[:service_provider] == 'systemd' }
      end
    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.



46
47
48
# File 'lib/voxpupuli/test/facts.rb', line 46

def add_mocked_facts!
  (RspecPuppetFacts.)
end

#add_stdlib_factsObject



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
99
100
101
102
103
104
105
106
107
108
# File 'lib/voxpupuli/test/facts.rb', line 73

def add_stdlib_facts
  if RSpec.configuration.facterdb_string_keys
    add_custom_fact 'puppet_environmentpath', '/etc/puppetlabs/code/environments'
    add_custom_fact 'puppet_vardir', '/opt/puppetlabs/puppet/cache'
    add_custom_fact 'root_home', '/root'
  else
    add_custom_fact :puppet_environmentpath, '/etc/puppetlabs/code/environments'
    add_custom_fact :puppet_vardir, '/opt/puppetlabs/puppet/cache'
    add_custom_fact :root_home, '/root'
  end

  # Rough conversion of grepping in the puppet source:
  # grep defaultfor lib/puppet/provider/service/*.rb
  service_provider = RSpec.configuration.facterdb_string_keys ? 'service_provider' : :service_provider
  add_custom_fact service_provider, lambda { |_os, facts|
    os = RSpec.configuration.facterdb_string_keys ? facts['os'] : facts[:os]
    case os['family'].downcase
    when 'archlinux', 'debian', 'redhat'
      'systemd'
    when 'darwin'
      'launchd'
    when 'freebsd'
      'freebsd'
    when 'gentoo'
      'openrc'
    when 'openbsd'
      'openbsd'
    when 'suse'
      (os['release']['major'].to_i >= 12) ? 'systemd' : 'redhat'
    when 'windows'
      'windows'
    else
      'init'
    end
  }
end

#apply_overrides!(facts, overrides, enforce_strings) ⇒ Object

A private helper to override_facts



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/voxpupuli/test/facts.rb', line 27

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



67
68
69
70
71
# File 'lib/voxpupuli/test/facts.rb', line 67

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



20
21
22
23
24
# File 'lib/voxpupuli/test/facts.rb', line 20

def override_facts(base_facts, **overrides)
  facts = Marshal.load(Marshal.dump(base_facts))
  apply_overrides!(facts, overrides, false)
  facts
end