lib/rspec-puppet-facts.rb
# 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.
#
# @return [Hash <String => Hash>]
# @param [Hash] opts
# @option opts [String,Array<String>] :hardwaremodels The OS architecture names, i.e. x86_64
# @option opts [Array<Hash>] :supported_os If this options is provided the data
# will be used instead of the "operatingsystem_support" section if the metadata file
# even if the file is missing.
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.
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 .
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|
      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
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 | 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
Use the provided options or the data from the metadata.json file
to find a set of matching facts in the FacterDB.
Use the provided options or the data from the metadata . json file to find a set of matching facts in the FacterDB .
# Fix facter bug
None
Hash <String => Hash>
opts
:supported_os If this options is provided the data will be used instead of the "operatingsystem_support" section if the metadata file even if the file is missing.
: supported_os If this options is provided the data will be used instead of the " operatingsystem_support " section if the metadata file even if the file is missing .
None