From 016811658a536f5c9c90d53e56fd7260f470bb8d Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Fri, 17 May 2024 16:15:13 -0700 Subject: [PATCH 1/8] (FACT-3468) Create Log instance in the base resolver's init_resolver method Previously, some resolvers eagerly created a Log, while others relied on the BaseResolver to lazily create it. Now eagerly create the Log in the BaseResolver's init_resolver method, which all derived resolvers already call. This preserves the behavior where the derived class' name is printed in the log message: DEBUG Facter::Resolvers::LsbRelease - Command lsb_release ... This is because `Log.new(self)` refers to the derived resolver class, e.g. Facter::Resolvers::LsbRelease, not the BaseResolver. The base_resolver_spec tests were modified because they were testing that Facter::Log.new was called a certain way. Instead test the behavior that `BaseResolver.log` method returns an instance of Facter::Log and calling the method multiple times returns the same object. --- lib/facter/resolvers/base_resolver.rb | 5 +++-- lib/facter/resolvers/bsd/processors.rb | 1 - lib/facter/resolvers/disks.rb | 2 -- lib/facter/resolvers/dmi.rb | 2 -- lib/facter/resolvers/filesystems.rb | 2 -- lib/facter/resolvers/fips_enabled.rb | 2 -- lib/facter/resolvers/freebsd/processors.rb | 1 - lib/facter/resolvers/identity.rb | 2 -- lib/facter/resolvers/macosx/mountpoints.rb | 2 -- lib/facter/resolvers/memory.rb | 2 -- lib/facter/resolvers/mountpoints.rb | 2 -- lib/facter/resolvers/processors.rb | 2 -- lib/facter/resolvers/solaris/mountpoints.rb | 2 -- lib/facter/resolvers/solaris/networking.rb | 1 - lib/facter/resolvers/ssh.rb | 2 -- lib/facter/resolvers/uname.rb | 2 -- lib/facter/resolvers/windows/dmi_bios.rb | 1 - .../resolvers/windows/dmi_computersystem.rb | 1 - .../windows/hardware_architecture.rb | 1 - lib/facter/resolvers/windows/identity.rb | 1 - lib/facter/resolvers/windows/kernel.rb | 2 -- lib/facter/resolvers/windows/memory.rb | 2 -- lib/facter/resolvers/windows/networking.rb | 1 - lib/facter/resolvers/windows/ssh.rb | 2 -- lib/facter/resolvers/windows/system32.rb | 2 -- lib/facter/resolvers/windows/timezone.rb | 1 - lib/facter/resolvers/windows/uptime.rb | 2 -- .../resolvers/windows/virtualization.rb | 2 -- .../resolvers/windows/win_os_description.rb | 2 -- spec/facter/resolvers/base_resolver_spec.rb | 22 +++++-------------- 30 files changed, 9 insertions(+), 65 deletions(-) diff --git a/lib/facter/resolvers/base_resolver.rb b/lib/facter/resolvers/base_resolver.rb index 7b67fed0e2..4bac50597c 100644 --- a/lib/facter/resolvers/base_resolver.rb +++ b/lib/facter/resolvers/base_resolver.rb @@ -3,8 +3,8 @@ module Facter module Resolvers class BaseResolver - def self.log - @log ||= Log.new(self) + class << self + attr_reader :log end def self.invalidate_cache @@ -14,6 +14,7 @@ def self.invalidate_cache def self.init_resolver @fact_list = {} @semaphore = Mutex.new + @log = Log.new(self) end def self.subscribe_to_manager diff --git a/lib/facter/resolvers/bsd/processors.rb b/lib/facter/resolvers/bsd/processors.rb index 8922784687..7a0f5935a2 100644 --- a/lib/facter/resolvers/bsd/processors.rb +++ b/lib/facter/resolvers/bsd/processors.rb @@ -5,7 +5,6 @@ module Resolvers module Bsd class Processors < BaseResolver init_resolver - @log = Facter::Log.new(self) class << self private diff --git a/lib/facter/resolvers/disks.rb b/lib/facter/resolvers/disks.rb index c184b87d35..eb824cad8d 100644 --- a/lib/facter/resolvers/disks.rb +++ b/lib/facter/resolvers/disks.rb @@ -4,8 +4,6 @@ module Facter module Resolvers module Linux class Disks < BaseResolver - @log = Facter::Log.new(self) - init_resolver DIR = '/sys/block' diff --git a/lib/facter/resolvers/dmi.rb b/lib/facter/resolvers/dmi.rb index da12403b8a..d27e40111f 100644 --- a/lib/facter/resolvers/dmi.rb +++ b/lib/facter/resolvers/dmi.rb @@ -4,8 +4,6 @@ module Facter module Resolvers module Linux class DmiBios < BaseResolver - @log = Facter::Log.new(self) - init_resolver class << self diff --git a/lib/facter/resolvers/filesystems.rb b/lib/facter/resolvers/filesystems.rb index 74edbe20dd..e381b61d5c 100644 --- a/lib/facter/resolvers/filesystems.rb +++ b/lib/facter/resolvers/filesystems.rb @@ -8,8 +8,6 @@ class Filesystems < BaseResolver init_resolver - @log = Facter::Log.new(self) - class << self private diff --git a/lib/facter/resolvers/fips_enabled.rb b/lib/facter/resolvers/fips_enabled.rb index fe45e8b49f..9898b51c1e 100644 --- a/lib/facter/resolvers/fips_enabled.rb +++ b/lib/facter/resolvers/fips_enabled.rb @@ -8,8 +8,6 @@ class FipsEnabled < BaseResolver init_resolver - @log = Facter::Log.new(self) - class << self private diff --git a/lib/facter/resolvers/freebsd/processors.rb b/lib/facter/resolvers/freebsd/processors.rb index 2fa5370103..d12e5f0fc5 100644 --- a/lib/facter/resolvers/freebsd/processors.rb +++ b/lib/facter/resolvers/freebsd/processors.rb @@ -7,7 +7,6 @@ module Resolvers module Freebsd class Processors < BaseResolver init_resolver - @log = Facter::Log.new(self) class << self private diff --git a/lib/facter/resolvers/identity.rb b/lib/facter/resolvers/identity.rb index c0c210baf3..e4a7d97c00 100644 --- a/lib/facter/resolvers/identity.rb +++ b/lib/facter/resolvers/identity.rb @@ -3,8 +3,6 @@ module Facter module Resolvers class PosxIdentity < BaseResolver - @log = Facter::Log.new(self) - init_resolver class << self diff --git a/lib/facter/resolvers/macosx/mountpoints.rb b/lib/facter/resolvers/macosx/mountpoints.rb index b589b8f500..89def9e2a3 100644 --- a/lib/facter/resolvers/macosx/mountpoints.rb +++ b/lib/facter/resolvers/macosx/mountpoints.rb @@ -7,8 +7,6 @@ class Mountpoints < BaseResolver include Facter::Util::Resolvers::FilesystemHelper init_resolver - @log = Facter::Log.new(self) - class << self private diff --git a/lib/facter/resolvers/memory.rb b/lib/facter/resolvers/memory.rb index 8b1ada8d02..76c954583b 100644 --- a/lib/facter/resolvers/memory.rb +++ b/lib/facter/resolvers/memory.rb @@ -6,8 +6,6 @@ module Linux class Memory < BaseResolver init_resolver - @log = Facter::Log.new(self) - class << self private diff --git a/lib/facter/resolvers/mountpoints.rb b/lib/facter/resolvers/mountpoints.rb index fa22e3550b..c681c5fff7 100644 --- a/lib/facter/resolvers/mountpoints.rb +++ b/lib/facter/resolvers/mountpoints.rb @@ -7,8 +7,6 @@ class Mountpoints < BaseResolver init_resolver - @log = Facter::Log.new(self) - class << self private diff --git a/lib/facter/resolvers/processors.rb b/lib/facter/resolvers/processors.rb index f89ce56fc3..20aa58af0b 100644 --- a/lib/facter/resolvers/processors.rb +++ b/lib/facter/resolvers/processors.rb @@ -4,8 +4,6 @@ module Facter module Resolvers module Linux class Processors < BaseResolver - @log = Facter::Log.new(self) - init_resolver MHZ_TO_HZ = 1_000_000 diff --git a/lib/facter/resolvers/solaris/mountpoints.rb b/lib/facter/resolvers/solaris/mountpoints.rb index 4a38c1b758..da4365a588 100644 --- a/lib/facter/resolvers/solaris/mountpoints.rb +++ b/lib/facter/resolvers/solaris/mountpoints.rb @@ -7,8 +7,6 @@ class Mountpoints < BaseResolver include Facter::Util::Resolvers::FilesystemHelper init_resolver - @log = Facter::Log.new(self) - class << self private diff --git a/lib/facter/resolvers/solaris/networking.rb b/lib/facter/resolvers/solaris/networking.rb index 82ea3a3d22..ad6e1c1293 100644 --- a/lib/facter/resolvers/solaris/networking.rb +++ b/lib/facter/resolvers/solaris/networking.rb @@ -6,7 +6,6 @@ module Resolvers module Solaris class Networking < BaseResolver init_resolver - @log = Facter::Log.new(self) class << self private diff --git a/lib/facter/resolvers/ssh.rb b/lib/facter/resolvers/ssh.rb index 2535c6d2ad..55ccecf881 100644 --- a/lib/facter/resolvers/ssh.rb +++ b/lib/facter/resolvers/ssh.rb @@ -3,8 +3,6 @@ module Facter module Resolvers class Ssh < BaseResolver - @log = Facter::Log.new(self) - init_resolver FILE_NAMES = %w[ssh_host_rsa_key.pub ssh_host_dsa_key.pub ssh_host_ecdsa_key.pub ssh_host_ed25519_key.pub].freeze diff --git a/lib/facter/resolvers/uname.rb b/lib/facter/resolvers/uname.rb index 8213dcb9fc..49dd3809c5 100644 --- a/lib/facter/resolvers/uname.rb +++ b/lib/facter/resolvers/uname.rb @@ -3,8 +3,6 @@ module Facter module Resolvers class Uname < BaseResolver - @log = Facter::Log.new(self) - init_resolver class << self diff --git a/lib/facter/resolvers/windows/dmi_bios.rb b/lib/facter/resolvers/windows/dmi_bios.rb index 90abf45c7c..e435a0f198 100644 --- a/lib/facter/resolvers/windows/dmi_bios.rb +++ b/lib/facter/resolvers/windows/dmi_bios.rb @@ -3,7 +3,6 @@ module Facter module Resolvers class DMIBios < BaseResolver - @log = Facter::Log.new(self) init_resolver class << self diff --git a/lib/facter/resolvers/windows/dmi_computersystem.rb b/lib/facter/resolvers/windows/dmi_computersystem.rb index 6c4ed0845c..7d5040b324 100644 --- a/lib/facter/resolvers/windows/dmi_computersystem.rb +++ b/lib/facter/resolvers/windows/dmi_computersystem.rb @@ -3,7 +3,6 @@ module Facter module Resolvers class DMIComputerSystem < BaseResolver - @log = Facter::Log.new(self) init_resolver class << self diff --git a/lib/facter/resolvers/windows/hardware_architecture.rb b/lib/facter/resolvers/windows/hardware_architecture.rb index 9d0d9e6607..a02f8790fc 100644 --- a/lib/facter/resolvers/windows/hardware_architecture.rb +++ b/lib/facter/resolvers/windows/hardware_architecture.rb @@ -24,7 +24,6 @@ def read_hardware_information(fact_name) build_facts_list(hardware: hard, architecture: arch) @fact_list[fact_name] rescue LoadError => e - log = Facter::Log.new(self) log.debug("The ffi gem has not been installed: #{e}") end diff --git a/lib/facter/resolvers/windows/identity.rb b/lib/facter/resolvers/windows/identity.rb index 3d9d08f530..ac3b43d3f9 100644 --- a/lib/facter/resolvers/windows/identity.rb +++ b/lib/facter/resolvers/windows/identity.rb @@ -4,7 +4,6 @@ module Facter module Resolvers class Identity < BaseResolver NAME_SAM_COMPATIBLE = 2 - @log = Facter::Log.new(self) init_resolver diff --git a/lib/facter/resolvers/windows/kernel.rb b/lib/facter/resolvers/windows/kernel.rb index 6949c9fd84..2f7f3e04c3 100644 --- a/lib/facter/resolvers/windows/kernel.rb +++ b/lib/facter/resolvers/windows/kernel.rb @@ -3,8 +3,6 @@ module Facter module Resolvers class Kernel < BaseResolver - @log = Facter::Log.new(self) - init_resolver class << self diff --git a/lib/facter/resolvers/windows/memory.rb b/lib/facter/resolvers/windows/memory.rb index 51f71a8061..bd3608b6ae 100644 --- a/lib/facter/resolvers/windows/memory.rb +++ b/lib/facter/resolvers/windows/memory.rb @@ -3,8 +3,6 @@ module Facter module Resolvers class Memory < BaseResolver - @log = Facter::Log.new(self) - init_resolver class << self diff --git a/lib/facter/resolvers/windows/networking.rb b/lib/facter/resolvers/windows/networking.rb index 3c6cc22804..6c2c5bfe65 100644 --- a/lib/facter/resolvers/windows/networking.rb +++ b/lib/facter/resolvers/windows/networking.rb @@ -4,7 +4,6 @@ module Facter module Resolvers module Windows class Networking < BaseResolver - @log = Facter::Log.new(self) init_resolver class << self diff --git a/lib/facter/resolvers/windows/ssh.rb b/lib/facter/resolvers/windows/ssh.rb index 96753d06cf..6547373ada 100644 --- a/lib/facter/resolvers/windows/ssh.rb +++ b/lib/facter/resolvers/windows/ssh.rb @@ -4,8 +4,6 @@ module Facter module Resolvers module Windows class Ssh < BaseResolver - @log = Facter::Log.new(self) - init_resolver FILE_NAMES = %w[ssh_host_rsa_key.pub ssh_host_dsa_key.pub diff --git a/lib/facter/resolvers/windows/system32.rb b/lib/facter/resolvers/windows/system32.rb index 355bd0c8e6..d627c37f53 100644 --- a/lib/facter/resolvers/windows/system32.rb +++ b/lib/facter/resolvers/windows/system32.rb @@ -3,8 +3,6 @@ module Facter module Resolvers class System32 < BaseResolver - @log = Facter::Log.new(self) - init_resolver class << self diff --git a/lib/facter/resolvers/windows/timezone.rb b/lib/facter/resolvers/windows/timezone.rb index f21a4481c6..0c5a16d5ed 100644 --- a/lib/facter/resolvers/windows/timezone.rb +++ b/lib/facter/resolvers/windows/timezone.rb @@ -34,7 +34,6 @@ def codepage_from_api require_relative '../../../facter/resolvers/windows/ffi/winnls_ffi' WinnlsFFI.GetACP.to_s rescue LoadError => e - log = Facter::Log.new(self) log.debug("Could not retrieve codepage: #{e}") end end diff --git a/lib/facter/resolvers/windows/uptime.rb b/lib/facter/resolvers/windows/uptime.rb index d27a563839..6e3391e471 100644 --- a/lib/facter/resolvers/windows/uptime.rb +++ b/lib/facter/resolvers/windows/uptime.rb @@ -6,8 +6,6 @@ module Facter module Resolvers module Windows class Uptime < BaseResolver - @log = Facter::Log.new(self) - init_resolver class << self diff --git a/lib/facter/resolvers/windows/virtualization.rb b/lib/facter/resolvers/windows/virtualization.rb index 390518f8ff..e955028cf1 100644 --- a/lib/facter/resolvers/windows/virtualization.rb +++ b/lib/facter/resolvers/windows/virtualization.rb @@ -4,8 +4,6 @@ module Facter module Resolvers module Windows class Virtualization < BaseResolver - @log = Facter::Log.new(self) - init_resolver class << self diff --git a/lib/facter/resolvers/windows/win_os_description.rb b/lib/facter/resolvers/windows/win_os_description.rb index 9ff0b03873..e16337293b 100644 --- a/lib/facter/resolvers/windows/win_os_description.rb +++ b/lib/facter/resolvers/windows/win_os_description.rb @@ -3,8 +3,6 @@ module Facter module Resolvers class WinOsDescription < BaseResolver - @log = Facter::Log.new(self) - init_resolver class << self diff --git a/spec/facter/resolvers/base_resolver_spec.rb b/spec/facter/resolvers/base_resolver_spec.rb index ae6a49c192..c09e1f4546 100644 --- a/spec/facter/resolvers/base_resolver_spec.rb +++ b/spec/facter/resolvers/base_resolver_spec.rb @@ -14,21 +14,12 @@ def self.post_resolve(fact_name, _options) end describe '#log' do - before do - allow(Facter::Log).to receive(:new).with(resolver).and_return('logger') + it 'returns the log' do + expect(resolver.log).to be_an_instance_of(Facter::Log) end - it 'initializes the log' do - resolver.log - - expect(Facter::Log).to have_received(:new).with(resolver) - end - - it 'initializes the log only once' do - resolver.log - resolver.log - - expect(Facter::Log).to have_received(:new).with(resolver).once + it 'returns the same log instance each time' do + expect(resolver.log).to be_equal(resolver.log) end end @@ -77,13 +68,12 @@ def self.post_resolve(fact_name, _options) context 'when Load Error is raised' do before do allow(resolver).to receive(:post_resolve).and_raise(LoadError) - allow(Facter::Log).to receive(:new).with(resolver).and_return(instance_double(Facter::Log, error: nil)) end it 'logs the Load Error exception at the error level' do - resolver.resolve(fact) + expect(resolver.log).to receive(:error).with(/Resolving fact #{fact}, but got LoadError/) - expect(resolver.log).to have_received(:error).with(/Resolving fact #{fact}, but got LoadError/) + resolver.resolve(fact) end it 'sets the fact to nil' do From 2cfe64e667349ed5b3489a21e4431a10d36daef1 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Fri, 17 May 2024 17:50:08 -0700 Subject: [PATCH 2/8] (FACT-3468) Don't mutate resolver global state Previously, many tests set an instance_spy logger on their resolver's `log` class variable. But only some of those tests cleared the instance_spy at the end. Therefore, the spy could leak from one test to another, see where one case was fixed in 06b1d5b12. To prevent that from happening again, just let the resolver use its log. Since we're not using instance spies, we have to set the expectation on the log before calling the method that causes the log to be written to. One motivation for installing a spy is to prevent messages from being written to the terminal. But this accomplishes the same thing but installing a global Logger in spec_helper that just writes to a StringIO in memory. --- spec/facter/facts/freebsd/virtual_spec.rb | 2 -- spec/facter/facts/linux/virtual_spec.rb | 2 -- spec/facter/facts/openbsd/virtual_spec.rb | 2 -- .../facter/facts/openbsd/virtual_spec.rb.save | 2 -- spec/facter/resolvers/aix/disks_spec.rb | 9 ++---- spec/facter/resolvers/aix/memory_spec.rb | 7 ++-- spec/facter/resolvers/aix/mountpoints_spec.rb | 6 ++-- spec/facter/resolvers/aix/networking_spec.rb | 9 +++--- spec/facter/resolvers/aix/os_level_spec.rb | 5 +-- spec/facter/resolvers/aix/partitions_spec.rb | 6 ++-- spec/facter/resolvers/aix/processors_spec.rb | 2 -- .../resolvers/amzn/os_release_rpm_spec.rb | 5 +-- spec/facter/resolvers/augeas_spec.rb | 13 ++++---- spec/facter/resolvers/az_spec.rb | 2 -- spec/facter/resolvers/bsd/processors_spec.rb | 3 -- spec/facter/resolvers/ec2_spec.rb | 3 -- .../resolvers/freebsd/processors_spec.rb | 3 -- .../resolvers/freebsd/swap_memory_spec.rb | 4 +-- .../resolvers/freebsd/system_memory_spec.rb | 2 -- spec/facter/resolvers/hostname_spec.rb | 3 -- .../resolvers/linux/docker_uptime_spec.rb | 16 +++------- spec/facter/resolvers/linux/hostname_spec.rb | 15 +++++---- spec/facter/resolvers/linux/lscpu_spec.rb | 4 +-- .../facter/resolvers/linux/networking_spec.rb | 32 +++++++++---------- spec/facter/resolvers/lpar_spec.rb | 5 +-- spec/facter/resolvers/lspci_spec.rb | 5 +-- spec/facter/resolvers/macosx/dmi_spec.rb | 4 +-- .../resolvers/macosx/filesystems_spec.rb | 5 +-- .../resolvers/macosx/load_averages_spec.rb | 4 +-- .../resolvers/macosx/processors_spec.rb | 5 +-- .../resolvers/macosx/swap_memory_spec.rb | 4 +-- .../resolvers/macosx/system_memory_spec.rb | 6 ++-- spec/facter/resolvers/mountpoints_spec.rb | 21 +++--------- spec/facter/resolvers/networking_spec.rb | 13 +++----- .../resolvers/openbsd/mountpoints_spec.rb | 6 ++-- spec/facter/resolvers/partitions_spec.rb | 6 ++-- spec/facter/resolvers/redhat_release_spec.rb | 2 -- spec/facter/resolvers/selinux_spec.rb | 5 +-- .../resolvers/solaris/dmi_sparc_spec.rb | 12 ++----- spec/facter/resolvers/solaris/dmi_spec.rb | 9 ++---- .../resolvers/solaris/filesystem_spec.rb | 4 +-- .../resolvers/solaris/ipaddress_spec.rb | 7 ++-- spec/facter/resolvers/solaris/ldom_spec.rb | 5 +-- spec/facter/resolvers/solaris/memory_spec.rb | 9 ++---- .../resolvers/solaris/zone_name_spec.rb | 4 +-- spec/facter/resolvers/solaris/zone_spec.rb | 10 +++--- spec/facter/resolvers/suse_release_spec.rb | 2 -- spec/facter/resolvers/sw_vers_spec.rb | 8 ++--- spec/facter/resolvers/system_profile_spec.rb | 6 ---- spec/facter/resolvers/uname_spec.rb | 6 +--- spec/facter/resolvers/virt_what_spec.rb | 5 +-- spec/facter/resolvers/vmware_spec.rb | 5 +-- .../windows/aio_agent_version_spec.rb | 15 ++++----- .../facter/resolvers/windows/dmi_bios_spec.rb | 5 +-- .../windows/dmi_computersystem_spec.rb | 6 +--- .../facter/resolvers/windows/identity_spec.rb | 13 +++----- spec/facter/resolvers/windows/kernel_spec.rb | 6 +--- spec/facter/resolvers/windows/memory_spec.rb | 12 +++---- .../resolvers/windows/networking_spec.rb | 9 +++--- .../resolvers/windows/processors_spec.rb | 9 ++---- .../facter/resolvers/windows/system32_spec.rb | 12 +++---- spec/facter/resolvers/windows/uptime_spec.rb | 14 +++----- .../resolvers/windows/virtualization_spec.rb | 4 +-- .../windows/win_os_description_spec.rb | 5 +-- spec/facter/resolvers/wpar_spec.rb | 5 +-- spec/facter/resolvers/xen_spec.rb | 4 +-- spec/facter/resolvers/zfs_spec.rb | 5 +-- spec/facter/resolvers/zpool_spec.rb | 5 +-- spec/facter/util/aix/odm_query_spec.rb | 6 ++-- .../util/facts/posix/virtual_detector_spec.rb | 3 -- spec/facter/util/facts/uptime_parser_spec.rb | 17 ++++------ spec/facter/util/linux/dhcp_spec.rb | 2 +- spec/facter/util/linux/routing_table_spec.rb | 4 +-- spec/facter/util/linux/socket_parser_spec.rb | 8 +++-- spec/facter/util/resolvers/http_spec.rb | 21 +++++++----- .../facter/util/windows/network_utils_spec.rb | 6 ++-- spec/spec_helper.rb | 4 +++ spec_integration/facter_resolve_spec.rb | 27 +++++++++------- 78 files changed, 190 insertions(+), 377 deletions(-) diff --git a/spec/facter/facts/freebsd/virtual_spec.rb b/spec/facter/facts/freebsd/virtual_spec.rb index cf35314678..360dfb278f 100644 --- a/spec/facter/facts/freebsd/virtual_spec.rb +++ b/spec/facter/facts/freebsd/virtual_spec.rb @@ -5,10 +5,8 @@ subject(:fact) { Facts::Freebsd::Virtual.new } let(:virtual_detector_double) { class_spy(Facter::Util::Facts::Posix::VirtualDetector) } - let(:log_spy) { instance_spy(Facter::Log) } before do - allow(Facter::Log).to receive(:new).and_return(log_spy) allow(Facter::Util::Facts::Posix::VirtualDetector).to receive(:platform).and_return(value) end diff --git a/spec/facter/facts/linux/virtual_spec.rb b/spec/facter/facts/linux/virtual_spec.rb index b6a41cf3d9..9e07ffa6c7 100644 --- a/spec/facter/facts/linux/virtual_spec.rb +++ b/spec/facter/facts/linux/virtual_spec.rb @@ -5,10 +5,8 @@ subject(:fact) { Facts::Linux::Virtual.new } let(:virtual_detector_double) { class_spy(Facter::Util::Facts::Posix::VirtualDetector) } - let(:log_spy) { instance_spy(Facter::Log) } before do - allow(Facter::Log).to receive(:new).and_return(log_spy) allow(Facter::Util::Facts::Posix::VirtualDetector).to receive(:platform).and_return(value) end diff --git a/spec/facter/facts/openbsd/virtual_spec.rb b/spec/facter/facts/openbsd/virtual_spec.rb index 70dd50f93f..3323bb7b72 100644 --- a/spec/facter/facts/openbsd/virtual_spec.rb +++ b/spec/facter/facts/openbsd/virtual_spec.rb @@ -5,10 +5,8 @@ subject(:fact) { Facts::Openbsd::Virtual.new } let(:virtual_detector_double) { class_spy(Facter::Util::Facts::Posix::VirtualDetector) } - let(:log_spy) { instance_spy(Facter::Log) } before do - allow(Facter::Log).to receive(:new).and_return(log_spy) allow(Facter::Util::Facts::Posix::VirtualDetector).to receive(:platform).and_return(value) end diff --git a/spec/facter/facts/openbsd/virtual_spec.rb.save b/spec/facter/facts/openbsd/virtual_spec.rb.save index 70dd50f93f..3323bb7b72 100644 --- a/spec/facter/facts/openbsd/virtual_spec.rb.save +++ b/spec/facter/facts/openbsd/virtual_spec.rb.save @@ -5,10 +5,8 @@ describe Facts::Openbsd::Virtual do subject(:fact) { Facts::Openbsd::Virtual.new } let(:virtual_detector_double) { class_spy(Facter::Util::Facts::Posix::VirtualDetector) } - let(:log_spy) { instance_spy(Facter::Log) } before do - allow(Facter::Log).to receive(:new).and_return(log_spy) allow(Facter::Util::Facts::Posix::VirtualDetector).to receive(:platform).and_return(value) end diff --git a/spec/facter/resolvers/aix/disks_spec.rb b/spec/facter/resolvers/aix/disks_spec.rb index 2b7b465dd7..b8d7e931ee 100644 --- a/spec/facter/resolvers/aix/disks_spec.rb +++ b/spec/facter/resolvers/aix/disks_spec.rb @@ -3,12 +3,9 @@ describe Facter::Resolvers::Aix::Disks do subject(:resolver) { Facter::Resolvers::Aix::Disks } - let(:logger_spy) { instance_spy(Facter::Log) } - before do - resolver.instance_variable_set(:@log, logger_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('lspv', { logger: logger_spy }) + .with('lspv', logger: an_instance_of(Facter::Log)) .and_return(result) end @@ -33,7 +30,7 @@ before do allow(Facter::Core::Execution).to receive(:execute) - .with('lspv hdisk0', { logger: logger_spy }) + .with('lspv hdisk0', logger: an_instance_of(Facter::Log)) .and_return(load_fixture('lspv_disk_output').read) end @@ -44,7 +41,7 @@ context 'when second lspv call fails' do before do allow(Facter::Core::Execution).to receive(:execute) - .with('lspv hdisk0', { logger: logger_spy }) + .with('lspv hdisk0', logger: an_instance_of(Facter::Log)) .and_return('') end diff --git a/spec/facter/resolvers/aix/memory_spec.rb b/spec/facter/resolvers/aix/memory_spec.rb index 31b378bc7b..c0e7202888 100644 --- a/spec/facter/resolvers/aix/memory_spec.rb +++ b/spec/facter/resolvers/aix/memory_spec.rb @@ -3,15 +3,12 @@ describe Facter::Resolvers::Aix::Memory do subject(:resolver) { Facter::Resolvers::Aix::Memory } - let(:log_spy) { instance_spy(Facter::Log) } - before do - resolver.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('svmon', { logger: log_spy }) + .with('svmon', logger: an_instance_of(Facter::Log)) .and_return(svmon_content) allow(Facter::Core::Execution).to receive(:execute) - .with('pagesize', { logger: log_spy }) + .with('pagesize', logger: an_instance_of(Facter::Log)) .and_return(pagesize_content) end diff --git a/spec/facter/resolvers/aix/mountpoints_spec.rb b/spec/facter/resolvers/aix/mountpoints_spec.rb index 2e4229a608..05aa4afbec 100644 --- a/spec/facter/resolvers/aix/mountpoints_spec.rb +++ b/spec/facter/resolvers/aix/mountpoints_spec.rb @@ -16,15 +16,13 @@ device: '/var/share', filesystem: 'nfs3', options: [], size: '68.50 GiB', size_bytes: 73_549_217_792, used: '4.93 GiB', used_bytes: 5_295_804_416 } } end - let(:log_spy) { instance_spy(Facter::Log) } before do - Facter::Resolvers::Aix::Mountpoints.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('mount', { logger: log_spy }) + .with('mount', logger: an_instance_of(Facter::Log)) .and_return(load_fixture('mount').read) allow(Facter::Core::Execution).to receive(:execute) - .with('df -P', { logger: log_spy }) + .with('df -P', logger: an_instance_of(Facter::Log)) .and_return(load_fixture('df').read) end diff --git a/spec/facter/resolvers/aix/networking_spec.rb b/spec/facter/resolvers/aix/networking_spec.rb index 927df04325..003d853595 100644 --- a/spec/facter/resolvers/aix/networking_spec.rb +++ b/spec/facter/resolvers/aix/networking_spec.rb @@ -3,7 +3,6 @@ describe Facter::Resolvers::Aix::Networking do subject(:networking_resolver) { Facter::Resolvers::Aix::Networking } - let(:log_spy) { instance_spy(Facter::Log) } let(:ffi_interfaces) do { 'en0' => { bindings: [{ address: '10.32.77.1', netmask: '255.255.255.0', network: '10.32.77.0' }] }, @@ -13,13 +12,12 @@ end before do - networking_resolver.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('netstat -rn', { logger: log_spy }) + .with('netstat -rn', logger: an_instance_of(Facter::Log)) .and_return(netstat_rn) allow(Facter::Core::Execution).to receive(:execute) - .with('netstat -in', { logger: log_spy }) + .with('netstat -in', logger: an_instance_of(Facter::Log)) .and_return(netstat_in) allow(Facter::Resolvers::Aix::FfiHelper).to receive(:read_interfaces).and_return(ffi_interfaces) @@ -93,8 +91,9 @@ end it 'returns a nil MTU' do + expect(networking_resolver.log).not_to receive(:error).with(/undefined method/) + expect(networking_resolver.resolve(:mtu)).to be_nil - expect(log_spy).not_to have_received(:debug).with(/undefined method/) end end end diff --git a/spec/facter/resolvers/aix/os_level_spec.rb b/spec/facter/resolvers/aix/os_level_spec.rb index 3c340e32a8..9834aa5091 100644 --- a/spec/facter/resolvers/aix/os_level_spec.rb +++ b/spec/facter/resolvers/aix/os_level_spec.rb @@ -3,12 +3,9 @@ describe Facter::Resolvers::Aix::OsLevel do subject(:os_level) { Facter::Resolvers::Aix::OsLevel } - let(:log_spy) { instance_spy(Facter::Log) } - before do - os_level.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('/usr/bin/oslevel -s', { logger: log_spy }) + .with('/usr/bin/oslevel -s', logger: an_instance_of(Facter::Log)) .and_return(output) end diff --git a/spec/facter/resolvers/aix/partitions_spec.rb b/spec/facter/resolvers/aix/partitions_spec.rb index 764c5c3df1..3877fa4d58 100644 --- a/spec/facter/resolvers/aix/partitions_spec.rb +++ b/spec/facter/resolvers/aix/partitions_spec.rb @@ -4,10 +4,8 @@ subject(:resolver) { Facter::Resolvers::Aix::Partitions } let(:odm_query_spy) { instance_spy(Facter::Util::Aix::ODMQuery) } - let(:logger_spy) { instance_spy(Facter::Log) } before do - resolver.instance_variable_set(:@log, logger_spy) allow(Facter::Util::Aix::ODMQuery).to receive(:new).and_return(odm_query_spy) allow(odm_query_spy).to receive(:equals).with('PdDvLn', 'logical_volume/lvsubclass/lvtype') allow(odm_query_spy).to receive(:execute).and_return(result) @@ -38,10 +36,10 @@ before do allow(Facter::Core::Execution).to receive(:execute) - .with('lslv -L hd5', { logger: logger_spy }) + .with('lslv -L hd5', logger: an_instance_of(Facter::Log)) .and_return(load_fixture('lslv_output').read) allow(Facter::Core::Execution).to receive(:execute) - .with('lslv -L hd6', { logger: logger_spy }) + .with('lslv -L hd6', logger: an_instance_of(Facter::Log)) .and_return('') end diff --git a/spec/facter/resolvers/aix/processors_spec.rb b/spec/facter/resolvers/aix/processors_spec.rb index c84634bd51..63d7073b6c 100644 --- a/spec/facter/resolvers/aix/processors_spec.rb +++ b/spec/facter/resolvers/aix/processors_spec.rb @@ -7,10 +7,8 @@ let(:odm_query_spy2) { instance_spy(Facter::Util::Aix::ODMQuery) } let(:odm_query_spy3) { instance_spy(Facter::Util::Aix::ODMQuery) } let(:odm_query_spy4) { instance_spy(Facter::Util::Aix::ODMQuery) } - let(:logger_spy) { instance_spy(Facter::Log) } before do - resolver.instance_variable_set(:@log, logger_spy) allow(Facter::Util::Aix::ODMQuery).to receive(:new).and_return(odm_query_spy, odm_query_spy2, odm_query_spy3, diff --git a/spec/facter/resolvers/amzn/os_release_rpm_spec.rb b/spec/facter/resolvers/amzn/os_release_rpm_spec.rb index be2e24d672..b7c98010db 100644 --- a/spec/facter/resolvers/amzn/os_release_rpm_spec.rb +++ b/spec/facter/resolvers/amzn/os_release_rpm_spec.rb @@ -3,12 +3,9 @@ describe Facter::Resolvers::Amzn::OsReleaseRpm do subject(:os_release_resolver) { Facter::Resolvers::Amzn::OsReleaseRpm } - let(:log_spy) { instance_spy(Facter::Log) } - before do - os_release_resolver.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with("rpm -q --qf '%s\\n%s\\n%s\\n%s' -f /etc/os-release", { logger: log_spy }) + .with("rpm -q --qf '%s\\n%s\\n%s\\n%s' -f /etc/os-release", logger: an_instance_of(Facter::Log)) .and_return(os_release_content) end diff --git a/spec/facter/resolvers/augeas_spec.rb b/spec/facter/resolvers/augeas_spec.rb index 65af91ea46..e3562dc309 100644 --- a/spec/facter/resolvers/augeas_spec.rb +++ b/spec/facter/resolvers/augeas_spec.rb @@ -3,11 +3,9 @@ describe Facter::Resolvers::Augeas do subject(:augeas) { Facter::Resolvers::Augeas } - let(:log_spy) { instance_spy(Facter::Log) } let(:readable) { false } before do - augeas.instance_variable_set(:@log, log_spy) allow(File).to receive(:readable?).with('/opt/puppetlabs/puppet/bin/augparse').and_return(readable) end @@ -18,7 +16,7 @@ context 'when augparse is installed' do before do allow(Facter::Core::Execution).to receive(:execute) - .with('augparse --version 2>&1', { logger: log_spy }) + .with('augparse --version 2>&1', logger: an_instance_of(Facter::Log)) .and_return('augparse 1.12.0 ') end @@ -32,7 +30,7 @@ before do allow(Facter::Core::Execution).to receive(:execute) - .with('/opt/puppetlabs/puppet/bin/augparse --version 2>&1', { logger: log_spy }) + .with('/opt/puppetlabs/puppet/bin/augparse --version 2>&1', logger: an_instance_of(Facter::Log)) .and_return('augparse 1.12.0 ') end @@ -44,7 +42,7 @@ context 'when augparse is not installed' do before do allow(Facter::Core::Execution).to receive(:execute) - .with('augparse --version 2>&1', { logger: log_spy }) + .with('augparse --version 2>&1', logger: an_instance_of(Facter::Log)) .and_return('sh: augparse: command not found') end @@ -78,9 +76,10 @@ end it 'rescues LoadError error and logs at debug level' do - augeas.resolve(:augeas_version) + allow(augeas.log).to receive(:debug) + expect(augeas.log).to receive(:debug).with(/Resolving fact augeas_version, but got load_error_message/) - expect(log_spy).to have_received(:debug).with(/Resolving fact augeas_version, but got load_error_message/) + augeas.resolve(:augeas_version) end end end diff --git a/spec/facter/resolvers/az_spec.rb b/spec/facter/resolvers/az_spec.rb index 8e0416976b..10b762de63 100644 --- a/spec/facter/resolvers/az_spec.rb +++ b/spec/facter/resolvers/az_spec.rb @@ -4,12 +4,10 @@ subject(:az) { Facter::Resolvers::Az } let(:uri) { 'http://169.254.169.254/metadata/instance?api-version=2020-09-01' } - let(:log_spy) { instance_spy(Facter::Log) } before do allow(Facter::Util::Resolvers::Http).to receive(:get_request) .with(uri, { Metadata: 'true' }, { session: 5 }, false).and_return(output) - az.instance_variable_set(:@log, log_spy) end after do diff --git a/spec/facter/resolvers/bsd/processors_spec.rb b/spec/facter/resolvers/bsd/processors_spec.rb index c48d5dcb25..b75ed23789 100644 --- a/spec/facter/resolvers/bsd/processors_spec.rb +++ b/spec/facter/resolvers/bsd/processors_spec.rb @@ -3,7 +3,6 @@ describe Facter::Resolvers::Bsd::Processors do subject(:resolver) { Facter::Resolvers::Bsd::Processors } - let(:log_spy) { instance_spy(Facter::Log) } let(:logicalcount) { 2 } let(:models) do ['Intel(r) Xeon(r) Gold 6138 CPU @ 2.00GHz', 'Intel(r) Xeon(r) Gold 6138 CPU @ 2.00GHz'] @@ -23,8 +22,6 @@ .to receive(:sysctl) .with(:uint32_t, [6, 12]) .and_return(2592) - - resolver.instance_variable_set(:@log, log_spy) end after do diff --git a/spec/facter/resolvers/ec2_spec.rb b/spec/facter/resolvers/ec2_spec.rb index 85d27a6106..77dc27de1a 100644 --- a/spec/facter/resolvers/ec2_spec.rb +++ b/spec/facter/resolvers/ec2_spec.rb @@ -7,17 +7,14 @@ let(:userdata_uri) { "#{base_uri}/user-data/" } let(:metadata_uri) { "#{base_uri}/meta-data/" } let(:token_uri) { "#{base_uri}/api/token" } - let(:log_spy) { instance_spy(Facter::Log) } before do - Facter::Util::Resolvers::Http.instance_variable_set(:@log, log_spy) allow(Socket).to receive(:tcp) if Gem.win_platform? end after do ec2.invalidate_cache Facter::Util::Resolvers::AwsToken.reset - Facter::Util::Resolvers::Http.instance_variable_set(:@log, nil) end shared_examples_for 'ec2' do diff --git a/spec/facter/resolvers/freebsd/processors_spec.rb b/spec/facter/resolvers/freebsd/processors_spec.rb index 5e9e887e86..4891ac3896 100644 --- a/spec/facter/resolvers/freebsd/processors_spec.rb +++ b/spec/facter/resolvers/freebsd/processors_spec.rb @@ -3,7 +3,6 @@ describe Facter::Resolvers::Freebsd::Processors do subject(:resolver) { Facter::Resolvers::Freebsd::Processors } - let(:log_spy) { instance_spy(Facter::Log) } let(:logicalcount) { 2 } let(:models) do ['Intel(r) Xeon(r) Gold 6138 CPU @ 2.00GHz', 'Intel(r) Xeon(r) Gold 6138 CPU @ 2.00GHz'] @@ -23,8 +22,6 @@ .to receive(:sysctl_by_name) .with(:uint32_t, 'hw.clockrate') .and_return(2592) - - resolver.instance_variable_set(:@log, log_spy) end after do diff --git a/spec/facter/resolvers/freebsd/swap_memory_spec.rb b/spec/facter/resolvers/freebsd/swap_memory_spec.rb index c885acd009..ee0d51594e 100644 --- a/spec/facter/resolvers/freebsd/swap_memory_spec.rb +++ b/spec/facter/resolvers/freebsd/swap_memory_spec.rb @@ -3,7 +3,6 @@ describe Facter::Resolvers::Freebsd::SwapMemory do subject(:swap_memory) { Facter::Resolvers::Freebsd::SwapMemory } - let(:log_spy) { instance_spy(Facter::Log) } let(:available_bytes) { 4_294_967_296 } let(:total_bytes) { 4_294_967_296 } let(:used_bytes) { 0 } @@ -11,9 +10,8 @@ let(:encrypted) { true } before do - swap_memory.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('swapinfo -k', { logger: log_spy }) + .with('swapinfo -k', logger: an_instance_of(Facter::Log)) .and_return(load_fixture('freebsd_swapinfo').read) end diff --git a/spec/facter/resolvers/freebsd/system_memory_spec.rb b/spec/facter/resolvers/freebsd/system_memory_spec.rb index a95471ec13..daf3b393d6 100644 --- a/spec/facter/resolvers/freebsd/system_memory_spec.rb +++ b/spec/facter/resolvers/freebsd/system_memory_spec.rb @@ -3,14 +3,12 @@ describe Facter::Resolvers::Freebsd::SystemMemory do subject(:system_memory) { Facter::Resolvers::Freebsd::SystemMemory } - let(:log_spy) { instance_spy(Facter::Log) } let(:available_bytes) { 2_696_462_336 } let(:total_bytes) { 17_043_554_304 } let(:used_bytes) { 14_347_091_968 } let(:capacity) { '84.18%' } before do - system_memory.instance_variable_set(:@log, log_spy) allow(Facter::Freebsd::FfiHelper).to receive(:sysctl_by_name) .with(:long, 'vm.stats.vm.v_page_size') .and_return(4096) diff --git a/spec/facter/resolvers/hostname_spec.rb b/spec/facter/resolvers/hostname_spec.rb index 487b8d67df..43e3ca44d3 100644 --- a/spec/facter/resolvers/hostname_spec.rb +++ b/spec/facter/resolvers/hostname_spec.rb @@ -3,11 +3,8 @@ describe Facter::Resolvers::Hostname do subject(:hostname_resolver) { Facter::Resolvers::Hostname } - let(:log_spy) { instance_spy(Facter::Log) } - describe '#resolve' do before do - hostname_resolver.instance_variable_set(:@log, log_spy) allow(Socket).to receive(:gethostname).and_return(host) allow(Facter::Util::FileHelper).to receive(:safe_read) .with('/etc/resolv.conf') diff --git a/spec/facter/resolvers/linux/docker_uptime_spec.rb b/spec/facter/resolvers/linux/docker_uptime_spec.rb index 8f63c9d749..49df673176 100644 --- a/spec/facter/resolvers/linux/docker_uptime_spec.rb +++ b/spec/facter/resolvers/linux/docker_uptime_spec.rb @@ -3,19 +3,13 @@ describe Facter::Resolvers::Linux::DockerUptime do subject(:resolver) { Facter::Resolvers::Linux::DockerUptime } - let(:log_spy) { instance_spy(Facter::Log) } - after { Facter::Resolvers::Linux::DockerUptime.invalidate_cache } - before do - resolver.instance_variable_set(:@log, log_spy) - end - context 'when the uptime is less than 1 minutes' do before do allow(Facter::Core::Execution) .to receive(:execute) - .with('ps -o etime= -p "1"', { logger: log_spy }) + .with('ps -o etime= -p "1"', logger: an_instance_of(Facter::Log)) .and_return('20') allow(Facter::Util::Resolvers::UptimeHelper) @@ -45,7 +39,7 @@ before do allow(Facter::Core::Execution) .to receive(:execute) - .with('ps -o etime= -p "1"', { logger: log_spy }) + .with('ps -o etime= -p "1"', logger: an_instance_of(Facter::Log)) .and_return('10:20') allow(Facter::Util::Resolvers::UptimeHelper) @@ -75,7 +69,7 @@ before do allow(Facter::Core::Execution) .to receive(:execute) - .with('ps -o etime= -p "1"', { logger: log_spy }) + .with('ps -o etime= -p "1"', logger: an_instance_of(Facter::Log)) .and_return('3:10:20') allow(Facter::Util::Resolvers::UptimeHelper) @@ -105,7 +99,7 @@ before do allow(Facter::Core::Execution) .to receive(:execute) - .with('ps -o etime= -p "1"', { logger: log_spy }) + .with('ps -o etime= -p "1"', logger: an_instance_of(Facter::Log)) .and_return('1-3:10:20') allow(Facter::Util::Resolvers::UptimeHelper) @@ -135,7 +129,7 @@ before do allow(Facter::Core::Execution) .to receive(:execute) - .with('ps -o etime= -p "1"', { logger: log_spy }) + .with('ps -o etime= -p "1"', logger: an_instance_of(Facter::Log)) .and_return('2-3:10:20') allow(Facter::Util::Resolvers::UptimeHelper) diff --git a/spec/facter/resolvers/linux/hostname_spec.rb b/spec/facter/resolvers/linux/hostname_spec.rb index bf30d34985..a69bdebf8e 100644 --- a/spec/facter/resolvers/linux/hostname_spec.rb +++ b/spec/facter/resolvers/linux/hostname_spec.rb @@ -3,8 +3,6 @@ describe Facter::Resolvers::Linux::Hostname do subject(:hostname_resolver) { Facter::Resolvers::Linux::Hostname } - let(:log_spy) { instance_spy(Facter::Log) } - shared_examples 'detects values' do it 'detects hostname' do expect(hostname_resolver.resolve(:hostname)).to eq(hostname) @@ -21,7 +19,6 @@ describe '#resolve' do before do - hostname_resolver.instance_variable_set(:@log, log_spy) allow(Socket).to receive(:gethostname).and_return(host) allow(Facter::Util::FileHelper).to receive(:safe_read) .with('/etc/resolv.conf') @@ -94,8 +91,10 @@ end it 'logs that ffi canot be loaded' do + allow(hostname_resolver.log).to receive(:debug) + expect(hostname_resolver.log).to receive(:debug).with('cannot load ffi') + hostname_resolver.resolve(:hostname) - expect(log_spy).to have_received(:debug).with('cannot load ffi') end it 'does not resolve hostname' do @@ -185,8 +184,10 @@ end it 'logs that ffi canot be loaded' do + allow(hostname_resolver.log).to receive(:debug) + expect(hostname_resolver.log).to receive(:debug).with('cannot load ffi') + hostname_resolver.resolve(:domain) - expect(log_spy).to have_received(:debug).with('cannot load ffi') end it_behaves_like 'detects values' @@ -229,8 +230,10 @@ end it 'logs that ffi canot be loaded' do + allow(hostname_resolver.log).to receive(:debug) + expect(hostname_resolver.log).to receive(:debug).with('cannot load ffi') + hostname_resolver.resolve(:hostname) - expect(log_spy).to have_received(:debug).with('cannot load ffi') end it_behaves_like 'detects values' diff --git a/spec/facter/resolvers/linux/lscpu_spec.rb b/spec/facter/resolvers/linux/lscpu_spec.rb index 1fb038f7ce..d44a4c26f3 100644 --- a/spec/facter/resolvers/linux/lscpu_spec.rb +++ b/spec/facter/resolvers/linux/lscpu_spec.rb @@ -2,10 +2,9 @@ describe Facter::Resolvers::Linux::Lscpu do before do - Facter::Resolvers::Linux::Lscpu.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution) .to receive(:execute) - .with("lscpu | grep -e 'Thread(s)' -e 'Core(s)'", { logger: log_spy }) + .with("lscpu | grep -e 'Thread(s)' -e 'Core(s)'", logger: an_instance_of(Facter::Log)) .and_return(lscpu_output) end @@ -13,7 +12,6 @@ Facter::Resolvers::Linux::Lscpu.invalidate_cache end - let(:log_spy) { instance_spy(Facter::Log) } let(:cores_per_socket) { 1 } let(:threads_per_core) { 2 } let(:lscpu_output) do diff --git a/spec/facter/resolvers/linux/networking_spec.rb b/spec/facter/resolvers/linux/networking_spec.rb index 73dec0a8ee..0c592bae95 100644 --- a/spec/facter/resolvers/linux/networking_spec.rb +++ b/spec/facter/resolvers/linux/networking_spec.rb @@ -3,19 +3,20 @@ describe Facter::Resolvers::Linux::Networking do subject(:networking_linux) { Facter::Resolvers::Linux::Networking } - let(:log_spy) { instance_spy(Facter::Log) } - describe '#resolve' do before do - networking_linux.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('ip link show', { logger: log_spy }).and_return(load_fixture('ip_link_show').read) + .with('ip link show', logger: an_instance_of(Facter::Log)).and_return(load_fixture('ip_link_show').read) allow(Facter::Util::Linux::SocketParser).to receive(:retrieve_interfaces) - .with(log_spy).and_return(socket_interfaces) - allow(Facter::Util::Linux::Dhcp).to receive(:dhcp).with('lo', '1', log_spy).and_return('10.32.22.9') - allow(Facter::Util::Linux::Dhcp).to receive(:dhcp).with('ens160', '2', log_spy).and_return('10.32.22.10') + .and_return(socket_interfaces) + allow(Facter::Util::Linux::Dhcp).to receive(:dhcp) + .with('lo', '1', an_instance_of(Facter::Log)) + .and_return('10.32.22.9') + allow(Facter::Util::Linux::Dhcp).to receive(:dhcp) + .with('ens160', '2', an_instance_of(Facter::Log)) + .and_return('10.32.22.10') allow(Facter::Util::Linux::RoutingTable).to receive(:read_routing_table) - .with(log_spy).and_return([[{ interface: 'ens192', ip: '10.16.125.217' }], []]) + .and_return([[{ interface: 'ens192', ip: '10.16.125.217' }], []]) allow(Facter::Util::Resolvers::Networking::PrimaryInterface).to receive(:read_from_proc_route) .and_return('ens160') allow(File).to receive(:exist?).and_call_original @@ -123,8 +124,7 @@ networking_linux.resolve(:interfaces) networking_linux.resolve(:interfaces) - expect(Facter::Util::Linux::SocketParser).to have_received(:retrieve_interfaces) - .with(log_spy).once + expect(Facter::Util::Linux::SocketParser).to have_received(:retrieve_interfaces).once end end @@ -134,8 +134,7 @@ networking_linux.invalidate_cache networking_linux.resolve(:interfaces) - expect(Facter::Util::Linux::SocketParser).to have_received(:retrieve_interfaces) - .with(log_spy).twice + expect(Facter::Util::Linux::SocketParser).to have_received(:retrieve_interfaces).twice end end @@ -190,7 +189,7 @@ context 'when dhcp is not available on the os' do before do - allow(Facter::Util::Linux::Dhcp).to receive(:dhcp).with('lo', '1', log_spy).and_return(nil) + allow(Facter::Util::Linux::Dhcp).to receive(:dhcp).with('lo', '1', an_instance_of(Facter::Log)).and_return(nil) end it 'does not add dhcp to lo interface' do @@ -203,7 +202,7 @@ context 'when ip route show finds an IP, Socket lib did not retrieve' do before do allow(Facter::Util::Linux::RoutingTable).to receive(:read_routing_table) - .with(log_spy).and_return([[{ interface: 'ens160', ip: '10.16.125.217' }], []]) + .and_return([[{ interface: 'ens160', ip: '10.16.125.217' }], []]) end let(:socket_interfaces) do @@ -241,7 +240,6 @@ context 'when asking for primary interface' do before do - Facter::Util::Resolvers::Networking::PrimaryInterface.instance_variable_set(:@log, log_spy) allow(Facter::Util::Resolvers::Networking::PrimaryInterface).to receive(:read_from_proc_route).and_return(nil) end @@ -321,8 +319,8 @@ before do allow(Facter::Util::Resolvers::Networking::PrimaryInterface).to receive(:read_from_proc_route).and_return(nil) allow(Facter::Util::Resolvers::Networking::PrimaryInterface).to receive(:read_from_ip_route).and_return(nil) - allow(Facter::Util::Linux::Dhcp).to receive(:dhcp).with('lo', '1', log_spy).and_return(nil) - allow(Facter::Util::Linux::Dhcp).to receive(:dhcp).with('ens160', '2', log_spy).and_return(nil) + allow(Facter::Util::Linux::Dhcp).to receive(:dhcp).with('lo', '1', an_instance_of(Facter::Log)).and_return(nil) + allow(Facter::Util::Linux::Dhcp).to receive(:dhcp).with('ens160', '2', an_instance_of(Facter::Log)).and_return(nil) allow(File).to receive(:exist?).with('/proc/net/if_inet6').and_return(true) allow(Facter::Util::FileHelper).to receive(:safe_read) .with('/proc/net/if_inet6', nil).and_return(load_fixture('proc_net_if_inet6').read) diff --git a/spec/facter/resolvers/lpar_spec.rb b/spec/facter/resolvers/lpar_spec.rb index 16f8b68eca..d8f686f0af 100644 --- a/spec/facter/resolvers/lpar_spec.rb +++ b/spec/facter/resolvers/lpar_spec.rb @@ -3,12 +3,9 @@ describe Facter::Resolvers::Lpar do subject(:lpar_resolver) { Facter::Resolvers::Lpar } - let(:log_spy) { instance_spy(Facter::Log) } - before do - lpar_resolver.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('/usr/bin/lparstat -i', { logger: log_spy }) + .with('/usr/bin/lparstat -i', logger: an_instance_of(Facter::Log)) .and_return(load_fixture('lparstat_i').read) lpar_resolver.invalidate_cache end diff --git a/spec/facter/resolvers/lspci_spec.rb b/spec/facter/resolvers/lspci_spec.rb index d29ebc043b..64e5a7f466 100644 --- a/spec/facter/resolvers/lspci_spec.rb +++ b/spec/facter/resolvers/lspci_spec.rb @@ -3,12 +3,9 @@ describe Facter::Resolvers::Lspci do subject(:lspci_resolver) { Facter::Resolvers::Lspci } - let(:log_spy) { instance_spy(Facter::Log) } - before do - lspci_resolver.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('lspci', { logger: log_spy }).and_return(output) + .with('lspci', logger: an_instance_of(Facter::Log)).and_return(output) end after do diff --git a/spec/facter/resolvers/macosx/dmi_spec.rb b/spec/facter/resolvers/macosx/dmi_spec.rb index f487176db6..8da35ddb93 100644 --- a/spec/facter/resolvers/macosx/dmi_spec.rb +++ b/spec/facter/resolvers/macosx/dmi_spec.rb @@ -3,13 +3,11 @@ describe Facter::Resolvers::Macosx::DmiBios do subject(:dmi_resolver) { Facter::Resolvers::Macosx::DmiBios } - let(:log_spy) { instance_spy(Facter::Log) } let(:macosx_model) { 'MacBookPro11,4' } before do - dmi_resolver.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('sysctl -n hw.model', { logger: log_spy }) + .with('sysctl -n hw.model', logger: an_instance_of(Facter::Log)) .and_return(macosx_model) end diff --git a/spec/facter/resolvers/macosx/filesystems_spec.rb b/spec/facter/resolvers/macosx/filesystems_spec.rb index cb90d278a6..8893be8477 100644 --- a/spec/facter/resolvers/macosx/filesystems_spec.rb +++ b/spec/facter/resolvers/macosx/filesystems_spec.rb @@ -3,12 +3,9 @@ describe Facter::Resolvers::Macosx::Filesystems do subject(:filesystems_resolver) { Facter::Resolvers::Macosx::Filesystems } - let(:log_spy) { instance_spy(Facter::Log) } - before do - filesystems_resolver.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('mount', { logger: log_spy }) + .with('mount', logger: an_instance_of(Facter::Log)) .and_return(load_fixture('macosx_filesystems').read) end diff --git a/spec/facter/resolvers/macosx/load_averages_spec.rb b/spec/facter/resolvers/macosx/load_averages_spec.rb index ebb43ad4f6..3fd9181ebc 100644 --- a/spec/facter/resolvers/macosx/load_averages_spec.rb +++ b/spec/facter/resolvers/macosx/load_averages_spec.rb @@ -3,13 +3,11 @@ describe Facter::Resolvers::Macosx::LoadAverages do subject(:load_averages) { Facter::Resolvers::Macosx::LoadAverages } - let(:log_spy) { instance_spy(Facter::Log) } let(:expected_result) { { '1m' => 0.00, '5m' => 0.03, '15m' => 0.03 } } before do - load_averages.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('sysctl -n vm.loadavg', { logger: log_spy }) + .with('sysctl -n vm.loadavg', logger: an_instance_of(Facter::Log)) .and_return('{ 0.00 0.03 0.03 }') end diff --git a/spec/facter/resolvers/macosx/processors_spec.rb b/spec/facter/resolvers/macosx/processors_spec.rb index 19cc8c50dc..9e9c60c056 100644 --- a/spec/facter/resolvers/macosx/processors_spec.rb +++ b/spec/facter/resolvers/macosx/processors_spec.rb @@ -3,7 +3,6 @@ describe Facter::Resolvers::Macosx::Processors do subject(:processors_resolver) { Facter::Resolvers::Macosx::Processors } - let(:log_spy) { instance_spy(Facter::Log) } let(:logicalcount) { 4 } let(:models) do ['Intel(R) Xeon(R) CPU E5-2697 v4 @ 2.30GHz', 'Intel(R) Xeon(R) CPU E5-2697 v4 @ 2.30GHz', @@ -15,11 +14,9 @@ let(:threads_per_core) { 1 } before do - processors_resolver.instance_variable_set(:@log, log_spy) - allow(Facter::Core::Execution) .to receive(:execute) - .with(query_string, { logger: log_spy }) + .with(query_string, logger: an_instance_of(Facter::Log)) .and_return(output) end diff --git a/spec/facter/resolvers/macosx/swap_memory_spec.rb b/spec/facter/resolvers/macosx/swap_memory_spec.rb index 2657129deb..76cee7d32c 100644 --- a/spec/facter/resolvers/macosx/swap_memory_spec.rb +++ b/spec/facter/resolvers/macosx/swap_memory_spec.rb @@ -3,7 +3,6 @@ describe Facter::Resolvers::Macosx::SwapMemory do subject(:swap_memory) { Facter::Resolvers::Macosx::SwapMemory } - let(:log_spy) { instance_spy(Facter::Log) } let(:available_bytes) { 1_729_363_968 } let(:total_bytes) { 3_221_225_472 } let(:used_bytes) { 1_491_861_504 } @@ -11,9 +10,8 @@ let(:encrypted) { true } before do - swap_memory.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('sysctl -n vm.swapusage', { logger: log_spy }) + .with('sysctl -n vm.swapusage', logger: an_instance_of(Facter::Log)) .and_return('total = 3072.00M used = 1422.75M free = 1649.25M (encrypted)') end diff --git a/spec/facter/resolvers/macosx/system_memory_spec.rb b/spec/facter/resolvers/macosx/system_memory_spec.rb index d5727d1e2e..69e20124d2 100644 --- a/spec/facter/resolvers/macosx/system_memory_spec.rb +++ b/spec/facter/resolvers/macosx/system_memory_spec.rb @@ -3,20 +3,18 @@ describe Facter::Resolvers::Macosx::SystemMemory do subject(:system_memory) { Facter::Resolvers::Macosx::SystemMemory } - let(:log_spy) { instance_spy(Facter::Log) } let(:available_bytes) { 5_590_519_808 } let(:total_bytes) { 34_359_738_368 } let(:used_bytes) { 28_769_218_560 } let(:capacity) { '83.73%' } before do - system_memory.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('sysctl -n hw.memsize', { logger: log_spy }) + .with('sysctl -n hw.memsize', logger: an_instance_of(Facter::Log)) .and_return('34359738368') allow(Facter::Core::Execution).to receive(:execute) - .with('vm_stat', { logger: log_spy }) + .with('vm_stat', logger: an_instance_of(Facter::Log)) .and_return(load_fixture('vm_stat').read) end diff --git a/spec/facter/resolvers/mountpoints_spec.rb b/spec/facter/resolvers/mountpoints_spec.rb index 177487b170..339f9df9ba 100644 --- a/spec/facter/resolvers/mountpoints_spec.rb +++ b/spec/facter/resolvers/mountpoints_spec.rb @@ -80,25 +80,18 @@ used: '0 bytes', used_bytes: 0 }] end - let(:logger) { instance_spy(Facter::Log) } before do stub_const('Sys::Filesystem::Error', StandardError) allow(Facter::Util::Resolvers::FilesystemHelper).to \ receive(:read_mountpoint_stats).with(mount.mount_point).and_raise(Sys::Filesystem::Error) - - mountpoints.instance_variable_set(:@log, logger) - end - - after do - Facter.instance_variable_set(:@logger, nil) end it 'logs into debug if it cannot receive mountpoints stats' do - Facter::Resolvers::Mountpoints.resolve(:mountpoints) - - expect(logger).to have_received(:debug) + expect(Facter::Resolvers::Mountpoints.log).to receive(:debug) .with("Could not get stats for mountpoint #{mount.mount_point}, got StandardError") + + Facter::Resolvers::Mountpoints.resolve(:mountpoints) end it 'fallbacks to default values' do @@ -133,16 +126,13 @@ end context 'when root device has partuuid' do - let(:log) { instance_spy(Facter::Log) } - before do allow(Facter::Util::FileHelper).to receive(:safe_read) .with('/proc/cmdline') .and_return(load_fixture('cmdline_root_device_partuuid').read) allow(Facter::Core::Execution).to receive(:execute) - .with('blkid', { logger: log }) + .with('blkid', logger: an_instance_of(Facter::Log)) .and_return(load_fixture('blkid_output_root_has_partuuid').read) - Facter::Resolvers::Mountpoints.instance_variable_set(:@log, log) end it 'returns the path instead of the PARTUUID' do @@ -154,9 +144,8 @@ context 'when blkid command is not available' do before do allow(Facter::Core::Execution).to receive(:execute) - .with('blkid', { logger: log }) + .with('blkid', logger: an_instance_of(Facter::Log)) .and_return('blkid: command not found') - Facter::Resolvers::Mountpoints.instance_variable_set(:@log, log) end it 'returns the partition path as PARTUUID' do diff --git a/spec/facter/resolvers/networking_spec.rb b/spec/facter/resolvers/networking_spec.rb index c7be2776cb..e9f4645e67 100644 --- a/spec/facter/resolvers/networking_spec.rb +++ b/spec/facter/resolvers/networking_spec.rb @@ -3,24 +3,21 @@ describe Facter::Resolvers::Networking do subject(:networking) { Facter::Resolvers::Networking } - let(:log_spy) { instance_spy(Facter::Log) } - describe '#resolve' do before do - networking.instance_variable_set(:@log, log_spy) allow(Facter::Util::Resolvers::Networking::PrimaryInterface) .to receive(:read_from_route) .and_return(primary) allow(Facter::Core::Execution) - .to receive(:execute).with('ifconfig -a', { logger: log_spy }).and_return(interfaces) + .to receive(:execute).with('ifconfig -a', logger: an_instance_of(Facter::Log)).and_return(interfaces) allow(Facter::Core::Execution) - .to receive(:execute).with('ipconfig getoption en0 server_identifier', { logger: log_spy }).and_return(dhcp) + .to receive(:execute).with('ipconfig getoption en0 server_identifier', logger: an_instance_of(Facter::Log)).and_return(dhcp) allow(Facter::Core::Execution) - .to receive(:execute).with('ipconfig getoption en0.1 server_identifier', { logger: log_spy }).and_return(dhcp) + .to receive(:execute).with('ipconfig getoption en0.1 server_identifier', logger: an_instance_of(Facter::Log)).and_return(dhcp) allow(Facter::Core::Execution) - .to receive(:execute).with('ipconfig getoption llw0 server_identifier', { logger: log_spy }).and_return('') + .to receive(:execute).with('ipconfig getoption llw0 server_identifier', logger: an_instance_of(Facter::Log)).and_return('') allow(Facter::Core::Execution) - .to receive(:execute).with('ipconfig getoption awdl0 server_identifier', { logger: log_spy }).and_return(dhcp) + .to receive(:execute).with('ipconfig getoption awdl0 server_identifier', logger: an_instance_of(Facter::Log)).and_return(dhcp) end after do diff --git a/spec/facter/resolvers/openbsd/mountpoints_spec.rb b/spec/facter/resolvers/openbsd/mountpoints_spec.rb index 387c815e1d..db223425e3 100644 --- a/spec/facter/resolvers/openbsd/mountpoints_spec.rb +++ b/spec/facter/resolvers/openbsd/mountpoints_spec.rb @@ -13,15 +13,13 @@ size: '2.90 GiB', size_bytes: 3_114_448_896, used: '1.66 GiB', used_bytes: 1_780_609_024 } } end - let(:log_spy) { instance_spy(Facter::Log) } before do - Facter::Resolvers::Openbsd::Mountpoints.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('mount', { logger: log_spy }) + .with('mount', logger: an_instance_of(Facter::Log)) .and_return(load_fixture('openbsd_filesystems').read) allow(Facter::Core::Execution).to receive(:execute) - .with('df -P', { logger: log_spy }) + .with('df -P', logger: an_instance_of(Facter::Log)) .and_return(load_fixture('openbsd_df').read) end diff --git a/spec/facter/resolvers/partitions_spec.rb b/spec/facter/resolvers/partitions_spec.rb index 7152d3662e..d97bc22229 100644 --- a/spec/facter/resolvers/partitions_spec.rb +++ b/spec/facter/resolvers/partitions_spec.rb @@ -5,11 +5,9 @@ let(:sys_block_path) { '/sys/block' } let(:sys_block_subdirs) { ['.', '..', 'sda'] } - let(:logger) { instance_spy(Facter::Log) } before do Facter::Resolvers::Partitions.invalidate_cache - Facter::Resolvers::Partitions.instance_variable_set(:@log, logger) end context 'when /sys/block is not readable' do @@ -40,11 +38,11 @@ allow(Facter::Core::Execution).to receive(:which) .with('blkid').and_return('/usr/bin/blkid') allow(Facter::Core::Execution).to receive(:execute) - .with('blkid', { logger: logger }).and_return(load_fixture('blkid_output').read) + .with('blkid', logger: an_instance_of(Facter::Log)).and_return(load_fixture('blkid_output').read) allow(Facter::Core::Execution).to receive(:which) .with('lsblk').and_return('/usr/bin/lsblk') allow(Facter::Core::Execution).to receive(:execute) - .with('lsblk -fp', { logger: logger }).and_return(load_fixture('lsblk_output').read) + .with('lsblk -fp', logger: an_instance_of(Facter::Log)).and_return(load_fixture('lsblk_output').read) end context 'when block has a device subdir' do diff --git a/spec/facter/resolvers/redhat_release_spec.rb b/spec/facter/resolvers/redhat_release_spec.rb index 029b02402b..196b27f8ac 100644 --- a/spec/facter/resolvers/redhat_release_spec.rb +++ b/spec/facter/resolvers/redhat_release_spec.rb @@ -3,8 +3,6 @@ describe Facter::Resolvers::RedHatRelease do subject(:redhat_release) { Facter::Resolvers::RedHatRelease } - let(:log_spy) { instance_spy(Facter::Log) } - after do Facter::Resolvers::RedHatRelease.invalidate_cache end diff --git a/spec/facter/resolvers/selinux_spec.rb b/spec/facter/resolvers/selinux_spec.rb index c4e64c8ace..a64f922aaf 100644 --- a/spec/facter/resolvers/selinux_spec.rb +++ b/spec/facter/resolvers/selinux_spec.rb @@ -3,16 +3,13 @@ describe Facter::Resolvers::SELinux do subject(:selinux_resolver) { Facter::Resolvers::SELinux } - let(:log_spy) { instance_spy(Facter::Log) } - after do selinux_resolver.invalidate_cache end before do - selinux_resolver.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('cat /proc/self/mounts', { logger: log_spy }) + .with('cat /proc/self/mounts', logger: an_instance_of(Facter::Log)) .and_return(load_fixture(file).read) end diff --git a/spec/facter/resolvers/solaris/dmi_sparc_spec.rb b/spec/facter/resolvers/solaris/dmi_sparc_spec.rb index d8b0d66823..ce2ddecb5f 100644 --- a/spec/facter/resolvers/solaris/dmi_sparc_spec.rb +++ b/spec/facter/resolvers/solaris/dmi_sparc_spec.rb @@ -4,17 +4,14 @@ describe '#resolve' do subject(:resolver) { Facter::Resolvers::Solaris::DmiSparc } - let(:log_spy) { instance_spy(Facter::Log) } - before do - resolver.instance_variable_set(:@log, log_spy) allow(File).to receive(:executable?).with('/usr/sbin/prtdiag').and_return(true) allow(Facter::Core::Execution).to receive(:execute) - .with('/usr/sbin/prtdiag', { logger: log_spy }) + .with('/usr/sbin/prtdiag', logger: an_instance_of(Facter::Log)) .and_return(load_fixture('prtdiag').read) allow(File).to receive(:executable?).with('/usr/sbin/sneep').and_return(true) allow(Facter::Core::Execution).to receive(:execute) - .with('/usr/sbin/sneep', { logger: log_spy }).and_return('random_string') + .with('/usr/sbin/sneep', logger: an_instance_of(Facter::Log)).and_return('random_string') end after do @@ -37,13 +34,10 @@ describe '#reolve under a non-global zone' do subject(:resolver) { Facter::Resolvers::Solaris::DmiSparc } - let(:log_spy) { instance_spy(Facter::Log) } - before do - resolver.instance_variable_set(:@log, log_spy) allow(File).to receive(:executable?).with('/usr/sbin/prtdiag').and_return(true) allow(Facter::Core::Execution).to receive(:execute) - .with('/usr/sbin/prtdiag', { logger: log_spy }) + .with('/usr/sbin/prtdiag', logger: an_instance_of(Facter::Log)) .and_return('prtdiag can only be run in the global zone') end diff --git a/spec/facter/resolvers/solaris/dmi_spec.rb b/spec/facter/resolvers/solaris/dmi_spec.rb index 7024ccc636..d9cd6a6d87 100644 --- a/spec/facter/resolvers/solaris/dmi_spec.rb +++ b/spec/facter/resolvers/solaris/dmi_spec.rb @@ -4,19 +4,16 @@ describe '#resolve' do subject(:resolver) { Facter::Resolvers::Solaris::Dmi } - let(:log_spy) { instance_spy(Facter::Log) } - before do - resolver.instance_variable_set(:@log, log_spy) allow(File).to receive(:executable?).with('/usr/sbin/smbios').and_return(true) allow(Facter::Core::Execution).to receive(:execute) - .with('/usr/sbin/smbios -t SMB_TYPE_BIOS', { logger: log_spy }) + .with('/usr/sbin/smbios -t SMB_TYPE_BIOS', logger: an_instance_of(Facter::Log)) .and_return(load_fixture('smbios_bios').read) allow(Facter::Core::Execution).to receive(:execute) - .with('/usr/sbin/smbios -t SMB_TYPE_SYSTEM', { logger: log_spy }) + .with('/usr/sbin/smbios -t SMB_TYPE_SYSTEM', logger: an_instance_of(Facter::Log)) .and_return(load_fixture('smbios_system').read) allow(Facter::Core::Execution).to receive(:execute) - .with('/usr/sbin/smbios -t SMB_TYPE_CHASSIS', { logger: log_spy }) + .with('/usr/sbin/smbios -t SMB_TYPE_CHASSIS', logger: an_instance_of(Facter::Log)) .and_return(load_fixture('smbios_chassis').read) end diff --git a/spec/facter/resolvers/solaris/filesystem_spec.rb b/spec/facter/resolvers/solaris/filesystem_spec.rb index 0d77bae785..39aa0986c1 100644 --- a/spec/facter/resolvers/solaris/filesystem_spec.rb +++ b/spec/facter/resolvers/solaris/filesystem_spec.rb @@ -4,13 +4,11 @@ subject(:filesystems_resolver) { Facter::Resolvers::Solaris::Filesystem } let(:filesystems) { 'hsfs,nfs,pcfs,udfs,ufs' } - let(:log_spy) { instance_spy(Facter::Log) } before do - filesystems_resolver.instance_variable_set(:@log, log_spy) allow(File).to receive(:executable?).with('/usr/sbin/sysdef').and_return(true) allow(Facter::Core::Execution).to receive(:execute) - .with('/usr/sbin/sysdef', { logger: log_spy }) + .with('/usr/sbin/sysdef', logger: an_instance_of(Facter::Log)) .and_return(load_fixture('solaris_filesystems').read) end diff --git a/spec/facter/resolvers/solaris/ipaddress_spec.rb b/spec/facter/resolvers/solaris/ipaddress_spec.rb index 82e9f3b62a..1603ca0855 100644 --- a/spec/facter/resolvers/solaris/ipaddress_spec.rb +++ b/spec/facter/resolvers/solaris/ipaddress_spec.rb @@ -3,17 +3,14 @@ describe Facter::Resolvers::Solaris::Ipaddress do subject(:ipaddress) { Facter::Resolvers::Solaris::Ipaddress } - let(:log) { instance_spy(Facter::Log) } - describe '#resolve' do after do ipaddress.invalidate_cache end before do - ipaddress.instance_variable_set(:@log, log) allow(Facter::Core::Execution).to receive(:execute) - .with('route -n get default | grep interface', { logger: log }) + .with('route -n get default | grep interface', logger: an_instance_of(Facter::Log)) .and_return(route) end @@ -23,7 +20,7 @@ before do allow(Facter::Core::Execution).to receive(:execute) - .with('ifconfig net0', { logger: log }) + .with('ifconfig net0', logger: an_instance_of(Facter::Log)) .and_return(ifconfig) end diff --git a/spec/facter/resolvers/solaris/ldom_spec.rb b/spec/facter/resolvers/solaris/ldom_spec.rb index f35c3e3eb1..fb841dc96b 100644 --- a/spec/facter/resolvers/solaris/ldom_spec.rb +++ b/spec/facter/resolvers/solaris/ldom_spec.rb @@ -3,13 +3,10 @@ describe Facter::Resolvers::Solaris::Ldom do subject(:resolver) { Facter::Resolvers::Solaris::Ldom } - let(:log_spy) { instance_spy(Facter::Log) } - before do - resolver.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution) .to receive(:execute) - .with('/usr/sbin/virtinfo -a -p', { logger: log_spy }) + .with('/usr/sbin/virtinfo -a -p', logger: an_instance_of(Facter::Log)) .and_return(output) end diff --git a/spec/facter/resolvers/solaris/memory_spec.rb b/spec/facter/resolvers/solaris/memory_spec.rb index 4d40275e41..fea0dd1e03 100644 --- a/spec/facter/resolvers/solaris/memory_spec.rb +++ b/spec/facter/resolvers/solaris/memory_spec.rb @@ -3,21 +3,18 @@ describe Facter::Resolvers::Solaris::Memory do subject(:resolver) { Facter::Resolvers::Solaris::Memory } - let(:log_spy) { instance_spy(Facter::Log) } - before do - resolver.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution) .to receive(:execute) - .with('/usr/bin/kstat -m unix -n system_pages', { logger: log_spy }) + .with('/usr/bin/kstat -m unix -n system_pages', logger: an_instance_of(Facter::Log)) .and_return(kstat_output) allow(Facter::Core::Execution) .to receive(:execute) - .with('pagesize', { logger: log_spy }) + .with('pagesize', logger: an_instance_of(Facter::Log)) .and_return(pagesize) allow(Facter::Core::Execution) .to receive(:execute) - .with('/usr/sbin/swap -l', { logger: log_spy }) + .with('/usr/sbin/swap -l', logger: an_instance_of(Facter::Log)) .and_return(swap_output) end diff --git a/spec/facter/resolvers/solaris/zone_name_spec.rb b/spec/facter/resolvers/solaris/zone_name_spec.rb index 2e2147c353..4bd2fb57cd 100644 --- a/spec/facter/resolvers/solaris/zone_name_spec.rb +++ b/spec/facter/resolvers/solaris/zone_name_spec.rb @@ -4,15 +4,13 @@ subject(:solaris_zone) { Facter::Resolvers::Solaris::ZoneName } let(:zone_name_output) { 'global' } - let(:log_spy) { instance_spy(Facter::Log) } before do - solaris_zone.instance_variable_set(:@log, log_spy) allow(File).to receive(:executable?) .with('/bin/zonename') .and_return(true) allow(Facter::Core::Execution).to receive(:execute) - .with('/bin/zonename', { logger: log_spy }) + .with('/bin/zonename', logger: an_instance_of(Facter::Log)) .and_return(zone_name_output) end diff --git a/spec/facter/resolvers/solaris/zone_spec.rb b/spec/facter/resolvers/solaris/zone_spec.rb index 88fe5b8041..65f04fb986 100644 --- a/spec/facter/resolvers/solaris/zone_spec.rb +++ b/spec/facter/resolvers/solaris/zone_spec.rb @@ -3,12 +3,9 @@ describe Facter::Resolvers::Solaris::Zone do subject(:solaris_zone) { Facter::Resolvers::Solaris::Zone } - let(:log_spy) { instance_spy(Facter::Log) } - before do - solaris_zone.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('/usr/sbin/zoneadm list -cp', { logger: log_spy }) + .with('/usr/sbin/zoneadm list -cp', logger: an_instance_of(Facter::Log)) .and_return(output) end @@ -37,9 +34,10 @@ let(:output) { '' } it 'prints debug message' do - solaris_zone.resolve(:zone) - expect(log_spy).to have_received(:debug) + expect(solaris_zone.log).to receive(:debug) .with('Command /usr/sbin/zoneadm list -cp returned an empty result') + + solaris_zone.resolve(:zone) end end end diff --git a/spec/facter/resolvers/suse_release_spec.rb b/spec/facter/resolvers/suse_release_spec.rb index 75dbb30787..9edaf06cb8 100644 --- a/spec/facter/resolvers/suse_release_spec.rb +++ b/spec/facter/resolvers/suse_release_spec.rb @@ -3,8 +3,6 @@ describe Facter::Resolvers::SuseRelease do subject(:suse_release) { Facter::Resolvers::SuseRelease } - let(:log_spy) { instance_spy(Facter::Log) } - before do allow(Facter::Util::FileHelper).to receive(:safe_read) .with('/etc/SuSE-release', nil) diff --git a/spec/facter/resolvers/sw_vers_spec.rb b/spec/facter/resolvers/sw_vers_spec.rb index 7684ac8eee..36a25d7ae4 100644 --- a/spec/facter/resolvers/sw_vers_spec.rb +++ b/spec/facter/resolvers/sw_vers_spec.rb @@ -3,14 +3,11 @@ describe Facter::Resolvers::SwVers do subject(:sw_vers) { Facter::Resolvers::SwVers } - let(:log_spy) { instance_spy(Facter::Log) } - context 'with ProductVersionExtra' do before do Facter::Resolvers::SwVers.invalidate_cache - sw_vers.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('sw_vers', { logger: log_spy }) + .with('sw_vers', logger: an_instance_of(Facter::Log)) .and_return("ProductName:\tmacOS\nProductVersion:\t13.3.1\n"\ "ProductVersionExtra:\t(a)\nBuildVersion:\t22E772610a\n") end @@ -35,9 +32,8 @@ context 'without ProductVersionExtra' do before do Facter::Resolvers::SwVers.invalidate_cache - sw_vers.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('sw_vers', { logger: log_spy }) + .with('sw_vers', logger: an_instance_of(Facter::Log)) .and_return("ProductName:\tMac OS X\nProductVersion:\t10.14.1\n"\ "BuildVersion:\t18B75\n") end diff --git a/spec/facter/resolvers/system_profile_spec.rb b/spec/facter/resolvers/system_profile_spec.rb index 393eb10cdd..59ee1fec7e 100644 --- a/spec/facter/resolvers/system_profile_spec.rb +++ b/spec/facter/resolvers/system_profile_spec.rb @@ -3,8 +3,6 @@ describe Facter::Resolvers::Macosx::SystemProfiler do subject(:system_profiler) { Facter::Resolvers::Macosx::SystemProfiler } - let(:log_spy) { instance_spy(Facter::Log) } - let(:sp_hardware_data_type_hash) do { model_name: 'MacBook Pro', @@ -55,10 +53,6 @@ } end - before do - system_profiler.instance_variable_set(:@log, log_spy) - end - context 'when information is obtain from SPHardwareDataType' do before do allow(Facter::Util::Macosx::SystemProfileExecutor) diff --git a/spec/facter/resolvers/uname_spec.rb b/spec/facter/resolvers/uname_spec.rb index e035d58237..bcc8774c66 100644 --- a/spec/facter/resolvers/uname_spec.rb +++ b/spec/facter/resolvers/uname_spec.rb @@ -3,17 +3,14 @@ describe Facter::Resolvers::Uname do subject(:uname_resolver) { Facter::Resolvers::Uname } - let(:log_spy) { instance_spy(Facter::Log) } - before do - uname_resolver.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) .with('uname -m && uname -n && uname -p && uname -r && uname -s && - uname -v', { logger: log_spy }) + uname -v', logger: an_instance_of(Facter::Log)) .and_return('x86_64 wifi.tsr.corp.puppet.net i386 @@ -23,7 +20,6 @@ end after do - uname_resolver.instance_variable_set(:@log, nil) Facter::Resolvers::Uname.invalidate_cache end diff --git a/spec/facter/resolvers/virt_what_spec.rb b/spec/facter/resolvers/virt_what_spec.rb index 84b19f71c7..2ec877cbc1 100644 --- a/spec/facter/resolvers/virt_what_spec.rb +++ b/spec/facter/resolvers/virt_what_spec.rb @@ -3,15 +3,12 @@ describe Facter::Resolvers::VirtWhat do subject(:virt_what_resolver) { Facter::Resolvers::VirtWhat } - let(:log_spy) { instance_spy(Facter::Log) } - after do virt_what_resolver.invalidate_cache end before do - virt_what_resolver.instance_variable_set(:@log, log_spy) - allow(Facter::Core::Execution).to receive(:execute).with('virt-what', { logger: log_spy }).and_return(content) + allow(Facter::Core::Execution).to receive(:execute).with('virt-what', logger: an_instance_of(Facter::Log)).and_return(content) end context 'when virt-what fails' do diff --git a/spec/facter/resolvers/vmware_spec.rb b/spec/facter/resolvers/vmware_spec.rb index c0e827ac6c..f825821a1b 100644 --- a/spec/facter/resolvers/vmware_spec.rb +++ b/spec/facter/resolvers/vmware_spec.rb @@ -3,11 +3,8 @@ describe Facter::Resolvers::Vmware do subject(:vmware_resolver) { Facter::Resolvers::Vmware } - let(:log_spy) { instance_spy(Facter::Log) } - before do - vmware_resolver.instance_variable_set(:@log, log_spy) - allow(Facter::Core::Execution).to receive(:execute).with('vmware -v', { logger: log_spy }).and_return(output) + allow(Facter::Core::Execution).to receive(:execute).with('vmware -v', logger: an_instance_of(Facter::Log)).and_return(output) end after do diff --git a/spec/facter/resolvers/windows/aio_agent_version_spec.rb b/spec/facter/resolvers/windows/aio_agent_version_spec.rb index 8d416c84be..823c3e0f30 100644 --- a/spec/facter/resolvers/windows/aio_agent_version_spec.rb +++ b/spec/facter/resolvers/windows/aio_agent_version_spec.rb @@ -6,7 +6,6 @@ let(:puppet_version) { '7.0.1' } let(:reg) { instance_spy(Win32::Registry::HKEY_LOCAL_MACHINE) } - let(:log) { instance_spy(Facter::Log) } before do allow(Win32::Registry::HKEY_LOCAL_MACHINE).to receive(:open).with('SOFTWARE\\Puppet Labs\\Puppet').and_yield(reg) @@ -14,7 +13,6 @@ .to receive(:safe_read) .with('path_to_puppet/VERSION', nil) .and_return(puppet_version) - allow(Facter::Resolvers::BaseResolver).to receive(:log).and_return(log) end after do @@ -30,9 +28,9 @@ end it 'logs debug message specific to none existent path' do - aio_agent_resolver.resolve(:aio_agent_version) + expect(aio_agent_resolver.log).to receive(:debug).with('The registry path SOFTWARE\Puppet Labs\Puppet does not exist') - expect(log).to have_received(:debug).with('The registry path SOFTWARE\Puppet Labs\Puppet does not exist') + aio_agent_resolver.resolve(:aio_agent_version) end end @@ -76,9 +74,9 @@ end it 'logs debug message for 64 bit register' do - aio_agent_resolver.resolve(:aio_agent_version) + expect(aio_agent_resolver.log).to receive(:debug).with('Could not read Puppet AIO path from 64 bit registry') - expect(log).to have_received(:debug).with('Could not read Puppet AIO path from 64 bit registry') + aio_agent_resolver.resolve(:aio_agent_version) end it 'returns path from registry specific to 32 bit windows' do @@ -91,9 +89,10 @@ end it 'logs debug error for 32 bit registry' do - aio_agent_resolver.resolve(:aio_agent_version) + allow(aio_agent_resolver.log).to receive(:debug) + expect(aio_agent_resolver.log).to receive(:debug).with('Could not read Puppet AIO path from 32 bit registry') - expect(log).to have_received(:debug).with('Could not read Puppet AIO path from 32 bit registry') + aio_agent_resolver.resolve(:aio_agent_version) end end end diff --git a/spec/facter/resolvers/windows/dmi_bios_spec.rb b/spec/facter/resolvers/windows/dmi_bios_spec.rb index 5ce5f13491..a84f4f92bc 100644 --- a/spec/facter/resolvers/windows/dmi_bios_spec.rb +++ b/spec/facter/resolvers/windows/dmi_bios_spec.rb @@ -1,14 +1,11 @@ # frozen_string_literal: true describe Facter::Resolvers::DMIBios do - let(:logger) { instance_spy(Facter::Log) } - before do win = double('Facter::Util::Windows::Win32Ole') allow(Facter::Util::Windows::Win32Ole).to receive(:new).and_return(win) allow(win).to receive(:return_first).with('SELECT Manufacturer,SerialNumber from Win32_BIOS').and_return(comp) - Facter::Resolvers::DMIBios.instance_variable_set(:@log, logger) end after do @@ -35,7 +32,7 @@ let(:comp) {} it 'logs debug message and serial_number is nil' do - allow(logger).to receive(:debug) + allow(Facter::Resolvers::DMIBios.log).to receive(:debug) .with('WMI query returned no results for Win32_BIOS with values Manufacturer and SerialNumber.') expect(Facter::Resolvers::DMIBios.resolve(:serial_number)).to be(nil) end diff --git a/spec/facter/resolvers/windows/dmi_computersystem_spec.rb b/spec/facter/resolvers/windows/dmi_computersystem_spec.rb index 082a4d47a7..f14a287d86 100644 --- a/spec/facter/resolvers/windows/dmi_computersystem_spec.rb +++ b/spec/facter/resolvers/windows/dmi_computersystem_spec.rb @@ -1,15 +1,11 @@ # frozen_string_literal: true describe Facter::Resolvers::DMIComputerSystem do - let(:logger) { instance_spy(Facter::Log) } - before do win = double('Facter::Util::Windows::Win32Ole') allow(Facter::Util::Windows::Win32Ole).to receive(:new).and_return(win) allow(win).to receive(:return_first).with('SELECT Name,UUID FROM Win32_ComputerSystemProduct').and_return(comp) - - Facter::Resolvers::DMIComputerSystem.instance_variable_set(:@log, logger) end after do @@ -32,7 +28,7 @@ let(:comp) {} it 'logs debug message and name is nil' do - allow(logger).to receive(:debug) + allow(Facter::Resolvers::DMIComputerSystem.log).to receive(:debug) .with('WMI query returned no results for Win32_ComputerSystemProduct with values Name and UUID.') expect(Facter::Resolvers::DMIComputerSystem.resolve(:name)).to be(nil) end diff --git a/spec/facter/resolvers/windows/identity_spec.rb b/spec/facter/resolvers/windows/identity_spec.rb index bbdde63b6c..6d07b416f0 100644 --- a/spec/facter/resolvers/windows/identity_spec.rb +++ b/spec/facter/resolvers/windows/identity_spec.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true describe Facter::Resolvers::Identity do - let(:logger) { instance_spy(Facter::Log) } - before do size_ptr = double('FFI::MemoryPointer', read_uint32: 1) name_ptr = double('FFI::MemoryPointer', read_wide_string_with_length: user_name) @@ -13,13 +11,10 @@ allow(FFI::MemoryPointer).to receive(:new).with(:wchar, size_ptr.read_uint32).and_return(name_ptr) allow(IdentityFFI).to receive(:GetUserNameExW).with(2, name_ptr, size_ptr).and_return(error_geting_user?) allow(IdentityFFI).to receive(:IsUserAnAdmin).and_return(admin?) - - Facter::Resolvers::Identity.instance_variable_set(:@log, logger) end after do Facter::Resolvers::Identity.invalidate_cache - Facter::Resolvers::Identity.instance_variable_set(:@log, nil) end describe '#resolve when user is administrator' do @@ -74,13 +69,13 @@ let(:admin?) { 0 } it 'logs debug message when trying to resolve user' do - allow(logger).to receive(:debug) + allow(Facter::Resolvers::Identity.log).to receive(:debug) .with("failure resolving identity facts: #{error_number}") expect(Facter::Resolvers::Identity.resolve(:user)).to be(nil) end it 'logs debug message when trying to find if user is privileged' do - allow(logger).to receive(:debug) + allow(Facter::Resolvers::Identity.log).to receive(:debug) .with("failure resolving identity facts: #{error_number}") expect(Facter::Resolvers::Identity.resolve(:privileged)).to be(nil) end @@ -93,13 +88,13 @@ let(:admin?) { 0 } it 'logs debug message when trying to resolve user' do - allow(logger).to receive(:debug) + allow(Facter::Resolvers::Identity.log).to receive(:debug) .with("failure resolving identity facts: #{error_number}") expect(Facter::Resolvers::Identity.resolve(:user)).to be(nil) end it 'logs debug message when trying to find if user is privileged' do - allow(logger).to receive(:debug) + allow(Facter::Resolvers::Identity.log).to receive(:debug) .with("failure resolving identity facts: #{error_number}") expect(Facter::Resolvers::Identity.resolve(:privileged)).to be(nil) end diff --git a/spec/facter/resolvers/windows/kernel_spec.rb b/spec/facter/resolvers/windows/kernel_spec.rb index 2807928317..f57c9f7203 100644 --- a/spec/facter/resolvers/windows/kernel_spec.rb +++ b/spec/facter/resolvers/windows/kernel_spec.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true describe Facter::Resolvers::Kernel do - let(:logger) { instance_spy(Facter::Log) } - before do ver_ptr = double('FFI::MemoryPointer') ver = double('OsVersionInfoEx', size: nil) @@ -16,8 +14,6 @@ allow(ver).to receive(:[]).with(:dwMajorVersion).and_return(maj) allow(ver).to receive(:[]).with(:dwMinorVersion).and_return(min) allow(ver).to receive(:[]).with(:dwBuildNumber).and_return(buildnr) - - Facter::Resolvers::Kernel.instance_variable_set(:@log, logger) end after do @@ -50,7 +46,7 @@ let(:buildnr) { 123 } it 'logs debug message and kernel version nil' do - allow(logger).to receive(:debug).with('Calling Windows RtlGetVersion failed') + allow(Facter::Resolvers::Kernel.log).to receive(:debug).with('Calling Windows RtlGetVersion failed') expect(Facter::Resolvers::Kernel.resolve(:kernelversion)).to be(nil) end diff --git a/spec/facter/resolvers/windows/memory_spec.rb b/spec/facter/resolvers/windows/memory_spec.rb index a67e1f26e4..af4c5e20bd 100644 --- a/spec/facter/resolvers/windows/memory_spec.rb +++ b/spec/facter/resolvers/windows/memory_spec.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true describe Facter::Resolvers::Memory do - let(:logger) { instance_spy(Facter::Log) } - before do state_ptr = double('FFI::MemoryPointer', size: nil) state = double('PerformanceInformation', size: nil) @@ -15,8 +13,6 @@ allow(state).to receive(:[]).with(:PhysicalTotal).and_return(total) allow(state).to receive(:[]).with(:PageSize).and_return(page_size) allow(state).to receive(:[]).with(:PhysicalAvailable).and_return(available) - - Facter::Resolvers::Memory.instance_variable_set(:@log, logger) end after do @@ -53,7 +49,7 @@ let(:available) { 23 } it 'detects total_bytes as nil' do - allow(logger).to receive(:debug) + allow(Facter::Resolvers::Memory.log).to receive(:debug) .with('Available or Total bytes are zero could not proceed further') expect(Facter::Resolvers::Memory.resolve(:total_bytes)).to be(nil) end @@ -78,7 +74,7 @@ let(:available) { 0 } it 'detects total bytes as nil' do - allow(logger).to receive(:debug) + allow(Facter::Resolvers::Memory.log).to receive(:debug) .with('Available or Total bytes are zero could not proceed further') expect(Facter::Resolvers::Memory.resolve(:total_bytes)).to be(nil) end @@ -103,7 +99,7 @@ let(:available) { 4096 } it 'detects total bytes as nil' do - allow(logger).to receive(:debug) + allow(Facter::Resolvers::Memory.log).to receive(:debug) .with('Available or Total bytes are zero could not proceed further') expect(Facter::Resolvers::Memory.resolve(:total_bytes)).to be(nil) end @@ -128,7 +124,7 @@ let(:available) { 824_031 } it 'logs debug message and detects total bytes as nil' do - allow(logger).to receive(:debug).with('Resolving memory facts failed') + allow(Facter::Resolvers::Memory.log).to receive(:debug).with('Resolving memory facts failed') expect(Facter::Resolvers::Memory.resolve(:total_bytes)).to be(nil) end diff --git a/spec/facter/resolvers/windows/networking_spec.rb b/spec/facter/resolvers/windows/networking_spec.rb index 61607d1166..b99a7693c6 100644 --- a/spec/facter/resolvers/windows/networking_spec.rb +++ b/spec/facter/resolvers/windows/networking_spec.rb @@ -1,10 +1,11 @@ # frozen_string_literal: true +require 'ostruct' + describe Facter::Resolvers::Windows::Networking do subject(:resolver) { Facter::Resolvers::Windows::Networking } describe '#resolve' do - let(:logger) { instance_spy(Facter::Log) } let(:size_ptr) { instance_spy(FFI::MemoryPointer) } let(:adapter_address) { instance_spy(FFI::MemoryPointer) } let(:reg) { instance_spy('Win32::Registry::HKEY_LOCAL_MACHINE') } @@ -26,8 +27,6 @@ .and_yield(reg) allow(reg).to receive(:[]).with('Domain').and_return(domain) allow(reg).to receive(:close) - - resolver.instance_variable_set(:@log, logger) end after do @@ -38,7 +37,7 @@ let(:error_code) { NetworkingFFI::ERROR_NO_DATA } before do - allow(logger).to receive(:debug).with('Unable to retrieve networking facts!') + allow(resolver.log).to receive(:debug).with('Unable to retrieve networking facts!') end it 'returns interfaces fact as nil' do @@ -48,7 +47,7 @@ it 'logs debug message' do resolver.resolve(:interfaces) - expect(logger).to have_received(:debug).with('Unable to retrieve networking facts!') + expect(resolver.log).to have_received(:debug).with('Unable to retrieve networking facts!') end it 'returns nil for domain' do diff --git a/spec/facter/resolvers/windows/processors_spec.rb b/spec/facter/resolvers/windows/processors_spec.rb index 6df962966d..98372c6a01 100644 --- a/spec/facter/resolvers/windows/processors_spec.rb +++ b/spec/facter/resolvers/windows/processors_spec.rb @@ -3,8 +3,6 @@ describe Facter::Resolvers::Processors do subject(:resolver) { Facter::Resolvers::Processors } - let(:logger) { instance_spy(Facter::Log) } - before do win = double('Facter::Util::Windows::Win32Ole') query_string = 'SELECT Name,'\ @@ -16,7 +14,6 @@ allow(win).to receive(:exec_query) .with(query_string) .and_return(proc) - resolver.instance_variable_set(:@log, logger) end after do @@ -108,7 +105,7 @@ end it 'logs that is unknown architecture' do - allow(logger).to receive(:debug) + allow(resolver.log).to receive(:debug) .with('Unable to determine processor type: unknown architecture') expect(resolver.resolve(:isa)).to be(nil) end @@ -118,7 +115,7 @@ let(:proc) { nil } it 'logs that query failed and isa nil' do - allow(logger).to receive(:debug) + allow(resolver.log).to receive(:debug) .with('WMI query returned no results'\ 'for Win32_Processor with values Name, Architecture and NumberOfLogicalProcessors.') expect(resolver.resolve(:isa)).to be(nil) @@ -159,7 +156,7 @@ end it 'detects that isa is nil' do - allow(logger).to receive(:debug) + allow(resolver.log).to receive(:debug) .with('Unable to determine processor type: unknown architecture') expect(resolver.resolve(:isa)).to be(nil) end diff --git a/spec/facter/resolvers/windows/system32_spec.rb b/spec/facter/resolvers/windows/system32_spec.rb index 92098d9ec1..6389bd5091 100644 --- a/spec/facter/resolvers/windows/system32_spec.rb +++ b/spec/facter/resolvers/windows/system32_spec.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true describe Facter::Resolvers::System32 do - let(:logger) { instance_spy(Facter::Log) } - before do allow(ENV).to receive(:[]).with('SystemRoot').and_return(win_path) @@ -10,8 +8,6 @@ allow(FFI::MemoryPointer).to receive(:new).with(:win32_bool, 1).and_return(bool_ptr) allow(System32FFI).to receive(:GetCurrentProcess).and_return(2) allow(System32FFI).to receive(:IsWow64Process).with(2, bool_ptr).and_return(bool) - - Facter::Resolvers::System32.instance_variable_set(:@log, logger) end after do @@ -44,8 +40,8 @@ let(:is_wow) { false } it 'detects system32 dir is nil and prints debug message' do - allow(logger).to receive(:debug).with('Unable to find correct value for SystemRoot'\ - ' enviroment variable') + allow(Facter::Resolvers::System32.log).to receive(:debug) + .with('Unable to find correct value for SystemRoot enviroment variable') expect(Facter::Resolvers::System32.resolve(:system32)).to be(nil) end end @@ -56,7 +52,7 @@ let(:is_wow) { false } it 'detects system32 dir is nil and prints debug message' do - allow(logger).to receive(:debug).with('Unable to find correct value for SystemRoot'\ + allow(Facter::Resolvers::System32.log).to receive(:debug).with('Unable to find correct value for SystemRoot'\ ' enviroment variable') expect(Facter::Resolvers::System32.resolve(:system32)).to be(nil) end @@ -68,7 +64,7 @@ let(:is_wow) { false } it 'detects system32 dir is nil and prints debug message' do - allow(logger).to receive(:debug).with('IsWow64Process failed') + allow(Facter::Resolvers::System32.log).to receive(:debug).with('IsWow64Process failed') expect(Facter::Resolvers::System32.resolve(:system32)).to be(nil) end end diff --git a/spec/facter/resolvers/windows/uptime_spec.rb b/spec/facter/resolvers/windows/uptime_spec.rb index 7a49d972e4..67304dabf8 100644 --- a/spec/facter/resolvers/windows/uptime_spec.rb +++ b/spec/facter/resolvers/windows/uptime_spec.rb @@ -1,16 +1,12 @@ # frozen_string_literal: true describe Facter::Resolvers::Windows::Uptime do - let(:logger) { instance_spy(Facter::Log) } - before do win = double('Facter::Util::Windows::Win32Ole') allow(Facter::Util::Windows::Win32Ole).to receive(:new).and_return(win) allow(win).to receive(:return_first).with('SELECT LocalDateTime,LastBootUpTime FROM Win32_OperatingSystem') .and_return(comp) - - Facter::Resolvers::Windows::Uptime.instance_variable_set(:@log, logger) end after do @@ -134,13 +130,13 @@ let(:last_bootup_time) { '20010201120506+0700' } before do - allow(logger).to receive(:debug).with('Unable to determine system uptime!') + allow(Facter::Resolvers::Windows::Uptime.log).to receive(:debug).with('Unable to determine system uptime!') end it 'logs that is unable to determine system uptime and all facts are nil' do Facter::Resolvers::Windows::Uptime.resolve(:days) - expect(logger).to have_received(:debug).with('Unable to determine system uptime!') + expect(Facter::Resolvers::Windows::Uptime.log).to have_received(:debug).with('Unable to determine system uptime!') end it 'uptime fact is nil' do @@ -152,10 +148,10 @@ let(:comp) { nil } it 'logs that query failed and days nil' do - allow(logger).to receive(:debug) + allow(Facter::Resolvers::Windows::Uptime.log).to receive(:debug) .with('WMI query returned no results'\ 'for Win32_OperatingSystem with values LocalDateTime and LastBootUpTime.') - allow(logger).to receive(:debug) + allow(Facter::Resolvers::Windows::Uptime.log).to receive(:debug) .with('Unable to determine system uptime!') expect(Facter::Resolvers::Windows::Uptime.resolve(:days)).to be(nil) end @@ -177,7 +173,7 @@ let(:comp) { double('WIN32OLE', LocalDateTime: nil, LastBootUpTime: nil) } it 'logs that is unable to determine system uptime and days fact is nil' do - allow(logger).to receive(:debug) + allow(Facter::Resolvers::Windows::Uptime.log).to receive(:debug) .with('Unable to determine system uptime!') expect(Facter::Resolvers::Windows::Uptime.resolve(:days)).to be(nil) end diff --git a/spec/facter/resolvers/windows/virtualization_spec.rb b/spec/facter/resolvers/windows/virtualization_spec.rb index c57506590b..b38a39dab2 100644 --- a/spec/facter/resolvers/windows/virtualization_spec.rb +++ b/spec/facter/resolvers/windows/virtualization_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true describe Facter::Resolvers::Windows::Virtualization do - let(:logger) { instance_spy(Facter::Log) } let(:win32ole) { instance_spy('WIN32OLE') } let(:win32ole2) { instance_spy('WIN32OLE') } let(:win) { instance_spy('Facter::Util::Windows::Win32Ole') } @@ -10,7 +9,6 @@ allow(Facter::Util::Windows::Win32Ole).to receive(:new).and_return(win) allow(win).to receive(:exec_query).with('SELECT Manufacturer,Model,OEMStringArray FROM Win32_ComputerSystem') .and_return(query_result) - Facter::Resolvers::Windows::Virtualization.instance_variable_set(:@log, logger) Facter::Resolvers::Windows::Virtualization.invalidate_cache end @@ -223,7 +221,7 @@ let(:query_result) { nil } it 'logs that query failed and virtual nil' do - allow(logger).to receive(:debug) + allow(Facter::Resolvers::Windows::Virtualization.log).to receive(:debug) .with('WMI query returned no results'\ ' for Win32_ComputerSystem with values Manufacturer, Model and OEMStringArray.') expect(Facter::Resolvers::Windows::Virtualization.resolve(:virtual)).to be(nil) diff --git a/spec/facter/resolvers/windows/win_os_description_spec.rb b/spec/facter/resolvers/windows/win_os_description_spec.rb index 63cb1a8130..c65e86b11a 100644 --- a/spec/facter/resolvers/windows/win_os_description_spec.rb +++ b/spec/facter/resolvers/windows/win_os_description_spec.rb @@ -1,15 +1,12 @@ # frozen_string_literal: true describe Facter::Resolvers::WinOsDescription do - let(:logger) { instance_spy(Facter::Log) } - before do win = double('Facter::Util::Windows::Win32Ole') allow(Facter::Util::Windows::Win32Ole).to receive(:new).and_return(win) allow(win).to receive(:return_first).with('SELECT ProductType,OtherTypeDescription FROM Win32_OperatingSystem') .and_return(comp) - Facter::Resolvers::WinOsDescription.instance_variable_set(:@log, logger) end after do @@ -20,7 +17,7 @@ let(:comp) { nil } it 'logs debug message and facts are nil' do - allow(logger).to receive(:debug) + allow(Facter::Resolvers::WinOsDescription.log).to receive(:debug) .with('WMI query returned no results for Win32_OperatingSystem'\ 'with values ProductType and OtherTypeDescription.') diff --git a/spec/facter/resolvers/wpar_spec.rb b/spec/facter/resolvers/wpar_spec.rb index 363dd75498..0d997c3c67 100644 --- a/spec/facter/resolvers/wpar_spec.rb +++ b/spec/facter/resolvers/wpar_spec.rb @@ -1,12 +1,9 @@ # frozen_string_literal: true describe Facter::Resolvers::Wpar do - let(:log_spy) { instance_spy(Facter::Log) } - before do - Facter::Resolvers::Wpar.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('/usr/bin/lparstat -W', { logger: log_spy }) + .with('/usr/bin/lparstat -W', logger: an_instance_of(Facter::Log)) .and_return(open3_result) end diff --git a/spec/facter/resolvers/xen_spec.rb b/spec/facter/resolvers/xen_spec.rb index 3b3db6b9f7..ef97a7a0c7 100644 --- a/spec/facter/resolvers/xen_spec.rb +++ b/spec/facter/resolvers/xen_spec.rb @@ -5,11 +5,9 @@ let(:proc_xen_file) { false } let(:xvda1_file) { false } - let(:log_spy) { instance_spy(Facter::Log) } let(:domains) { '' } before do - xen_resolver.instance_variable_set(:@log, log_spy) allow(File).to receive(:exist?).and_call_original allow(File).to receive(:exist?).with('/dev/xen/evtchn').and_return(evtchn_file) allow(File).to receive(:exist?).with('/proc/xen').and_return(proc_xen_file) @@ -18,7 +16,7 @@ allow(File).to receive(:exist?).with('/usr/sbin/xl').and_return(false) allow(File).to receive(:exist?).with('/usr/sbin/xm').and_return(true) allow(Facter::Core::Execution).to receive(:execute) - .with('/usr/sbin/xm list', { logger: log_spy }).and_return(domains) + .with('/usr/sbin/xm list', logger: an_instance_of(Facter::Log)).and_return(domains) xen_resolver.invalidate_cache end diff --git a/spec/facter/resolvers/zfs_spec.rb b/spec/facter/resolvers/zfs_spec.rb index a89e35ecf9..0f49c6a4f4 100644 --- a/spec/facter/resolvers/zfs_spec.rb +++ b/spec/facter/resolvers/zfs_spec.rb @@ -3,12 +3,9 @@ describe Facter::Resolvers::ZFS do subject(:zfs_resolver) { Facter::Resolvers::ZFS } - let(:log_spy) { instance_spy(Facter::Log) } - before do - zfs_resolver.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('zfs upgrade -v', { logger: log_spy }) + .with('zfs upgrade -v', logger: an_instance_of(Facter::Log)) .and_return(output) end diff --git a/spec/facter/resolvers/zpool_spec.rb b/spec/facter/resolvers/zpool_spec.rb index b63e6c73c6..3c52c42a46 100644 --- a/spec/facter/resolvers/zpool_spec.rb +++ b/spec/facter/resolvers/zpool_spec.rb @@ -3,12 +3,9 @@ describe Facter::Resolvers::Zpool do subject(:zpool_resolver) { Facter::Resolvers::Zpool } - let(:log_spy) { instance_spy(Facter::Log) } - before do - zpool_resolver.instance_variable_set(:@log, log_spy) allow(Facter::Core::Execution).to receive(:execute) - .with('zpool upgrade -v', { logger: log_spy }) + .with('zpool upgrade -v', logger: an_instance_of(Facter::Log)) .and_return(output) end diff --git a/spec/facter/util/aix/odm_query_spec.rb b/spec/facter/util/aix/odm_query_spec.rb index 1719ce151b..84eec30ca4 100644 --- a/spec/facter/util/aix/odm_query_spec.rb +++ b/spec/facter/util/aix/odm_query_spec.rb @@ -2,17 +2,15 @@ describe Facter::Util::Aix::ODMQuery do let(:odm_query) { Facter::Util::Aix::ODMQuery.new } - let(:log_spy) { instance_spy(Facter::Log) } before do - odm_query.instance_variable_set(:@log, log_spy) stub_const('Facter::Util::Aix::ODMQuery::REPOS', ['CuAt']) end it 'creates a query' do odm_query.equals('name', '12345') - expect(Facter::Core::Execution).to receive(:execute).with("odmget -q \"name='12345'\" CuAt", { logger: log_spy }) + expect(Facter::Core::Execution).to receive(:execute).with("odmget -q \"name='12345'\" CuAt", logger: an_instance_of(Facter::Log)) odm_query.execute end @@ -20,7 +18,7 @@ odm_query.equals('field1', 'value').like('field2', 'value*') expect(Facter::Core::Execution).to receive(:execute) - .with("odmget -q \"field1='value' AND field2 like 'value*'\" CuAt", { logger: log_spy }) + .with("odmget -q \"field1='value' AND field2 like 'value*'\" CuAt", logger: an_instance_of(Facter::Log)) odm_query.execute end end diff --git a/spec/facter/util/facts/posix/virtual_detector_spec.rb b/spec/facter/util/facts/posix/virtual_detector_spec.rb index 72dfa39366..df9413b3be 100644 --- a/spec/facter/util/facts/posix/virtual_detector_spec.rb +++ b/spec/facter/util/facts/posix/virtual_detector_spec.rb @@ -4,11 +4,8 @@ subject(:detector) { Facter::Util::Facts::Posix::VirtualDetector } describe '#platform' do - let(:logger_mock) { instance_spy(Facter::Log) } - before do Facter::Util::Facts::Posix::VirtualDetector.instance_variable_set(:@fact_value, nil) - allow(Facter::Log).to receive(:new).and_return(logger_mock) end context 'when in docker' do diff --git a/spec/facter/util/facts/uptime_parser_spec.rb b/spec/facter/util/facts/uptime_parser_spec.rb index 382c53c642..76f7bfd58e 100644 --- a/spec/facter/util/facts/uptime_parser_spec.rb +++ b/spec/facter/util/facts/uptime_parser_spec.rb @@ -5,11 +5,6 @@ let(:uptime_proc_file_cmd) { '/bin/cat /proc/uptime' } let(:kern_boottime_cmd) { 'sysctl -n kern.boottime' } let(:uptime_cmd) { 'uptime' } - let(:log_spy) { instance_spy(Facter::Log) } - - before do - Facter::Util::Facts::UptimeParser.instance_variable_set(:@log, log_spy) - end context 'when a /proc/uptime file exists' do let(:proc_uptime_value) { '2672.69 20109.75' } @@ -17,7 +12,7 @@ it 'returns the correct result' do allow(Facter::Core::Execution) .to receive(:execute) - .with(uptime_proc_file_cmd, { logger: log_spy }) + .with(uptime_proc_file_cmd, logger: an_instance_of(Facter::Log)) .and_return(proc_uptime_value) expect(Facter::Util::Facts::UptimeParser.uptime_seconds_unix).to eq(2672) @@ -36,12 +31,12 @@ allow(Facter::Core::Execution) .to receive(:execute) - .with(uptime_proc_file_cmd, { logger: log_spy }) + .with(uptime_proc_file_cmd, logger: an_instance_of(Facter::Log)) .and_return('') allow(Facter::Core::Execution) .to receive(:execute) - .with(kern_boottime_cmd, { logger: log_spy }) + .with(kern_boottime_cmd, logger: an_instance_of(Facter::Log)) .and_return(kern_boottime_value) expect(Facter::Util::Facts::UptimeParser.uptime_seconds_unix).to eq(60) @@ -52,12 +47,12 @@ before do allow(Facter::Core::Execution) .to receive(:execute) - .with(uptime_proc_file_cmd, { logger: log_spy }) + .with(uptime_proc_file_cmd, logger: an_instance_of(Facter::Log)) .and_return('') allow(Facter::Core::Execution) .to receive(:execute) - .with(kern_boottime_cmd, { logger: log_spy }) + .with(kern_boottime_cmd, logger: an_instance_of(Facter::Log)) .and_return('') end @@ -65,7 +60,7 @@ it 'returns the correct result' do allow(Facter::Core::Execution) .to receive(:execute) - .with(uptime_cmd, { logger: log_spy }) + .with(uptime_cmd, logger: an_instance_of(Facter::Log)) .and_return(cmd_output) expect(Facter::Util::Facts::UptimeParser.uptime_seconds_unix).to eq(result) diff --git a/spec/facter/util/linux/dhcp_spec.rb b/spec/facter/util/linux/dhcp_spec.rb index bd3d3ac209..26273ce9e4 100644 --- a/spec/facter/util/linux/dhcp_spec.rb +++ b/spec/facter/util/linux/dhcp_spec.rb @@ -71,7 +71,7 @@ allow(Facter::Core::Execution).to receive(:which) .with('dhcpcd').and_return('/usr/bin/dhcpcd') allow(Facter::Core::Execution).to receive(:execute) - .with('/usr/bin/dhcpcd -U ens160', { logger: log_spy }).and_return(load_fixture('dhcpcd').read) + .with('/usr/bin/dhcpcd -U ens160', logger: log_spy).and_return(load_fixture('dhcpcd').read) dhcp_search.instance_eval { @dhcpcd_command = nil } end diff --git a/spec/facter/util/linux/routing_table_spec.rb b/spec/facter/util/linux/routing_table_spec.rb index d3aa5586d3..4446d0e48f 100644 --- a/spec/facter/util/linux/routing_table_spec.rb +++ b/spec/facter/util/linux/routing_table_spec.rb @@ -9,9 +9,9 @@ context 'when ip route show finds an IP, Socket lib did not retrieve' do before do allow(Facter::Core::Execution).to receive(:execute) - .with('ip route show', { logger: log_spy }).and_return(load_fixture('ip_route_show').read) + .with('ip route show', logger: log_spy).and_return(load_fixture('ip_route_show').read) allow(Facter::Core::Execution).to receive(:execute) - .with('ip -6 route show', { logger: log_spy }).and_return(load_fixture('ip_-6_route_show').read) + .with('ip -6 route show', logger: log_spy).and_return(load_fixture('ip_-6_route_show').read) end it 'returns the ipv4 info' do diff --git a/spec/facter/util/linux/socket_parser_spec.rb b/spec/facter/util/linux/socket_parser_spec.rb index a67c4d3f29..fd409a5a5b 100644 --- a/spec/facter/util/linux/socket_parser_spec.rb +++ b/spec/facter/util/linux/socket_parser_spec.rb @@ -27,7 +27,7 @@ def ifaddr_obj(name, ip, mac, netmask, ipv4_type) allow(Socket).to receive(:getifaddrs).and_return(ifaddrs) allow(Socket).to receive(:const_defined?).with(:PF_LINK).and_return(true) allow(Facter::Core::Execution).to receive(:execute) - .with('ip -o link show', { logger: log_spy }).and_return(load_fixture('ip_link_show_all').read) + .with('ip -o link show', logger: log_spy).and_return(load_fixture('ip_link_show_all').read) end let(:result) do @@ -77,9 +77,11 @@ def ifaddr_obj(name, ip, mac, netmask, ipv4_type) before do allow(Facter::Core::Execution).to receive(:execute) - .with('ip -o link show', logger: log_spy).and_return(load_fixture('ip_link_show_all').read) + .with('ip -o link show', logger: log_spy) + .and_return(load_fixture('ip_link_show_all').read) allow(Facter::Util::FileHelper).to receive(:safe_read) - .with('/proc/net/bonding/bond0', nil).and_return(load_fixture('bond_interface_data').read) + .with('/proc/net/bonding/bond0', nil) + .and_return(load_fixture('bond_interface_data').read) end it 'retrieves eth2 interface' do diff --git a/spec/facter/util/resolvers/http_spec.rb b/spec/facter/util/resolvers/http_spec.rb index 48984c4da8..84b9ce748a 100644 --- a/spec/facter/util/resolvers/http_spec.rb +++ b/spec/facter/util/resolvers/http_spec.rb @@ -4,10 +4,8 @@ subject(:http) { Facter::Util::Resolvers::Http } let(:url) { 'http://169.254.169.254/meta-data/' } - let(:log_spy) { instance_spy(Facter::Log) } before do - http.instance_variable_set(:@log, log_spy) allow(Gem).to receive(:win_platform?).and_return(false) end @@ -44,8 +42,10 @@ end it 'logs error code' do + allow(http.instance_variable_get(:@log)).to receive(:debug) + expect(http.instance_variable_get(:@log)).to receive(:debug).with("Request to #{url} failed with error code 500") + http.send(client_method, url) - expect(log_spy).to have_received(:debug).with("Request to #{url} failed with error code 500") end end @@ -59,9 +59,10 @@ end it 'logs error message' do + allow(http.instance_variable_get(:@log)).to receive(:debug) + expect(http.instance_variable_get(:@log)).to receive(:debug).with("Trying to connect to #{url} but got: execution expired") + http.send(client_method, url) - expect(log_spy).to have_received(:debug) - .with("Trying to connect to #{url} but got: execution expired") end end @@ -75,8 +76,10 @@ end it 'logs error message' do + allow(http.instance_variable_get(:@log)).to receive(:debug) + expect(http.instance_variable_get(:@log)).to receive(:debug).with("Trying to connect to #{url} but got: some error") + http.send(client_method, url) - expect(log_spy).to have_received(:debug).with("Trying to connect to #{url} but got: some error") end end @@ -112,9 +115,11 @@ end it 'logs error message' do - http.send(client_method, url) - expect(log_spy).to have_received(:debug) + allow(http.instance_variable_get(:@log)).to receive(:debug) + expect(http.instance_variable_get(:@log)).to receive(:debug) .with(/((Operation|Connection) timed out)|(A connection attempt.*)/) + + http.send(client_method, url) end end diff --git a/spec/facter/util/windows/network_utils_spec.rb b/spec/facter/util/windows/network_utils_spec.rb index c4af55c525..50731c2969 100644 --- a/spec/facter/util/windows/network_utils_spec.rb +++ b/spec/facter/util/windows/network_utils_spec.rb @@ -3,8 +3,8 @@ Ps = Struct.new(:PhysicalAddress, :PhysicalAddressLength) describe NetworkUtils do - describe '#address_to_strig' do - let(:logger) { instance_spy(Facter::Log) } + describe '#address_to_string' do + let(:logger) { NetworkUtils.instance_variable_get(:@log) } let(:addr) { instance_spy('SocketAddress') } let(:size) { instance_spy(FFI::MemoryPointer) } let(:buffer) { instance_spy(FFI::MemoryPointer) } @@ -19,8 +19,6 @@ allow(NetworkingFFI).to receive(:WSAAddressToStringW) .with(address, length, FFI::Pointer::NULL, buffer, size).and_return(error) allow(NetworkUtils).to receive(:extract_address).with(buffer).and_return('10.123.0.2') - - NetworkUtils.instance_variable_set(:@log, logger) end context 'when lpSockaddr is null' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 793b6e2c93..69e782402c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -76,6 +76,10 @@ def colorize(str, color) mocks.verify_partial_doubles = true end + config.before(:all) do + Facter::Log.class_variable_set(:@@logger, Logger.new(StringIO.new)) # rubocop:disable Style/ClassVars + end + config.before do LegacyFacter.clear Facter.clear_messages diff --git a/spec_integration/facter_resolve_spec.rb b/spec_integration/facter_resolve_spec.rb index 1bceaba25c..195ed69b8e 100644 --- a/spec_integration/facter_resolve_spec.rb +++ b/spec_integration/facter_resolve_spec.rb @@ -24,25 +24,30 @@ FileUtils.chmod('+x', ext_fact_path) it 'detects facter invocations recursively and stops external facts from recursing' do + # in-process entrypoints write to our in-memory logger, not stdout/err facter_entrypoints = [ - proc { `bundle exec facter --external-dir #{ext_dir} os` }, - proc { Facter.resolve("--external-dir #{ext_dir}") }, - proc { Facter.resolve("os.family --external-dir #{ext_dir}") }, - proc { - Facter::OptionStore.external_dir = [ext_dir] - Facter.to_hash - } + [proc { `bundle exec facter --external-dir #{ext_dir} os` }, false], + [proc { Facter.resolve("--external-dir #{ext_dir}") }, true], + [proc { Facter.resolve("os.family --external-dir #{ext_dir}") }, true], + [ + proc do + Facter::OptionStore.external_dir = [ext_dir] + Facter.to_hash + end, true + ] ] time = Benchmark.measure do # This iterates over the supported entry points that would attempt to resolve # external facts; if any one of them descends into recursion, it will exceed # the DEFAULT_EXECUTION_TIMEOUT; otherwise, each of these calls should be only # be a few seconds. - facter_entrypoints.each_with_index do |entrypoint, index| - if index.zero? - expect { entrypoint.call }.to output(/Recursion detected/).to_stderr_from_any_process + logger = Facter::Log.class_variable_get(:@@logger) + facter_entrypoints.each do |entrypoint, inprocess| + if inprocess + expect(logger).to receive(:warn).with(/Recursion detected/) + entrypoint.call else - expect { entrypoint.call }.to output(/Recursion detected/).to_stdout_from_any_process + expect { entrypoint.call }.to output(/Recursion detected/).to_stderr_from_any_process end Facter.reset Facter.clear From 707e8f928135613033af5b9248f60254c9d17d91 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Fri, 17 May 2024 22:16:38 -0700 Subject: [PATCH 3/8] (FACT-3468) Initialize aggregate log Previously if an aggregate fact was evaluated twice it would attempt to log a warning but its `log` method was never defined, leading to NoMethodError. It appears this was broken in ee582c5714228ceff61a1105cf439cb4ca53ff12 when the Legacy logger was removed. This commit adds a private `log` method to lazily create the log as is done elsewhere and adds a test. Since string literals are frozen, we need to dup prior to modifying it. --- lib/facter/custom_facts/core/aggregate.rb | 6 +++++- spec/custom_facts/core/aggregate_spec.rb | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/facter/custom_facts/core/aggregate.rb b/lib/facter/custom_facts/core/aggregate.rb index 9e795edbdd..b8964d0886 100644 --- a/lib/facter/custom_facts/core/aggregate.rb +++ b/lib/facter/custom_facts/core/aggregate.rb @@ -109,7 +109,7 @@ def options(options) # @api private def evaluate(&block) if @last_evaluated - msg = "Already evaluated #{@name}" + msg = +"Already evaluated #{@name}" msg << " at #{@last_evaluated}" if msg.is_a? String msg << ', reevaluating anyways' log.warn msg @@ -197,6 +197,10 @@ def resolution_type private + def log + @log ||= Facter::Log.new(self) + end + def evaluate_params(name) raise ArgumentError, "#{self.class.name}#chunk requires a block" unless block_given? raise ArgumentError, "#{self.class.name}#expected chunk name to be a Symbol" unless name.is_a? Symbol diff --git a/spec/custom_facts/core/aggregate_spec.rb b/spec/custom_facts/core/aggregate_spec.rb index bba2db0ccb..f0636cebda 100644 --- a/spec/custom_facts/core/aggregate_spec.rb +++ b/spec/custom_facts/core/aggregate_spec.rb @@ -4,6 +4,7 @@ subject(:aggregate_res) { Facter::Core::Aggregate.new('aggregated', fact) } let(:fact) { double('stub_fact', name: 'stub_fact') } + let(:logger) { Facter::Log.class_variable_get(:@@logger) } it 'can be resolved' do expect(aggregate_res).to be_a_kind_of LegacyFacter::Core::Resolvable @@ -136,5 +137,11 @@ expect(aggregate_res).to receive(:has_weight).with(5) aggregate_res.evaluate { has_weight(5) } end + + it 'warns if the block is evaluated more than once' do + expect(logger).to receive(:warn).with(/Already evaluated aggregated at.*reevaluating anyways/) + aggregate_res.evaluate {} + aggregate_res.evaluate {} + end end end From 5033650df35fe8faa4752c593fcc84c6fe8a8af2 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Fri, 17 May 2024 23:13:27 -0700 Subject: [PATCH 4/8] (FACT-3468) Don't mutate Facter's global logger Previously, tests were clearing out Facter's global logger after each test. Now just set expectations on the logger. Since we're no longer using an instance_spy, the expectation needs to be set on the Logger before calling the subject. --- spec/custom_facts/core/resolvable_spec.rb | 15 +--- spec/facter/facter_spec.rb | 86 ++++++++++------------- 2 files changed, 42 insertions(+), 59 deletions(-) diff --git a/spec/custom_facts/core/resolvable_spec.rb b/spec/custom_facts/core/resolvable_spec.rb index 8b2dc7b8f8..232ab770ed 100644 --- a/spec/custom_facts/core/resolvable_spec.rb +++ b/spec/custom_facts/core/resolvable_spec.rb @@ -49,22 +49,13 @@ class FlushFakeError < StandardError; end end context 'with a fact whose value is invalid UTF-8 string' do - let(:logger) { instance_spy(Facter::Log) } - - before do - allow(Facter::Log).to receive(:new).and_return(logger) - end - - after do - Facter.instance_variable_set(:@logger, nil) - end + let(:logger) { Facter.send(:logger) } it 'cannot resolve and logs error with fact name' do + expect(logger).to receive(:error).with(/Fact resolution fact='stub fact', resolution='resolvable' resolved to an invalid value: String "\\xC3\(" doesn't match the reported encoding UTF-8/) + resolvable.resolve_value = "\xc3\x28" resolvable.value - expect(logger).to have_received(:error).with("Fact resolution fact='stub fact', resolution='resolvable' "\ - "resolved to an invalid value: String \"\\xC3(\" doesn't match "\ - 'the reported encoding UTF-8') end end end diff --git a/spec/facter/facter_spec.rb b/spec/facter/facter_spec.rb index df8aaa3b42..78c7cbfb3c 100644 --- a/spec/facter/facter_spec.rb +++ b/spec/facter/facter_spec.rb @@ -14,8 +14,8 @@ :core, 'mountpoints') end - let(:logger) { instance_spy(Facter::Log) } let(:config_reader_double) { class_spy(Facter::ConfigReader) } + let(:logger) { Facter.send(:logger) } sorted_fact_hash = { 'mountpoints' => { '/' => { 'device' => 'dev2', 'filesystem' => 'ext4' }, '/boot' => { 'device' => 'dev1', 'filesystem' => 'ext4' } } } @@ -32,15 +32,10 @@ allow(Facter::Options).to receive(:[]).with(:blocked_facts).and_return([]) allow(Facter::Options).to receive(:[]).with(:block_list).and_return([]) - allow(Facter::Log).to receive(:new).and_return(logger) Facter.clear allow(Facter::SessionCache).to receive(:invalidate_all_caches) end - after do - Facter.instance_variable_set(:@logger, nil) - end - def stub_facts(resolved_facts) allow(Facter::FactManager.instance).to receive(:resolve_facts).and_return(resolved_facts) end @@ -366,9 +361,9 @@ def stub_no_fact end it 'logs a debug message' do - Facter.load_external(true) + expect(logger).to receive(:debug).with('Facter.load_external(true) called. External facts will be loaded') - expect(logger).to have_received(:debug).with('Facter.load_external(true) called. External facts will be loaded') + Facter.load_external(true) end end @@ -501,9 +496,9 @@ def stub_no_fact let(:is_debug) { false } it "doesn't log anything" do - Facter.debug(message) + expect(logger).not_to receive(:debug).with(message) - expect(logger).not_to have_received(:debug).with(message) + Facter.debug(message) end end end @@ -530,17 +525,17 @@ def stub_no_fact it 'logs exception message' do exception.set_backtrace(backtrace) - Facter.log_exception(exception, message) + expect(logger).to receive(:error).with(expected_message) - expect(logger).to have_received(:error).with(expected_message) + Facter.log_exception(exception, message) end end shared_examples 'when exception param is not an exception' do it 'logs exception message' do - Facter.log_exception(exception, message) + expect(logger).to receive(:error).with(expected_message) - expect(logger).to have_received(:error).with(expected_message) + Facter.log_exception(exception, message) end end @@ -578,9 +573,9 @@ def stub_no_fact it 'logs exception message' do exception.set_backtrace(backtrace) - Facter.log_exception(exception) + expect(logger).to receive(:error).with(expected_message) - expect(logger).to have_received(:error).with(expected_message) + Facter.log_exception(exception) end end @@ -651,9 +646,9 @@ def stub_no_fact it 'logs exception message' do exception.set_backtrace(backtrace) - Facter.log_exception(exception) + expect(logger).to receive(:error).with(expected_message) - expect(logger).to have_received(:error).with(expected_message) + Facter.log_exception(exception) end end @@ -662,9 +657,9 @@ def stub_no_fact let(:expected_message) { 'FlushFakeError' } it 'logs exception message' do - Facter.log_exception(exception) + expect(logger).to receive(:error).with(expected_message) - expect(logger).to have_received(:error).with(expected_message) + Facter.log_exception(exception) end end @@ -710,21 +705,21 @@ def stub_no_fact it 'calls logger with the debug message' do message = 'Some error message' - Facter.debugonce(message) + expect(logger).to receive(:debugonce).with(message) - expect(logger).to have_received(:debugonce).with(message) + Facter.debugonce(message) end it 'writes empty message when message is nil' do - Facter.debugonce(nil) + expect(logger).to receive(:debugonce).with(nil) - expect(logger).to have_received(:debugonce).with(nil) + Facter.debugonce(nil) end it 'when message is a hash' do - Facter.debugonce({ warn: 'message' }) + expect(logger).to receive(:debugonce).with({ warn: 'message' }) - expect(logger).to have_received(:debugonce).with({ warn: 'message' }) + Facter.debugonce({ warn: 'message' }) end it 'returns nil' do @@ -740,9 +735,9 @@ def stub_no_fact end it 'does not call the logger' do - Facter.debugonce('message') + expect(logger).not_to receive(:debug) - expect(logger).not_to have_received(:debug) + Facter.debugonce('message') end end end @@ -767,21 +762,21 @@ def stub_no_fact it 'calls logger with the warning message' do message = 'Some error message' - Facter.warnonce(message) + expect(logger).to receive(:warnonce).with(message) - expect(logger).to have_received(:warnonce).with(message) + Facter.warnonce(message) end it 'writes empty message when message is nil' do - Facter.warnonce(nil) + expect(logger).to receive(:warnonce).with(nil) - expect(logger).to have_received(:warnonce).with(nil) + Facter.warnonce(nil) end it 'when message is a hash' do - Facter.warnonce({ warn: 'message' }) + expect(logger).to receive(:warnonce).with({ warn: 'message' }) - expect(logger).to have_received(:warnonce).with({ warn: 'message' }) + Facter.warnonce({ warn: 'message' }) end it 'returns nil' do @@ -792,39 +787,36 @@ def stub_no_fact end describe '#warn' do - before do - allow(logger).to receive(:warn) - end - it 'calls logger' do message = 'Some error message' - Facter.warn(message) + expect(logger).to receive(:warn).with(message) - expect(logger).to have_received(:warn).with(message) + Facter.warn(message) end it 'when message is nil' do - Facter.warn(nil) + expect(logger).to receive(:warn).with('') - expect(logger).to have_received(:warn).with('') + Facter.warn(nil) end it 'when message is empty string' do + expect(logger).to receive(:warn).with('') + Facter.warn('') - expect(logger).to have_received(:warn).with('') end it 'when message is a hash' do - Facter.warn({ warn: 'message' }) + expect(logger).to receive(:warn).with('{:warn=>"message"}') - expect(logger).to have_received(:warn).with('{:warn=>"message"}') + Facter.warn({ warn: 'message' }) end it 'when message is an array' do - Facter.warn([1, 2, 3]) + expect(logger).to receive(:warn).with('[1, 2, 3]') - expect(logger).to have_received(:warn).with('[1, 2, 3]') + Facter.warn([1, 2, 3]) end it 'returns nil' do From 745322e0ba4bafad9be59730e305ddd3d9eaebcf Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Fri, 17 May 2024 23:18:17 -0700 Subject: [PATCH 5/8] (FACT-3468) Don't mutate Collection global log Previously, the Collection's class variable was cleared after each test. Now resolve the logger and set expectations on the log. We have to use `send` because the `log` method is private. --- spec/custom_facts/util/collection_spec.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/spec/custom_facts/util/collection_spec.rb b/spec/custom_facts/util/collection_spec.rb index 6ee3e0dab2..eb378a8828 100755 --- a/spec/custom_facts/util/collection_spec.rb +++ b/spec/custom_facts/util/collection_spec.rb @@ -23,17 +23,12 @@ def load(collection) load end let(:collection) { LegacyFacter::Util::Collection.new(internal_loader, external_loader) } - let(:logger) { instance_spy(Facter::Log) } + let(:logger) { collection.send(:log) } before do Singleton.__init__(Facter::FactManager) Singleton.__init__(Facter::FactLoader) Singleton.__init__(Facter::ClassDiscoverer) - allow(Facter::Log).to receive(:new).and_return(logger) - end - - after do - LegacyFacter::Util::Collection.instance_variable_set(:@log, nil) end it 'delegates its load_all method to its loader' do From c42324f8fcfe9674e7df0d4c970b4ab224d1eb20 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Fri, 17 May 2024 23:24:49 -0700 Subject: [PATCH 6/8] (FACT-3468) Get Facter global logger instead of stubbing it Previously, we were overwriting the global logger with an instance spy. In some cases we were not clearing it when we were done or we set it to a new instance of Logger.new(STDOUT). In the first case, the instance spy could leak across to other tests. In the second case, we've replaced the global logger with a different instance instead of replacing it with the original instance. All of this can be avoided by just getting the logger and setting expectations on it. Since it's a "class instance variable" we have to use `:@@logger` syntax. Since the global logger is not a spy, then we have to set the expectation before calling the method that triggers the message to be sent such as: expect(logger).to receive(:debug).with('...') log.debug('debug_message') --- spec/custom_facts/util/resolution_spec.rb | 6 +- spec/facter/util/file_helper_spec.rb | 21 ++-- spec/framework/config/config_reader_spec.rb | 7 +- spec/framework/config/fact_groups_spec.rb | 8 +- .../internal/internal_fact_manager_spec.rb | 24 ++-- spec/framework/logging/logger_spec.rb | 104 +++++++++--------- 6 files changed, 78 insertions(+), 92 deletions(-) diff --git a/spec/custom_facts/util/resolution_spec.rb b/spec/custom_facts/util/resolution_spec.rb index af0293f367..14c27c8dd7 100755 --- a/spec/custom_facts/util/resolution_spec.rb +++ b/spec/custom_facts/util/resolution_spec.rb @@ -5,11 +5,7 @@ subject(:resolution) { Facter::Util::Resolution.new(:foo, stub_fact) } let(:stub_fact) { double('fact', name: :stubfact) } - let(:logger) { instance_spy(Facter::Log) } - - before do - allow(Facter::Log).to receive(:new).and_return(logger) - end + let(:logger) { Facter::Log.class_variable_get(:@@logger) } it 'requires a name' do expect { Facter::Util::Resolution.new }.to raise_error(ArgumentError) diff --git a/spec/facter/util/file_helper_spec.rb b/spec/facter/util/file_helper_spec.rb index ee56b496c3..18d7c8835f 100644 --- a/spec/facter/util/file_helper_spec.rb +++ b/spec/facter/util/file_helper_spec.rb @@ -10,10 +10,9 @@ "Facter::Util::FileHelper - #{Facter::CYAN}File at: /Users/admin/file.txt is not accessible.#{Facter::RESET}" end let(:array_content) { ['line 1', 'line 2', 'line 3'] } - let(:logger_double) { instance_spy(Facter::Log) } + let(:log) { Facter::Log.class_variable_get(:@@logger) } before do - Facter::Log.class_variable_set(:@@logger, logger_double) allow(Facter).to receive(:debugging?).and_return(true) allow(Facter::OptionStore).to receive(:color).and_return(true) end @@ -59,9 +58,9 @@ end it "doesn't log anything" do - file_helper.safe_read(path) + expect(log).not_to receive(:debug) - expect(logger_double).not_to have_received(:debug) + file_helper.safe_read(path) end end @@ -89,10 +88,10 @@ end it 'logs a debug message' do - file_helper.safe_read(path) + allow(log).to receive(:debug) + expect(log).to receive(:debug).with(error_message) - expect(logger_double).to have_received(:debug) - .with(error_message) + file_helper.safe_read(path) end end end @@ -138,9 +137,9 @@ end it "doesn't log anything" do - file_helper.safe_readlines(path) + expect(log).not_to receive(:debug) - expect(logger_double).not_to have_received(:debug) + file_helper.safe_readlines(path) end end @@ -168,9 +167,9 @@ end it 'logs a debug message' do - file_helper.safe_read(path) + expect(log).to receive(:debug).with(error_message) - expect(logger_double).to have_received(:debug).with(error_message) + file_helper.safe_read(path) end end end diff --git a/spec/framework/config/config_reader_spec.rb b/spec/framework/config/config_reader_spec.rb index b2bcbbd711..708ed2ef60 100644 --- a/spec/framework/config/config_reader_spec.rb +++ b/spec/framework/config/config_reader_spec.rb @@ -143,18 +143,17 @@ context 'with invalid config file' do let(:config) { 'some corrupt information' } - let(:log) { instance_spy(Facter::Log) } + let(:log) { Facter::Log.class_variable_get(:@@logger) } before do - allow(Facter::Log).to receive(:new).and_return(log) allow(Hocon).to receive(:load).and_raise(StandardError) allow(log).to receive(:warn) end it 'loggs a warning' do - config_reader.init + expect(log).to receive(:warn).with(/Facter failed to read config file/) - expect(log).to have_received(:warn).with(/Facter failed to read config file/) + config_reader.init end end diff --git a/spec/framework/config/fact_groups_spec.rb b/spec/framework/config/fact_groups_spec.rb index 9ce3ba48c7..1625df5a94 100644 --- a/spec/framework/config/fact_groups_spec.rb +++ b/spec/framework/config/fact_groups_spec.rb @@ -132,16 +132,18 @@ context 'when ttls has hour instead of hour' do let(:ttls) { ['operating system' => '1 hour', 'memory' => '1 day', 'hostname' => '30 invalid_unit'] } - let(:logger) { instance_spy(Facter::Log) } + let(:logger) { Facter::Log.class_variable_get(:@@logger) } before do allow(Facter::Log).to receive(:new).and_return(logger) end it 'logs an error message' do + allow(logger).to receive(:error) + expect(logger).to receive(:error).with('Could not parse time unit invalid_units '\ + "(try #{Facter::FactGroups::STRING_TO_SECONDS.keys.reject(&:empty?).join(', ')})").twice + fg.get_group_ttls('hostname') - expect(logger).to have_received(:error).with('Could not parse time unit invalid_units '\ - "(try #{Facter::FactGroups::STRING_TO_SECONDS.keys.reject(&:empty?).join(', ')})").twice end it 'returns os ttl in seconds' do diff --git a/spec/framework/core/fact/internal/internal_fact_manager_spec.rb b/spec/framework/core/fact/internal/internal_fact_manager_spec.rb index eb499f35cb..b9dea4834f 100644 --- a/spec/framework/core/fact/internal/internal_fact_manager_spec.rb +++ b/spec/framework/core/fact/internal/internal_fact_manager_spec.rb @@ -82,7 +82,7 @@ type: :core) end - let(:logger_double) { instance_spy(Logger) } + let(:logger) { Facter::Log.class_variable_get(:@@logger) } before do allow(os_name_class_spy).to receive(:new).and_return(os_name_instance_spy) @@ -90,13 +90,6 @@ exception = StandardError.new('error_message') exception.set_backtrace(%w[backtrace]) allow(os_name_instance_spy).to receive(:call_the_resolver).and_raise(exception) - - allow(logger_double).to receive(:error) - Facter::Log.class_variable_set(:@@logger, logger_double) - end - - after do - Facter::Log.class_variable_set(:@@logger, Logger.new(STDOUT)) end it 'does not store the fact value' do @@ -108,19 +101,20 @@ it 'logs backtrace as error with --trace option' do allow(Facter::Options).to receive(:[]) allow(Facter::Options).to receive(:[]).with(:trace).and_return(true) - internal_fact_manager.resolve_facts([searched_fact]) - expect(logger_double) - .to have_received(:error) + expect(logger) + .to receive(:error) .with("Facter::InternalFactManager - #{colorize("error_message\nbacktrace", Facter::RED)}") - end - it 'logs error message as error without --trace option' do internal_fact_manager.resolve_facts([searched_fact]) + end - expect(logger_double) - .to have_received(:error) + it 'logs error message as error without --trace option' do + expect(logger) + .to receive(:error) .with("Facter::InternalFactManager - #{colorize('error_message', Facter::RED)}") + + internal_fact_manager.resolve_facts([searched_fact]) end end end diff --git a/spec/framework/logging/logger_spec.rb b/spec/framework/logging/logger_spec.rb index 110ab1e312..da5698a5da 100644 --- a/spec/framework/logging/logger_spec.rb +++ b/spec/framework/logging/logger_spec.rb @@ -3,17 +3,12 @@ describe Logger do subject(:log) { Facter::Log.new(Class) } - let(:multi_logger_double) { instance_spy(Logger) } + let(:logger) { Facter::Log.class_variable_get(:@@logger) } before do - Facter::Log.class_variable_set(:@@logger, multi_logger_double) Facter::Options[:color] = false end - after do - Facter::Log.class_variable_set(:@@logger, Logger.new(STDOUT)) - end - describe '#debug' do before do allow(Facter).to receive(:debugging?).and_return(true) @@ -23,26 +18,26 @@ it 'no debug messages are sent' do allow(Facter).to receive(:debugging?).and_return(false) - log.debug('info_message') + expect(logger).not_to receive(:debug) - expect(multi_logger_double).not_to have_received(:debug) + log.debug('info_message') end end shared_examples 'writes debug message with no color' do - it 'calls debug on multi_logger' do - log.debug('debug_message') + it 'calls debug on logger' do + expect(logger).to receive(:debug).with('Class - debug_message') - expect(multi_logger_double).to have_received(:debug).with('Class - debug_message') + log.debug('debug_message') end end shared_examples 'writes debug message with color' do - it 'calls debug on multi_logger' do - log.debug('debug_message') - - expect(multi_logger_double).to have_received(:debug) + it 'calls debug on logger' do + expect(logger).to receive(:debug) .with("Class - #{Facter::CYAN}debug_message#{Facter::RESET}") + + log.debug('debug_message') end end @@ -116,18 +111,18 @@ it 'writes the same debug message only once' do message = 'Some error message' + expect(logger).to receive(:debug).once.with("Class - #{message}") + log.debugonce(message) log.debugonce(message) - - expect(multi_logger_double).to have_received(:debug).once.with("Class - #{message}") end end describe '#info' do it 'writes info message' do - log.info('info_message') + expect(logger).to receive(:info).with('Class - info_message') - expect(multi_logger_double).to have_received(:info).with('Class - info_message') + log.info('info_message') end context 'when non Windows OS' do @@ -141,11 +136,11 @@ end it 'print Green (32) info message' do - log.info('info_message') - - expect(multi_logger_double) - .to have_received(:info) + expect(logger) + .to receive(:info) .with("Class - #{Facter::GREEN}info_message#{Facter::RESET}") + + log.info('info_message') end end end @@ -161,10 +156,10 @@ end it 'print info message' do - log.info('info_message') - - expect(multi_logger_double).to have_received(:info) + expect(logger).to receive(:info) .with("Class - #{Facter::GREEN}info_message#{Facter::RESET}") + + log.info('info_message') end end end @@ -172,9 +167,9 @@ describe '#warn' do it 'writes warn message' do - log.warn('warn_message') + expect(logger).to receive(:warn).with('Class - warn_message') - expect(multi_logger_double).to have_received(:warn).with('Class - warn_message') + log.warn('warn_message') end context 'when non Windows OS' do @@ -188,11 +183,11 @@ end it 'print Yellow (33) info message' do - log.warn('warn_message') - - expect(multi_logger_double) - .to have_received(:warn) + expect(logger) + .to receive(:warn) .with("Class - #{Facter::YELLOW}warn_message#{Facter::RESET}") + + log.warn('warn_message') end end end @@ -208,10 +203,10 @@ end it 'print warn message' do - log.warn('warn_message') - - expect(multi_logger_double).to have_received(:warn) + expect(logger).to receive(:warn) .with("Class - #{Facter::YELLOW}warn_message#{Facter::RESET}") + + log.warn('warn_message') end end end @@ -225,10 +220,10 @@ it 'writes the same debug message only once' do message = 'Some error message' + expect(logger).to receive(:warn).once.with("Class - #{message}") + log.warnonce(message) log.warnonce(message) - - expect(multi_logger_double).to have_received(:warn).once.with("Class - #{message}") end end @@ -236,31 +231,31 @@ it 'writes error message with color on macosx' do allow(OsDetector.instance).to receive(:detect).and_return(:macosx) - log.error('error_message', true) + expect(logger).to receive(:error).with("Class - #{Facter::RED}error_message#{Facter::RESET}") - expect(multi_logger_double).to have_received(:error).with("Class - #{Facter::RED}error_message#{Facter::RESET}") + log.error('error_message', true) end it 'writes error message colorized on Windows' do allow(OsDetector.instance).to receive(:identifier).and_return(:windows) - log.error('error_message', true) + expect(logger).to receive(:error).with("Class - #{Facter::RED}error_message#{Facter::RESET}") - expect(multi_logger_double).to have_received(:error).with("Class - #{Facter::RED}error_message#{Facter::RESET}") + log.error('error_message', true) end it 'writes error message' do - log.error('error_message') + expect(logger).to receive(:error).with('Class - error_message') - expect(multi_logger_double).to have_received(:error).with('Class - error_message') + log.error('error_message') end end describe '#level=' do it 'sets the log level' do - Facter::Log.level = :error + expect(logger).to receive(:level=).with(:error) - expect(multi_logger_double).to have_received(:level=).with(:error) + Facter::Log.level = :error end end @@ -268,20 +263,21 @@ let(:exception) { Exception.new('Test exception') } it 'writes exception message without --trace option' do - log.log_exception(exception) + expect(logger).to receive(:error).with("Class - #{colorize('Test exception', Facter::RED)}") - expect(multi_logger_double).to have_received(:error).with("Class - #{colorize('Test exception', Facter::RED)}") + log.log_exception(exception) end it 'writes exception message and backtrace with --trace option' do allow(Facter::Options).to receive(:[]) allow(Facter::Options).to receive(:[]).with(:trace).and_return(true) allow(exception).to receive(:backtrace).and_return(['backtrace:1']) - log.log_exception(exception) - expect(multi_logger_double) - .to have_received(:error) + expect(logger) + .to receive(:error) .with("Class - #{colorize("Test exception\nbacktrace:1", Facter::RED)}") + + log.log_exception(exception) end end @@ -293,21 +289,21 @@ it 'clears debugonce messages' do message = 'Some error message' + expect(logger).to receive(:debug).twice.with("Class - #{message}") + log.debugonce(message) Facter::Log.clear_messages log.debugonce(message) - - expect(multi_logger_double).to have_received(:debug).twice.with("Class - #{message}") end it 'clears warnonce messages' do message = 'Some error message' + expect(logger).to receive(:warn).twice.with("Class - #{message}") + log.warnonce(message) Facter::Log.clear_messages log.warnonce(message) - - expect(multi_logger_double).to have_received(:warn).twice.with("Class - #{message}") end end From e4c9ba38825aabff173a0d5233bc0665b1c9e828 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Fri, 17 May 2024 23:25:45 -0700 Subject: [PATCH 7/8] (FACT-3468) Pass Log to execution methods Previously, we were stubbing Facter::Log.new to return our instance_spy. But we can just pass the instance_spy as an keyword argument to the `execute` method. Confusingly the `execute` method takes a `logger` keyword argument, but internally it creates a `Facter::Log` object, so the argument should really be called `log`, but that's part of the execution API and can't be changed. --- .../core/execution/fact_manager_spec.rb | 8 +++----- spec/custom_facts/core/execution/posix_spec.rb | 2 +- spec/custom_facts/core/execution_spec.rb | 16 ++++++++++++---- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/spec/custom_facts/core/execution/fact_manager_spec.rb b/spec/custom_facts/core/execution/fact_manager_spec.rb index 1a6ce8f7f6..f89edac77c 100644 --- a/spec/custom_facts/core/execution/fact_manager_spec.rb +++ b/spec/custom_facts/core/execution/fact_manager_spec.rb @@ -134,17 +134,15 @@ def handy_method before do allow(Facter::Core::Execution::Popen3).to receive(:popen3e).with({ 'LC_ALL' => 'C', 'LANG' => 'C' }, command) .and_return(['', 'some error']) - allow(Facter::Log).to receive(:new).with(executor).and_return(logger) - allow(Facter::Log).to receive(:new).with('foo').and_return(logger) allow(File).to receive(:executable?).with(command).and_return(true) allow(FileTest).to receive(:file?).with(command).and_return(true) end - it 'loggs warning messages on stderr' do - executor.execute(command) + it 'logs warning messages on stderr' do + executor.execute(command, logger: logger) expect(logger).to have_received(:debug).with('Command /bin/foo completed with '\ - 'the following stderr message: some error') + 'the following stderr message: some error') end end diff --git a/spec/custom_facts/core/execution/posix_spec.rb b/spec/custom_facts/core/execution/posix_spec.rb index db05a234df..6634f4c86e 100644 --- a/spec/custom_facts/core/execution/posix_spec.rb +++ b/spec/custom_facts/core/execution/posix_spec.rb @@ -107,7 +107,7 @@ end context 'when calling execute_command' do - let(:logger) { instance_spy(Logger) } + let(:logger) { instance_spy(Facter::Log) } it 'executes a command' do expect(posix_executor.execute_command('/usr/bin/true', nil, logger)).to eq(['', '']) diff --git a/spec/custom_facts/core/execution_spec.rb b/spec/custom_facts/core/execution_spec.rb index d43c4f287c..4f18df89d9 100644 --- a/spec/custom_facts/core/execution_spec.rb +++ b/spec/custom_facts/core/execution_spec.rb @@ -50,7 +50,6 @@ before do allow(impl).to receive(:which).with('waffles').and_return('/under/the/honey/are/the/waffles') allow(impl).to receive(:execute_command) - allow(logger).to receive(:warn) end context 'with default parameters' do @@ -75,11 +74,20 @@ end end - it 'emits a warning' do + it 'emits a warning to the default logger' do + expect(logger).to receive(:warn) + .with('Unexpected key passed to Facter::Core::Execution.execute option: time_limit,bad_opt' \ + ' - valid keys: on_fail,expand,logger,timeout') + execution.execute('waffles', time_limit: 90, bad_opt: true) - expect(logger).to have_received(:warn) + end + + it 'ignores the passed in logger when logging the warning' do + expect(logger).to receive(:warn) .with('Unexpected key passed to Facter::Core::Execution.execute option: time_limit,bad_opt' \ - ' - valid keys: on_fail,expand,logger,timeout') + ' - valid keys: on_fail,expand,logger,timeout') + + execution.execute('waffles', time_limit: 90, bad_opt: true, logger: Facter::Log.new('ignored')) end end end From 27e7fd511753f9725b5a2e2023dcc3daec8a2a0f Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Mon, 20 May 2024 16:14:25 -0700 Subject: [PATCH 8/8] (FACT-3468) Handle Socket.gethostname returning nil If gethostname returned nil, then we called `empty?` on nil which raised a NoMethodError. Instead check for nil and don't attempt to resolve the hostname fact. Also cleanup some tests which were writing errors to the log unnecessarily. --- lib/facter/resolvers/hostname.rb | 5 ++++ .../util/facts/posix/virtual_detector_spec.rb | 1 + spec/framework/config/fact_groups_spec.rb | 25 +++++++++++-------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/facter/resolvers/hostname.rb b/lib/facter/resolvers/hostname.rb index c84811a388..eba8782db1 100644 --- a/lib/facter/resolvers/hostname.rb +++ b/lib/facter/resolvers/hostname.rb @@ -19,6 +19,11 @@ def post_resolve(fact_name, _options) def retrieve_info(fact_name) require 'socket' output = Socket.gethostname + unless output + log.debug('Socket.gethostname failed to return hostname') + return + end + hostname, domain = retrieve_from_fqdn(output) fqdn = retrieve_with_addrinfo(hostname) if hostname_and_no_domain?(hostname, domain) diff --git a/spec/facter/util/facts/posix/virtual_detector_spec.rb b/spec/facter/util/facts/posix/virtual_detector_spec.rb index df9413b3be..63f23911cf 100644 --- a/spec/facter/util/facts/posix/virtual_detector_spec.rb +++ b/spec/facter/util/facts/posix/virtual_detector_spec.rb @@ -279,6 +279,7 @@ before do allow(Facter::Resolvers::Containers).to receive(:resolve).with(:vm).and_return(nil) allow(Facter::Resolvers::Freebsd::Virtual).to receive(:resolve).with(:vm).and_return(nil) + allow(Facter::Resolvers::Openbsd::Virtual).to receive(:resolve).with(:vm).and_return(nil) allow(Facter::Resolvers::Linux::DmiBios).to receive(:resolve).with(:bios_vendor).and_return(nil) allow(Facter::Resolvers::Linux::DmiBios).to receive(:resolve).with(:product_name).and_return(nil) allow(Facter::Resolvers::VirtWhat).to receive(:resolve).with(:vm).and_return('lxc') diff --git a/spec/framework/config/fact_groups_spec.rb b/spec/framework/config/fact_groups_spec.rb index 1625df5a94..2655127bcc 100644 --- a/spec/framework/config/fact_groups_spec.rb +++ b/spec/framework/config/fact_groups_spec.rb @@ -130,8 +130,21 @@ end end - context 'when ttls has hour instead of hour' do - let(:ttls) { ['operating system' => '1 hour', 'memory' => '1 day', 'hostname' => '30 invalid_unit'] } + context 'when ttls has singular hour instead of plural hours' do + let(:ttls) { ['operating system' => '1 hour', 'memory' => '1 day'] } + + it 'returns os ttl in seconds' do + expect(fg.get_group_ttls('operating system')).to eq(3600) + end + + it 'returns memory ttl in seconds' do + expect(fg.get_group_ttls('memory')).to eq(86_400) + end + end + + context 'when ttls are invalid' do + let(:ttls) { ['hostname' => '30 invalid_unit'] } + let(:logger) { Facter::Log.class_variable_get(:@@logger) } before do @@ -145,14 +158,6 @@ fg.get_group_ttls('hostname') end - - it 'returns os ttl in seconds' do - expect(fg.get_group_ttls('operating system')).to eq(3600) - end - - it 'returns memory ttl in seconds' do - expect(fg.get_group_ttls('memory')).to eq(86_400) - end end end end