From 112cc58bcabb5034882f94a4d3533d6ea38fb9de Mon Sep 17 00:00:00 2001 From: BogdanIrimie Date: Fri, 26 Jun 2020 14:01:51 +0300 Subject: [PATCH 1/9] (FACT-2218) Add virtual fact for macosx. --- lib/facts/macosx/virtual.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 lib/facts/macosx/virtual.rb diff --git a/lib/facts/macosx/virtual.rb b/lib/facts/macosx/virtual.rb new file mode 100644 index 0000000000..846da58170 --- /dev/null +++ b/lib/facts/macosx/virtual.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module Facts + module Macosx + class Virtual + FACT_NAME = 'virtual' + + def call_the_resolver + fact_value = check_vmware || check_virtualbox || check_parallels + + Facter::ResolvedFact.new(FACT_NAME, fact_value) + end + + private + + def check_vmware + model_identifier = Facter::Resolvers::SystemProfiler.resolve(:model_identifier) + return 'vmware' if model_identifier&.start_with?('VMware') + end + + def check_virtualbox + boot_rom_version = Facter::Resolvers::SystemProfiler.resolve(:boot_rom_version) + return 'virtualbox' if boot_rom_version&.start_with?('VirtualBox') + end + + def check_parallels + subsystem_vendor_id = Facter::Resolvers::SystemProfiler.resolve(:subsystem_vendor_id) + return 'parallels' if subsystem_vendor_id&.start_with?('0x1ab8') + end + end + end +end From e33a16ae65b40ba4b6a5c68ee31c9293d5366a38 Mon Sep 17 00:00:00 2001 From: BogdanIrimie Date: Fri, 26 Jun 2020 15:54:31 +0300 Subject: [PATCH 2/9] (FACT-2218) Add resolver for SPEthernetDataType --- lib/facts/macosx/virtual.rb | 2 +- .../macosx/system_profile_2_resolver.rb | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 lib/resolvers/macosx/system_profile_2_resolver.rb diff --git a/lib/facts/macosx/virtual.rb b/lib/facts/macosx/virtual.rb index 846da58170..8f46751ae0 100644 --- a/lib/facts/macosx/virtual.rb +++ b/lib/facts/macosx/virtual.rb @@ -24,7 +24,7 @@ def check_virtualbox end def check_parallels - subsystem_vendor_id = Facter::Resolvers::SystemProfiler.resolve(:subsystem_vendor_id) + subsystem_vendor_id = Facter::Resolvers::SystemProfiler_2.resolve(:subsystem_vendor_id) return 'parallels' if subsystem_vendor_id&.start_with?('0x1ab8') end end diff --git a/lib/resolvers/macosx/system_profile_2_resolver.rb b/lib/resolvers/macosx/system_profile_2_resolver.rb new file mode 100644 index 0000000000..329dab8989 --- /dev/null +++ b/lib/resolvers/macosx/system_profile_2_resolver.rb @@ -0,0 +1,39 @@ + +# frozen_string_literal: true + +module Facter + module Resolvers + class SystemProfiler_2 < BaseResolver + + @semaphore = Mutex.new + @fact_list = {} + + class << self + private + + def post_resolve(fact_name) + @fact_list.fetch(fact_name) { retrieve_system_profiler(fact_name) } + end + + def retrieve_system_profiler(fact_name) + @fact_list ||= {} + + log.debug 'Executing command: system_profiler SPSoftwareDataType SPHardwareDataType' + output = Facter::Core::Execution.execute( + 'system_profiler SPEthernetDataType', logger: log + ).force_encoding('UTF-8') + @fact_list = output.scan(/.*:[ ].*$/).map { |e| e.strip.match(/(.*?): (.*)/).captures }.to_h + normalize_factlist + + @fact_list[fact_name] + end + + def normalize_factlist + @fact_list = @fact_list.map do |k, v| + [k.downcase.tr(' ', '_').delete("\(\)").to_sym, v] + end.to_h + end + end + end + end +end From 589fff35c4f5f681e07a2f8e297392c3b9164e63 Mon Sep 17 00:00:00 2001 From: BogdanIrimie Date: Sun, 28 Jun 2020 11:28:59 +0300 Subject: [PATCH 3/9] (FACT-2218) Remove system_profile_2_resolver. Create lists with information from system profile records. --- lib/facts/macosx/virtual.rb | 2 +- .../macosx/system_profile_2_resolver.rb | 39 ------------------- .../macosx/system_profiler_resolver.rb | 34 +++++----------- 3 files changed, 11 insertions(+), 64 deletions(-) delete mode 100644 lib/resolvers/macosx/system_profile_2_resolver.rb diff --git a/lib/facts/macosx/virtual.rb b/lib/facts/macosx/virtual.rb index 8f46751ae0..846da58170 100644 --- a/lib/facts/macosx/virtual.rb +++ b/lib/facts/macosx/virtual.rb @@ -24,7 +24,7 @@ def check_virtualbox end def check_parallels - subsystem_vendor_id = Facter::Resolvers::SystemProfiler_2.resolve(:subsystem_vendor_id) + subsystem_vendor_id = Facter::Resolvers::SystemProfiler.resolve(:subsystem_vendor_id) return 'parallels' if subsystem_vendor_id&.start_with?('0x1ab8') end end diff --git a/lib/resolvers/macosx/system_profile_2_resolver.rb b/lib/resolvers/macosx/system_profile_2_resolver.rb deleted file mode 100644 index 329dab8989..0000000000 --- a/lib/resolvers/macosx/system_profile_2_resolver.rb +++ /dev/null @@ -1,39 +0,0 @@ - -# frozen_string_literal: true - -module Facter - module Resolvers - class SystemProfiler_2 < BaseResolver - - @semaphore = Mutex.new - @fact_list = {} - - class << self - private - - def post_resolve(fact_name) - @fact_list.fetch(fact_name) { retrieve_system_profiler(fact_name) } - end - - def retrieve_system_profiler(fact_name) - @fact_list ||= {} - - log.debug 'Executing command: system_profiler SPSoftwareDataType SPHardwareDataType' - output = Facter::Core::Execution.execute( - 'system_profiler SPEthernetDataType', logger: log - ).force_encoding('UTF-8') - @fact_list = output.scan(/.*:[ ].*$/).map { |e| e.strip.match(/(.*?): (.*)/).captures }.to_h - normalize_factlist - - @fact_list[fact_name] - end - - def normalize_factlist - @fact_list = @fact_list.map do |k, v| - [k.downcase.tr(' ', '_').delete("\(\)").to_sym, v] - end.to_h - end - end - end - end -end diff --git a/lib/resolvers/macosx/system_profiler_resolver.rb b/lib/resolvers/macosx/system_profiler_resolver.rb index 70eb659c6e..2a02275bc5 100644 --- a/lib/resolvers/macosx/system_profiler_resolver.rb +++ b/lib/resolvers/macosx/system_profiler_resolver.rb @@ -3,30 +3,16 @@ module Facter module Resolvers class SystemProfiler < BaseResolver - # model_name - # model_identifier - # processor_name - # processor_speed - # number_of_processors - # total_number_of_cores - # l2_cache_per_core - # l3_cache - # hyper-threading_technology - # memory - # boot_rom_version - # serial_number_system - # hardware_uuid - # activation_lock_status - # system_version - # kernel_version - # boot_volume - # boot_mode - # computer_name - # user_name - # secure_virtual_memory - # system_integrity_protection - # time_since_boot - # smc_version_system + sp_hardware_data_type = %i[model_name model_identifier processor_speed number_of_processors processor_name + total_number_of_cores l2_cache_per_core l3_cache memory boot_rom_version + smc_version_system serial_number_system hardware_uuid, hyper-threading_technology, + activation_lock_status] + + sp_software_data_type = %i[system_version kernel_version boot_volume boot_mode computer_name + user_name secure_virtual_memory system_integrity_protection time_since_boot] + + sp_ethernet_data_type = %i[type bus vendor_id device_id subsystem_vendor_id + subsystem_id revision_id bsd_name kext_name location version] @semaphore = Mutex.new @fact_list = {} From a1779fb86336148150a9703b37e478c7e976456a Mon Sep 17 00:00:00 2001 From: BogdanIrimie Date: Sun, 28 Jun 2020 12:00:36 +0300 Subject: [PATCH 4/9] (FACT-2218) Add system profile executor. Use executor in system profile resolver. Update namespace for System profile resolver. --- lib/facts/macosx/is_virtual.rb | 6 +-- lib/facts/macosx/system_profiler/boot_mode.rb | 2 +- .../system_profiler/boot_rom_version.rb | 2 +- .../macosx/system_profiler/boot_volume.rb | 2 +- .../macosx/system_profiler/computer_name.rb | 2 +- lib/facts/macosx/system_profiler/cores.rb | 2 +- .../macosx/system_profiler/hardware_uuid.rb | 2 +- .../macosx/system_profiler/kernel_version.rb | 2 +- .../system_profiler/l2_cache_per_core.rb | 2 +- lib/facts/macosx/system_profiler/l3_cache.rb | 2 +- lib/facts/macosx/system_profiler/memory.rb | 2 +- .../system_profiler/model_identifier.rb | 2 +- .../macosx/system_profiler/model_name.rb | 2 +- .../macosx/system_profiler/processor_name.rb | 2 +- .../macosx/system_profiler/processor_speed.rb | 2 +- .../macosx/system_profiler/processors.rb | 2 +- .../system_profiler/secure_virtual_memory.rb | 2 +- .../macosx/system_profiler/serial_number.rb | 2 +- .../macosx/system_profiler/smc_version.rb | 2 +- .../macosx/system_profiler/system_version.rb | 2 +- lib/facts/macosx/system_profiler/uptime.rb | 2 +- lib/facts/macosx/system_profiler/username.rb | 2 +- lib/facts/macosx/virtual.rb | 6 +-- .../macosx/system_profiler_resolver.rb | 50 +++++++++---------- .../macosx/utils/system_profile_executor.rb | 35 +++++++++++++ 25 files changed, 87 insertions(+), 52 deletions(-) create mode 100644 lib/resolvers/macosx/utils/system_profile_executor.rb diff --git a/lib/facts/macosx/is_virtual.rb b/lib/facts/macosx/is_virtual.rb index a351abda39..67c74727f9 100644 --- a/lib/facts/macosx/is_virtual.rb +++ b/lib/facts/macosx/is_virtual.rb @@ -16,13 +16,13 @@ def virtual? end def hypervisor_name - model_identifier = Facter::Resolvers::SystemProfiler.resolve(:model_identifier) + model_identifier = Facter::Resolvers::Macosx::SystemProfiler.resolve(:model_identifier) return 'vmware' if model_identifier&.start_with?('VMware') - boot_rom_version = Facter::Resolvers::SystemProfiler.resolve(:boot_rom_version) + boot_rom_version = Facter::Resolvers::Macosx::SystemProfiler.resolve(:boot_rom_version) return 'virtualbox' if boot_rom_version&.start_with?('VirtualBox') - subsystem_vendor_id = Facter::Resolvers::SystemProfiler.resolve(:subsystem_vendor_id) + subsystem_vendor_id = Facter::Resolvers::Macosx::SystemProfiler.resolve(:subsystem_vendor_id) return 'parallels' if subsystem_vendor_id&.start_with?('0x1ab8') end end diff --git a/lib/facts/macosx/system_profiler/boot_mode.rb b/lib/facts/macosx/system_profiler/boot_mode.rb index 06bb8f121d..7288af4b83 100644 --- a/lib/facts/macosx/system_profiler/boot_mode.rb +++ b/lib/facts/macosx/system_profiler/boot_mode.rb @@ -8,7 +8,7 @@ class BootMode ALIASES = 'sp_boot_mode' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:boot_mode) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:boot_mode) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/boot_rom_version.rb b/lib/facts/macosx/system_profiler/boot_rom_version.rb index 48dd31f8a6..95ea808470 100644 --- a/lib/facts/macosx/system_profiler/boot_rom_version.rb +++ b/lib/facts/macosx/system_profiler/boot_rom_version.rb @@ -8,7 +8,7 @@ class BootRomVersion ALIASES = 'sp_boot_rom_version' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:boot_rom_version) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:boot_rom_version) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/boot_volume.rb b/lib/facts/macosx/system_profiler/boot_volume.rb index 4fa6a9bb7a..83c878d1b2 100644 --- a/lib/facts/macosx/system_profiler/boot_volume.rb +++ b/lib/facts/macosx/system_profiler/boot_volume.rb @@ -8,7 +8,7 @@ class BootVolume ALIASES = 'sp_boot_volume' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:boot_volume) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:boot_volume) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/computer_name.rb b/lib/facts/macosx/system_profiler/computer_name.rb index 8e09e281f6..cb321e45fc 100644 --- a/lib/facts/macosx/system_profiler/computer_name.rb +++ b/lib/facts/macosx/system_profiler/computer_name.rb @@ -8,7 +8,7 @@ class ComputerName ALIASES = 'sp_local_host_name' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:computer_name) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:computer_name) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/cores.rb b/lib/facts/macosx/system_profiler/cores.rb index a858f7e839..b0beb22766 100644 --- a/lib/facts/macosx/system_profiler/cores.rb +++ b/lib/facts/macosx/system_profiler/cores.rb @@ -8,7 +8,7 @@ class Cores ALIASES = 'sp_number_processors' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:total_number_of_cores) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:total_number_of_cores) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/hardware_uuid.rb b/lib/facts/macosx/system_profiler/hardware_uuid.rb index 6ffee71f61..ffb6b60162 100644 --- a/lib/facts/macosx/system_profiler/hardware_uuid.rb +++ b/lib/facts/macosx/system_profiler/hardware_uuid.rb @@ -8,7 +8,7 @@ class HardwareUuid ALIASES = 'sp_hardware_uuid' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:hardware_uuid) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:hardware_uuid) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/kernel_version.rb b/lib/facts/macosx/system_profiler/kernel_version.rb index 5e67dea87f..35bcdd5ed7 100644 --- a/lib/facts/macosx/system_profiler/kernel_version.rb +++ b/lib/facts/macosx/system_profiler/kernel_version.rb @@ -8,7 +8,7 @@ class KernelVersion ALIASES = 'sp_kernel_version' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:kernel_version) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:kernel_version) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/l2_cache_per_core.rb b/lib/facts/macosx/system_profiler/l2_cache_per_core.rb index 63fd0facf7..4510e8f328 100644 --- a/lib/facts/macosx/system_profiler/l2_cache_per_core.rb +++ b/lib/facts/macosx/system_profiler/l2_cache_per_core.rb @@ -8,7 +8,7 @@ class L2CachePerCore ALIASES = 'sp_l2_cache_per_core' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:l2_cache_per_core) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:l2_cache_per_core) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/l3_cache.rb b/lib/facts/macosx/system_profiler/l3_cache.rb index 681ea1151c..ea8e03b52c 100644 --- a/lib/facts/macosx/system_profiler/l3_cache.rb +++ b/lib/facts/macosx/system_profiler/l3_cache.rb @@ -8,7 +8,7 @@ class L3Cache ALIASES = 'sp_l3_cache' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:l3_cache) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:l3_cache) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/memory.rb b/lib/facts/macosx/system_profiler/memory.rb index 46d35f0404..e076169278 100644 --- a/lib/facts/macosx/system_profiler/memory.rb +++ b/lib/facts/macosx/system_profiler/memory.rb @@ -8,7 +8,7 @@ class Memory ALIASES = 'sp_memory' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:memory) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:memory) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/model_identifier.rb b/lib/facts/macosx/system_profiler/model_identifier.rb index 06535376c7..bcd91ce8bc 100644 --- a/lib/facts/macosx/system_profiler/model_identifier.rb +++ b/lib/facts/macosx/system_profiler/model_identifier.rb @@ -8,7 +8,7 @@ class ModelIdentifier ALIASES = 'sp_machine_model' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:model_identifier) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:model_identifier) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/model_name.rb b/lib/facts/macosx/system_profiler/model_name.rb index 7cd2fdde65..d46702a215 100644 --- a/lib/facts/macosx/system_profiler/model_name.rb +++ b/lib/facts/macosx/system_profiler/model_name.rb @@ -8,7 +8,7 @@ class ModelName ALIASES = 'sp_machine_name' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:model_name) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:model_name) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/processor_name.rb b/lib/facts/macosx/system_profiler/processor_name.rb index 00f6e95e4d..cf0e7a5cb9 100644 --- a/lib/facts/macosx/system_profiler/processor_name.rb +++ b/lib/facts/macosx/system_profiler/processor_name.rb @@ -8,7 +8,7 @@ class ProcessorName ALIASES = 'sp_processor_name' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:processor_name) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:processor_name) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/processor_speed.rb b/lib/facts/macosx/system_profiler/processor_speed.rb index 00b09e1392..5e3fc9931e 100644 --- a/lib/facts/macosx/system_profiler/processor_speed.rb +++ b/lib/facts/macosx/system_profiler/processor_speed.rb @@ -8,7 +8,7 @@ class ProcessorSpeed ALIASES = 'sp_current_processor_speed' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:processor_speed) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:processor_speed) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/processors.rb b/lib/facts/macosx/system_profiler/processors.rb index 89ee5d3114..8712e718cc 100644 --- a/lib/facts/macosx/system_profiler/processors.rb +++ b/lib/facts/macosx/system_profiler/processors.rb @@ -8,7 +8,7 @@ class Processors ALIASES = 'sp_cpu_type' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:number_of_processors) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:number_of_processors) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/secure_virtual_memory.rb b/lib/facts/macosx/system_profiler/secure_virtual_memory.rb index 189f17314b..f92c52ef7a 100644 --- a/lib/facts/macosx/system_profiler/secure_virtual_memory.rb +++ b/lib/facts/macosx/system_profiler/secure_virtual_memory.rb @@ -8,7 +8,7 @@ class SecureVirtualMemory ALIASES = 'sp_secure_vm' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:secure_virtual_memory) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:secure_virtual_memory) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/serial_number.rb b/lib/facts/macosx/system_profiler/serial_number.rb index 81be57c4d5..1a27b299f9 100644 --- a/lib/facts/macosx/system_profiler/serial_number.rb +++ b/lib/facts/macosx/system_profiler/serial_number.rb @@ -8,7 +8,7 @@ class SerialNumber ALIASES = 'sp_serial_number' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:serial_number_system) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:serial_number_system) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/smc_version.rb b/lib/facts/macosx/system_profiler/smc_version.rb index 0794d9c852..cffd4d37ab 100644 --- a/lib/facts/macosx/system_profiler/smc_version.rb +++ b/lib/facts/macosx/system_profiler/smc_version.rb @@ -8,7 +8,7 @@ class SmcVersion ALIASES = 'sp_smc_version_system' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:smc_version_system) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:smc_version_system) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/system_version.rb b/lib/facts/macosx/system_profiler/system_version.rb index 8e774f3e03..d4e76c7657 100644 --- a/lib/facts/macosx/system_profiler/system_version.rb +++ b/lib/facts/macosx/system_profiler/system_version.rb @@ -8,7 +8,7 @@ class SystemVersion ALIASES = 'sp_os_version' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:system_version) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:system_version) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/uptime.rb b/lib/facts/macosx/system_profiler/uptime.rb index bcf6e19d3d..8da9122738 100644 --- a/lib/facts/macosx/system_profiler/uptime.rb +++ b/lib/facts/macosx/system_profiler/uptime.rb @@ -8,7 +8,7 @@ class Uptime ALIASES = 'sp_uptime' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:time_since_boot) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:time_since_boot) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/system_profiler/username.rb b/lib/facts/macosx/system_profiler/username.rb index 34ac22fc6d..9a09242b20 100644 --- a/lib/facts/macosx/system_profiler/username.rb +++ b/lib/facts/macosx/system_profiler/username.rb @@ -8,7 +8,7 @@ class Username ALIASES = 'sp_user_name' def call_the_resolver - fact_value = Facter::Resolvers::SystemProfiler.resolve(:user_name) + fact_value = Facter::Resolvers::Macosx::SystemProfiler.resolve(:user_name) [Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] end end diff --git a/lib/facts/macosx/virtual.rb b/lib/facts/macosx/virtual.rb index 846da58170..409185e53c 100644 --- a/lib/facts/macosx/virtual.rb +++ b/lib/facts/macosx/virtual.rb @@ -14,17 +14,17 @@ def call_the_resolver private def check_vmware - model_identifier = Facter::Resolvers::SystemProfiler.resolve(:model_identifier) + model_identifier = Facter::Resolvers::Macosx::SystemProfiler.resolve(:model_identifier) return 'vmware' if model_identifier&.start_with?('VMware') end def check_virtualbox - boot_rom_version = Facter::Resolvers::SystemProfiler.resolve(:boot_rom_version) + boot_rom_version = Facter::Resolvers::Macosx::SystemProfiler.resolve(:boot_rom_version) return 'virtualbox' if boot_rom_version&.start_with?('VirtualBox') end def check_parallels - subsystem_vendor_id = Facter::Resolvers::SystemProfiler.resolve(:subsystem_vendor_id) + subsystem_vendor_id = Facter::Resolvers::Macosx::SystemProfiler.resolve(:subsystem_vendor_id) return 'parallels' if subsystem_vendor_id&.start_with?('0x1ab8') end end diff --git a/lib/resolvers/macosx/system_profiler_resolver.rb b/lib/resolvers/macosx/system_profiler_resolver.rb index 2a02275bc5..5c0a5cf242 100644 --- a/lib/resolvers/macosx/system_profiler_resolver.rb +++ b/lib/resolvers/macosx/system_profiler_resolver.rb @@ -2,45 +2,45 @@ module Facter module Resolvers - class SystemProfiler < BaseResolver - sp_hardware_data_type = %i[model_name model_identifier processor_speed number_of_processors processor_name + module Macosx + class SystemProfiler < BaseResolver + SP_HARDWARE_DATA_TYPE = %i[model_name model_identifier processor_speed number_of_processors processor_name total_number_of_cores l2_cache_per_core l3_cache memory boot_rom_version smc_version_system serial_number_system hardware_uuid, hyper-threading_technology, activation_lock_status] - sp_software_data_type = %i[system_version kernel_version boot_volume boot_mode computer_name + SP_SOFTWARE_DATA_TYPE = %i[system_version kernel_version boot_volume boot_mode computer_name user_name secure_virtual_memory system_integrity_protection time_since_boot] - sp_ethernet_data_type = %i[type bus vendor_id device_id subsystem_vendor_id + SP_ETHERNET_DATA_TYPE = %i[type bus vendor_id device_id subsystem_vendor_id subsystem_id revision_id bsd_name kext_name location version] - @semaphore = Mutex.new - @fact_list = {} + @semaphore = Mutex.new + @fact_list = {} - class << self - private + class << self + private - def post_resolve(fact_name) - @fact_list.fetch(fact_name) { retrieve_system_profiler(fact_name) } - end + def post_resolve(fact_name) + @fact_list.fetch(fact_name) { retrieve_system_profiler(fact_name) } + end - def retrieve_system_profiler(fact_name) - @fact_list ||= {} + def retrieve_system_profiler(fact_name) + @fact_list ||= {} - log.debug 'Executing command: system_profiler SPSoftwareDataType SPHardwareDataType' - output = Facter::Core::Execution.execute( - 'system_profiler SPHardwareDataType SPSoftwareDataType', logger: log - ).force_encoding('UTF-8') - @fact_list = output.scan(/.*:[ ].*$/).map { |e| e.strip.match(/(.*?): (.*)/).captures }.to_h - normalize_factlist + log.debug 'Executing command: system_profiler SPSoftwareDataType SPHardwareDataType' - @fact_list[fact_name] - end + case fact_name + when *SP_HARDWARE_DATA_TYPE + @fact_list.merge!(SystemProfileExecutor.execute('SPHardwareDataType')) + when *SP_SOFTWARE_DATA_TYPE + @fact_list.merge!(SystemProfileExecutor.execute('SPSoftwareDataType')) + when *SP_ETHERNET_DATA_TYPE + @fact_list.merge!(SystemProfileExecutor.execute('SPEthernetDataType')) + end - def normalize_factlist - @fact_list = @fact_list.map do |k, v| - [k.downcase.tr(' ', '_').delete("\(\)").to_sym, v] - end.to_h + @fact_list[fact_name] + end end end end diff --git a/lib/resolvers/macosx/utils/system_profile_executor.rb b/lib/resolvers/macosx/utils/system_profile_executor.rb new file mode 100644 index 0000000000..b222f50a69 --- /dev/null +++ b/lib/resolvers/macosx/utils/system_profile_executor.rb @@ -0,0 +1,35 @@ + +module Facter + module Resolvers + module Macosx + class SystemProfileExecutor + @log = Log.new(self) + + class << self + def execute(category_name) + @log.debug "Executing command: system_profiler #{category_name}" + output = Facter::Core::Execution.execute( + "system_profiler #{category_name}", logger: @log + ).force_encoding('UTF-8') + + system_profiler_hash = output_to_hash(output) + + normalize_keys(system_profiler_hash) + end + + private + + def output_to_hash(output) + output.scan(/.*:[ ].*$/).map { |e| e.strip.match(/(.*?): (.*)/).captures }.to_h + end + + def normalize_keys(system_profiler_hash) + system_profiler_hash.map do |k, v| + [k.downcase.tr(' ', '_').delete("\(\)").to_sym, v] + end.to_h + end + end + end + end + end +end From 29692cd47d9806deb454c406347309b24e4bb766 Mon Sep 17 00:00:00 2001 From: BogdanIrimie Date: Mon, 29 Jun 2020 12:11:11 +0300 Subject: [PATCH 5/9] (FACT-2218) Fix namespace in tests. --- spec/facter/facts/macosx/is_virtual_spec.rb | 24 +++++++++---------- .../macosx/system_profiler/boot_mode_spec.rb | 6 ++--- .../system_profiler/boot_rom_version_spec.rb | 6 ++--- .../system_profiler/boot_volume_spec.rb | 6 ++--- .../system_profiler/computer_name_spec.rb | 6 ++--- .../macosx/system_profiler/cores_spec.rb | 6 ++--- .../system_profiler/hardware_uuid_spec.rb | 6 ++--- .../system_profiler/kernel_version_spec.rb | 6 ++--- .../system_profiler/l2_cache_per_core_spec.rb | 6 ++--- .../macosx/system_profiler/l3_cache_spec.rb | 6 ++--- .../macosx/system_profiler/memory_spec.rb | 6 ++--- .../system_profiler/model_identifier_spec.rb | 6 ++--- .../macosx/system_profiler/model_name_spec.rb | 6 ++--- .../system_profiler/processor_name_spec.rb | 6 ++--- .../system_profiler/processor_speed_spec.rb | 6 ++--- .../macosx/system_profiler/processors_spec.rb | 6 ++--- .../secure_virtual_memory_spec.rb | 6 ++--- .../system_profiler/serial_number_spec.rb | 6 ++--- .../system_profiler/smc_version_spec.rb | 6 ++--- .../system_profiler/system_version_spec.rb | 6 ++--- .../macosx/system_profiler/uptime_spec.rb | 6 ++--- .../macosx/system_profiler/username_spec.rb | 6 ++--- .../resolvers/system_profile_resolver_spec.rb | 2 +- 23 files changed, 76 insertions(+), 76 deletions(-) diff --git a/spec/facter/facts/macosx/is_virtual_spec.rb b/spec/facter/facts/macosx/is_virtual_spec.rb index bad2a7f8a1..f291b76aaf 100644 --- a/spec/facter/facts/macosx/is_virtual_spec.rb +++ b/spec/facter/facts/macosx/is_virtual_spec.rb @@ -5,33 +5,33 @@ subject(:fact) { Facts::Macosx::IsVirtual.new } before do - allow(Facter::Resolvers::SystemProfiler).to receive(:resolve) + allow(Facter::Resolvers::Macosx::SystemProfiler).to receive(:resolve) .with(:model_identifier) .and_return('MacBookPro11,4') - allow(Facter::Resolvers::SystemProfiler).to receive(:resolve) + allow(Facter::Resolvers::Macosx::SystemProfiler).to receive(:resolve) .with(:boot_rom_version) .and_return('1037.60.58.0.0 (iBridge: 17.16.12551.0.0,0)') - allow(Facter::Resolvers::SystemProfiler).to receive(:resolve) + allow(Facter::Resolvers::Macosx::SystemProfiler).to receive(:resolve) .with(:subsystem_vendor_id) .and_return('0x123') end - it 'calls Facter::Resolvers::SystemProfile with model_identifier' do - expect(Facter::Resolvers::SystemProfiler).to receive(:resolve) + it 'calls Facter::Resolvers::Macosx::SystemProfile with model_identifier' do + expect(Facter::Resolvers::Macosx::SystemProfiler).to receive(:resolve) .with(:model_identifier) fact.call_the_resolver end - it 'calls Facter::Resolvers::SystemProfile with boot_rom_version' do - expect(Facter::Resolvers::SystemProfiler).to receive(:resolve) + it 'calls Facter::Resolvers::Macosx::SystemProfile with boot_rom_version' do + expect(Facter::Resolvers::Macosx::SystemProfiler).to receive(:resolve) .with(:boot_rom_version) fact.call_the_resolver end - it 'calls Facter::Resolvers::SystemProfile with subsystem_vendor_id' do - allow(Facter::Resolvers::SystemProfiler).to receive(:resolve) + it 'calls Facter::Resolvers::Macosx::SystemProfile with subsystem_vendor_id' do + allow(Facter::Resolvers::Macosx::SystemProfiler).to receive(:resolve) .with(:subsystem_vendor_id) fact.call_the_resolver end @@ -39,7 +39,7 @@ context 'when on virtual machine' do context 'with hypervisor vmware' do before do - allow(Facter::Resolvers::SystemProfiler) + allow(Facter::Resolvers::Macosx::SystemProfiler) .to receive(:resolve) .with(:model_identifier) .and_return('VMware') @@ -56,7 +56,7 @@ context 'when hypervisor VirtualBox' do before do - allow(Facter::Resolvers::SystemProfiler) + allow(Facter::Resolvers::Macosx::SystemProfiler) .to receive(:resolve) .with(:boot_rom_version) .and_return('VirtualBox') @@ -73,7 +73,7 @@ context 'when hypervisor Parallels' do before do - allow(Facter::Resolvers::SystemProfiler) + allow(Facter::Resolvers::Macosx::SystemProfiler) .to receive(:resolve) .with(:subsystem_vendor_id) .and_return('0x1ab8') diff --git a/spec/facter/facts/macosx/system_profiler/boot_mode_spec.rb b/spec/facter/facts/macosx/system_profiler/boot_mode_spec.rb index e975f3f0b6..b2155fdaa1 100644 --- a/spec/facter/facts/macosx/system_profiler/boot_mode_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/boot_mode_spec.rb @@ -7,13 +7,13 @@ let(:value) { 'Normal' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:boot_mode).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:boot_mode) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:boot_mode) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/boot_rom_version_spec.rb b/spec/facter/facts/macosx/system_profiler/boot_rom_version_spec.rb index 8c593b7631..9de054bac9 100644 --- a/spec/facter/facts/macosx/system_profiler/boot_rom_version_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/boot_rom_version_spec.rb @@ -7,13 +7,13 @@ let(:value) { '194.0.0.0.0' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:boot_rom_version).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:boot_rom_version) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:boot_rom_version) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/boot_volume_spec.rb b/spec/facter/facts/macosx/system_profiler/boot_volume_spec.rb index 2d24b577f9..d472505477 100644 --- a/spec/facter/facts/macosx/system_profiler/boot_volume_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/boot_volume_spec.rb @@ -7,13 +7,13 @@ let(:value) { 'Macintosh HD' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:boot_volume).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:boot_volume) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:boot_volume) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/computer_name_spec.rb b/spec/facter/facts/macosx/system_profiler/computer_name_spec.rb index d20c6e0e44..35401f0cff 100644 --- a/spec/facter/facts/macosx/system_profiler/computer_name_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/computer_name_spec.rb @@ -7,13 +7,13 @@ let(:value) { 'Test1’s MacBook Pro' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:computer_name).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:computer_name) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:computer_name) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/cores_spec.rb b/spec/facter/facts/macosx/system_profiler/cores_spec.rb index 1b83616e90..29fa4e0992 100644 --- a/spec/facter/facts/macosx/system_profiler/cores_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/cores_spec.rb @@ -7,13 +7,13 @@ let(:value) { '' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:total_number_of_cores).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:total_number_of_cores) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:total_number_of_cores) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/hardware_uuid_spec.rb b/spec/facter/facts/macosx/system_profiler/hardware_uuid_spec.rb index b8b12f0fd8..03135d0a01 100644 --- a/spec/facter/facts/macosx/system_profiler/hardware_uuid_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/hardware_uuid_spec.rb @@ -7,13 +7,13 @@ let(:value) { '7C3B701F-B88A-56C6-83F4-ACBD450075C4' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:hardware_uuid).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:hardware_uuid) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:hardware_uuid) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/kernel_version_spec.rb b/spec/facter/facts/macosx/system_profiler/kernel_version_spec.rb index 47c84e40a0..a0babc89d6 100644 --- a/spec/facter/facts/macosx/system_profiler/kernel_version_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/kernel_version_spec.rb @@ -7,13 +7,13 @@ let(:value) { 'Darwin 18.7.0' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:kernel_version).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:kernel_version) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:kernel_version) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/l2_cache_per_core_spec.rb b/spec/facter/facts/macosx/system_profiler/l2_cache_per_core_spec.rb index b0d01a9788..a3902971dc 100644 --- a/spec/facter/facts/macosx/system_profiler/l2_cache_per_core_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/l2_cache_per_core_spec.rb @@ -7,13 +7,13 @@ let(:value) { '256 KB' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:l2_cache_per_core).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:l2_cache_per_core) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:l2_cache_per_core) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/l3_cache_spec.rb b/spec/facter/facts/macosx/system_profiler/l3_cache_spec.rb index 7b4485785e..7b8428ead9 100644 --- a/spec/facter/facts/macosx/system_profiler/l3_cache_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/l3_cache_spec.rb @@ -7,13 +7,13 @@ let(:value) { '6 MB' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:l3_cache).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:l3_cache) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:l3_cache) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/memory_spec.rb b/spec/facter/facts/macosx/system_profiler/memory_spec.rb index fc89e33e6a..258d7d8be2 100644 --- a/spec/facter/facts/macosx/system_profiler/memory_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/memory_spec.rb @@ -7,13 +7,13 @@ let(:value) { '16 GB' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:memory).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:memory) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:memory) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/model_identifier_spec.rb b/spec/facter/facts/macosx/system_profiler/model_identifier_spec.rb index fa7b95881a..50abebc6e4 100644 --- a/spec/facter/facts/macosx/system_profiler/model_identifier_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/model_identifier_spec.rb @@ -7,13 +7,13 @@ let(:value) { 'MacBookPro11,4' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:model_identifier).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:model_identifier) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:model_identifier) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/model_name_spec.rb b/spec/facter/facts/macosx/system_profiler/model_name_spec.rb index 9f1cf00b85..9837a4267a 100644 --- a/spec/facter/facts/macosx/system_profiler/model_name_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/model_name_spec.rb @@ -7,13 +7,13 @@ let(:value) { 'MacBook Pro' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:model_name).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:model_name) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:model_name) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/processor_name_spec.rb b/spec/facter/facts/macosx/system_profiler/processor_name_spec.rb index 9aa38d58cd..5be1806800 100644 --- a/spec/facter/facts/macosx/system_profiler/processor_name_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/processor_name_spec.rb @@ -7,13 +7,13 @@ let(:value) { 'Intel Core i7' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:processor_name).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:processor_name) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:processor_name) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/processor_speed_spec.rb b/spec/facter/facts/macosx/system_profiler/processor_speed_spec.rb index c16b5fa820..0754b823a6 100644 --- a/spec/facter/facts/macosx/system_profiler/processor_speed_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/processor_speed_spec.rb @@ -7,13 +7,13 @@ let(:value) { '2.8 GHz' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:processor_speed).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:processor_speed) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:processor_speed) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/processors_spec.rb b/spec/facter/facts/macosx/system_profiler/processors_spec.rb index b452169131..e549cf6d21 100644 --- a/spec/facter/facts/macosx/system_profiler/processors_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/processors_spec.rb @@ -7,13 +7,13 @@ let(:value) { '1' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:number_of_processors).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:number_of_processors) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:number_of_processors) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/secure_virtual_memory_spec.rb b/spec/facter/facts/macosx/system_profiler/secure_virtual_memory_spec.rb index 3cdcbf5289..4ef9df9a02 100644 --- a/spec/facter/facts/macosx/system_profiler/secure_virtual_memory_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/secure_virtual_memory_spec.rb @@ -7,13 +7,13 @@ let(:value) { 'Enabled' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:secure_virtual_memory).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:secure_virtual_memory) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:secure_virtual_memory) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/serial_number_spec.rb b/spec/facter/facts/macosx/system_profiler/serial_number_spec.rb index 26fc26a56a..092f6c3af6 100644 --- a/spec/facter/facts/macosx/system_profiler/serial_number_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/serial_number_spec.rb @@ -7,13 +7,13 @@ let(:value) { 'C02WW1LAG8WL' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:serial_number_system).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:serial_number_system) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:serial_number_system) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/smc_version_spec.rb b/spec/facter/facts/macosx/system_profiler/smc_version_spec.rb index bc4b2e9bb5..96663ba092 100644 --- a/spec/facter/facts/macosx/system_profiler/smc_version_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/smc_version_spec.rb @@ -7,13 +7,13 @@ let(:value) { '2.29f24' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:smc_version_system).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:smc_version_system) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:smc_version_system) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/system_version_spec.rb b/spec/facter/facts/macosx/system_profiler/system_version_spec.rb index 44fd00b998..10e39c957a 100644 --- a/spec/facter/facts/macosx/system_profiler/system_version_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/system_version_spec.rb @@ -7,13 +7,13 @@ let(:value) { 'macOS 10.14.6 (18G95)' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:system_version).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:system_version) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:system_version) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/uptime_spec.rb b/spec/facter/facts/macosx/system_profiler/uptime_spec.rb index c12e176779..0936b22611 100644 --- a/spec/facter/facts/macosx/system_profiler/uptime_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/uptime_spec.rb @@ -7,13 +7,13 @@ let(:value) { '26 days 22:12' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:time_since_boot).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:time_since_boot) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:time_since_boot) end it 'returns a resolved fact' do diff --git a/spec/facter/facts/macosx/system_profiler/username_spec.rb b/spec/facter/facts/macosx/system_profiler/username_spec.rb index a84cbd85f1..cf0a09de07 100644 --- a/spec/facter/facts/macosx/system_profiler/username_spec.rb +++ b/spec/facter/facts/macosx/system_profiler/username_spec.rb @@ -7,13 +7,13 @@ let(:value) { 'Test1 Test2 (test1.test2)' } before do - allow(Facter::Resolvers::SystemProfiler).to \ + allow(Facter::Resolvers::Macosx::SystemProfiler).to \ receive(:resolve).with(:user_name).and_return(value) end - it 'calls Facter::Resolvers::SystemProfiler' do + it 'calls Facter::Resolvers::Macosx::SystemProfiler' do fact.call_the_resolver - expect(Facter::Resolvers::SystemProfiler).to have_received(:resolve).with(:user_name) + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:user_name) end it 'returns a resolved fact' do diff --git a/spec/facter/resolvers/system_profile_resolver_spec.rb b/spec/facter/resolvers/system_profile_resolver_spec.rb index 06923feee1..de5d08028c 100644 --- a/spec/facter/resolvers/system_profile_resolver_spec.rb +++ b/spec/facter/resolvers/system_profile_resolver_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -describe Facter::Resolvers::SystemProfiler do +describe Facter::Resolvers::Macosx::SystemProfiler do subject(:system_profiler) { Facter::Resolvers::SystemProfiler } let(:log_spy) { instance_spy(Facter::Log) } From 6f9bac656afc5ff7b29df5c0c120184f7bc0c336 Mon Sep 17 00:00:00 2001 From: BogdanIrimie Date: Mon, 29 Jun 2020 16:15:54 +0300 Subject: [PATCH 6/9] (FACT-2218) Reorganize test. --- .../macosx/system_profiler_resolver.rb | 12 +- .../macosx/utils/system_profile_executor.rb | 1 + .../resolvers/system_profile_resolver_spec.rb | 204 ++++++++++++------ 3 files changed, 145 insertions(+), 72 deletions(-) diff --git a/lib/resolvers/macosx/system_profiler_resolver.rb b/lib/resolvers/macosx/system_profiler_resolver.rb index 5c0a5cf242..feb0a4d34b 100644 --- a/lib/resolvers/macosx/system_profiler_resolver.rb +++ b/lib/resolvers/macosx/system_profiler_resolver.rb @@ -5,15 +5,15 @@ module Resolvers module Macosx class SystemProfiler < BaseResolver SP_HARDWARE_DATA_TYPE = %i[model_name model_identifier processor_speed number_of_processors processor_name - total_number_of_cores l2_cache_per_core l3_cache memory boot_rom_version - smc_version_system serial_number_system hardware_uuid, hyper-threading_technology, - activation_lock_status] + total_number_of_cores l2_cache_per_core l3_cache memory boot_rom_version + smc_version_system serial_number_system hardware_uuid hyper-threading_technology + activation_lock_status].freeze SP_SOFTWARE_DATA_TYPE = %i[system_version kernel_version boot_volume boot_mode computer_name - user_name secure_virtual_memory system_integrity_protection time_since_boot] + user_name secure_virtual_memory system_integrity_protection time_since_boot].freeze SP_ETHERNET_DATA_TYPE = %i[type bus vendor_id device_id subsystem_vendor_id - subsystem_id revision_id bsd_name kext_name location version] + subsystem_id revision_id bsd_name kext_name location version].freeze @semaphore = Mutex.new @fact_list = {} @@ -28,8 +28,6 @@ def post_resolve(fact_name) def retrieve_system_profiler(fact_name) @fact_list ||= {} - log.debug 'Executing command: system_profiler SPSoftwareDataType SPHardwareDataType' - case fact_name when *SP_HARDWARE_DATA_TYPE @fact_list.merge!(SystemProfileExecutor.execute('SPHardwareDataType')) diff --git a/lib/resolvers/macosx/utils/system_profile_executor.rb b/lib/resolvers/macosx/utils/system_profile_executor.rb index b222f50a69..2c6f247e70 100644 --- a/lib/resolvers/macosx/utils/system_profile_executor.rb +++ b/lib/resolvers/macosx/utils/system_profile_executor.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Facter module Resolvers diff --git a/spec/facter/resolvers/system_profile_resolver_spec.rb b/spec/facter/resolvers/system_profile_resolver_spec.rb index de5d08028c..543e96e089 100644 --- a/spec/facter/resolvers/system_profile_resolver_spec.rb +++ b/spec/facter/resolvers/system_profile_resolver_spec.rb @@ -1,98 +1,172 @@ # frozen_string_literal: true describe Facter::Resolvers::Macosx::SystemProfiler do - subject(:system_profiler) { Facter::Resolvers::SystemProfiler } + 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', + model_identifier: 'MacBookPro11,4', + processor_name: 'Intel Core i7', + processor_speed: '2.8 GHz', + number_of_processors: '1', + total_number_of_cores: '4', + l2_cache_per_core: '256 KB', + l3_cache: '6 MB', + 'hyper-threading_technology': 'Enabled', + memory: '16 GB', + boot_rom_version: '1037.60.58.0.0 (iBridge: 17.16.12551.0.0,0)', + smc_version_system: '2.29f24', + serial_number_system: '123456789AAA', + hardware_uuid: '12345678-1111-2222-AAAA-AABBCCDDEEFF', + activation_lock_status: 'Disabled' + } + end + + let(:sp_software_data_type) do + { + system_version: 'macOS 10.15.2 (19C57)', + kernel_version: 'Darwin 19.2.0', + boot_volume: 'Macintosh HD', + boot_mode: 'Normal', + computer_name: 'Test1’s MacBook Pro', + user_name: 'Test1 Test2 (test1.test2)', + secure_virtual_memory: 'Enabled', + system_integrity_protection: 'Enabled', + time_since_boot: '3:28' + } + end + + let(:sp_ethernet_data_type) do + { + type: 'Ethernet Controller', + bus: 'PCI', + vendor_id: '0x8086', + device_id: '0x100f', + subsystem_vendor_id: '0x1ab8', + subsystem_id: '0x0400', + revision_id: '0x0000', + bsd_name: 'en0', + kext_name: 'AppleIntel8254XEthernet.kext', + location: '/System/Library/Extensions/IONetworkingFamily.kext/Contents/PlugIns/AppleIntel8254XEthernet.kext', + version: '3.1.5' + } + end + before do system_profiler.instance_variable_set(:@log, log_spy) - allow(Facter::Core::Execution).to receive(:execute) - .with('system_profiler SPHardwareDataType SPSoftwareDataType', logger: log_spy) - .and_return(load_fixture('system_profiler').read) end - it 'returns boot_mode' do - expect(system_profiler.resolve(:boot_mode)).to eq('Normal') - end + context 'when information is obtain from SPHardwareDataType' do + before do + allow(Facter::Resolvers::Macosx::SystemProfileExecutor) + .to receive(:execute) + .with('SPHardwareDataType') + .and_return(sp_hardware_data_type_hash) + end - it 'returns boot_rom_version' do - expect(system_profiler.resolve(:boot_rom_version)).to eq('1037.60.58.0.0 (iBridge: 17.16.12551.0.0,0)') - end + it 'returns boot_rom_version' do + expect(system_profiler.resolve(:boot_rom_version)).to eq('1037.60.58.0.0 (iBridge: 17.16.12551.0.0,0)') + end - it 'returns boot_volume' do - expect(system_profiler.resolve(:boot_volume)).to eq('Macintosh HD') - end + it 'returns cores' do + expect(system_profiler.resolve(:total_number_of_cores)).to eq('4') + end - it 'returns computer_name' do - expect(system_profiler.resolve(:computer_name)).to eq('Test1’s MacBook Pro') - end + it 'returns hardware_uuid' do + expect(system_profiler.resolve(:hardware_uuid)).to eq('12345678-1111-2222-AAAA-AABBCCDDEEFF') + end - it 'returns cores' do - expect(system_profiler.resolve(:total_number_of_cores)).to eq('4') - end + it 'returns l2_cache_per_core' do + expect(system_profiler.resolve(:l2_cache_per_core)).to eq('256 KB') + end - it 'returns hardware_uuid' do - expect(system_profiler.resolve(:hardware_uuid)).to eq('12345678-1111-2222-AAAA-AABBCCDDEEFF') - end + it 'returns l3_cache' do + expect(system_profiler.resolve(:l3_cache)).to eq('6 MB') + end - it 'returns kernel_version' do - expect(system_profiler.resolve(:kernel_version)).to eq('Darwin 19.2.0') - end + it 'returns memory' do + expect(system_profiler.resolve(:memory)).to eq('16 GB') + end - it 'returns l2_cache_per_core' do - expect(system_profiler.resolve(:l2_cache_per_core)).to eq('256 KB') - end + it 'returns model_identifier' do + expect(system_profiler.resolve(:model_identifier)).to eq('MacBookPro11,4') + end - it 'returns l3_cache' do - expect(system_profiler.resolve(:l3_cache)).to eq('6 MB') - end + it 'returns model_name' do + expect(system_profiler.resolve(:model_name)).to eq('MacBook Pro') + end - it 'returns memory' do - expect(system_profiler.resolve(:memory)).to eq('16 GB') - end + it 'returns processor_name' do + expect(system_profiler.resolve(:processor_name)).to eq('Intel Core i7') + end - it 'returns model_identifier' do - expect(system_profiler.resolve(:model_identifier)).to eq('MacBookPro11,4') - end + it 'returns processor_speed' do + expect(system_profiler.resolve(:processor_speed)).to eq('2.8 GHz') + end - it 'returns model_name' do - expect(system_profiler.resolve(:model_name)).to eq('MacBook Pro') - end + it 'returns number_of_processors' do + expect(system_profiler.resolve(:number_of_processors)).to eq('1') + end - it 'returns processor_name' do - expect(system_profiler.resolve(:processor_name)).to eq('Intel Core i7') - end + it 'returns serial_number' do + expect(system_profiler.resolve(:serial_number_system)).to eq('123456789AAA') + end - it 'returns processor_speed' do - expect(system_profiler.resolve(:processor_speed)).to eq('2.8 GHz') + it 'returns smc_version' do + expect(system_profiler.resolve(:smc_version_system)).to eq('2.29f24') + end end - it 'returns number_of_processors' do - expect(system_profiler.resolve(:number_of_processors)).to eq('1') - end + context 'when information is obtained from SPSoftwareDataType' do + before do + allow(Facter::Resolvers::Macosx::SystemProfileExecutor) + .to receive(:execute) + .with('SPSoftwareDataType') + .and_return(sp_software_data_type) + end - it 'returns secure_virtual_memory' do - expect(system_profiler.resolve(:secure_virtual_memory)).to eq('Enabled') - end + it 'returns boot_mode' do + expect(system_profiler.resolve(:boot_mode)).to eq('Normal') + end - it 'returns serial_number' do - expect(system_profiler.resolve(:serial_number_system)).to eq('123456789AAA') - end + it 'returns boot_volume' do + expect(system_profiler.resolve(:boot_volume)).to eq('Macintosh HD') + end - it 'returns smc_version' do - expect(system_profiler.resolve(:smc_version_system)).to eq('2.29f24') - end + it 'returns computer_name' do + expect(system_profiler.resolve(:computer_name)).to eq('Test1’s MacBook Pro') + end - it 'returns system_version' do - expect(system_profiler.resolve(:system_version)).to eq('macOS 10.15.2 (19C57)') - end + it 'returns kernel_version' do + expect(system_profiler.resolve(:kernel_version)).to eq('Darwin 19.2.0') + end - it 'returns time_since_boot' do - expect(system_profiler.resolve(:time_since_boot)).to eq('3:28') - end + it 'returns secure_virtual_memory' do + expect(system_profiler.resolve(:secure_virtual_memory)).to eq('Enabled') + end + + it 'returns system_version' do + expect(system_profiler.resolve(:system_version)).to eq('macOS 10.15.2 (19C57)') + end + + it 'returns time_since_boot' do + expect(system_profiler.resolve(:time_since_boot)).to eq('3:28') + end - it 'returns username' do - expect(system_profiler.resolve(:user_name)).to eq('Test1 Test2 (test1.test2)') + it 'returns username' do + expect(system_profiler.resolve(:user_name)).to eq('Test1 Test2 (test1.test2)') + end + end + + context 'when information is obtained from SPEthernetDataType' do + before do + allow(Facter::Resolvers::Macosx::SystemProfileExecutor) + .to receive(:execute) + .with('SPEthernetDataType') + .and_return(sp_ethernet_data_type) + end end end From cbeffa0bb0554e211e80a70bbdeff853d9119a0d Mon Sep 17 00:00:00 2001 From: BogdanIrimie Date: Mon, 29 Jun 2020 16:24:16 +0300 Subject: [PATCH 7/9] (FACT-2218) Add test for ethnernat data type. --- .../resolvers/system_profile_resolver_spec.rb | 45 +++++++++++++++++++ .../system_profile_sp_ethernet_data_type | 15 +++++++ .../system_profile_sp_hardware_data_type | 19 ++++++++ .../system_profile_sp_software_data_type | 13 ++++++ 4 files changed, 92 insertions(+) create mode 100644 spec/fixtures/system_profile_sp_ethernet_data_type create mode 100644 spec/fixtures/system_profile_sp_hardware_data_type create mode 100644 spec/fixtures/system_profile_sp_software_data_type diff --git a/spec/facter/resolvers/system_profile_resolver_spec.rb b/spec/facter/resolvers/system_profile_resolver_spec.rb index 543e96e089..060f640c5b 100644 --- a/spec/facter/resolvers/system_profile_resolver_spec.rb +++ b/spec/facter/resolvers/system_profile_resolver_spec.rb @@ -168,5 +168,50 @@ .with('SPEthernetDataType') .and_return(sp_ethernet_data_type) end + + it 'returns type' do + expect(system_profiler.resolve(:type)).to eq('Ethernet Controller') + end + + it 'returns bus' do + expect(system_profiler.resolve(:bus)).to eq('PCI') + end + + it 'returns vendor_id' do + expect(system_profiler.resolve(:vendor_id)).to eq('0x8086') + end + + it 'returns device_id' do + expect(system_profiler.resolve(:device_id)).to eq('0x100f') + end + + it 'returns subsystem_vendor_id' do + expect(system_profiler.resolve(:subsystem_vendor_id)).to eq('0x1ab8') + end + + it 'returns subsystem_id' do + expect(system_profiler.resolve(:subsystem_id)).to eq('0x0400') + end + + it 'returns revision_id' do + expect(system_profiler.resolve(:revision_id)).to eq('0x0000') + end + + it 'returns bsd_name' do + expect(system_profiler.resolve(:bsd_name)).to eq('en0') + end + + it 'returns kext_name' do + expect(system_profiler.resolve(:kext_name)).to eq('AppleIntel8254XEthernet.kext') + end + + it 'returns location' do + expect(system_profiler.resolve(:location)) + .to eq('/System/Library/Extensions/IONetworkingFamily.kext/Contents/PlugIns/AppleIntel8254XEthernet.kext') + end + + it 'returns version' do + expect(system_profiler.resolve(:version)).to eq('3.1.5') + end end end diff --git a/spec/fixtures/system_profile_sp_ethernet_data_type b/spec/fixtures/system_profile_sp_ethernet_data_type new file mode 100644 index 0000000000..484fd71233 --- /dev/null +++ b/spec/fixtures/system_profile_sp_ethernet_data_type @@ -0,0 +1,15 @@ +Ethernet Cards: + + ethernet: + + Type: Ethernet Controller + Bus: PCI + Vendor ID: 0x8086 + Device ID: 0x100f + Subsystem Vendor ID: 0x1ab8 + Subsystem ID: 0x0400 + Revision ID: 0x0000 + BSD name: en0 + Kext name: AppleIntel8254XEthernet.kext + Location: /System/Library/Extensions/IONetworkingFamily.kext/Contents/PlugIns/AppleIntel8254XEthernet.kext + Version: 3.1.5 \ No newline at end of file diff --git a/spec/fixtures/system_profile_sp_hardware_data_type b/spec/fixtures/system_profile_sp_hardware_data_type new file mode 100644 index 0000000000..885877957e --- /dev/null +++ b/spec/fixtures/system_profile_sp_hardware_data_type @@ -0,0 +1,19 @@ +Hardware: + + Hardware Overview: + + Model Name: MacBook Pro + Model Identifier: MacBookPro11,4 + Processor Name: Intel Core i7 + Processor Speed: 2.8 GHz + Number of Processors: 1 + Total Number of Cores: 4 + L2 Cache (per Core): 256 KB + L3 Cache: 6 MB + Hyper-Threading Technology: Enabled + Memory: 16 GB + Boot ROM Version: 1037.60.58.0.0 (iBridge: 17.16.12551.0.0,0) + SMC Version (system): 2.29f24 + Serial Number (system): 123456789AAA + Hardware UUID: 12345678-1111-2222-AAAA-AABBCCDDEEFF + Activation Lock Status: Disabled \ No newline at end of file diff --git a/spec/fixtures/system_profile_sp_software_data_type b/spec/fixtures/system_profile_sp_software_data_type new file mode 100644 index 0000000000..b807b182f2 --- /dev/null +++ b/spec/fixtures/system_profile_sp_software_data_type @@ -0,0 +1,13 @@ +Software: + + System Software Overview: + + System Version: macOS 10.15.2 (19C57) + Kernel Version: Darwin 19.2.0 + Boot Volume: Macintosh HD + Boot Mode: Normal + Computer Name: Test1’s MacBook Pro + User Name: Test1 Test2 (test1.test2) + Secure Virtual Memory: Enabled + System Integrity Protection: Enabled + Time since boot: 3:28 \ No newline at end of file From fdfac8a4bdb80ec235c6924295b37b4539cf07f8 Mon Sep 17 00:00:00 2001 From: BogdanIrimie Date: Mon, 29 Jun 2020 17:26:46 +0300 Subject: [PATCH 8/9] (FACT-2218) Add test system profile executor. --- .rubocop_todo.yml | 2 + .../macosx/utils/system_profile_executor.rb | 4 +- .../utils/system_profile_executor_spec.rb | 48 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 spec/facter/resolvers/macosx/utils/system_profile_executor_spec.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 5ad815e82f..127653c9ff 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -92,6 +92,8 @@ RSpec/FilePath: - 'spec/framework/formatters/legacy_fact_formatter_spec.rb' - 'spec/framework/formatters/yaml_fact_formatter_spec.rb' - 'spec/framework/utils/utils_spec.rb' + - 'spec/facter/resolvers/macosx/utils/system_profile_executor_spec.rb' + # Offense count: 15 # Configuration parameters: AssignmentOnly. diff --git a/lib/resolvers/macosx/utils/system_profile_executor.rb b/lib/resolvers/macosx/utils/system_profile_executor.rb index 2c6f247e70..3acbe0aabe 100644 --- a/lib/resolvers/macosx/utils/system_profile_executor.rb +++ b/lib/resolvers/macosx/utils/system_profile_executor.rb @@ -11,7 +11,9 @@ def execute(category_name) @log.debug "Executing command: system_profiler #{category_name}" output = Facter::Core::Execution.execute( "system_profiler #{category_name}", logger: @log - ).force_encoding('UTF-8') + )&.force_encoding('UTF-8') + + return unless output system_profiler_hash = output_to_hash(output) diff --git a/spec/facter/resolvers/macosx/utils/system_profile_executor_spec.rb b/spec/facter/resolvers/macosx/utils/system_profile_executor_spec.rb new file mode 100644 index 0000000000..463b7c6115 --- /dev/null +++ b/spec/facter/resolvers/macosx/utils/system_profile_executor_spec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +describe Facter::Resolvers::Macosx::SystemProfileExecutor do + subject(:system_profiler_executor) { Facter::Resolvers::Macosx::SystemProfileExecutor } + + describe '#execute' do + before do + allow(Facter::Core::Execution).to receive(:execute) + end + + it 'calls Facter::Core::Execution.execute' do + system_profiler_executor.execute('SPEthernetDataType') + + expect(Facter::Core::Execution).to have_received(:execute) + end + + context 'when executing system_profiler with SPEthernetDataType argument' do + let(:eth_data_hash) do + { + type: 'Ethernet Controller', + bus: 'PCI', + vendor_id: '0x8086', + device_id: '0x100f', + subsystem_vendor_id: '0x1ab8', + subsystem_id: '0x0400', + revision_id: '0x0000', + bsd_name: 'en0', + kext_name: 'AppleIntel8254XEthernet.kext', + location: '/System/Library/Extensions/IONetworkingFamily.kext/Contents/PlugIns/AppleIntel8254XEthernet.kext', + version: '3.1.5' + } + end + + before do + allow(Facter::Core::Execution) + .to receive(:execute) + .with('system_profiler SPEthernetDataType', any_args) + .and_return(load_fixture('system_profile_sp_ethernet_data_type').read) + end + + it 'returns a hash with the information' do + ethernet_data_type_hash = system_profiler_executor.execute('SPEthernetDataType') + + expect(ethernet_data_type_hash).to eq(eth_data_hash) + end + end + end +end From 09c9f9ce77c85d8412a23fd52f46989cf194bfed Mon Sep 17 00:00:00 2001 From: BogdanIrimie Date: Mon, 29 Jun 2020 17:55:47 +0300 Subject: [PATCH 9/9] (FACT-2218) Add test for virtual fact. Refactor the test from is_virtual. --- spec/facter/facts/macosx/is_virtual_spec.rb | 50 +++++------ spec/facter/facts/macosx/virtual_spec.rb | 96 +++++++++++++++++++++ 2 files changed, 118 insertions(+), 28 deletions(-) create mode 100644 spec/facter/facts/macosx/virtual_spec.rb diff --git a/spec/facter/facts/macosx/is_virtual_spec.rb b/spec/facter/facts/macosx/is_virtual_spec.rb index f291b76aaf..5cc2e2b9b2 100644 --- a/spec/facter/facts/macosx/is_virtual_spec.rb +++ b/spec/facter/facts/macosx/is_virtual_spec.rb @@ -18,22 +18,30 @@ .and_return('0x123') end - it 'calls Facter::Resolvers::Macosx::SystemProfile with model_identifier' do - expect(Facter::Resolvers::Macosx::SystemProfiler).to receive(:resolve) - .with(:model_identifier) - fact.call_the_resolver - end + context 'when on physical machine' do + it 'calls Facter::Resolvers::Macosx::SystemProfile with model_identifier' do + fact.call_the_resolver - it 'calls Facter::Resolvers::Macosx::SystemProfile with boot_rom_version' do - expect(Facter::Resolvers::Macosx::SystemProfiler).to receive(:resolve) - .with(:boot_rom_version) - fact.call_the_resolver - end + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:model_identifier) + end - it 'calls Facter::Resolvers::Macosx::SystemProfile with subsystem_vendor_id' do - allow(Facter::Resolvers::Macosx::SystemProfiler).to receive(:resolve) - .with(:subsystem_vendor_id) - fact.call_the_resolver + it 'calls Facter::Resolvers::Macosx::SystemProfile with boot_rom_version' do + fact.call_the_resolver + + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:boot_rom_version) + end + + it 'calls Facter::Resolvers::Macosx::SystemProfile with subsystem_vendor_id' do + fact.call_the_resolver + + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:subsystem_vendor_id) + end + + it 'returns resolved fact with false value' do + expect(fact.call_the_resolver) + .to be_an_instance_of(Facter::ResolvedFact) + .and have_attributes(name: 'is_virtual', value: false) + end end context 'when on virtual machine' do @@ -49,8 +57,6 @@ expect(fact.call_the_resolver) .to be_an_instance_of(Facter::ResolvedFact) .and have_attributes(name: 'is_virtual', value: true) - - fact.call_the_resolver end end @@ -66,8 +72,6 @@ expect(fact.call_the_resolver) .to be_an_instance_of(Facter::ResolvedFact) .and have_attributes(name: 'is_virtual', value: true) - - fact.call_the_resolver end end @@ -83,18 +87,8 @@ expect(fact.call_the_resolver) .to be_an_instance_of(Facter::ResolvedFact) .and have_attributes(name: 'is_virtual', value: true) - - fact.call_the_resolver end end end - - context 'when on physical machine' do - it 'returns resolved fact with false value' do - expect(fact.call_the_resolver) - .to be_an_instance_of(Facter::ResolvedFact) - .and have_attributes(name: 'is_virtual', value: false) - end - end end end diff --git a/spec/facter/facts/macosx/virtual_spec.rb b/spec/facter/facts/macosx/virtual_spec.rb new file mode 100644 index 0000000000..0b5aa8047e --- /dev/null +++ b/spec/facter/facts/macosx/virtual_spec.rb @@ -0,0 +1,96 @@ +# frozen_string_literal: true + +describe Facts::Macosx::Virtual do + subject(:fact) { Facts::Macosx::Virtual.new } + + describe '#call_the_resolver' do + before do + allow(Facter::Resolvers::Macosx::SystemProfiler).to receive(:resolve) + .with(:model_identifier) + .and_return('MacBookPro11,4') + + allow(Facter::Resolvers::Macosx::SystemProfiler).to receive(:resolve) + .with(:boot_rom_version) + .and_return('1037.60.58.0.0 (iBridge: 17.16.12551.0.0,0)') + + allow(Facter::Resolvers::Macosx::SystemProfiler).to receive(:resolve) + .with(:subsystem_vendor_id) + .and_return('0x123') + end + + context 'when on physical machine' do + it 'calls Facter::Resolvers::Macosx::SystemProfile with model_identifier' do + fact.call_the_resolver + + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:model_identifier) + end + + it 'calls Facter::Resolvers::Macosx::SystemProfile with boot_rom_version' do + fact.call_the_resolver + + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:boot_rom_version) + end + + it 'calls Facter::Resolvers::Macosx::SystemProfile with subsystem_vendor_id' do + fact.call_the_resolver + + expect(Facter::Resolvers::Macosx::SystemProfiler).to have_received(:resolve).with(:subsystem_vendor_id) + end + + it 'returns resolved fact with true value' do + expect(fact.call_the_resolver) + .to be_an_instance_of(Facter::ResolvedFact) + .and have_attributes(name: 'virtual', value: nil) + + fact.call_the_resolver + end + end + + context 'when on virtual machine' do + context 'with hypervisor vmware' do + before do + allow(Facter::Resolvers::Macosx::SystemProfiler) + .to receive(:resolve) + .with(:model_identifier) + .and_return('VMware') + end + + it 'returns resolved fact with true value' do + expect(fact.call_the_resolver) + .to be_an_instance_of(Facter::ResolvedFact) + .and have_attributes(name: 'virtual', value: 'vmware') + end + end + + context 'when hypervisor VirtualBox' do + before do + allow(Facter::Resolvers::Macosx::SystemProfiler) + .to receive(:resolve) + .with(:boot_rom_version) + .and_return('VirtualBox') + end + + it 'returns resolved fact with true value' do + expect(fact.call_the_resolver) + .to be_an_instance_of(Facter::ResolvedFact) + .and have_attributes(name: 'virtual', value: 'virtualbox') + end + end + + context 'when hypervisor Parallels' do + before do + allow(Facter::Resolvers::Macosx::SystemProfiler) + .to receive(:resolve) + .with(:subsystem_vendor_id) + .and_return('0x1ab8') + end + + it 'returns resolved fact with true value' do + expect(fact.call_the_resolver) + .to be_an_instance_of(Facter::ResolvedFact) + .and have_attributes(name: 'virtual', value: 'parallels') + end + end + end + end +end