This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(FACT-2654) Add ec2 facts for Windows
- Loading branch information
Oana Tanasoiu
committed
Jun 9, 2020
1 parent
13ad3e6
commit ca730cf
Showing
4 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facts | ||
module Windows | ||
class Ec2Metadata | ||
FACT_NAME = 'ec2_metadata' | ||
|
||
def call_the_resolver | ||
return Facter::ResolvedFact.new(FACT_NAME, nil) unless aws? | ||
|
||
fact_value = Facter::Resolvers::Ec2.resolve(:metadata) | ||
|
||
Facter::ResolvedFact.new(FACT_NAME, fact_value.empty? ? nil : fact_value) | ||
end | ||
|
||
def aws? | ||
virtual = Facter::Resolvers::Virtualization.resolve(:virtual) | ||
|
||
virtual == 'kvm' || virtual =~ /xen/ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facts | ||
module Windows | ||
class Ec2Userdata | ||
FACT_NAME = 'ec2_userdata' | ||
|
||
def call_the_resolver | ||
return Facter::ResolvedFact.new(FACT_NAME, nil) unless aws? | ||
|
||
fact_value = Facter::Resolvers::Ec2.resolve(:userdata) | ||
|
||
Facter::ResolvedFact.new(FACT_NAME, fact_value.empty? ? nil : fact_value) | ||
end | ||
|
||
def aws? | ||
virtual = Facter::Resolvers::Virtualization.resolve(:virtual) | ||
|
||
virtual == 'kvm' || virtual =~ /xen/ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Facts::Windows::Ec2Metadata do | ||
describe '#call_the_resolver' do | ||
subject(:fact) { Facts::Windows::Ec2Metadata.new } | ||
|
||
before do | ||
allow(Facter::Resolvers::Ec2).to receive(:resolve).with(:metadata).and_return(value) | ||
allow(Facter::Resolvers::Virtualization).to receive(:resolve).with(:virtual).and_return(hypervisor) | ||
end | ||
|
||
context 'when hypervisor is not kvm or xen' do | ||
let(:hypervisor) { nil } | ||
let(:value) { nil } | ||
|
||
it 'returns ec2 metadata fact as nil' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \ | ||
have_attributes(name: 'ec2_metadata', value: nil) | ||
end | ||
|
||
it "doesn't call Ec2 resolver" do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::Ec2).not_to have_received(:resolve).with(:metadata) | ||
end | ||
end | ||
|
||
context 'when hypervisor is xen' do | ||
let(:hypervisor) { 'xen' } | ||
|
||
context 'when resolver returns a value' do | ||
let(:value) { 'some custom value' } | ||
|
||
it 'calls Facter::Resolvers::Ec2' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::Ec2).to have_received(:resolve).with(:metadata) | ||
end | ||
|
||
it 'returns ec2 metadata fact' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \ | ||
have_attributes(name: 'ec2_metadata', value: value) | ||
end | ||
end | ||
|
||
context 'when resolver returns empty string' do | ||
let(:value) { '' } | ||
|
||
it 'returns ec2 userdata fact as nil' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \ | ||
have_attributes(name: 'ec2_metadata', value: nil) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Facts::Windows::Ec2Userdata do | ||
describe '#call_the_resolver' do | ||
subject(:fact) { Facts::Windows::Ec2Userdata.new } | ||
|
||
before do | ||
allow(Facter::Resolvers::Ec2).to receive(:resolve).with(:userdata).and_return(value) | ||
allow(Facter::Resolvers::Virtualization).to receive(:resolve).with(:virtual).and_return(hypervisor) | ||
end | ||
|
||
context 'when hypervisor is not kvm or xen' do | ||
let(:hypervisor) { nil } | ||
let(:value) { nil } | ||
|
||
it 'returns ec2 userdata fact as nil' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \ | ||
have_attributes(name: 'ec2_userdata', value: nil) | ||
end | ||
|
||
it "doesn't call Ec2 resolver" do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::Ec2).not_to have_received(:resolve).with(:userdata) | ||
end | ||
end | ||
|
||
context 'when hypervisor is xen' do | ||
let(:hypervisor) { 'xen' } | ||
|
||
context 'when resolver returns a value' do | ||
let(:value) { 'some custom value' } | ||
|
||
it 'calls Facter::Resolvers::Ec2' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::Ec2).to have_received(:resolve).with(:userdata) | ||
end | ||
|
||
it 'returns ec2 userdata fact' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \ | ||
have_attributes(name: 'ec2_userdata', value: value) | ||
end | ||
end | ||
|
||
context 'when resolver returns empty string' do | ||
let(:value) { '' } | ||
|
||
it 'returns ec2 userdata fact as nil' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \ | ||
have_attributes(name: 'ec2_userdata', value: nil) | ||
end | ||
end | ||
end | ||
end | ||
end |