From b2f1bb1abfb572f859f3ce50d5e5c2fb66437bc9 Mon Sep 17 00:00:00 2001 From: Alexander Fisher Date: Thu, 27 Apr 2023 21:37:36 +0100 Subject: [PATCH] Add `include_legacy_facts` setting When set to false (the default for Puppet 8+), all legacy facts are pruned before the catalog is compiled. --- lib/rspec-puppet.rb | 1 + lib/rspec-puppet/adapters.rb | 6 +- lib/rspec-puppet/legacy_facts.rb | 130 +++++++++++++++++++++++++++++++ lib/rspec-puppet/support.rb | 5 ++ 4 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 lib/rspec-puppet/legacy_facts.rb diff --git a/lib/rspec-puppet.rb b/lib/rspec-puppet.rb index 887eb1075..d293f7021 100644 --- a/lib/rspec-puppet.rb +++ b/lib/rspec-puppet.rb @@ -62,6 +62,7 @@ def self.current_example c.add_setting :fixture_hiera_configs, default: {} c.add_setting :use_fixture_spec_hiera, default: false c.add_setting :fallback_to_default_hiera, default: true + c.add_setting :include_legacy_facts, default: Gem::Version.new(Puppet::PUPPETVERSION) < Gem::Version.new('8.0.0') c.instance_eval do def trusted_server_facts diff --git a/lib/rspec-puppet/adapters.rb b/lib/rspec-puppet/adapters.rb index d2f9d711f..40ef4c4b0 100644 --- a/lib/rspec-puppet/adapters.rb +++ b/lib/rspec-puppet/adapters.rb @@ -173,7 +173,8 @@ def settings_map %i[environmentpath environmentpath], %i[hiera_config hiera_config], %i[strict_variables strict_variables], - %i[manifest manifest] + %i[manifest manifest], + %i[include_legacy_facts include_legacy_facts] ) end @@ -270,7 +271,8 @@ def setup_puppet(example_group) def settings_map super.push( %i[basemodulepath basemodulepath], - %i[vendormoduledir vendormoduledir] + %i[vendormoduledir vendormoduledir], + %i[include_legacy_facts include_legacy_facts] ) end diff --git a/lib/rspec-puppet/legacy_facts.rb b/lib/rspec-puppet/legacy_facts.rb new file mode 100644 index 000000000..5eec4840c --- /dev/null +++ b/lib/rspec-puppet/legacy_facts.rb @@ -0,0 +1,130 @@ +module RspecPuppet + # This module contains lists of all legacy facts + module LegacyFacts + # Used to determine if a fact is a legacy fact or not + # + # @return [Boolean] Is the fact a legacy fact + # @param [String] fact Fact name + def self.legacy_fact?(fact) + legacy_facts.include?(fact.to_sym) or fact.match(Regexp.union(legacy_fact_regexes)) + end + + # @api private + def self.legacy_fact_regexes + [ + /\Ablockdevice_(?.+)_model\Z/, + /\Ablockdevice_(?.+)_size\Z/, + /\Ablockdevice_(?.+)_vendor\Z/, + /\Aipaddress6_(?.+)\Z/, + /\Aipaddress_(?.+)\Z/, + /\Amacaddress_(?.+)\Z/, + /\Amtu_(?.+)\Z/, + /\Anetmask6_(?.+)\Z/, + /\Anetmask_(?.+)\Z/, + /\Anetwork6_(?.+)\Z/, + /\Anetwork_(?.+)\Z/, + /\Ascope6_(?.+)\Z/, + /\Aldom_(?.+)\Z/, + /\Aprocessor\d+\Z/, + /\Asp_(?.+)\Z/, + /\Assh(?.+)key\Z/, + /\Asshfp_(?.+)\Z/, + /\Azone_(?.+)_brand\Z/, + /\Azone_(?.+)_id\Z/, + /\Azone_(?.+)_iptype\Z/, + /\Azone_(?.+)_name\Z/, + /\Azone_(?.+)_path\Z/, + /\Azone_(?.+)_status\Z/, + /\Azone_(?.+)_uuid\Z/ + ] + end + + # @api private + def self.legacy_facts + %i[ + architecture + augeasversion + blockdevices + bios_release_date + bios_vendor + bios_version + boardassettag + boardmanufacturer + boardproductname + boardserialnumber + chassisassettag + chassistype + dhcp_servers + domain + fqdn + gid + hardwareisa + hardwaremodel + hostname + id + interfaces + ipaddress + ipaddress6 + lsbdistcodename + lsbdistdescription + lsbdistid + lsbdistrelease + lsbmajdistrelease + lsbminordistrelease + lsbrelease + macaddress + macosx_buildversion + macosx_productname + macosx_productversion + macosx_productversion_major + macosx_productversion_minor + macosx_productversion_patch + manufacturer + memoryfree + memoryfree_mb + memorysize + memorysize_mb + netmask + netmask6 + network + network6 + operatingsystem + operatingsystemmajrelease + operatingsystemrelease + osfamily + physicalprocessorcount + processorcount + productname + rubyplatform + rubysitedir + rubyversion + scope6 + selinux + selinux_config_mode + selinux_config_policy + selinux_current_mode + selinux_enforced + selinux_policyversion + serialnumber + swapencrypted + swapfree + swapfree_mb + swapsize + swapsize_mb + windows_edition_id + windows_installation_type + windows_product_name + windows_release_id + system32 + uptime + uptime_days + uptime_hours + uptime_seconds + uuid + xendomains + zonename + zones + ] + end + end +end diff --git a/lib/rspec-puppet/support.rb b/lib/rspec-puppet/support.rb index ec7ef5947..a9c673d4f 100644 --- a/lib/rspec-puppet/support.rb +++ b/lib/rspec-puppet/support.rb @@ -4,6 +4,7 @@ require 'rspec-puppet/adapters' require 'rspec-puppet/raw_string' require 'rspec-puppet/sensitive' +require 'rspec-puppet/legacy_facts' module RSpec::Puppet module Support @@ -285,6 +286,10 @@ def facts_hash(node) # Facter currently supports lower case facts. Bug FACT-777 has been submitted to support case sensitive # facts. result_facts.transform_keys(&:downcase) + + # Prune legacy facts wherever they came from (rspec-puppet, rspec-puppet-facts, default_facts etc.) + result_facts.delete_if { |fact, _value| RspecPuppet::LegacyFacts.legacy_fact?(fact) } unless RSpec.configuration.include_legacy_facts + result_facts end def node_params_hash