Skip to content

Commit 00a23cc

Browse files
committed
Add include_legacy_facts setting
When set to false (the default for Puppet 8+), all legacy facts are pruned before the catalog is compiled.
1 parent f4c5c71 commit 00a23cc

File tree

5 files changed

+149
-16
lines changed

5 files changed

+149
-16
lines changed

.rubocop_todo.yml

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2023-04-11 21:10:40 UTC using RuboCop version 1.50.0.
3+
# on 2023-04-27 23:06:14 UTC using RuboCop version 1.50.2.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 1
10-
# Configuration parameters: Severity, Include.
11-
# Include: **/*.gemspec
12-
Gemspec/RequiredRubyVersion:
13-
Exclude:
14-
- 'rspec-puppet.gemspec'
15-
169
# Offense count: 3
1710
# Configuration parameters: IgnoreLiteralBranches, IgnoreConstantBranches.
1811
Lint/DuplicateBranch:
@@ -90,15 +83,15 @@ Metrics/ClassLength:
9083
Metrics/CyclomaticComplexity:
9184
Max: 18
9285

93-
# Offense count: 53
86+
# Offense count: 55
9487
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
9588
Metrics/MethodLength:
96-
Max: 37
89+
Max: 84
9790

98-
# Offense count: 2
91+
# Offense count: 3
9992
# Configuration parameters: CountComments, CountAsOne.
10093
Metrics/ModuleLength:
101-
Max: 425
94+
Max: 427
10295

10396
# Offense count: 2
10497
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
@@ -255,7 +248,7 @@ RSpec/MultipleDescribes:
255248
- 'spec/unit/adapters_spec.rb'
256249
- 'spec/unit/monkey_patches_spec.rb'
257250

258-
# Offense count: 44
251+
# Offense count: 46
259252
RSpec/MultipleExpectations:
260253
Max: 8
261254

@@ -399,7 +392,7 @@ Style/StringConcatenation:
399392
- 'lib/rspec-puppet/matchers/compile.rb'
400393
- 'lib/rspec-puppet/support.rb'
401394

402-
# Offense count: 28
395+
# Offense count: 29
403396
# This cop supports safe autocorrection (--autocorrect).
404397
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
405398
# URISchemes: http, https

lib/rspec-puppet.rb

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def self.current_example
6262
c.add_setting :fixture_hiera_configs, default: {}
6363
c.add_setting :use_fixture_spec_hiera, default: false
6464
c.add_setting :fallback_to_default_hiera, default: true
65+
c.add_setting :include_legacy_facts, default: Gem::Version.new(Puppet::PUPPETVERSION) < Gem::Version.new('8.0.0')
6566

6667
c.instance_eval do
6768
def trusted_server_facts

lib/rspec-puppet/adapters.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ def settings_map
173173
%i[environmentpath environmentpath],
174174
%i[hiera_config hiera_config],
175175
%i[strict_variables strict_variables],
176-
%i[manifest manifest]
176+
%i[manifest manifest],
177+
%i[include_legacy_facts include_legacy_facts]
177178
)
178179
end
179180

@@ -270,7 +271,8 @@ def setup_puppet(example_group)
270271
def settings_map
271272
super.push(
272273
%i[basemodulepath basemodulepath],
273-
%i[vendormoduledir vendormoduledir]
274+
%i[vendormoduledir vendormoduledir],
275+
%i[include_legacy_facts include_legacy_facts]
274276
)
275277
end
276278

lib/rspec-puppet/legacy_facts.rb

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

lib/rspec-puppet/support.rb

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'rspec-puppet/adapters'
55
require 'rspec-puppet/raw_string'
66
require 'rspec-puppet/sensitive'
7+
require 'rspec-puppet/legacy_facts'
78

89
module RSpec::Puppet
910
module Support
@@ -285,6 +286,10 @@ def facts_hash(node)
285286
# Facter currently supports lower case facts. Bug FACT-777 has been submitted to support case sensitive
286287
# facts.
287288
result_facts.transform_keys(&:downcase)
289+
290+
# Prune legacy facts wherever they came from (rspec-puppet, rspec-puppet-facts, default_facts etc.)
291+
result_facts.delete_if { |fact, _value| RspecPuppet::LegacyFacts.legacy_fact?(fact) } unless RSpec.configuration.include_legacy_facts
292+
result_facts
288293
end
289294

290295
def node_params_hash

0 commit comments

Comments
 (0)