rspec-puppet-facts 3.0.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -2
- data/.rubocop_todo.yml +62 -5
- data/CHANGELOG.md +66 -12
- data/Gemfile +4 -4
- data/README.md +56 -29
- data/Rakefile +19 -7
- data/lib/rspec-puppet-facts/version.rb +1 -1
- data/lib/rspec-puppet-facts.rb +42 -58
- data/rspec-puppet-facts.gemspec +2 -2
- data/spec/fixtures/metadata.json +4 -5
- data/spec/rspec_puppet_facts_spec.rb +209 -322
- metadata +9 -15
data/lib/rspec-puppet-facts.rb
CHANGED
|
@@ -58,8 +58,7 @@ module RspecPuppetFacts
|
|
|
58
58
|
# @api private
|
|
59
59
|
def on_supported_os_implementation(opts = {})
|
|
60
60
|
unless /\A\d+\.\d+(?:\.\d+)*\z/.match?((facterversion = opts[:facterversion]))
|
|
61
|
-
raise ArgumentError, ":facterversion must be in the format 'n.n' or "
|
|
62
|
-
"'n.n.n' (n is numeric), not '#{facterversion}'"
|
|
61
|
+
raise ArgumentError, ":facterversion must be in the format 'n.n' or 'n.n.n' (n is numeric), not '#{facterversion}'"
|
|
63
62
|
end
|
|
64
63
|
|
|
65
64
|
filter = []
|
|
@@ -67,7 +66,6 @@ module RspecPuppetFacts
|
|
|
67
66
|
if os_sup['operatingsystemrelease']
|
|
68
67
|
Array(os_sup['operatingsystemrelease']).map do |operatingsystemmajrelease|
|
|
69
68
|
opts[:hardwaremodels].each do |hardwaremodel|
|
|
70
|
-
|
|
71
69
|
os_release_filter = "/^#{Regexp.escape(operatingsystemmajrelease.split(' ')[0])}/"
|
|
72
70
|
case os_sup['operatingsystem']
|
|
73
71
|
when /BSD/i
|
|
@@ -82,48 +80,47 @@ module RspecPuppetFacts
|
|
|
82
80
|
"/^#{operatingsystemmajrelease}-/"
|
|
83
81
|
end
|
|
84
82
|
when /Windows/i
|
|
85
|
-
hardwaremodel =
|
|
83
|
+
hardwaremodel = 'x86_64'
|
|
86
84
|
os_sup['operatingsystem'] = os_sup['operatingsystem'].downcase
|
|
87
85
|
operatingsystemmajrelease = operatingsystemmajrelease[/\A(?:Server )?(.+)/i, 1]
|
|
88
86
|
|
|
89
87
|
# force quoting because windows releases can contain spaces
|
|
90
88
|
os_release_filter = "\"#{operatingsystemmajrelease}\""
|
|
91
|
-
|
|
92
|
-
if operatingsystemmajrelease == '2016' && Puppet::Util::Package.versioncmp(facterversion, '3.4') < 0
|
|
93
|
-
os_release_filter = '/^10\\.0\\./'
|
|
94
|
-
end
|
|
95
89
|
when /Amazon/i
|
|
96
90
|
# Tighten the regex for Amazon Linux 2 so that we don't pick up Amazon Linux 2016 or 2017 facts
|
|
97
|
-
os_release_filter =
|
|
91
|
+
os_release_filter = '/^2$/' if operatingsystemmajrelease == '2'
|
|
98
92
|
end
|
|
99
93
|
|
|
100
94
|
filter << {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
95
|
+
'os.name' => os_sup['operatingsystem'],
|
|
96
|
+
'os.release.full' => os_release_filter,
|
|
97
|
+
'os.hardware' => hardwaremodel,
|
|
104
98
|
}
|
|
105
99
|
end
|
|
106
100
|
end
|
|
107
101
|
else
|
|
108
102
|
opts[:hardwaremodels].each do |hardwaremodel|
|
|
109
103
|
filter << {
|
|
110
|
-
|
|
111
|
-
|
|
104
|
+
'os.name' => os_sup['operatingsystem'],
|
|
105
|
+
'os.hardware' => hardwaremodel,
|
|
112
106
|
}
|
|
113
107
|
end
|
|
114
108
|
end
|
|
115
109
|
end
|
|
116
110
|
|
|
117
|
-
strict_requirement = RspecPuppetFacts
|
|
111
|
+
strict_requirement = RspecPuppetFacts.facter_version_to_strict_requirement(facterversion)
|
|
118
112
|
|
|
119
|
-
loose_requirement = RspecPuppetFacts
|
|
113
|
+
loose_requirement = RspecPuppetFacts.facter_version_to_loose_requirement(facterversion)
|
|
120
114
|
received_facts = []
|
|
121
115
|
|
|
122
116
|
# FacterDB may have newer versions of facter data for which it contains a subset of all possible
|
|
123
117
|
# facter data (see FacterDB 0.5.2 for Facter releases 3.8 and 3.9). In this situation we need to
|
|
124
118
|
# cycle through and downgrade Facter versions per platform type until we find matching Facter data.
|
|
119
|
+
facterversion_key = RSpec.configuration.facterdb_string_keys ? 'facterversion' : :facterversion
|
|
125
120
|
filter.each do |filter_spec|
|
|
126
|
-
versions = FacterDB.get_facts(filter_spec).to_h
|
|
121
|
+
versions = FacterDB.get_facts(filter_spec, symbolize_keys: !RSpec.configuration.facterdb_string_keys).to_h do |facts|
|
|
122
|
+
[Gem::Version.new(facts[facterversion_key]), facts]
|
|
123
|
+
end
|
|
127
124
|
|
|
128
125
|
version, facts = versions.select { |v, _f| strict_requirement =~ v }.max_by { |v, _f| v }
|
|
129
126
|
|
|
@@ -131,9 +128,7 @@ module RspecPuppetFacts
|
|
|
131
128
|
version, facts = versions.select { |v, _f| loose_requirement =~ v }.max_by { |v, _f| v } if loose_requirement
|
|
132
129
|
next unless version
|
|
133
130
|
|
|
134
|
-
if RspecPuppetFacts.spec_facts_strict?
|
|
135
|
-
raise ArgumentError, "No facts were found in the FacterDB for Facter v#{facterversion} on #{filter_spec}, aborting"
|
|
136
|
-
end
|
|
131
|
+
raise ArgumentError, "No facts were found in the FacterDB for Facter v#{facterversion} on #{filter_spec}, aborting" if RspecPuppetFacts.spec_facts_strict?
|
|
137
132
|
|
|
138
133
|
RspecPuppetFacts.warning "No facts were found in the FacterDB for Facter v#{facterversion} on #{filter_spec}, using v#{version} instead"
|
|
139
134
|
end
|
|
@@ -148,33 +143,19 @@ module RspecPuppetFacts
|
|
|
148
143
|
|
|
149
144
|
os_facts_hash = {}
|
|
150
145
|
received_facts.map do |facts|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
elsif facts[:operatingsystem] == 'OpenBSD'
|
|
156
|
-
operatingsystemmajrelease = facts[:operatingsystemrelease]
|
|
157
|
-
elsif facts[:operatingsystem] == 'windows' && facts[:operatingsystemrelease].start_with?('10.0.')
|
|
158
|
-
operatingsystemmajrelease = '2016'
|
|
159
|
-
elsif facts.dig(:os, 'release', 'major')
|
|
160
|
-
operatingsystemmajrelease = facts[:os]['release']['major']
|
|
161
|
-
elsif facts.dig(:os, 'distro', 'release', 'major')
|
|
162
|
-
operatingsystemmajrelease = facts[:os]['distro']['release']['major']
|
|
163
|
-
else
|
|
164
|
-
if facts[:operatingsystemmajrelease].nil?
|
|
165
|
-
operatingsystemmajrelease = facts[:operatingsystemrelease].split('.')[0]
|
|
166
|
-
else
|
|
167
|
-
operatingsystemmajrelease = facts[:operatingsystemmajrelease]
|
|
168
|
-
end
|
|
146
|
+
os_fact = RSpec.configuration.facterdb_string_keys ? facts['os'] : facts[:os]
|
|
147
|
+
unless os_fact
|
|
148
|
+
RspecPuppetFacts.warning "No os fact was found in FacterDB for: #{facts}"
|
|
149
|
+
next
|
|
169
150
|
end
|
|
170
|
-
|
|
171
|
-
|
|
151
|
+
|
|
152
|
+
os = "#{os_fact['name'].downcase}-#{os_fact['release']['major']}-#{os_fact['hardware']}"
|
|
153
|
+
next if RspecPuppetFacts.spec_facts_os_filter && !os.start_with?(RspecPuppetFacts.spec_facts_os_filter)
|
|
154
|
+
|
|
172
155
|
facts.merge! RspecPuppetFacts.common_facts
|
|
173
156
|
os_facts_hash[os] = RspecPuppetFacts.with_custom_facts(os, facts)
|
|
174
157
|
end
|
|
175
158
|
|
|
176
|
-
return stringify_keys(os_facts_hash) if RSpec.configuration.facterdb_string_keys
|
|
177
|
-
|
|
178
159
|
os_facts_hash
|
|
179
160
|
end
|
|
180
161
|
|
|
@@ -212,7 +193,7 @@ module RspecPuppetFacts
|
|
|
212
193
|
def self.register_custom_fact(name, value, options)
|
|
213
194
|
@custom_facts ||= {}
|
|
214
195
|
name = RSpec.configuration.facterdb_string_keys ? name.to_s : name.to_sym
|
|
215
|
-
@custom_facts[name] = {:
|
|
196
|
+
@custom_facts[name] = { options: options, value: value }
|
|
216
197
|
end
|
|
217
198
|
|
|
218
199
|
# Adds any custom facts according to the rules defined for the operating
|
|
@@ -231,7 +212,7 @@ module RspecPuppetFacts
|
|
|
231
212
|
value = fact[:value].respond_to?(:call) ? fact[:value].call(os, facts) : fact[:value]
|
|
232
213
|
# if merge_facts passed, merge supplied facts into facts hash
|
|
233
214
|
if fact[:options][:merge_facts]
|
|
234
|
-
facts.deep_merge!({name => value})
|
|
215
|
+
facts.deep_merge!({ name => value })
|
|
235
216
|
else
|
|
236
217
|
facts[name] = value
|
|
237
218
|
end
|
|
@@ -254,7 +235,7 @@ module RspecPuppetFacts
|
|
|
254
235
|
# @return [nil,String]
|
|
255
236
|
# @api private
|
|
256
237
|
def self.spec_facts_os_filter
|
|
257
|
-
ENV
|
|
238
|
+
ENV.fetch('SPEC_FACTS_OS', nil)
|
|
258
239
|
end
|
|
259
240
|
|
|
260
241
|
# If SPEC_FACTS_STRICT is set to `yes`, RspecPuppetFacts will error on missing FacterDB entries, instead of warning & skipping the tests, or using an older facter version.
|
|
@@ -270,10 +251,11 @@ module RspecPuppetFacts
|
|
|
270
251
|
# @return [Hash <Symbol => String>]
|
|
271
252
|
def self.common_facts
|
|
272
253
|
return @common_facts if @common_facts
|
|
254
|
+
|
|
273
255
|
@common_facts = {
|
|
274
|
-
:
|
|
275
|
-
:
|
|
276
|
-
:
|
|
256
|
+
puppetversion: Puppet.version,
|
|
257
|
+
rubysitedir: RbConfig::CONFIG['sitelibdir'],
|
|
258
|
+
rubyversion: RUBY_VERSION,
|
|
277
259
|
}
|
|
278
260
|
|
|
279
261
|
@common_facts[:mco_version] = MCollective::VERSION if mcollective?
|
|
@@ -281,6 +263,7 @@ module RspecPuppetFacts
|
|
|
281
263
|
if augeas?
|
|
282
264
|
@common_facts[:augeasversion] = Augeas.open(nil, nil, Augeas::NO_MODL_AUTOLOAD).get('/augeas/version')
|
|
283
265
|
end
|
|
266
|
+
@common_facts = stringify_keys(@common_facts) if RSpec.configuration.facterdb_string_keys
|
|
284
267
|
|
|
285
268
|
@common_facts
|
|
286
269
|
end
|
|
@@ -317,8 +300,9 @@ module RspecPuppetFacts
|
|
|
317
300
|
# @api private
|
|
318
301
|
def self.meta_supported_os
|
|
319
302
|
unless metadata['operatingsystem_support'].is_a? Array
|
|
320
|
-
|
|
303
|
+
raise StandardError, 'Unknown operatingsystem support in the metadata file!'
|
|
321
304
|
end
|
|
305
|
+
|
|
322
306
|
metadata['operatingsystem_support']
|
|
323
307
|
end
|
|
324
308
|
|
|
@@ -330,8 +314,9 @@ module RspecPuppetFacts
|
|
|
330
314
|
def self.metadata
|
|
331
315
|
return @metadata if @metadata
|
|
332
316
|
unless File.file? metadata_file
|
|
333
|
-
|
|
317
|
+
raise StandardError, "Can't find metadata.json... dunno why"
|
|
334
318
|
end
|
|
319
|
+
|
|
335
320
|
content = File.read metadata_file
|
|
336
321
|
@metadata = JSON.parse content
|
|
337
322
|
end
|
|
@@ -347,7 +332,7 @@ module RspecPuppetFacts
|
|
|
347
332
|
# @param message [String]
|
|
348
333
|
# @api private
|
|
349
334
|
def self.warning(message)
|
|
350
|
-
|
|
335
|
+
warn message
|
|
351
336
|
end
|
|
352
337
|
|
|
353
338
|
# Reset the memoization
|
|
@@ -397,7 +382,7 @@ module RspecPuppetFacts
|
|
|
397
382
|
elsif /\A[0-9]+\Z/.match?(version)
|
|
398
383
|
# Interpret 3 as < 4
|
|
399
384
|
"< #{version.to_i + 1}"
|
|
400
|
-
else
|
|
385
|
+
else # rubocop:disable Style/EmptyElse
|
|
401
386
|
# This would be the same as the strict requirement
|
|
402
387
|
nil
|
|
403
388
|
end
|
|
@@ -415,13 +400,13 @@ module RspecPuppetFacts
|
|
|
415
400
|
fd = File.open(json_path, 'rb:UTF-8')
|
|
416
401
|
data = JSON.parse(fd.read)
|
|
417
402
|
|
|
418
|
-
version_map = data.map
|
|
403
|
+
version_map = data.map do |_, versions|
|
|
419
404
|
if versions['puppet'].nil? || versions['facter'].nil?
|
|
420
405
|
nil
|
|
421
406
|
else
|
|
422
407
|
[Gem::Version.new(versions['puppet']), versions['facter']]
|
|
423
408
|
end
|
|
424
|
-
|
|
409
|
+
end.compact
|
|
425
410
|
|
|
426
411
|
puppet_gem_version = Gem::Version.new(puppet_version)
|
|
427
412
|
applicable_versions = version_map.select { |p, _| puppet_gem_version >= p }
|
|
@@ -430,7 +415,7 @@ module RspecPuppetFacts
|
|
|
430
415
|
return Facter.version
|
|
431
416
|
end
|
|
432
417
|
|
|
433
|
-
applicable_versions.
|
|
418
|
+
applicable_versions.max_by { |p, _| p }.last
|
|
434
419
|
rescue JSON::ParserError
|
|
435
420
|
warning "#{json_path} contains invalid JSON, defaulting to Facter #{Facter.version}"
|
|
436
421
|
Facter.version
|
|
@@ -440,7 +425,6 @@ module RspecPuppetFacts
|
|
|
440
425
|
end
|
|
441
426
|
|
|
442
427
|
RSpec.configure do |c|
|
|
443
|
-
c.add_setting :default_facter_version,
|
|
444
|
-
|
|
445
|
-
c.add_setting :facterdb_string_keys, :default => false
|
|
428
|
+
c.add_setting :default_facter_version, default: RspecPuppetFacts.facter_version_for_puppet_version(Puppet.version)
|
|
429
|
+
c.add_setting :facterdb_string_keys, default: false
|
|
446
430
|
end
|
data/rspec-puppet-facts.gemspec
CHANGED
|
@@ -23,10 +23,10 @@ Gem::Specification.new do |s|
|
|
|
23
23
|
s.add_development_dependency 'rspec', '~> 3.12'
|
|
24
24
|
s.add_development_dependency 'yard', '~> 0.9.34'
|
|
25
25
|
|
|
26
|
-
s.add_development_dependency 'voxpupuli-rubocop', '~> 2.
|
|
26
|
+
s.add_development_dependency 'voxpupuli-rubocop', '~> 2.8.0'
|
|
27
27
|
|
|
28
28
|
s.add_runtime_dependency 'deep_merge', '~> 1.2'
|
|
29
29
|
s.add_runtime_dependency 'facter', '< 5'
|
|
30
|
-
s.add_runtime_dependency 'facterdb', '
|
|
30
|
+
s.add_runtime_dependency 'facterdb', '~> 3.1'
|
|
31
31
|
s.add_runtime_dependency 'puppet', '>= 7', '< 9'
|
|
32
32
|
end
|
data/spec/fixtures/metadata.json
CHANGED
|
@@ -15,16 +15,15 @@
|
|
|
15
15
|
{
|
|
16
16
|
"operatingsystem": "Debian",
|
|
17
17
|
"operatingsystemrelease": [
|
|
18
|
-
"
|
|
19
|
-
"
|
|
18
|
+
"11",
|
|
19
|
+
"12"
|
|
20
20
|
]
|
|
21
21
|
},
|
|
22
22
|
{
|
|
23
23
|
"operatingsystem": "RedHat",
|
|
24
24
|
"operatingsystemrelease": [
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"7"
|
|
25
|
+
"8",
|
|
26
|
+
"9"
|
|
28
27
|
]
|
|
29
28
|
}
|
|
30
29
|
],
|