Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

Commit

Permalink
(FACT-2654) Add ec2 facts for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Oana Tanasoiu committed Jun 9, 2020
1 parent 13ad3e6 commit ca730cf
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/facts/windows/ec2_metadata.rb
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
23 changes: 23 additions & 0 deletions lib/facts/windows/ec2_userdata.rb
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
54 changes: 54 additions & 0 deletions spec/facter/facts/windows/ec2_metadata_spec.rb
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
54 changes: 54 additions & 0 deletions spec/facter/facts/windows/ec2_userdata_spec.rb
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

0 comments on commit ca730cf

Please sign in to comment.