module PuppetSpecFacts

you can generate appropriate json files with: facter –json > filename.json then dump the file in the facts directory with the .json extension. If you’re cool you’ll use facter –json | jq . > filename.json to prettyprint it

Constants

VERSION

Public Class Methods

fact_style(fact_hash) click to toggle source
# File lib/puppet_spec_facts.rb, line 134
def self.fact_style(fact_hash)
  if SemVer.new(fact_hash['facterversion']) > SemVer.new('2.0.0')
    return 'structured'  if fact_hash['system_uptime'].is_a?(Hash)
    return 'stringified' if fact_hash['system_uptime'] == nil
  elsif SemVer.new(fact_hash['facterversion']) < SemVer.new('2.0.0')
    return 'stringified' if fact_hash['system_uptime'] == nil
  end
  raise('cannot determine whether fact hash is structured or stringified')
end
facts_for_platform_by_fact(select_facts={}) click to toggle source
# File lib/puppet_spec_facts.rb, line 47
def self.facts_for_platform_by_fact(select_facts={})
  puppet_platforms.select { |platform, facts|
    match = true
    select_facts.each { |query_fact, query_value|
      match = false if facts[query_fact] != query_value
    }
    match
  }
end
facts_for_platform_by_name(platforms) click to toggle source
# File lib/puppet_spec_facts.rb, line 57
def self.facts_for_platform_by_name(platforms)
  platforms = [platforms] if platforms.is_a?(String)
  results = {}
  platforms.each do |name|
    results[:"#{name}"] = puppet_platforms[name]
  end
  results
end
get_jsonfile(filename) click to toggle source
# File lib/puppet_spec_facts.rb, line 144
def self.get_jsonfile(filename)
  JSON.parse(File.read(filename))
end
is_pe(fact_hash) click to toggle source
# File lib/puppet_spec_facts.rb, line 114
def self.is_pe(fact_hash)
  return false unless fact_hash['puppetversion']
  return true if fact_hash['puppetversion'].downcase.include?('puppet enterprise')
  false
end
make_sane_name(fact_hash) click to toggle source

private

# File lib/puppet_spec_facts.rb, line 103
def self.make_sane_name(fact_hash)
  osname = name_osname(fact_hash)
  codename = name_codename(fact_hash)
  version = name_version(fact_hash)
  architecture = fact_hash['architecture']
  puppet_version = puppet_name(fact_hash)
  fact_style = fact_style(fact_hash)

  return [osname, codename, version, architecture, puppet_version, fact_style].compact.join('_')
end
name_codename(fact_hash) click to toggle source
# File lib/puppet_spec_facts.rb, line 97
def self.name_codename(fact_hash)
  return fact_hash['lsbdistcodename'] unless ['RedHat','Gentoo'].include?(fact_hash['osfamily'])
  nil
end
name_osname(fact_hash) click to toggle source
# File lib/puppet_spec_facts.rb, line 84
def self.name_osname(fact_hash)
  osname ||= fact_hash['lsbdistid']
  osname ||= fact_hash['operatingsystem']
  osname ||= fact_hash['kernel']
  osname ||= fact_hash['osfamily']
end
name_version(fact_hash) click to toggle source
# File lib/puppet_spec_facts.rb, line 91
def self.name_version(fact_hash)
  version = fact_hash['lsbdistrelease']
  version ||= fact_hash['macosx_productversion']
  version ||= fact_hash['operatingsystemrelease']
end
overload_facts(fact_hash) click to toggle source

inject some additional facts to simplify lookups from rspec

# File lib/puppet_spec_facts.rb, line 31
def self.overload_facts(fact_hash)
  fact_hash['fact_style']     = fact_style(fact_hash)
  fact_hash['is_pe']          = 'true' if is_pe(fact_hash)
  fact_hash['concat_basedir'] = '/fake_concat_basedir'
  fact_hash['path']           = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
  return fact_hash
end
puppet_name(fact_hash) click to toggle source
# File lib/puppet_spec_facts.rb, line 120
def self.puppet_name(fact_hash)
  if is_pe(fact_hash)
    version = fact_hash['puppetversion'].delete('()').split[3]
    return "PE-#{version}"
  else
    return fact_hash['puppetversion']
  end
end
puppet_platform_names() click to toggle source
# File lib/puppet_spec_facts.rb, line 39
def self.puppet_platform_names
  platform_names = []
  puppet_platforms.each do |name, value|
    platform_names << name
  end
  platform_names
end
puppet_platforms() click to toggle source
# File lib/puppet_spec_facts.rb, line 15
def self.puppet_platforms
  platforms = {}
  file_list = Dir.glob("#{@proj_root}/**/*.json")
  file_list.each do |filename|
    fact_hash = get_jsonfile(filename)
    platform_name = make_sane_name(fact_hash)

    if platforms[platform_name] != nil
      next
    end
    platforms[platform_name] = overload_facts(fact_hash)
  end
  platforms
end
resave_all_facts() click to toggle source
# File lib/puppet_spec_facts.rb, line 66
def self.resave_all_facts
  puppet_platforms.each do |name, hash|
    save_reorganized_facts_to_json(hash)
  end
end
save_reorganized_facts_to_json(fact_hash=nil, dir='facts') click to toggle source
# File lib/puppet_spec_facts.rb, line 72
def self.save_reorganized_facts_to_json(fact_hash=nil, dir='facts')
  namepath = name_osname(fact_hash)
  save_path = "#{@proj_root}/#{dir}/#{namepath}"
  filename = make_sane_name(fact_hash) + '.json'
  json_output = JSON.pretty_generate(fact_hash)
  FileUtils.mkdir_p save_path

  File.open("#{save_path}/#{filename}","w") do |f|
    f.write(json_output)
  end
end