Module: RspecPuppetFacts

Defined in:
lib/rspec-puppet-facts.rb,
lib/rspec-puppet-facts/version.rb

Overview

The purpose of this module is to simplify the Puppet module’s RSpec tests by looping through all supported OS’es and their facts data which is received from the FacterDB.

Defined Under Namespace

Modules: Version

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.common_factsHash <Symbol => String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

These facts are common for all OS’es and will be added to the facts retrieved from the FacterDB

Returns:

  • (Hash <Symbol => String>)


158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rspec-puppet-facts.rb', line 158

def self.common_facts
  return @common_facts if @common_facts
  @common_facts = {
      :mco_version   => MCollective::VERSION,
      :puppetversion => Puppet.version,
      :rubysitedir   => RbConfig::CONFIG['sitelibdir'],
      :rubyversion   => RUBY_VERSION,
  }
  if Puppet.features.augeas?
    @common_facts[:augeasversion] = Augeas.open(nil, nil, Augeas::NO_MODL_AUTOLOAD).get('/augeas/version')
  end
  @common_facts
end

.meta_supported_osArray<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the “operatingsystem_support” structure from the parsed metadata.json file in the metadata

Returns:

  • (Array<Hash>)

Raises:

  • (StandardError)

    if there is no “operatingsystem_support”



178
179
180
181
182
183
# File 'lib/rspec-puppet-facts.rb', line 178

def self.meta_supported_os
  unless ['operatingsystem_support'].is_a? Array
    fail StandardError, 'Unknown operatingsystem support in the metadata file!'
  end
  ['operatingsystem_support']
end

.metadataHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Read the metadata file and parse its JSON content.

Returns:

  • (Hash)

Raises:

  • (StandardError)

    if the metadata file is missing



190
191
192
193
194
195
196
197
# File 'lib/rspec-puppet-facts.rb', line 190

def self.
  return @metadata if @metadata
  unless File.file? 
    fail StandardError, "Can't find metadata.json... dunno why"
  end
  content = File.read 
  @metadata = JSON.parse content
end

.metadata_fileString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This file contains the Puppet module’s metadata

Returns:

  • (String)


202
203
204
# File 'lib/rspec-puppet-facts.rb', line 202

def self.
  'metadata.json'
end

.register_custom_fact(name, value, options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds a custom fact to the @custom_facts variable.

Parameters:

  • name (String)

    Fact name

  • value (String, Proc)

    Fact value. If proc, takes 2 params: os and facts hash

  • opts (Hash)


120
121
122
123
# File 'lib/rspec-puppet-facts.rb', line 120

def self.register_custom_fact(name, value, options)
  @custom_facts ||= {}
  @custom_facts[name.to_s] = {:options => options, :value => value}
end

.resetObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reset the memoization to make the saved structures be generated again



217
218
219
220
221
# File 'lib/rspec-puppet-facts.rb', line 217

def self.reset
  @custom_facts = nil
  @common_facts = nil
  @metadata = nil
end

.spec_facts_os_filternil, String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

If provided this filter can be used to limit the set of retrieved facts only to the matched OS names. The value is being taken from the SPEC_FACTS_OS environment variable and

Returns:

  • (nil, String)


150
151
152
# File 'lib/rspec-puppet-facts.rb', line 150

def self.spec_facts_os_filter
  ENV['SPEC_FACTS_OS']
end

.warning(message) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Print a warning message to the console

Parameters:

  • message (String)


209
210
211
# File 'lib/rspec-puppet-facts.rb', line 209

def self.warning(message)
  STDERR.puts message
end

.with_custom_facts(os, facts) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds any custom facts according to the rules defined for the operating system with the given facts.

Parameters:

  • os (String)

    Name of the operating system

  • facts (Hash)

    Facts hash

Returns:

  • (Hash)

    facts Facts hash with custom facts added



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/rspec-puppet-facts.rb', line 131

def self.with_custom_facts(os, facts)
  return facts unless @custom_facts

  @custom_facts.each do |name, fact|
    next if fact[:options][:confine] && !fact[:options][:confine].include?(os)
    next if fact[:options][:exclude] && fact[:options][:exclude].include?(os)

    facts[name] = fact[:value].respond_to?(:call) ? fact[:value].call(os, facts) : fact[:value]
  end

  facts
end

Instance Method Details

#add_custom_fact(name, value, options = {}) ⇒ Object

Register a custom fact that will be included in the facts hash. If it should be limited to a particular OS, pass a :confine option that contains the operating system(s) to confine to. If it should be excluded on a particular OS, use :exclude.

Parameters:

  • name (String)

    Fact name

  • value (String, Proc)

    Fact value. If proc, takes 2 params: os and facts hash

  • opts (Hash)


105
106
107
108
109
110
# File 'lib/rspec-puppet-facts.rb', line 105

def add_custom_fact(name, value, options = {})
  options[:confine] = [options[:confine]] if options[:confine].is_a?(String)
  options[:exclude] = [options[:exclude]] if options[:exclude].is_a?(String)

  RspecPuppetFacts.register_custom_fact(name, value, options)
end

#on_supported_os(opts = {}) ⇒ Hash <String => Hash>

Use the provided options or the data from the metadata.json file to find a set of matching facts in the FacterDB. OS names and facts can be used in the Puppet RSpec tests to run the examples against all supported facts combinations.

The list of received OS facts can also be filtered by the SPEC_FACTS_OS environment variable. For example, if the variable is set to “debian” only the OS names which start with “debian” will be returned. It allows a user to quickly run the tests only on a single facts set without any file modifications.

will be used instead of the “operatingsystem_support” section if the metadata file even if the file is missing.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :hardwaremodels (String, Array<String>)

    The OS architecture names, i.e. x86_64

  • :supported_os (Array<Hash>)

    If this options is provided the data

Returns:

  • (Hash <String => Hash>)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
# File 'lib/rspec-puppet-facts.rb', line 28

def on_supported_os(opts = {})
  opts[:hardwaremodels] ||= ['x86_64']
  opts[:hardwaremodels] = [opts[:hardwaremodels]] unless opts[:hardwaremodels].is_a? Array
  opts[:supported_os] ||= RspecPuppetFacts.meta_supported_os

  filter = []
  opts[:supported_os].map do |os_sup|
    if os_sup['operatingsystemrelease']
      os_sup['operatingsystemrelease'].map do |operatingsystemmajrelease|
        opts[:hardwaremodels].each do |hardwaremodel|

          if os_sup['operatingsystem'] =~ /BSD/i
            hardwaremodel = 'amd64'
          elsif os_sup['operatingsystem'] =~ /Solaris/i
            hardwaremodel = 'i86pc'
          elsif os_sup['operatingsystem'] =~ /Windows/i
            hardwaremodel = 'x64'
          end

          filter << {
              :facterversion          => "/^#{Facter.version[0..2]}/",
              :operatingsystem        => os_sup['operatingsystem'],
              :operatingsystemrelease => "/^#{operatingsystemmajrelease.split(' ')[0]}/",
              :hardwaremodel          => hardwaremodel,
          }
        end
      end
    else
      opts[:hardwaremodels].each do |hardwaremodel|
        filter << {
            :facterversion   => "/^#{Facter.version[0..2]}/",
            :operatingsystem => os_sup['operatingsystem'],
            :hardwaremodel   => hardwaremodel,
        }
      end
    end
  end

  received_facts = FacterDB::get_facts(filter)
  unless received_facts.any?
    RspecPuppetFacts.warning "No facts were found in the FacterDB for: #{filter.inspect}"
    return {}
  end

  os_facts_hash = {}
  received_facts.map do |facts|
    # Fix facter bug
    if facts[:operatingsystem] == 'Ubuntu'
      operatingsystemmajrelease = facts[:operatingsystemrelease].split('.')[0..1].join('.')
    elsif facts[:operatingsystem] == 'OpenBSD'
      operatingsystemmajrelease = facts[:operatingsystemrelease]
    else
      if facts[:operatingsystemmajrelease].nil?
        operatingsystemmajrelease = facts[:operatingsystemrelease].split('.')[0]
      else
        operatingsystemmajrelease = facts[:operatingsystemmajrelease]
      end
    end
    os = "#{facts[:operatingsystem].downcase}-#{operatingsystemmajrelease}-#{facts[:hardwaremodel]}"
    next unless os.start_with? RspecPuppetFacts.spec_facts_os_filter if RspecPuppetFacts.spec_facts_os_filter
    facts.merge! RspecPuppetFacts.common_facts
    os_facts_hash[os] = RspecPuppetFacts.with_custom_facts(os, facts)
  end
  os_facts_hash
end