Skip to content

Commit f81d6eb

Browse files
committed
Add option to suppress legacy facts
By default legacy facts will be removed in Puppet 8. This change mimics that.
1 parent 1418bca commit f81d6eb

File tree

3 files changed

+140
-1
lines changed

3 files changed

+140
-1
lines changed

ext/puppet_agent_components.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

lib/rspec-puppet-facts.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'facter'
33
require 'facterdb'
44
require 'json'
5+
require 'rspec-puppet-facts/legacy_facts'
56

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

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

@@ -395,10 +399,15 @@ def self.facter_version_for_puppet_version(puppet_version)
395399
ensure
396400
fd.close if fd
397401
end
402+
403+
def self.legacy_facts_default_for_puppet_version(puppet_version)
404+
Gem::Version.new(puppet_version) < Gem::Version.new('8.0.0')
405+
end
398406
end
399407

400408
RSpec.configure do |c|
401409
c.add_setting :default_facter_version,
402410
:default => RspecPuppetFacts.facter_version_for_puppet_version(Puppet.version)
403411
c.add_setting :facterdb_string_keys, :default => false
412+
c.add_setting :legacy_facts, :default => RspecPuppetFacts.legacy_facts_default_for_puppet_version(Puppet.version)
404413
end
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
module RspecPuppetFacts
2+
# This module contains lists of all legacy facts
3+
module LegacyFacts
4+
# Used to determine if a fact is a legacy fact or not
5+
#
6+
# @return [Boolean] Is the fact a legacy fact
7+
# @param [Symbol] fact Fact name
8+
def self.legacy_fact?(fact)
9+
legacy_facts.include?(fact) or fact.to_s.match(Regexp.union(legacy_fact_regexes))
10+
end
11+
12+
# @api private
13+
def self.legacy_fact_regexes
14+
[
15+
/\Ablockdevice_(?<devicename>.+)_model\Z/,
16+
/\Ablockdevice_(?<devicename>.+)_size\Z/,
17+
/\Ablockdevice_(?<devicename>.+)_vendor\Z/,
18+
/\Aipaddress6_(?<interface>.+)\Z/,
19+
/\Aipaddress_(?<interface>.+)\Z/,
20+
/\Amacaddress_(?<interface>.+)\Z/,
21+
/\Amtu_(?<interface>.+)\Z/,
22+
/\Anetmask6_(?<interface>.+)\Z/,
23+
/\Anetmask_(?<interface>.+)\Z/,
24+
/\Anetwork6_(?<interface>.+)\Z/,
25+
/\Anetwork_(?<interface>.+)\Z/,
26+
/\Ascope6_(?<interface>.+)\Z/,
27+
/\Aldom_(?<name>.+)\Z/,
28+
/\Aprocessor\d+\Z/,
29+
/\Asp_(?<name>.+)\Z/,
30+
/\Assh(?<algorithm>.+)key\Z/,
31+
/\Asshfp_(?<algorithm>.+)\Z/,
32+
/\Azone_(?<name>.+)_brand\Z/,
33+
/\Azone_(?<name>.+)_id\Z/,
34+
/\Azone_(?<name>.+)_iptype\Z/,
35+
/\Azone_(?<name>.+)_name\Z/,
36+
/\Azone_(?<name>.+)_path\Z/,
37+
/\Azone_(?<name>.+)_status\Z/,
38+
/\Azone_(?<name>.+)_uuid\Z/
39+
]
40+
end
41+
42+
# @api private
43+
def self.legacy_facts
44+
%i[
45+
architecture
46+
augeasversion
47+
blockdevices
48+
bios_release_date
49+
bios_vendor
50+
bios_version
51+
boardassettag
52+
boardmanufacturer
53+
boardproductname
54+
boardserialnumber
55+
chassisassettag
56+
chassistype
57+
dhcp_servers
58+
domain
59+
fqdn
60+
gid
61+
hardwareisa
62+
hardwaremodel
63+
hostname
64+
id
65+
interfaces
66+
ipaddress
67+
ipaddress6
68+
lsbdistcodename
69+
lsbdistdescription
70+
lsbdistid
71+
lsbdistrelease
72+
lsbmajdistrelease
73+
lsbminordistrelease
74+
lsbrelease
75+
macaddress
76+
macosx_buildversion
77+
macosx_productname
78+
macosx_productversion
79+
macosx_productversion_major
80+
macosx_productversion_minor
81+
macosx_productversion_patch
82+
manufacturer
83+
memoryfree
84+
memoryfree_mb
85+
memorysize
86+
memorysize_mb
87+
netmask
88+
netmask6
89+
network
90+
network6
91+
operatingsystem
92+
operatingsystemmajrelease
93+
operatingsystemrelease
94+
osfamily
95+
physicalprocessorcount
96+
processorcount
97+
productname
98+
rubyplatform
99+
rubysitedir
100+
rubyversion
101+
scope6
102+
selinux
103+
selinux_config_mode
104+
selinux_config_policy
105+
selinux_current_mode
106+
selinux_enforced
107+
selinux_policyversion
108+
serialnumber
109+
swapencrypted
110+
swapfree
111+
swapfree_mb
112+
swapsize
113+
swapsize_mb
114+
windows_edition_id
115+
windows_installation_type
116+
windows_product_name
117+
windows_release_id
118+
system32
119+
uptime
120+
uptime_days
121+
uptime_hours
122+
uptime_seconds
123+
uuid
124+
xendomains
125+
zonename
126+
zones
127+
]
128+
end
129+
end
130+
end

0 commit comments

Comments
 (0)