Skip to content

Commit

Permalink
Merge pull request #2029 from oanatmaria/FACT-2672
Browse files Browse the repository at this point in the history
(FACT-2672) Fix ssh fact output
  • Loading branch information
Bogdan Irimie authored Aug 17, 2020
2 parents 0853f4a + 8d196d6 commit 2e92f2d
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 62 deletions.
17 changes: 6 additions & 11 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# This configuration was generated by
# `rubocop --auto-gen-config --exclude-limit 1000`
# on 2020-06-11 15:48:25 +0300 using RuboCop version 0.81.0.
# on 2020-08-13 15:12:10 +0300 using RuboCop version 0.81.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 82
# Configuration parameters: CustomTransform, IgnoreMethods.
# Offense count: 81
# Configuration parameters: CustomTransform, IgnoreMethods, SpecSuffixOnly.
RSpec/FilePath:
Exclude:
- 'spec/custom_facts/core/aggregate_spec.rb'
Expand All @@ -32,7 +32,6 @@ RSpec/FilePath:
- 'spec/facter/facts_utils/windows_release_finder_spec.rb'
- 'spec/facter/model/fact_collection_spec.rb'
- 'spec/facter/model/resolved_fact_spec.rb'
- 'spec/facter/resolvers/aio_agent_version_spec.rb'
- 'spec/facter/resolvers/aix/architecture_resolver_spec.rb'
- 'spec/facter/resolvers/aix/ffi_helper_spec.rb'
- 'spec/facter/resolvers/aix/hardware_resolver_spec.rb'
Expand All @@ -43,8 +42,8 @@ RSpec/FilePath:
- 'spec/facter/resolvers/fips_enabled_resolver_spec.rb'
- 'spec/facter/resolvers/freebsd/ffi_helper_spec.rb'
- 'spec/facter/resolvers/identity_resolver_spec.rb'
- 'spec/facter/resolvers/load_averages_resolver_spec.rb'
- 'spec/facter/resolvers/macosx/dmi_resolver_spec.rb'
- 'spec/facter/resolvers/macosx/utils/system_profile_executor_spec.rb'
- 'spec/facter/resolvers/memory_resolver_spec.rb'
- 'spec/facter/resolvers/mountpoints_resolver_spec.rb'
- 'spec/facter/resolvers/processors_resolver_spec.rb'
Expand Down Expand Up @@ -92,8 +91,6 @@ RSpec/FilePath:
- 'spec/framework/formatters/legacy_fact_formatter_spec.rb'
- 'spec/framework/formatters/yaml_fact_formatter_spec.rb'
- 'spec/framework/utils/utils_spec.rb'
- 'spec/facter/resolvers/macosx/utils/system_profile_executor_spec.rb'


# Offense count: 15
# Configuration parameters: AssignmentOnly.
Expand All @@ -113,7 +110,7 @@ RSpec/LeakyConstantDeclaration:
- 'spec/facter/resolvers/macosx/mountpoints_resolver_spec.rb'
- 'spec/facter/resolvers/utils/windows/network_utils_spec.rb'

# Offense count: 96
# Offense count: 93
# Configuration parameters: EnforcedStyle.
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
Expand All @@ -134,7 +131,6 @@ RSpec/MessageSpies:
- 'spec/custom_facts/util/resolution_spec.rb'
- 'spec/facter/facts/aix/os/name_spec.rb'
- 'spec/facter/facts/aix/os/release_spec.rb'
- 'spec/facter/facts/macosx/is_virtual_spec.rb'
- 'spec/facter/facts/macosx/mountpoints_spec.rb'
- 'spec/facter/facts/macosx/os/architecture_spec.rb'
- 'spec/facter/facts/macosx/os/family_spec.rb'
Expand Down Expand Up @@ -180,7 +176,7 @@ RSpec/SubjectStub:
- 'spec/custom_facts/util/fact_spec.rb'
- 'spec/custom_facts/util/resolution_spec.rb'

# Offense count: 134
# Offense count: 130
# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.
RSpec/VerifiedDoubles:
Exclude:
Expand All @@ -190,7 +186,6 @@ RSpec/VerifiedDoubles:
- 'spec/custom_facts/util/directory_loader_spec.rb'
- 'spec/custom_facts/util/fact_spec.rb'
- 'spec/custom_facts/util/resolution_spec.rb'
- 'spec/facter/facts/aix/ssh_spec.rb'
- 'spec/facter/facts/macosx/memory/swap/capacity_spec.rb'
- 'spec/facter/facts/macosx/memory/swap/used_bytes_spec.rb'
- 'spec/facter/facts/macosx/memory/system/capacity_spec.rb'
Expand Down
4 changes: 3 additions & 1 deletion lib/facter/facts/aix/ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ def call_the_resolver
private

def fact_value
resolver_data.map! { |el| create_ssh_fact(el) }.inject(:merge!)
resolver_data.map { |el| create_ssh_fact(el) }.inject(:merge)
end

def resolver_data
Facter::Resolvers::SshResolver.resolve(:ssh)
end

def create_ssh_fact(ssh)
return {} unless ssh

{ ssh.name.to_sym => {
fingerprints: {
sha1: ssh.fingerprint.sha1,
Expand Down
28 changes: 19 additions & 9 deletions lib/facter/facts/linux/ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,30 @@ class Ssh
FACT_NAME = 'ssh'

def call_the_resolver
result = Facter::Resolvers::SshResolver.resolve(:ssh)
ssh_facts = {}
result.each { |ssh| ssh_facts.merge!(create_ssh_fact(ssh)) }
Facter::ResolvedFact.new(FACT_NAME, ssh_facts)
Facter::ResolvedFact.new(FACT_NAME, fact_value)
end

private

def fact_value
resolver_data.map { |el| create_ssh_fact(el) }.inject(:merge)
end

def resolver_data
Facter::Resolvers::SshResolver.resolve(:ssh)
end

def create_ssh_fact(ssh)
{ ssh.name.to_sym =>
{ fingerprints: { sha1: ssh.fingerprint.sha1,
sha256: ssh.fingerprint.sha256 },
key: ssh.key,
type: ssh.type } }
return {} unless ssh

{ ssh.name.to_sym => {
fingerprints: {
sha1: ssh.fingerprint.sha1,
sha256: ssh.fingerprint.sha256
},
key: ssh.key,
type: ssh.type
} }
end
end
end
Expand Down
30 changes: 19 additions & 11 deletions lib/facter/facts/macosx/ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@ class Ssh
FACT_NAME = 'ssh'

def call_the_resolver
result = Facter::Resolvers::SshResolver.resolve(:ssh)
ssh_facts = {}
result.each { |ssh| ssh_facts.merge!(create_ssh_fact(ssh)) }

ssh_facts = nil if ssh_facts.empty?
Facter::ResolvedFact.new(FACT_NAME, ssh_facts)
Facter::ResolvedFact.new(FACT_NAME, fact_value)
end

private

def fact_value
resolver_data.map { |el| create_ssh_fact(el) }.inject(:merge)
end

def resolver_data
Facter::Resolvers::SshResolver.resolve(:ssh)
end

def create_ssh_fact(ssh)
{ ssh.name.to_sym =>
{ fingerprints: { sha1: ssh.fingerprint.sha1,
sha256: ssh.fingerprint.sha256 },
key: ssh.key,
type: ssh.type } }
return {} unless ssh

{ ssh.name.to_sym => {
fingerprints: {
sha1: ssh.fingerprint.sha1,
sha256: ssh.fingerprint.sha256
},
key: ssh.key,
type: ssh.type
} }
end
end
end
Expand Down
28 changes: 19 additions & 9 deletions lib/facter/facts/solaris/ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,30 @@ class Ssh
FACT_NAME = 'ssh'

def call_the_resolver
result = Facter::Resolvers::SshResolver.resolve(:ssh)
ssh_facts = {}
result.each { |ssh| ssh_facts.merge!(create_ssh_fact(ssh)) }
Facter::ResolvedFact.new(FACT_NAME, ssh_facts)
Facter::ResolvedFact.new(FACT_NAME, fact_value)
end

private

def fact_value
resolver_data.map { |el| create_ssh_fact(el) }.inject(:merge)
end

def resolver_data
Facter::Resolvers::SshResolver.resolve(:ssh)
end

def create_ssh_fact(ssh)
{ ssh.name.to_sym =>
{ fingerprints: { sha1: ssh.fingerprint.sha1,
sha256: ssh.fingerprint.sha256 },
key: ssh.key,
type: ssh.type } }
return {} unless ssh

{ ssh.name.to_sym => {
fingerprints: {
sha1: ssh.fingerprint.sha1,
sha256: ssh.fingerprint.sha256
},
key: ssh.key,
type: ssh.type
} }
end
end
end
Expand Down
54 changes: 33 additions & 21 deletions spec/facter/facts/aix/ssh_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,40 @@

describe Facts::Aix::Ssh do
describe '#call_the_resolver' do
it 'returns a fact' do
ssh1 = Facter::Ssh.new(Facter::FingerPrint.new('test', 'test'), 'ecdsa', 'test', 'ecdsa')
ssh2 = Facter::Ssh.new(Facter::FingerPrint.new('test2', 'test2'), 'rsa', 'test2', 'rsa')
result = [ssh1, ssh2]
result_fact = { ssh1.name.to_sym =>
{ fingerprints:
{ sha1: ssh1.fingerprint.sha1,
sha256: ssh1.fingerprint.sha256 },
key: ssh1.key,
type: ssh1.type },
ssh2.name.to_sym =>
{ fingerprints:
{ sha1: ssh2.fingerprint.sha1,
sha256: ssh2.fingerprint.sha256 },
key: ssh2.key,
type: ssh2.type } }
expected_fact = double(Facter::ResolvedFact, name: 'ssh', value: result_fact)
allow(Facter::Resolvers::SshResolver).to receive(:resolve).with(:ssh).and_return(result)
allow(Facter::ResolvedFact).to receive(:new).with('ssh', result_fact).and_return(expected_fact)
subject(:fact) { Facts::Aix::Ssh.new }

fact = Facts::Aix::Ssh.new
expect(fact.call_the_resolver).to eq(expected_fact)
let(:ssh) do
[Facter::Ssh.new(Facter::FingerPrint.new('test', 'test'), 'ecdsa', 'test', 'ecdsa')]
end
let(:value) do
{ 'ecdsa' => { 'fingerprints' =>
{ 'sha1' => 'test', 'sha256' => 'test' },
'key' => 'test',
'type' => 'ecdsa' } }
end

before do
allow(Facter::Resolvers::SshResolver).to \
receive(:resolve).with(:ssh).and_return(ssh)
end

it 'calls Facter::Resolvers::SshResolver' do
fact.call_the_resolver
expect(Facter::Resolvers::SshResolver).to have_received(:resolve).with(:ssh)
end

it 'returns a resolved fact' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'ssh', value: value)
end

context 'when resolver returns empty array' do
let(:ssh) { [] }

it 'returns nil fact' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'ssh', value: nil)
end
end
end
end
9 changes: 9 additions & 0 deletions spec/facter/facts/linux/ssh_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,14 @@
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'ssh', value: value)
end

context 'when resolver returns empty array' do
let(:ssh) { [] }

it 'returns nil fact' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'ssh', value: nil)
end
end
end
end
9 changes: 9 additions & 0 deletions spec/facter/facts/solaris/ssh_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,14 @@
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'ssh', value: value)
end

context 'when resolver returns empty array' do
let(:ssh) { [] }

it 'returns nil fact' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'ssh', value: nil)
end
end
end
end
10 changes: 10 additions & 0 deletions spec/facter/resolvers/ssh_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,15 @@
expect(Facter::Resolvers::SshResolver.resolve(:ssh)).to eq([rsa_result, ed25519_result])
end
end

context 'when ssh fails to be retrieved' do
before do
paths.each { |path| allow(File).to receive(:directory?).with(path).and_return(false) }
end

it 'returns empty array' do
expect(Facter::Resolvers::SshResolver.resolve(:ssh)).to eq([])
end
end
end
end

0 comments on commit 2e92f2d

Please sign in to comment.