Skip to content

Commit

Permalink
Merge pull request #2093 from Filipovici-Andrei/fix_cloud_fact
Browse files Browse the repository at this point in the history
(FACT-2802) Fix Cloud resolver
  • Loading branch information
oanatmaria authored Sep 21, 2020
2 parents a8c5dc3 + 64b5876 commit 006e9e5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/facter/resolvers/cloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def post_resolve(fact_name)
def detect_azure(fact_name)
search_dirs = %w[/var/lib/dhcp /var/lib/NetworkManager]
search_dirs.each do |path|
next unless File.directory?(path)
next if !File.readable?(path) || !File.directory?(path)

files = Dir.entries(path)
files.select! { |filename| filename =~ /^dhclient.*lease.*$/ }
Expand Down
20 changes: 19 additions & 1 deletion spec/facter/resolvers/cloud_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

before do
cloud_resolver.instance_variable_set(:@log, log_spy)
allow(File).to receive(:readable?)
allow(File).to receive(:directory?)
allow(Dir).to receive(:entries)
end
Expand All @@ -17,6 +18,7 @@

context 'when lease files are not found' do
before do
allow(File).to receive(:readable?).with('/var/lib/dhcp').and_return(true)
allow(File).to receive(:directory?).with('/var/lib/dhcp').and_return(true)
allow(Dir).to receive(:entries).with('/var/lib/dhcp').and_return('.')
end
Expand All @@ -30,6 +32,7 @@
let(:content) { load_fixture('dhclient_rhel_lease_8').read }

before do
allow(File).to receive(:readable?).with('/var/lib/dhcp').and_return(true)
allow(File).to receive(:directory?).with('/var/lib/dhcp').and_return(true)
allow(Facter::Util::FileHelper)
.to receive(:safe_read)
Expand All @@ -47,6 +50,7 @@
let(:content) { load_fixture('dhcp_lease').read }

before do
allow(File).to receive(:readable?).with('/var/lib/dhcp').and_return(true)
allow(File).to receive(:directory?).with('/var/lib/dhcp').and_return(true)
allow(Facter::Util::FileHelper)
.to receive(:safe_read)
Expand All @@ -64,16 +68,30 @@
let(:content) { '' }

before do
allow(File).to receive(:readable?).with('/var/lib/dhcp').and_return(true)
allow(File).to receive(:directory?).with('/var/lib/dhcp').and_return(true)
allow(Facter::Util::FileHelper)
.to receive(:safe_read)
.with('/var/lib/dhcp/dhclient.rhel.lease.8')
.and_return(content)
.and_return([])
allow(Dir).to receive(:entries).with('/var/lib/dhcp').and_return(['.', 'dhclient.rhel.lease.8'])
end

it 'returns nil' do
expect(cloud_resolver.resolve(:cloud_provider)).to be_nil
end
end

context 'when we do not have read permissions for /var/lib/dhcp ' do
let(:content) { '' }

before do
allow(File).to receive(:readable?).with('/var/lib/dhcp').and_return(false)
allow(File).to receive(:directory?).with('/var/lib/dhcp').and_return(true)
end

it 'returns nil' do
expect(cloud_resolver.resolve(:cloud_provider)).to be_nil
end
end
end

0 comments on commit 006e9e5

Please sign in to comment.