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