Skip to content

Commit

Permalink
Make fact set key stringification opt-in for now
Browse files Browse the repository at this point in the history
This will allow us to push out a bug fix release and release this change
in a way that will allow users to migrate over without immediately
breaking their tests.
  • Loading branch information
rodjek committed Jul 26, 2019
1 parent 1a02b7d commit 25634f4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 20 deletions.
15 changes: 9 additions & 6 deletions lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

RSpec.configure do |c|
c.add_setting :default_facter_version, :default => Facter.version
c.add_setting :facterdb_string_keys, :default => false
end

# The purpose of this module is to simplify the Puppet
Expand Down Expand Up @@ -155,7 +156,9 @@ def on_supported_os(opts = {})
os_facts_hash[os] = RspecPuppetFacts.with_custom_facts(os, facts)
end

stringify_keys(os_facts_hash)
return stringify_keys(os_facts_hash) if RSpec.configuration.facterdb_string_keys

os_facts_hash
end

def stringify_keys(hash)
Expand Down Expand Up @@ -236,15 +239,15 @@ def self.spec_facts_strict?
def self.common_facts
return @common_facts if @common_facts
@common_facts = {
'puppetversion' => Puppet.version,
'rubysitedir' => RbConfig::CONFIG['sitelibdir'],
'rubyversion' => RUBY_VERSION,
:puppetversion => Puppet.version,
:rubysitedir => RbConfig::CONFIG['sitelibdir'],
:rubyversion => RUBY_VERSION,
}

@common_facts['mco_version'] = MCollective::VERSION if mcollective?
@common_facts[:mco_version] = MCollective::VERSION if mcollective?

if augeas?
@common_facts['augeasversion'] = Augeas.open(nil, nil, Augeas::NO_MODL_AUTOLOAD).get('/augeas/version')
@common_facts[:augeasversion] = Augeas.open(nil, nil, Augeas::NO_MODL_AUTOLOAD).get('/augeas/version')
end

@common_facts
Expand Down
70 changes: 56 additions & 14 deletions spec/rspec_puppet_facts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,48 @@
end

describe '#on_supported_os' do
context 'With RSpec.configuration.facterdb_string_keys' do
subject(:result) do
on_supported_os(
{
:supported_os => [
{
"operatingsystem" => "Debian",
"operatingsystemrelease" => ['7'],
},
],
}
)
end

let(:get_keys) do
proc { |r| r.keys + r.select { |_,v| v.is_a?(Hash) }.map { |_,v| get_keys.call(v) }.flatten }
end

context 'set to true' do
before(:each) do
RSpec.configuration.facterdb_string_keys = true
end

after(:each) do
RSpec.configuration.facterdb_string_keys = false
end

it 'returns a fact set with all the keys as Strings' do
expect(get_keys.call(result['debian-7-x86_64'])).to all(be_a(String))
end
end

context 'set to false' do
before(:each) do
RSpec.configuration.facterdb_string_keys = false
end

it 'returns a fact set with all the keys as Symbols or Strings' do
expect(get_keys.call(result['debian-7-x86_64'])).to all(be_a(Symbol).or(be_a(String)))
end
end
end

context 'Without specifying supported_os' do
subject { on_supported_os }
Expand Down Expand Up @@ -162,7 +204,7 @@
end

it 'returns a fact set for the specified release' do
expect(factsets).to include('redhat-7-x86_64' => include('operatingsystemmajrelease' => '7'))
expect(factsets).to include('redhat-7-x86_64' => include(:operatingsystemmajrelease => '7'))
end
end

Expand Down Expand Up @@ -457,7 +499,7 @@
facter_version = Facter.version.split('.')
is_expected.to match(
'centos-7-x86_64' => include(
'facterversion' => /\A#{facter_version[0]}\.#{facter_version[1]}\./
:facterversion => /\A#{facter_version[0]}\.#{facter_version[1]}\./
)
)
end
Expand All @@ -483,7 +525,7 @@
it 'returns facts from the specified default Facter version' do
is_expected.to match(
'centos-7-x86_64' => include(
'facterversion' => /\A3\.1\./
:facterversion => /\A3\.1\./
)
)
end
Expand All @@ -507,7 +549,7 @@
major, minor = Facter.version.split('.')
is_expected.to match(
'centos-7-x86_64' => include(
'facterversion' => /\A#{major}\.[#{minor}#{minor.to_i + 1}]\./
:facterversion => /\A#{major}\.[#{minor}#{minor.to_i + 1}]\./
)
)
end
Expand All @@ -534,7 +576,7 @@

it 'returns facts from a facter version matching 3.1' do
is_expected.to match(
'centos-7-x86_64' => include('facterversion' => '3.1.6')
'centos-7-x86_64' => include(:facterversion => '3.1.6')
)
end
end
Expand All @@ -551,7 +593,7 @@

it 'returns facts from a facter version matching 3.1' do
is_expected.to match(
'centos-7-x86_64' => include('facterversion' => '3.1.6')
'centos-7-x86_64' => include(:facterversion => '3.1.6')
)
end
end
Expand All @@ -568,7 +610,7 @@

it 'returns facts from a facter version matching 3.3' do
is_expected.to match(
'centos-7-x86_64' => include('facterversion' => '3.3.0')
'centos-7-x86_64' => include(:facterversion => '3.3.0')
)
end
end
Expand All @@ -585,7 +627,7 @@

it 'returns facts from a facter version matching 3.3' do
is_expected.to match(
'centos-7-x86_64' => include('facterversion' => '3.3.0')
'centos-7-x86_64' => include(:facterversion => '3.3.0')
)
end
end
Expand Down Expand Up @@ -626,12 +668,12 @@

it 'returns CentOS facts from a facter version matching 3.8' do
is_expected.to include(
'centos-7-x86_64' => include('facterversion' => '3.8.0')
'centos-7-x86_64' => include(:facterversion => '3.8.0')
)
end
it 'returns OpenSuSE facts from a facter version matching 3.9' do
is_expected.to include(
'opensuse-42-x86_64' => include('facterversion' => '3.9.2')
'opensuse-42-x86_64' => include(:facterversion => '3.9.2')
)
end
end
Expand Down Expand Up @@ -691,7 +733,7 @@
it 'should not add "augeasversion" if Augeas is supported' do
allow(described_class).to receive(:augeas?).and_return(false)
RspecPuppetFacts.reset
expect(subject.common_facts).not_to include 'augeasversion'
expect(subject.common_facts).not_to include(:augeasversion)
end

it 'should determine the Augeas version if Augeas is supported' do
Expand All @@ -708,7 +750,7 @@ def self.get(*_args)
allow(described_class).to receive(:augeas?).and_return(true)
stub_const('Augeas', Augeas_stub)
RspecPuppetFacts.reset
expect(subject.common_facts['augeasversion']).to eq 'my_version'
expect(subject.common_facts[:augeasversion]).to eq 'my_version'
end

it 'can output a warning message' do
Expand All @@ -727,7 +769,7 @@ module MCollective_stub
end

it 'includes an "mco_version" fact' do
expect(subject.common_facts).to include('mco_version' => 'my_version')
expect(subject.common_facts).to include(:mco_version => 'my_version')
end
end

Expand All @@ -738,7 +780,7 @@ module MCollective_stub
end

it 'does not include an "mco_version" fact' do
expect(subject.common_facts).not_to include('mco_version')
expect(subject.common_facts).not_to include(:mco_version)
end
end
end
Expand Down

0 comments on commit 25634f4

Please sign in to comment.