Skip to content

Commit

Permalink
Add option to suppress legacy facts
Browse files Browse the repository at this point in the history
By default legacy facts will be removed in Puppet 8. This change mimics
that.
  • Loading branch information
alexjfisher committed Mar 14, 2023
1 parent 1418bca commit f81d6eb
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ext/puppet_agent_components.json

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'facter'
require 'facterdb'
require 'json'
require 'rspec-puppet-facts/legacy_facts'

# The purpose of this module is to simplify the Puppet
# module's RSpec tests by looping through all supported
Expand All @@ -25,6 +26,7 @@ module RspecPuppetFacts
# @option opts [String,Array<String>] :hardwaremodels The OS architecture names, i.e. x86_64
# @option opts [Array<Hash>] :supported_os If this options is provided the data
# @option opts [String] :facterversion the facter version of which to
# @option opts [Boolean] :legacy_facts If legacy facts should be included
# select facts from, e.g.: '3.6'
# will be used instead of the "operatingsystem_support" section if the metadata file
# even if the file is missing.
Expand All @@ -33,6 +35,7 @@ def on_supported_os(opts = {})
opts[:hardwaremodels] = [opts[:hardwaremodels]] unless opts[:hardwaremodels].is_a? Array
opts[:supported_os] ||= RspecPuppetFacts.meta_supported_os
opts[:facterversion] ||= RSpec.configuration.default_facter_version
opts[:legacy_facts] ||= RSpec.configuration.legacy_facts

# This should list all variables that on_supported_os_implementation uses
cache_key = [
Expand Down Expand Up @@ -170,6 +173,7 @@ def on_supported_os_implementation(opts = {})
os = "#{facts[:operatingsystem].downcase}-#{operatingsystemmajrelease}-#{facts[:hardwaremodel]}"
next unless os.start_with? RspecPuppetFacts.spec_facts_os_filter if RspecPuppetFacts.spec_facts_os_filter
facts.merge! RspecPuppetFacts.common_facts
facts.delete_if { |fact, _value| RspecPuppetFacts::LegacyFacts.legacy_fact?(fact) } unless opts[:legacy_facts]
os_facts_hash[os] = RspecPuppetFacts.with_custom_facts(os, facts)
end

Expand Down Expand Up @@ -395,10 +399,15 @@ def self.facter_version_for_puppet_version(puppet_version)
ensure
fd.close if fd
end

def self.legacy_facts_default_for_puppet_version(puppet_version)
Gem::Version.new(puppet_version) < Gem::Version.new('8.0.0')
end
end

RSpec.configure do |c|
c.add_setting :default_facter_version,
:default => RspecPuppetFacts.facter_version_for_puppet_version(Puppet.version)
c.add_setting :facterdb_string_keys, :default => false
c.add_setting :legacy_facts, :default => RspecPuppetFacts.legacy_facts_default_for_puppet_version(Puppet.version)
end
130 changes: 130 additions & 0 deletions lib/rspec-puppet-facts/legacy_facts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
module RspecPuppetFacts
# 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 [Symbol] fact Fact name
def self.legacy_fact?(fact)
legacy_facts.include?(fact) or fact.to_s.match(Regexp.union(legacy_fact_regexes))
end

# @api private
def self.legacy_fact_regexes
[
/\Ablockdevice_(?<devicename>.+)_model\Z/,
/\Ablockdevice_(?<devicename>.+)_size\Z/,
/\Ablockdevice_(?<devicename>.+)_vendor\Z/,
/\Aipaddress6_(?<interface>.+)\Z/,
/\Aipaddress_(?<interface>.+)\Z/,
/\Amacaddress_(?<interface>.+)\Z/,
/\Amtu_(?<interface>.+)\Z/,
/\Anetmask6_(?<interface>.+)\Z/,
/\Anetmask_(?<interface>.+)\Z/,
/\Anetwork6_(?<interface>.+)\Z/,
/\Anetwork_(?<interface>.+)\Z/,
/\Ascope6_(?<interface>.+)\Z/,
/\Aldom_(?<name>.+)\Z/,
/\Aprocessor\d+\Z/,
/\Asp_(?<name>.+)\Z/,
/\Assh(?<algorithm>.+)key\Z/,
/\Asshfp_(?<algorithm>.+)\Z/,
/\Azone_(?<name>.+)_brand\Z/,
/\Azone_(?<name>.+)_id\Z/,
/\Azone_(?<name>.+)_iptype\Z/,
/\Azone_(?<name>.+)_name\Z/,
/\Azone_(?<name>.+)_path\Z/,
/\Azone_(?<name>.+)_status\Z/,
/\Azone_(?<name>.+)_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

0 comments on commit f81d6eb

Please sign in to comment.