rspec-puppet-facts 2.0.5 → 4.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.
@@ -7,6 +7,12 @@ describe RspecPuppetFacts do
7
7
  'spec/fixtures/metadata.json'
8
8
  end
9
9
 
10
+ describe '.stringify_keys' do
11
+ it {
12
+ expect(described_class.stringify_keys({ os: { family: 'RedHat' } })).to eq({ 'os' => { 'family' => 'RedHat' } })
13
+ }
14
+ end
15
+
10
16
  describe '.facter_version_for_puppet_version' do
11
17
  subject(:facter_version) do
12
18
  described_class.facter_version_for_puppet_version(puppet_version)
@@ -19,7 +25,7 @@ describe RspecPuppetFacts do
19
25
  let(:puppet_version) { Puppet.version }
20
26
 
21
27
  context 'when the component JSON file does not exist' do
22
- before(:each) do
28
+ before do
23
29
  allow(File).to receive(:file?).with(component_json_path).and_return(false)
24
30
  allow(described_class).to receive(:warning)
25
31
  end
@@ -36,7 +42,7 @@ describe RspecPuppetFacts do
36
42
  end
37
43
 
38
44
  context 'when the component JSON file is unreadable' do
39
- before(:each) do
45
+ before do
40
46
  allow(File).to receive(:readable?).with(component_json_path).and_return(false)
41
47
  allow(described_class).to receive(:warning)
42
48
  end
@@ -53,7 +59,7 @@ describe RspecPuppetFacts do
53
59
  end
54
60
 
55
61
  context 'when the component JSON file is unparseable' do
56
- before(:each) do
62
+ before do
57
63
  io = StringIO.new('this is not JSON!')
58
64
  allow(File).to receive(:open).with(component_json_path, anything).and_return(io)
59
65
  allow(described_class).to receive(:warning)
@@ -112,7 +118,7 @@ describe RspecPuppetFacts do
112
118
  context 'when passed a Puppet version lower than any known version' do
113
119
  let(:puppet_version) { '1.0.0' }
114
120
 
115
- before(:each) do
121
+ before do
116
122
  allow(described_class).to receive(:warning)
117
123
  end
118
124
 
@@ -133,41 +139,41 @@ describe RspecPuppetFacts do
133
139
  subject(:result) do
134
140
  on_supported_os(
135
141
  {
136
- :supported_os => [
142
+ supported_os: [
137
143
  {
138
- "operatingsystem" => "Debian",
139
- "operatingsystemrelease" => ['7'],
144
+ 'operatingsystem' => 'Debian',
145
+ 'operatingsystemrelease' => ['12'],
140
146
  },
141
147
  ],
142
- }
148
+ },
143
149
  )
144
150
  end
145
151
 
146
152
  let(:get_keys) do
147
- proc { |r| r.keys + r.select { |_,v| v.is_a?(Hash) }.map { |_,v| get_keys.call(v) }.flatten }
153
+ proc { |r| r.keys + r.select { |_, v| v.is_a?(Hash) }.map { |_, v| get_keys.call(v) }.flatten }
148
154
  end
149
155
 
150
156
  context 'set to true' do
151
- before(:each) do
157
+ before do
152
158
  RSpec.configuration.facterdb_string_keys = true
153
159
  end
154
160
 
155
- after(:each) do
161
+ after do
156
162
  RSpec.configuration.facterdb_string_keys = false
157
163
  end
158
164
 
159
165
  it 'returns a fact set with all the keys as Strings' do
160
- expect(get_keys.call(result['debian-7-x86_64'])).to all(be_a(String))
166
+ expect(get_keys.call(result['debian-12-x86_64'])).to all(be_a(String))
161
167
  end
162
168
  end
163
169
 
164
170
  context 'set to false' do
165
- before(:each) do
171
+ before do
166
172
  RSpec.configuration.facterdb_string_keys = false
167
173
  end
168
174
 
169
175
  it 'returns a fact set with all the keys as Symbols or Strings' do
170
- expect(get_keys.call(result['debian-7-x86_64'])).to all(be_a(Symbol).or(be_a(String)))
176
+ expect(get_keys.call(result['debian-12-x86_64'])).to all(be_a(Symbol).or(be_a(String)))
171
177
  end
172
178
  end
173
179
  end
@@ -176,7 +182,7 @@ describe RspecPuppetFacts do
176
182
  subject { on_supported_os }
177
183
 
178
184
  context 'Without metadata.json' do
179
- before(:each) do
185
+ before do
180
186
  expect(File).to receive(:file?).with('metadata.json').and_return false
181
187
  end
182
188
 
@@ -185,10 +191,10 @@ describe RspecPuppetFacts do
185
191
 
186
192
  context 'With a metadata.json' do
187
193
  it 'can load the metadata file' do
188
- allow(RspecPuppetFacts).to receive(:metadata_file).and_return(metadata_file)
189
- RspecPuppetFacts.reset
190
- expect(RspecPuppetFacts.metadata).to be_a Hash
191
- expect(RspecPuppetFacts.metadata['name']).to eq 'mcanevet-mymodule'
194
+ allow(described_class).to receive(:metadata_file).and_return(metadata_file)
195
+ described_class.reset
196
+ expect(described_class.metadata).to be_a Hash
197
+ expect(described_class.metadata['name']).to eq 'mcanevet-mymodule'
192
198
  end
193
199
 
194
200
  context 'With a valid metadata.json' do
@@ -197,41 +203,37 @@ describe RspecPuppetFacts do
197
203
  JSON.parse fixture
198
204
  end
199
205
 
200
- before :each do
201
- allow(RspecPuppetFacts).to receive(:metadata).and_return(metadata)
206
+ before do
207
+ allow(described_class).to receive(:metadata).and_return(metadata)
202
208
  end
203
209
 
204
- it 'should return a hash' do
210
+ it 'returns a hash' do
205
211
  is_expected.to be_a Hash
206
212
  end
207
213
 
208
- it 'should have 5 elements' do
209
- expect(subject.size).to eq 5
210
- end
211
-
212
- it 'should return supported OS' do
213
- expect(subject.keys.sort).to eq %w(
214
- debian-7-x86_64
215
- debian-8-x86_64
216
- redhat-5-x86_64
217
- redhat-6-x86_64
218
- redhat-7-x86_64
214
+ it 'returns supported OS' do
215
+ expect(subject.keys).to contain_exactly(
216
+ 'debian-11-x86_64',
217
+ 'debian-12-x86_64',
218
+ 'redhat-7-x86_64',
219
+ 'redhat-8-x86_64',
220
+ 'redhat-9-x86_64',
219
221
  )
220
222
  end
221
223
 
222
- it 'should be able to filter the received OS facts' do
223
- allow(RspecPuppetFacts).to receive(:spec_facts_os_filter).and_return('redhat')
224
- expect(subject.keys.sort).to eq %w(
225
- redhat-5-x86_64
226
- redhat-6-x86_64
227
- redhat-7-x86_64
224
+ it 'is able to filter the received OS facts' do
225
+ allow(described_class).to receive(:spec_facts_os_filter).and_return('redhat')
226
+ expect(subject.keys).to contain_exactly(
227
+ 'redhat-7-x86_64',
228
+ 'redhat-8-x86_64',
229
+ 'redhat-9-x86_64',
228
230
  )
229
231
  end
230
232
  end
231
233
 
232
234
  context 'With a broken metadata.json' do
233
- before :each do
234
- allow(RspecPuppetFacts).to receive(:metadata).and_return(metadata)
235
+ before do
236
+ allow(described_class).to receive(:metadata).and_return(metadata)
235
237
  end
236
238
 
237
239
  context 'With a missing operatingsystem_support section' do
@@ -245,63 +247,58 @@ describe RspecPuppetFacts do
245
247
  context 'With a wrong operatingsystem_support section' do
246
248
  let(:metadata) do
247
249
  {
248
- 'operatingsystem_support' => 'Ubuntu',
250
+ 'operatingsystem_support' => 'Ubuntu',
249
251
  }
250
252
  end
251
253
 
252
254
  it { expect { subject }.to raise_error(StandardError, /Unknown operatingsystem support/) }
253
255
  end
254
256
  end
255
-
256
257
  end
257
258
  end
258
259
 
259
260
  context 'When specifying supported_os' do
260
- subject {
261
+ subject do
261
262
  on_supported_os(
262
263
  {
263
- :supported_os => [
264
+ supported_os: [
264
265
  {
265
- "operatingsystem" => "Debian",
266
- "operatingsystemrelease" => [
267
- "7",
268
- "8",
269
- ]
266
+ 'operatingsystem' => 'Debian',
267
+ 'operatingsystemrelease' => %w[
268
+ 11
269
+ 12
270
+ ],
270
271
  },
271
272
  {
272
- "operatingsystem" => "RedHat",
273
- "operatingsystemrelease" => [
274
- "5",
275
- "6"
276
- ]
277
- }
278
- ]
279
- }
273
+ 'operatingsystem' => 'RedHat',
274
+ 'operatingsystemrelease' => %w[
275
+ 8
276
+ 9
277
+ ],
278
+ },
279
+ ],
280
+ },
280
281
  )
281
- }
282
-
283
- it 'should return a hash' do
284
- is_expected.to be_a Hash
285
282
  end
286
283
 
287
- it 'should have 4 elements' do
288
- expect(subject.size).to eq 4
284
+ it 'returns a hash' do
285
+ is_expected.to be_a Hash
289
286
  end
290
287
 
291
- it 'should return supported OS' do
292
- expect(subject.keys.sort).to eq %w(
293
- debian-7-x86_64
294
- debian-8-x86_64
295
- redhat-5-x86_64
296
- redhat-6-x86_64
288
+ it 'returns supported OS' do
289
+ expect(subject.keys).to contain_exactly(
290
+ 'debian-11-x86_64',
291
+ 'debian-12-x86_64',
292
+ 'redhat-8-x86_64',
293
+ 'redhat-9-x86_64',
297
294
  )
298
295
  end
299
296
 
300
- it 'should be able to filter the received OS facts' do
301
- allow(RspecPuppetFacts).to receive(:spec_facts_os_filter).and_return('redhat')
302
- expect(subject.keys.sort).to eq %w(
303
- redhat-5-x86_64
304
- redhat-6-x86_64
297
+ it 'is able to filter the received OS facts' do
298
+ allow(described_class).to receive(:spec_facts_os_filter).and_return('redhat')
299
+ expect(subject.keys).to contain_exactly(
300
+ 'redhat-8-x86_64',
301
+ 'redhat-9-x86_64',
305
302
  )
306
303
  end
307
304
  end
@@ -310,10 +307,10 @@ describe RspecPuppetFacts do
310
307
  subject(:factsets) do
311
308
  on_supported_os(
312
309
  {
313
- :supported_os => [
314
- { 'operatingsystem' => 'RedHat', 'operatingsystemrelease' => '7' },
315
- ]
316
- }
310
+ supported_os: [
311
+ { 'operatingsystem' => 'RedHat', 'operatingsystemrelease' => '9' },
312
+ ],
313
+ },
317
314
  )
318
315
  end
319
316
 
@@ -321,304 +318,278 @@ describe RspecPuppetFacts do
321
318
  expect(factsets).to be_a(Hash)
322
319
  end
323
320
 
324
- it 'returns a single fact set' do
325
- expect(factsets.size).to eq(1)
326
- end
327
-
328
321
  it 'returns a fact set for the specified release' do
329
- expect(factsets).to include('redhat-7-x86_64' => include(:operatingsystemmajrelease => '7'))
322
+ expect(factsets).to match('redhat-9-x86_64' => include(operatingsystemmajrelease: '9'))
330
323
  end
331
324
  end
332
325
 
333
326
  context 'When testing Ubuntu' do
334
- subject {
327
+ subject do
335
328
  on_supported_os(
336
329
  {
337
- :supported_os => [
330
+ supported_os: [
338
331
  {
339
- "operatingsystem" => "Ubuntu",
340
- "operatingsystemrelease" => [
341
- "12.04",
342
- "14.04",
343
- "16.04",
332
+ 'operatingsystem' => 'Ubuntu',
333
+ 'operatingsystemrelease' => [
334
+ '18.04',
335
+ '20.04',
336
+ '22.04',
344
337
  ],
345
338
  },
346
339
  ],
347
- }
340
+ },
348
341
  )
349
- }
350
-
351
- let(:expected_fact_sets) do
352
- ['ubuntu-12.04-x86_64', 'ubuntu-14.04-x86_64', 'ubuntu-16.04-x86_64']
353
342
  end
354
343
 
355
- it 'should return a hash' do
356
- expect(subject.class).to eq Hash
344
+ it 'returns a hash' do
345
+ expect(subject).to be_a Hash
357
346
  end
358
- it 'should have 3 elements' do
359
- expect(subject.size).to eq(expected_fact_sets.size)
360
- end
361
- it 'should return supported OS' do
362
- expect(subject.keys.sort).to eq(expected_fact_sets)
347
+
348
+ it 'returns supported OS' do
349
+ expect(subject.keys).to contain_exactly(
350
+ 'ubuntu-18.04-x86_64',
351
+ 'ubuntu-20.04-x86_64',
352
+ 'ubuntu-22.04-x86_64',
353
+ )
363
354
  end
364
355
  end
365
356
 
366
357
  context 'When testing FreeBSD 10' do
367
- subject {
358
+ subject do
368
359
  on_supported_os(
369
360
  {
370
- :supported_os => [
361
+ supported_os: [
371
362
  {
372
- "operatingsystem" => "FreeBSD",
373
- "operatingsystemrelease" => [
374
- "10",
363
+ 'operatingsystem' => 'FreeBSD',
364
+ 'operatingsystemrelease' => [
365
+ '13',
375
366
  ],
376
367
  },
377
368
  ],
378
- :facterversion => '2.4',
379
- }
369
+ facterversion: '4.5',
370
+ },
380
371
  )
381
- }
382
- it 'should return a hash' do
383
- expect(subject.class).to eq Hash
384
372
  end
385
- it 'should have 1 elements' do
386
- expect(subject.size).to eq 1
373
+
374
+ it 'returns a hash' do
375
+ expect(subject).to be_a Hash
387
376
  end
388
- it 'should return supported OS' do
389
- expect(subject.keys.sort).to eq [
390
- 'freebsd-10-amd64',
391
- ]
377
+
378
+ it 'returns supported OS' do
379
+ expect(subject.keys).to contain_exactly(
380
+ 'freebsd-13-amd64',
381
+ )
392
382
  end
393
383
  end
394
384
 
395
385
  context 'When testing OpenBSD' do
396
- subject {
386
+ subject do
397
387
  on_supported_os(
398
388
  {
399
- :supported_os => [
389
+ supported_os: [
400
390
  {
401
- "operatingsystem" => "OpenBSD",
402
- "operatingsystemrelease" => [
403
- "5.7",
391
+ 'operatingsystem' => 'OpenBSD',
392
+ 'operatingsystemrelease' => [
393
+ '7.5',
404
394
  ],
405
395
  },
406
396
  ],
407
- :facterversion => '2.4',
408
- }
397
+ facterversion: '4.7',
398
+ },
409
399
  )
410
- }
411
- it 'should return a hash' do
412
- expect(subject.class).to eq Hash
413
400
  end
414
- it 'should have 1 elements' do
415
- expect(subject.size).to eq 1
401
+
402
+ it 'returns a hash' do
403
+ expect(subject).to be_a Hash
416
404
  end
417
- it 'should return supported OS' do
418
- expect(subject.keys.sort).to eq [
419
- 'openbsd-5.7-amd64',
420
- ]
405
+
406
+ it 'returns supported OS' do
407
+ expect(subject.keys).to contain_exactly(
408
+ 'openbsd-7-amd64',
409
+ )
421
410
  end
422
411
  end
423
412
 
424
- context 'When testing Solaris 11', :if => Facter.version.to_f >= 2.0 do
425
- subject {
413
+ context 'When testing Solaris 11' do
414
+ subject do
426
415
  on_supported_os(
427
- {
428
- :supported_os => [
429
- {
430
- "operatingsystem" => "Solaris",
431
- "operatingsystemrelease" => [
432
- "11",
433
- ],
434
- },
416
+ {
417
+ supported_os: [
418
+ {
419
+ 'operatingsystem' => 'Solaris',
420
+ 'operatingsystemrelease' => [
421
+ '11',
435
422
  ],
436
- }
423
+ },
424
+ ],
425
+ facterversion: '4.0',
426
+ },
437
427
  )
438
- }
439
- it 'should return a hash' do
440
- expect(subject.class).to eq Hash
441
428
  end
442
- it 'should have 1 elements' do
443
- expect(subject.size).to eq 1
429
+
430
+ it 'returns a hash' do
431
+ expect(subject).to be_a Hash
444
432
  end
445
- it 'should return supported OS' do
446
- expect(subject.keys.sort).to eq %w(
447
- solaris-11-i86pc
433
+
434
+ it 'returns supported OS' do
435
+ pending('2024-06-07: we dont have a suitable solaris 11 factset in facterdb')
436
+ expect(subject.keys).to contain_exactly(
437
+ 'solaris-11-i86pc',
448
438
  )
449
439
  end
450
440
  end
451
441
 
452
442
  context 'When testing AIX 7.1' do
453
- subject {
443
+ subject do
454
444
  on_supported_os(
455
- {
456
- :supported_os => [
457
- {
458
- "operatingsystem" => "AIX",
459
- "operatingsystemrelease" => [
460
- "7.1", "7100"
461
- ],
462
- },
445
+ {
446
+ supported_os: [
447
+ {
448
+ 'operatingsystem' => 'AIX',
449
+ 'operatingsystemrelease' => [
450
+ '7.1', '7100',
463
451
  ],
464
- :facterversion => '3.9'
465
- }
452
+ },
453
+ ],
454
+ facterversion: '3.9',
455
+ },
466
456
  )
467
- }
468
- it 'should return a hash' do
469
- expect(subject.class).to eq Hash
470
457
  end
471
- it 'should have 1 elements' do
472
- expect(subject.size).to eq 1
458
+
459
+ it 'returns a hash' do
460
+ expect(subject).to be_a Hash
473
461
  end
474
- it 'should return supported OS' do
462
+
463
+ it 'returns supported OS' do
475
464
  # NOTE: See FACT-1827 for details on the IBM,8284-22A part
476
465
  # That has to match whatever hardware generated the facts file.
477
- expect(subject.keys.sort).to eq %w(
478
- aix-7100-IBM,8284-22A
466
+ pending('2024-06-07: we dont have a suitable solaris 11 factset in facterdb')
467
+ expect(subject.keys).to contain_exactly(
468
+ 'aix-7100-IBM,8284-22A',
479
469
  )
480
470
  end
481
471
  end
482
472
 
483
- context 'When testing Windows', :if => Facter.version.to_f >= 2.4 do
473
+ context 'When testing Windows' do
484
474
  subject do
485
475
  on_supported_os(
486
476
  {
487
- :supported_os => [
477
+ supported_os: [
488
478
  {
489
- 'operatingsystem' => 'Windows',
479
+ 'operatingsystem' => 'Windows',
490
480
  'operatingsystemrelease' => release,
491
- }
481
+ },
492
482
  ],
493
- :facterversion => facterversion,
494
- }
483
+ facterversion: facterversion,
484
+ },
495
485
  )
496
486
  end
497
487
 
498
- let(:facterversion) { '3.8.0' }
488
+ let(:facterversion) { '4.2' }
499
489
 
500
490
  context 'with a standard release' do
501
- let(:release) { ['7'] }
491
+ let(:release) { ['10'] }
502
492
 
503
493
  it { is_expected.to be_a(Hash) }
504
- it { is_expected.to have_attributes(:size => 1) }
505
- it { is_expected.to include('windows-7-x86_64' => an_instance_of(Hash)) }
494
+ it { is_expected.to match('windows-10-x86_64' => an_instance_of(Hash)) }
506
495
  end
507
496
 
508
497
  context 'with a revision release' do
509
498
  let(:release) { ['2012 R2'] }
510
499
 
511
500
  it { is_expected.to be_a(Hash) }
512
- it { is_expected.to have_attributes(:size => 1) }
513
- it { is_expected.to include('windows-2012 R2-x86_64' => an_instance_of(Hash)) }
501
+ it { is_expected.to match('windows-2012 R2-x86_64' => an_instance_of(Hash)) }
514
502
  end
515
503
 
516
504
  context 'with a Server prefixed release' do
517
505
  let(:release) { ['Server 2012'] }
518
506
 
519
507
  it { is_expected.to be_a(Hash) }
520
- it { is_expected.to have_attributes(:size => 1) }
521
- it { is_expected.to include('windows-2012-x86_64' => an_instance_of(Hash)) }
508
+ it { is_expected.to match('windows-2012-x86_64' => an_instance_of(Hash)) }
522
509
  end
523
510
 
524
511
  context 'with a 2016 release' do
525
512
  let(:release) { ['2016'] }
526
513
 
527
514
  it { is_expected.to be_a(Hash) }
528
- it { is_expected.to have_attributes(:size => 1) }
529
- it { is_expected.to include('windows-2016-x86_64' => an_instance_of(Hash)) }
530
- end
531
-
532
- context 'with a 2016 release and Facter < 3.4' do
533
- let(:release) { ['2016'] }
534
- let(:facterversion) { '3.3.0' }
535
-
536
- it { is_expected.to be_a(Hash) }
537
- it { is_expected.to have_attributes(:size => 1) }
538
- it 'munges the operatingsystemmajrelease to 2016' do
539
- is_expected.to include('windows-2016-x86_64' => an_instance_of(Hash))
540
- end
515
+ it { is_expected.to match('windows-2016-x86_64' => an_instance_of(Hash)) }
541
516
  end
542
517
  end
543
518
 
544
519
  context 'When operatingsystemrelease has space' do
545
- subject {
520
+ subject do
546
521
  on_supported_os(
547
522
  {
548
- :supported_os => [
523
+ supported_os: [
549
524
  {
550
- "operatingsystem" => "SLES",
551
- "operatingsystemrelease" => [
552
- "11 SP1"
553
- ]
554
- }
555
- ]
556
- }
525
+ 'operatingsystem' => 'SLES',
526
+ 'operatingsystemrelease' => [
527
+ '11 SP1',
528
+ ],
529
+ },
530
+ ],
531
+ },
557
532
  )
558
- }
559
- it 'should return a hash' do
560
- expect(subject.class).to eq Hash
561
533
  end
562
- it 'should have 1 elements' do
563
- expect(subject.size).to eq 1
534
+
535
+ it 'returns a hash' do
536
+ expect(subject).to be_a Hash
564
537
  end
565
- it 'should return supported OS' do
566
- expect(subject.keys.sort).to eq [
567
- 'sles-11-x86_64',
568
- ]
538
+
539
+ it 'returns supported OS' do
540
+ pending('2024-06-7: facterdb has no factset with space in the system release')
541
+ expect(subject.keys).to contain_exactly('sles-11-x86_64')
569
542
  end
570
543
  end
571
544
 
572
545
  context 'When specifying wrong supported_os' do
573
- subject {
546
+ subject do
574
547
  on_supported_os(
575
548
  {
576
- :supported_os => [
549
+ supported_os: [
577
550
  {
578
- "operatingsystem" => "Debian",
579
- "operatingsystemrelease" => [
580
- "4",
551
+ 'operatingsystem' => 'Debian',
552
+ 'operatingsystemrelease' => [
553
+ '4',
581
554
  ],
582
555
  },
583
- ]
584
- }
556
+ ],
557
+ },
585
558
  )
586
- }
559
+ end
587
560
 
588
- it 'should output warning message' do
589
- expect(RspecPuppetFacts).to receive(:warning).with(/No facts were found in the FacterDB/)
561
+ it 'outputs warning message' do
562
+ expect(described_class).to receive(:warning).with(/No facts were found in the FacterDB/)
590
563
  subject
591
564
  end
592
565
  end
593
566
 
594
567
  context 'When specifying rolling release operating system' do
595
- subject {
568
+ subject do
596
569
  on_supported_os(
597
570
  {
598
- :supported_os => [
571
+ supported_os: [
599
572
  {
600
- "operatingsystem" => "Debian",
601
- "operatingsystemrelease" => [
602
- "8",
573
+ 'operatingsystem' => 'Debian',
574
+ 'operatingsystemrelease' => [
575
+ '12',
603
576
  ],
604
577
  },
605
578
  {
606
- "operatingsystem" => "Archlinux",
579
+ 'operatingsystem' => 'Gentoo',
607
580
  },
608
581
  ],
609
- :facterversion => '2.4',
610
- }
582
+ facterversion: '4.6',
583
+ },
611
584
  )
612
- }
613
-
614
- it 'should return a hash' do
615
- expect(subject.class).to eq Hash
616
585
  end
617
- it 'should have 2 elements' do
618
- expect(subject.size).to eq 2
586
+
587
+ it 'returns a hash' do
588
+ expect(subject).to be_a Hash
619
589
  end
620
- it 'should return supported OS' do
621
- expect(subject.keys.sort).to include(a_string_matching(/\Aarchlinux-\d+-x86_64/), 'debian-8-x86_64')
590
+
591
+ it 'returns supported OS' do
592
+ expect(subject.keys).to contain_exactly(a_string_matching(/gentoo-\d+-x86_64/), 'debian-12-x86_64')
622
593
  end
623
594
  end
624
595
 
@@ -626,31 +597,28 @@ describe RspecPuppetFacts do
626
597
  subject do
627
598
  on_supported_os(
628
599
  {
629
- :supported_os => [
600
+ supported_os: [
630
601
  {
631
- 'operatingsystem' => 'IOS',
602
+ 'operatingsystem' => 'IOS',
632
603
  'operatingsystemrelease' => ['12.2(25)EWA9'],
633
- }
604
+ },
634
605
  ],
635
- }
606
+ },
636
607
  )
637
608
  end
638
609
 
639
- before(:each) do
640
- allow(RspecPuppetFacts).to receive(:warning).with(a_string_matching(/no facts were found/i))
641
- allow(FacterDB).to receive(:get_facts).and_call_original
610
+ before do
611
+ allow(described_class).to receive(:warning).with(a_string_matching(/no facts were found/i))
642
612
  end
643
613
 
644
614
  it 'escapes the parens in the filter' do
645
- filter = [
646
- include(
647
- :operatingsystem => "IOS",
648
- :operatingsystemrelease => "/^12\\.2\\(25\\)EWA9/",
649
- :hardwaremodel => "x86_64",
650
- ),
651
- ]
615
+ filter = {
616
+ 'os.name' => 'IOS',
617
+ 'os.release.full' => '/^12\\.2\\(25\\)EWA9/',
618
+ 'os.hardware' => 'x86_64',
619
+ }
652
620
 
653
- expect(FacterDB).to receive(:get_facts).with(filter)
621
+ expect(FacterDB).to receive(:get_facts).with(filter, symbolize_keys: true).once
654
622
  subject
655
623
  end
656
624
 
@@ -660,146 +628,91 @@ describe RspecPuppetFacts do
660
628
  end
661
629
 
662
630
  context 'With a default Facter version specified in the RSpec configuration' do
663
- before(:each) do
664
- RSpec.configuration.default_facter_version = '3.1.0'
665
- end
666
-
667
- after(:each) do
668
- RSpec.configuration.default_facter_version = Facter.version
669
- end
670
-
671
631
  subject do
672
632
  on_supported_os(
673
633
  supported_os: [
674
- { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] }
675
- ]
634
+ { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[9] },
635
+ ],
676
636
  )
677
637
  end
678
638
 
679
- it 'returns facts from the specified default Facter version' do
680
- is_expected.to match(
681
- 'centos-7-x86_64' => include(
682
- :facterversion => /\A3\.1\./
683
- )
684
- )
639
+ before do
640
+ RSpec.configuration.default_facter_version = '4.6.1'
685
641
  end
686
- end
687
642
 
688
- context 'With a version that is above the current gem' do
689
- before(:each) do
690
- allow(Facter).to receive(:version).and_return('2.4.5')
691
- end
692
-
693
- subject do
694
- on_supported_os(
695
- supported_os: [
696
- { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] }
697
- ],
698
- facterversion: "2.6"
699
- )
643
+ after do
644
+ RSpec.configuration.default_facter_version = Facter.version
700
645
  end
701
646
 
702
- it 'returns facts from a facter version matching future and below' do
703
- major, minor = Facter.version.split('.')
647
+ it 'returns facts from the specified default Facter version' do
704
648
  is_expected.to match(
705
- 'centos-7-x86_64' => include(
706
- :facterversion => /\A#{major}\.[#{minor}#{minor.to_i + 1}]\./
707
- )
649
+ 'centos-9-x86_64' => include(
650
+ facterversion: /\A4\.6\./,
651
+ ),
708
652
  )
709
653
  end
710
-
711
- context 'With SPEC_FACTS_STRICT set to `yes`' do
712
- before(:each) do
713
- allow(RspecPuppetFacts).to receive(:spec_facts_strict?).and_return(true)
714
- end
715
- it 'errors' do
716
- expect { subject }.to raise_error ArgumentError, /No facts were found in the FacterDB.*aborting/
717
- end
718
- end
719
654
  end
720
655
 
721
- context 'With a custom facterversion (3.1) in the options hash' do
656
+ context 'With a version that is above the current gem' do
722
657
  subject do
723
658
  on_supported_os(
724
659
  supported_os: [
725
- { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] }
660
+ { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[9] },
726
661
  ],
727
- facterversion: '3.1'
728
- )
729
- end
730
-
731
- it 'returns facts from a facter version matching 3.1' do
732
- is_expected.to match(
733
- 'centos-7-x86_64' => include(:facterversion => '3.1.6')
662
+ facterversion: '4.7.99',
734
663
  )
735
664
  end
736
- end
737
665
 
738
- context 'With a custom facterversion (3.1.2) in the options hash' do
739
- subject do
740
- on_supported_os(
741
- supported_os: [
742
- { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] }
743
- ],
744
- facterversion: '3.1.2'
745
- )
666
+ before do
667
+ allow(Facter).to receive(:version).and_return('4.6')
746
668
  end
747
669
 
748
- it 'returns facts from a facter version matching 3.1' do
670
+ it 'returns facts from a facter version matching version and below' do
749
671
  is_expected.to match(
750
- 'centos-7-x86_64' => include(:facterversion => '3.1.6')
672
+ 'centos-9-x86_64' => include(
673
+ facterversion: /\A4\.[0-7]\./,
674
+ ),
751
675
  )
752
676
  end
753
- end
754
677
 
755
- context 'With a custom facterversion (3.3) in the options hash' do
756
- subject do
757
- on_supported_os(
758
- supported_os: [
759
- { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] }
760
- ],
761
- facterversion: '3.3'
762
- )
763
- end
678
+ context 'With SPEC_FACTS_STRICT set to `yes`' do
679
+ before do
680
+ allow(described_class).to receive(:spec_facts_strict?).and_return(true)
681
+ end
764
682
 
765
- it 'returns facts from a facter version matching 3.3' do
766
- is_expected.to match(
767
- 'centos-7-x86_64' => include(:facterversion => '3.3.0')
768
- )
683
+ it 'errors' do
684
+ expect { subject }.to raise_error ArgumentError, /No facts were found in the FacterDB.*aborting/
685
+ end
769
686
  end
770
687
  end
771
688
 
772
- context 'With a custom facterversion (3.3.2) in the options hash' do
689
+ context 'With a custom facterversion (4.6) in the options hash' do
773
690
  subject do
774
691
  on_supported_os(
775
692
  supported_os: [
776
- { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] }
693
+ { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[9] },
777
694
  ],
778
- facterversion: '3.3.2'
695
+ facterversion: '4.6',
779
696
  )
780
697
  end
781
698
 
782
- it 'returns facts from a facter version matching 3.3' do
783
- is_expected.to match(
784
- 'centos-7-x86_64' => include(:facterversion => '3.3.0')
785
- )
699
+ it 'returns facts from a facter version matching 4.6' do
700
+ is_expected.to match('centos-9-x86_64' => include(facterversion: '4.6.1'))
786
701
  end
787
702
  end
788
703
 
789
- context 'When querying a fact set that does not have an operatingsystemmajrelease fact' do
704
+ context 'With a custom facterversion (4.6.1) in the options hash' do
790
705
  subject do
791
706
  on_supported_os(
792
707
  supported_os: [
793
- { 'operatingsystem' => 'SLES', 'operatingsystemrelease' => ['11'] }
708
+ { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[9] },
794
709
  ],
795
- facterversion: '2.1.0'
710
+ facterversion: '4.6.1',
796
711
  )
797
712
  end
798
713
 
799
- it 'splits the operatingsystemrelease fact value to get the major release' do
800
- is_expected.to match(
801
- 'sles-11-x86_64' => include(:operatingsystemrelease => '11.3')
802
- )
714
+ it 'returns facts from a facter version matching 4.6.1' do
715
+ is_expected.to match('centos-9-x86_64' => include(facterversion: '4.6.1'))
803
716
  end
804
717
  end
805
718
 
@@ -807,9 +720,9 @@ describe RspecPuppetFacts do
807
720
  let(:method_call) do
808
721
  on_supported_os(
809
722
  supported_os: [
810
- { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] }
723
+ { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] },
811
724
  ],
812
- facterversion: '3'
725
+ facterversion: '3',
813
726
  )
814
727
  end
815
728
 
@@ -823,125 +736,152 @@ describe RspecPuppetFacts do
823
736
  subject do
824
737
  on_supported_os(
825
738
  supported_os: [
826
- { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] },
827
- { 'operatingsystem' => 'OpenSuSE', 'operatingsystemrelease' => %w[42] }
739
+ { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[9] },
740
+ { 'operatingsystem' => 'Debian', 'operatingsystemrelease' => %w[12] },
828
741
  ],
829
- facterversion: '3.9.5'
742
+ facterversion: '4.6.1',
830
743
  )
831
744
  end
832
745
 
833
- before(:each) do
746
+ before do
834
747
  allow(FacterDB).to receive(:get_facts).and_call_original
835
748
  allow(FacterDB).to receive(:get_facts).with(
836
- a_hash_including(facterversion: "/\\A3\\.9\\./", operatingsystem: 'CentOS')
837
- ).and_return([])
749
+ { 'os.name' => 'CentOS', 'os.release.full' => '/^9/', 'os.hardware' => 'x86_64' }, symbolize_keys: true
750
+ ).and_wrap_original do |m, *args|
751
+ m.call(*args).reject { |facts| facts[:facterversion].start_with?('4.6.') }
752
+ end
838
753
  end
839
754
 
840
- it 'returns CentOS facts from a facter version matching 3.8' do
841
- is_expected.to include(
842
- 'centos-7-x86_64' => include(:facterversion => '3.8.0')
843
- )
755
+ it 'returns CentOS facts from a facter version matching 4.5' do
756
+ is_expected.to include('centos-9-x86_64' => include(facterversion: '4.5.2'))
844
757
  end
845
- it 'returns OpenSuSE facts from a facter version matching 3.9' do
846
- is_expected.to include(
847
- 'opensuse-42-x86_64' => include(:facterversion => '3.9.2')
848
- )
758
+
759
+ it 'returns Debian facts from a facter version matching 4.6.1' do
760
+ is_expected.to include('debian-12-x86_64' => include(facterversion: '4.6.1'))
849
761
  end
850
762
  end
851
763
  end
852
764
 
853
- context '#add_custom_fact' do
854
- subject {
765
+ describe '#add_custom_fact' do
766
+ subject do
855
767
  on_supported_os(
856
768
  {
857
- :supported_os => [
769
+ supported_os: [
858
770
  {
859
- "operatingsystem" => "RedHat",
860
- "operatingsystemrelease" => [
861
- "6",
862
- "7"
863
- ]
864
- }
865
- ]
866
- }
771
+ 'operatingsystem' => 'RedHat',
772
+ 'operatingsystemrelease' => %w[
773
+ 8
774
+ 9
775
+ ],
776
+ },
777
+ ],
778
+ },
867
779
  )
868
- }
780
+ end
869
781
 
870
- before(:each) do
871
- RspecPuppetFacts.reset
782
+ before do
783
+ described_class.reset
872
784
  end
873
785
 
874
786
  it 'adds a simple fact and value' do
875
787
  add_custom_fact 'root_home', '/root'
876
- expect(subject['redhat-7-x86_64']['root_home']).to eq '/root'
788
+ expect(subject['redhat-9-x86_64'][:root_home]).to eq '/root'
789
+ end
790
+
791
+ it 'merges a fact value into fact when merge_facts passed' do
792
+ add_custom_fact :identity, { 'user' => 'test_user' }, merge_facts: true
793
+ expect(subject['redhat-9-x86_64'][:identity]).to eq(
794
+ {
795
+ 'gid' => 0,
796
+ 'group' => 'root',
797
+ 'privileged' => true,
798
+ 'uid' => 0,
799
+ 'user' => 'test_user',
800
+ },
801
+ )
802
+ end
803
+
804
+ it 'overwrites fact' do
805
+ add_custom_fact :identity, { 'user' => 'other_user' }
806
+ expect(subject['redhat-9-x86_64'][:identity]).to eq(
807
+ {
808
+ 'user' => 'other_user',
809
+ },
810
+ )
877
811
  end
878
812
 
879
813
  it 'confines a fact to a particular operating system' do
880
- add_custom_fact 'root_home', '/root', :confine => 'redhat-7-x86_64'
881
- expect(subject['redhat-7-x86_64']['root_home']).to eq '/root'
882
- expect(subject['redhat-6-x86_64']['root_home']).to be_nil
814
+ add_custom_fact 'root_home', '/root', confine: 'redhat-9-x86_64'
815
+ expect(subject['redhat-9-x86_64'][:root_home]).to eq '/root'
816
+ expect(subject['redhat-8-x86_64'][:root_home]).to be_nil
883
817
  end
884
818
 
885
819
  it 'excludes a fact from a particular operating system' do
886
- add_custom_fact 'root_home', '/root', :exclude => 'redhat-7-x86_64'
887
- expect(subject['redhat-7-x86_64']['root_home']).to be_nil
888
- expect(subject['redhat-6-x86_64']['root_home']).to eq '/root'
820
+ add_custom_fact 'root_home', '/root', exclude: 'redhat-9-x86_64'
821
+ expect(subject['redhat-9-x86_64'][:root_home]).to be_nil
822
+ expect(subject['redhat-8-x86_64'][:root_home]).to eq '/root'
889
823
  end
890
824
 
891
825
  it 'takes a proc as a value' do
892
826
  add_custom_fact 'root_home', ->(_os, _facts) { '/root' }
893
- expect(subject['redhat-7-x86_64']['root_home']).to eq '/root'
827
+ expect(subject['redhat-9-x86_64'][:root_home]).to eq '/root'
828
+ end
829
+
830
+ it 'accepts sym fact key and stores fact key as sym' do
831
+ add_custom_fact :root_home, ->(_os, _facts) { '/root' }
832
+ expect(subject['redhat-9-x86_64'][:root_home]).to eq '/root'
894
833
  end
895
834
  end
896
835
 
897
- context '#misc' do
898
- it 'should have a common facts structure' do
899
- RspecPuppetFacts.reset
836
+ describe '#misc' do
837
+ it 'has a common facts structure' do
838
+ described_class.reset
900
839
  expect(subject.common_facts).to be_a Hash
901
840
  expect(subject.common_facts).not_to be_empty
902
841
  end
903
842
 
904
- it 'should not add "augeasversion" if Augeas is supported' do
843
+ it 'does not add "augeasversion" if Augeas is supported' do
905
844
  allow(described_class).to receive(:augeas?).and_return(false)
906
- RspecPuppetFacts.reset
845
+ described_class.reset
907
846
  expect(subject.common_facts).not_to include(:augeasversion)
908
847
  end
909
848
 
910
- it 'should determine the Augeas version if Augeas is supported' do
911
- module Augeas_stub
849
+ it 'determines the Augeas version if Augeas is supported' do
850
+ module AugeasStub # rubocop:todo Lint/ConstantDefinitionInBlock
912
851
  NO_MODL_AUTOLOAD = true
913
852
  def self.open(*_args)
914
853
  self
915
854
  end
855
+
916
856
  def self.get(*_args)
917
857
  'my_version'
918
858
  end
919
859
  end
920
860
 
921
861
  allow(described_class).to receive(:augeas?).and_return(true)
922
- stub_const('Augeas', Augeas_stub)
923
- RspecPuppetFacts.reset
862
+ stub_const('Augeas', AugeasStub)
863
+ described_class.reset
924
864
  expect(subject.common_facts[:augeasversion]).to eq 'my_version'
925
865
  end
926
866
 
927
867
  context 'when mcollective is available' do
928
- module MCollective_stub
868
+ module MCollectiveStub # rubocop:todo Lint/ConstantDefinitionInBlock
929
869
  VERSION = 'my_version'
930
870
  end
931
871
 
932
- before(:each) do
872
+ before do
933
873
  allow(described_class).to receive(:mcollective?).and_return(true)
934
- stub_const('MCollective', MCollective_stub)
874
+ stub_const('MCollective', MCollectiveStub)
935
875
  described_class.reset
936
876
  end
937
877
 
938
878
  it 'includes an "mco_version" fact' do
939
- expect(subject.common_facts).to include(:mco_version => 'my_version')
879
+ expect(subject.common_facts).to include(mco_version: 'my_version')
940
880
  end
941
881
  end
942
882
 
943
883
  context 'when mcollective is not available' do
944
- before(:each) do
884
+ before do
945
885
  allow(described_class).to receive(:mcollective?).and_return(false)
946
886
  described_class.reset
947
887
  end
@@ -952,69 +892,145 @@ describe RspecPuppetFacts do
952
892
  end
953
893
  end
954
894
 
955
- describe '.facter_version_to_filter' do
895
+ describe '.facter_version_to_strict_requirement' do
896
+ subject { described_class.facter_version_to_strict_requirement(version) }
897
+
898
+ context 'when passed a version that is a complex requirement' do
899
+ let(:version) { '~> 2.4' }
900
+
901
+ it { is_expected.to be_instance_of(Gem::Requirement) }
902
+ end
903
+
904
+ context 'when passed a version that is major' do
905
+ let(:version) { '1' }
906
+
907
+ it { is_expected.to be_instance_of(Gem::Requirement) }
908
+ end
909
+ end
910
+
911
+ describe '.facter_version_to_strict_requirement_string' do
912
+ subject { described_class.facter_version_to_strict_requirement_string(version) }
913
+
914
+ context 'when passed a version that is a complex requirement' do
915
+ let(:version) { '~> 2.4' }
916
+
917
+ it { is_expected.to eq('~> 2.4') }
918
+ end
919
+
920
+ context 'when passed a version that is major' do
921
+ let(:version) { '1' }
922
+
923
+ it { is_expected.to eq('~> 1.0') }
924
+ end
925
+
926
+ context 'when passed a version that is major.minor' do
927
+ let(:version) { '1.2' }
928
+
929
+ it { is_expected.to eq('~> 1.2.0') }
930
+ end
931
+
932
+ context 'when passed a version that is major.minor.patch' do
933
+ let(:version) { '1.2.3' }
934
+
935
+ it { is_expected.to eq('~> 1.2.3.0') }
936
+ end
937
+ end
938
+
939
+ describe '.facter_version_to_loose_requirement' do
940
+ subject { described_class.facter_version_to_loose_requirement(version) }
941
+
942
+ context 'when passed a version that is a complex requirement' do
943
+ let(:version) { '~> 2.4' }
944
+
945
+ it { is_expected.to be_nil }
946
+ end
947
+
948
+ context 'when passed a version that is major' do
949
+ let(:version) { '1' }
950
+
951
+ it { is_expected.to be_instance_of(Gem::Requirement) }
952
+ end
953
+ end
954
+
955
+ describe '.facter_version_to_loose_requirement_string' do
956
+ subject { described_class.facter_version_to_loose_requirement_string(version) }
957
+
958
+ context 'when passed a version that is a complex requirement (1)' do
959
+ let(:version) { '~> 2.4' }
960
+
961
+ it { is_expected.to be_nil }
962
+ end
963
+
964
+ context 'when passed a version that is a complex requirement (2)' do
965
+ let(:version) { '>= 3 < 5' }
966
+
967
+ it { is_expected.to be_nil }
968
+ end
969
+
970
+ context 'when passed a version that is major (1)' do
971
+ let(:version) { '1' }
972
+
973
+ it { is_expected.to eq('< 2') }
974
+ end
975
+
976
+ context 'when passed a version that is major (2)' do
977
+ let(:version) { '9' }
978
+
979
+ it { is_expected.to eq('< 10') }
980
+ end
981
+
982
+ context 'when passed a version that is major (3)' do
983
+ let(:version) { '10' }
984
+
985
+ it { is_expected.to eq('< 11') }
986
+ end
987
+
956
988
  context 'when passed a version that is major.minor (1)' do
957
- subject { RspecPuppetFacts.facter_version_to_filter('1.2') }
989
+ let(:version) { '1.2' }
958
990
 
959
- it 'returns the correct JGrep statement expression' do
960
- is_expected.to eq('/\A1\.2\./')
961
- end
991
+ it { is_expected.to eq('< 1.3') }
962
992
  end
963
993
 
964
994
  context 'when passed a version that is major.minor (2)' do
965
- subject { RspecPuppetFacts.facter_version_to_filter('10.2') }
995
+ let(:version) { '10.2' }
966
996
 
967
- it 'returns the correct JGrep statement expression' do
968
- is_expected.to eq('/\A10\.2\./')
969
- end
997
+ it { is_expected.to eq('< 10.3') }
970
998
  end
971
999
 
972
1000
  context 'when passed a version that is major.minor (3)' do
973
- subject { RspecPuppetFacts.facter_version_to_filter('1.20') }
1001
+ let(:version) { '1.20' }
974
1002
 
975
- it 'returns the correct JGrep statement expression' do
976
- is_expected.to eq('/\A1\.20\./')
977
- end
1003
+ it { is_expected.to eq('< 1.21') }
978
1004
  end
979
1005
 
980
1006
  context 'when passed a version that is major.minor (4)' do
981
- subject { RspecPuppetFacts.facter_version_to_filter('10.20') }
1007
+ let(:version) { '10.20' }
982
1008
 
983
- it 'returns the correct JGrep statement expression' do
984
- is_expected.to eq('/\A10\.20\./')
985
- end
1009
+ it { is_expected.to eq('< 10.21') }
986
1010
  end
987
1011
 
988
1012
  context 'when passed a version that is major.minor.patch (1)' do
989
- subject { RspecPuppetFacts.facter_version_to_filter('1.2.3') }
1013
+ let(:version) { '1.2.3' }
990
1014
 
991
- it 'returns the correct JGrep statement expression' do
992
- is_expected.to eq('/\A1\.2\./')
993
- end
1015
+ it { is_expected.to eq('< 1.3') }
994
1016
  end
995
1017
 
996
1018
  context 'when passed a version that is major.minor.patch (2)' do
997
- subject { RspecPuppetFacts.facter_version_to_filter('10.2.3') }
1019
+ let(:version) { '10.2.3' }
998
1020
 
999
- it 'returns the correct JGrep statement expression' do
1000
- is_expected.to eq('/\A10\.2\./')
1001
- end
1021
+ it { is_expected.to eq('< 10.3') }
1002
1022
  end
1003
1023
 
1004
1024
  context 'when passed a version that is major.minor.patch (3)' do
1005
- subject { RspecPuppetFacts.facter_version_to_filter('1.20.3') }
1025
+ let(:version) { '1.20.3' }
1006
1026
 
1007
- it 'returns the correct JGrep statement expression' do
1008
- is_expected.to eq('/\A1\.20\./')
1009
- end
1027
+ it { is_expected.to eq('< 1.21') }
1010
1028
  end
1011
1029
 
1012
1030
  context 'when passed a version that is major.minor.patch (4)' do
1013
- subject { RspecPuppetFacts.facter_version_to_filter('10.20.3') }
1031
+ let(:version) { '10.20.3' }
1014
1032
 
1015
- it 'returns the correct JGrep statement expression' do
1016
- is_expected.to eq('/\A10\.20\./')
1017
- end
1033
+ it { is_expected.to eq('< 10.21') }
1018
1034
  end
1019
1035
  end
1020
1036
  end