From 8d196d629291940645cb5791a31c9090378f75a0 Mon Sep 17 00:00:00 2001 From: Oana Tanasoiu Date: Thu, 13 Aug 2020 15:07:46 +0300 Subject: [PATCH] (FACT-2672) Fix ssh fact output --- .rubocop_todo.yml | 17 +++---- lib/facter/facts/aix/ssh.rb | 4 +- lib/facter/facts/linux/ssh.rb | 28 +++++++---- lib/facter/facts/macosx/ssh.rb | 30 +++++++----- lib/facter/facts/solaris/ssh.rb | 28 +++++++---- spec/facter/facts/aix/ssh_spec.rb | 54 +++++++++++++--------- spec/facter/facts/linux/ssh_spec.rb | 9 ++++ spec/facter/facts/solaris/ssh_spec.rb | 9 ++++ spec/facter/resolvers/ssh_resolver_spec.rb | 10 ++++ 9 files changed, 127 insertions(+), 62 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 127653c9ff..20890f5523 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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' @@ -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' @@ -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' @@ -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. @@ -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: @@ -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' @@ -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: @@ -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' diff --git a/lib/facter/facts/aix/ssh.rb b/lib/facter/facts/aix/ssh.rb index ed21c0569d..751712e6aa 100644 --- a/lib/facter/facts/aix/ssh.rb +++ b/lib/facter/facts/aix/ssh.rb @@ -12,7 +12,7 @@ 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 @@ -20,6 +20,8 @@ def resolver_data end def create_ssh_fact(ssh) + return {} unless ssh + { ssh.name.to_sym => { fingerprints: { sha1: ssh.fingerprint.sha1, diff --git a/lib/facter/facts/linux/ssh.rb b/lib/facter/facts/linux/ssh.rb index 37a66b0d18..e462e4eb1c 100644 --- a/lib/facter/facts/linux/ssh.rb +++ b/lib/facter/facts/linux/ssh.rb @@ -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 diff --git a/lib/facter/facts/macosx/ssh.rb b/lib/facter/facts/macosx/ssh.rb index 4282d02770..7c2b5b88df 100644 --- a/lib/facter/facts/macosx/ssh.rb +++ b/lib/facter/facts/macosx/ssh.rb @@ -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 diff --git a/lib/facter/facts/solaris/ssh.rb b/lib/facter/facts/solaris/ssh.rb index 60db014611..062c3b6acc 100644 --- a/lib/facter/facts/solaris/ssh.rb +++ b/lib/facter/facts/solaris/ssh.rb @@ -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 diff --git a/spec/facter/facts/aix/ssh_spec.rb b/spec/facter/facts/aix/ssh_spec.rb index afef72fbb9..cb981febfd 100644 --- a/spec/facter/facts/aix/ssh_spec.rb +++ b/spec/facter/facts/aix/ssh_spec.rb @@ -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 diff --git a/spec/facter/facts/linux/ssh_spec.rb b/spec/facter/facts/linux/ssh_spec.rb index a3dee267da..3730d98bfe 100644 --- a/spec/facter/facts/linux/ssh_spec.rb +++ b/spec/facter/facts/linux/ssh_spec.rb @@ -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 diff --git a/spec/facter/facts/solaris/ssh_spec.rb b/spec/facter/facts/solaris/ssh_spec.rb index 8bd423de41..5cb1772f42 100644 --- a/spec/facter/facts/solaris/ssh_spec.rb +++ b/spec/facter/facts/solaris/ssh_spec.rb @@ -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 diff --git a/spec/facter/resolvers/ssh_resolver_spec.rb b/spec/facter/resolvers/ssh_resolver_spec.rb index ef1931c7f1..fff9950dd9 100644 --- a/spec/facter/resolvers/ssh_resolver_spec.rb +++ b/spec/facter/resolvers/ssh_resolver_spec.rb @@ -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