Skip to content

Commit

Permalink
Add on_supported_operatingsystem method that returns an Array
Browse files Browse the repository at this point in the history
  • Loading branch information
mcanevet committed May 19, 2016
1 parent ab51279 commit 0aa0775
Show file tree
Hide file tree
Showing 3 changed files with 409 additions and 40 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ require 'spec_helper'

describe 'myclass' do

on_supported_os.each do |os, facts|
context "on #{os}" do
on_supported_operatingsystem.each do |facts|
context "on #{facts[:operatingsystem]} #{facts[:operatingsystemmajrelease]}" do
let(:facts) do
facts
end
Expand Down Expand Up @@ -119,8 +119,8 @@ require 'spec_helper'

describe Puppet::Type.type(:mytype) do

on_supported_os.each do |os, facts|
context "on #{os}" do
on_supported_operatingsystem.each do |facts|
context "on #{facts[:operatingsystem]} #{facts[:operatingsystemmajrelease]}" do
before :each do
Facter.clear
facts.each do |k, v|
Expand Down Expand Up @@ -184,8 +184,8 @@ require 'spec_helper'
describe Puppet::Parser::Functions.function(:myfunction) do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

on_supported_os.each do |os, facts|
context "on #{os}" do
on_supported_operatingsystem.each do |facts|
context "on #{facts[:operatingsystem]} #{facts[:operatingsystemmajrelease]}" do
before :each do
facts.each do |k, v|
scope.stubs(:lookupvar).with("::#{k}").returns(v)
Expand All @@ -207,7 +207,7 @@ require 'spec_helper'

describe 'myclass' do

on_supported_os({
on_supported_operatingsystem({
:hardwaremodels => ['i386', 'x86_64'],
:supported_os => [
{
Expand All @@ -225,8 +225,8 @@ describe 'myclass' do
]
}
],
}).each do |os, facts|
context "on #{os}" do
}).each do |facts|
context "on #{facts[:operatingsystem]} #{facts[:operatingsystemmajrelease]}" do
let(:facts) do
facts
end
Expand All @@ -246,8 +246,8 @@ require 'spec_helper'

describe 'myclass' do

on_supported_os.each do |os, facts|
context "on #{os}" do
on_supported_operatingsystem.each do |facts|
context "on #{facts[:operatingsystem]} #{facts[:operatingsystemmajrelease]}" do
let(:facts) do
facts.merge({
:foo => 'bar',
Expand Down
91 changes: 64 additions & 27 deletions lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,10 @@
# module's RSpec tests by looping through all supported
# OS'es and their facts data which is received from the FacterDB.
module RspecPuppetFacts
# 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.
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

def jgrep_filter(supported_os, opts)
filter = []
opts[:supported_os].map do |os_sup|
supported_os.map do |os_sup|
if os_sup['operatingsystemrelease']
os_sup['operatingsystemrelease'].map do |operatingsystemmajrelease|
opts[:hardwaremodels].each do |hardwaremodel|
Expand Down Expand Up @@ -62,15 +42,63 @@ def on_supported_os(opts = {})
end
end
end
filter
end

received_facts = FacterDB::get_facts(filter)
unless received_facts.any?
RspecPuppetFacts.warning "No facts were found in the FacterDB for: #{filter.inspect}"
return {}
# 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 [Array<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.
def on_supported_operatingsystem(opts = {})
opts[:hardwaremodels] ||= ['x86_64']
opts[:hardwaremodels] = [opts[:hardwaremodels]] unless opts[:hardwaremodels].is_a? Array
opts[:supported_os] ||= RspecPuppetFacts.meta_supported_os

os_facts_array = []
RspecPuppetFacts.facterdb_facts(jgrep_filter(opts[:supported_os], opts)).map do |facts|
next unless facts[:operatingsystem].downcase.start_with? RspecPuppetFacts.spec_facts_os_filter if RspecPuppetFacts.spec_facts_os_filter
os_facts_array << facts.merge(RspecPuppetFacts.common_facts)
end
os_facts_array
end

# 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.
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

os_facts_hash = {}
received_facts.map do |facts|
RspecPuppetFacts.facterdb_facts(jgrep_filter(opts[:supported_os], opts)).map do |facts|
os = "#{facts[:operatingsystem].downcase}-#{facts[:operatingsystemrelease].split('.')[0]}-#{facts[:hardwaremodel]}"
next unless os.start_with? RspecPuppetFacts.spec_facts_os_filter if RspecPuppetFacts.spec_facts_os_filter
facts.merge! RspecPuppetFacts.common_facts
Expand All @@ -89,6 +117,15 @@ def self.spec_facts_os_filter
ENV['SPEC_FACTS_OS']
end

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

# These facts are common for all OS'es and will be
# added to the facts retrieved from the FacterDB
# @api private
Expand Down

0 comments on commit 0aa0775

Please sign in to comment.