-
-
Save cdenneen/842def0ac9f177986b7f5ce0354e7c85 to your computer and use it in GitHub Desktop.
Facter stubbing examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require 'facter' | |
describe 'facter_value' do | |
context 'when stubbing Facter::Util::Collection#fact' do | |
before(:each) do | |
stub_fact = instance_double(Facter::Util::Fact, :value => 'foo') | |
allow(Facter.collection).to receive(:fact).with(:test_fact).and_return(stub_fact) | |
end | |
it 'can be accessed via Facter.value' do | |
expect(Facter.value(:test_fact)).to eq('foo') | |
end | |
it 'can be accessed via Facter::Util::Fact#value' do | |
expect(Facter.fact(:test_fact).value).to eq('foo') | |
end | |
it 'is present in the fact collection' do | |
expect(Facter.collection.fact(:test_fact)).not_to be_nil | |
end | |
end | |
context 'when stubbing Facter::Util::Fact#value' do | |
before(:each) do | |
allow(Facter.fact(:test_fact)).to receive(:value).and_return('foo') | |
end | |
it 'can be accessed via Facter::Util::Fact#value' do | |
expect(Facter.fact(:test_fact).value).to eq('foo') | |
end | |
it 'can not be accessed via Facter.value' do | |
expect(Facter.value(:test_fact)).to be_nil | |
end | |
it 'is not actually present in the fact collection' do | |
expect(Facter.collection.fact(:test_fact)).to be_nil | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Facter.add('host_type') do | |
setcode do | |
type = 'unknown' | |
if Facter.value(:ec2_metadata) | |
type = 'aws' | |
end | |
if Facter.value(:cloud) | |
if Facter.value(:cloud)['provider'] == 'azure' | |
type = 'azure' | |
end | |
if Facter.value(:cloud)['provider'] == 'google' | |
type = 'gcp' | |
end | |
end | |
type | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe 'custom fact host_type', type: :fact do | |
context 'supported operating systems' do | |
on_supported_os(requested_facterversion).each do |os, _facts| | |
context "on #{os}" do | |
before(:each) do | |
Facter.flush | |
allow(Facter.collection).to receive(:fact).with(anything).and_call_original | |
end | |
describe 'amazon' do | |
before(:each) do | |
stub_fact = instance_double(Facter::Util::Fact, value: {}) | |
allow(Facter.collection).to receive(:fact).with(:ec2_metadata).and_return(stub_fact) | |
end | |
it do | |
expect(Facter.fact(:host_type).value).to eq('aws') | |
end | |
end | |
describe 'azure' do | |
let(:cloud) { { 'provider' => 'azure', 'metadata' => {} } } | |
before(:each) do | |
stub_fact = instance_double(Facter::Util::Fact, value: cloud) | |
allow(Facter.collection).to receive(:fact).with(:cloud).and_return(stub_fact) | |
end | |
it do | |
expect(Facter.fact(:host_type).value).to eq('azure') | |
end | |
end | |
describe 'google' do | |
let(:cloud) { { 'provider' => 'google', 'metadata' => {} } } | |
before(:each) do | |
stub_fact = instance_double(Facter::Util::Fact, value: cloud) | |
allow(Facter.collection).to receive(:fact).with(:cloud).and_return(stub_fact) | |
end | |
it do | |
expect(Facter.fact(:host_type).value).to eq('gcp') | |
end | |
end | |
describe 'unknown host_type' do | |
before(:each) do | |
allow(Facter.collection).to receive(:fact).with(:ec2_metadata).and_return(nil) | |
end | |
it do | |
expect(Facter.fact(:host_type).value).to eq('unknown') | |
end | |
end | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puppet/profile_base - [dev●] » be rspec spec/unit/facter/host_type_spec.rb | |
custom fact host_type | |
supported operating systems | |
on ubuntu-14.04-x86_64 | |
amazon | |
should eq "aws" | |
azure | |
should eq "azure" | |
should eq "gcp" | |
unknown host_type | |
should eq "unknown" | |
on ubuntu-16.04-x86_64 | |
amazon | |
should eq "aws" | |
azure | |
should eq "azure" | |
should eq "gcp" | |
unknown host_type | |
should eq "unknown" | |
on windows-2012 R2-x86_64 | |
amazon | |
should eq "aws" | |
azure | |
should eq "azure" | |
should eq "gcp" | |
unknown host_type | |
should eq "unknown" | |
on centos-7-x86_64 | |
amazon | |
should eq "aws" | |
azure | |
should eq "azure" | |
should eq "gcp" | |
unknown host_type | |
should eq "unknown" | |
on centos-6-x86_64 | |
amazon | |
should eq "aws" | |
azure | |
should eq "azure" | |
should eq "gcp" | |
unknown host_type | |
should eq "unknown" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment