module RspecPuppetFactsUnsupported

A main module of rspec-puppet-facts-unsupported

A main module of rspec-puppet-facts-unsupported

Constants

CURRENT_FACT_NAMES
VERSION

Public Class Methods

verbose=(verbose) click to toggle source
# File lib/rspec-puppet-facts-unsupported/on_unsupported_os.rb, line 30
def self.verbose=(verbose)
  @@verbose = (verbose == true) # rubocop:disable Style/ClassVars
end
verbose?() click to toggle source
# File lib/rspec-puppet-facts-unsupported/on_unsupported_os.rb, line 34
def self.verbose?
  @@verbose
end

Public Instance Methods

on_unsupported_os(opts = {}) click to toggle source

Fetches an unsupported list of operating system's facts. List doesn't contains operating system described in Puppet's metadata.json file. @return Hash[String => Hash] A hash of key being os description and value being example machine facts @param Hash[Symbol => Object] opts A configuration hash with options @option opts [String,Array<String>] :hardwaremodels The OS architecture names, i.e. 'x86_64', /IBM/ or 'i86pc' @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. @option opts [Array<Hash>] :filters An array of extra filters to be passed to FacterDB to narrow the search @option opts [Array<Hash>] :order An order in which records should be returned. You can pass values like :random - to randomly shuffle records (exact shuffle seed will be printed on stderr to be able to reproduce invalid behaivior), `:original` - to return original list, an integer seed - to reproduce failiures. By default it is set to `:random` @option opts [Array<Hash>] :limit A limit of records to be returned. By default it is set to 2.

# File lib/rspec-puppet-facts-unsupported/on_unsupported_os.rb, line 22
def on_unsupported_os(opts = {})
  process_opts(opts)
  filters = calculate_filters(opts)
  facts = FacterDB.get_facts(filters)
  op = UnsupportedFilteringOperation.new(facts, opts)
  op.facts
end
verbose?() click to toggle source
# File lib/rspec-puppet-facts-unsupported/on_unsupported_os.rb, line 38
def verbose?
  RspecPuppetFactsUnsupported.verbose?
end

Protected Instance Methods

factname(fact, opts = {}) click to toggle source
# File lib/rspec-puppet-facts-unsupported/on_unsupported_os.rb, line 44
def factname(fact, opts = {})
  opts[:era] ||= :legancy
  fact_sym = fact.to_s.to_sym
  if opts[:era] == :legancy
    fact_sym
  elsif opts[:era] == :current
    CURRENT_FACT_NAMES[fact_sym]
  else
    raise "invalid era: #{opts[:era].inspect}"
  end
end

Private Instance Methods

calculate_filters(opts) click to toggle source
# File lib/rspec-puppet-facts-unsupported/on_unsupported_os.rb, line 130
def calculate_filters(opts)
  filters = []
  opts[:hardwaremodels].each do |hardwaremodel|
    filters << {
      facterversion: "/^#{Regexp.quote(system_facter_version)}/",
      hardwaremodel: hardwaremodel
    }
  end
  filters = filters.product(opts[:filters]).collect { |x, y| x.merge(y) } unless opts[:filters].empty?
  postprocess_filters(filters)
end
ensure_default_filters(opts) click to toggle source
# File lib/rspec-puppet-facts-unsupported/on_unsupported_os.rb, line 118
def ensure_default_filters(opts)
  opts[:filters] ||= []
  opts[:filters] = [opts[:filters]] unless opts[:filters].is_a? Array
end
ensure_default_hardwaremodels(opts) click to toggle source
# File lib/rspec-puppet-facts-unsupported/on_unsupported_os.rb, line 113
def ensure_default_hardwaremodels(opts)
  opts[:hardwaremodels] ||= ['x86_64']
  opts[:hardwaremodels] = [opts[:hardwaremodels]] unless opts[:hardwaremodels].is_a? Array
end
find_facter_version_matching_regexp(regexp) click to toggle source
# File lib/rspec-puppet-facts-unsupported/on_unsupported_os.rb, line 146
def find_facter_version_matching_regexp(regexp)
  (1..4).each do |major|
    (0..7).each do |minor|
      candidate = "#{major}.#{minor}.42"
      stripped_regexp = regexp.gsub(%r{^/+|/+$}, '')
      return candidate if Regexp.new(stripped_regexp).match(candidate)
    end
  end
  nil
end
postprocess_filters(filters) click to toggle source
# File lib/rspec-puppet-facts-unsupported/on_unsupported_os.rb, line 157
def postprocess_filters(filters)
  filters.map do |filter|
    facterversion = find_facter_version_matching_regexp(filter[:facterversion])
    if facterversion.split('.').first >= '3'
      current = factname(:hardwaremodel, era: :current)
      legancy = factname(:hardwaremodel)
      filter[current] = filter[legancy]
      filter.delete(legancy)
    end
    filter
  end
end
process_opts(opts) click to toggle source
# File lib/rspec-puppet-facts-unsupported/on_unsupported_os.rb, line 104
def process_opts(opts)
  opts[:randomizer] = Randomizer.new(opts)
  ensure_default_hardwaremodels opts
  ensure_default_filters opts
  opts[:limit] ||= 2
  opts[:supported_os] ||= RspecPuppetFacts.meta_supported_os
  process_order opts
end
process_order(opts) click to toggle source
# File lib/rspec-puppet-facts-unsupported/on_unsupported_os.rb, line 123
def process_order(opts)
  rnd = opts[:randomizer]
  message = "Shuffling unsupported OS's facts with seed: #{rnd.seed}\nSet environment variable " \
    "to reproduce this order, for ex. on Linux \`export RSPEC_PUPPET_FACTS_UNSUPPORTED_ORDER=#{rnd.seed}\`"
  $stderr.puts message if verbose? && rnd.should_randomize?
end
system_facter_version() click to toggle source
# File lib/rspec-puppet-facts-unsupported/on_unsupported_os.rb, line 142
def system_facter_version
  Facter.version.split('.')[0..-2].join('.')
end