rspec-puppet-facts 1.9.0 → 2.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.
@@ -1,12 +1,176 @@
1
1
  require 'spec_helper'
2
2
  require 'json'
3
+ require 'stringio'
3
4
 
4
5
  describe RspecPuppetFacts do
5
6
  let(:metadata_file) do
6
7
  'spec/fixtures/metadata.json'
7
8
  end
8
9
 
10
+ describe '.facter_version_for_puppet_version' do
11
+ subject(:facter_version) do
12
+ described_class.facter_version_for_puppet_version(puppet_version)
13
+ end
14
+
15
+ let(:component_json_path) do
16
+ File.expand_path(File.join(__dir__, '..', 'ext', 'puppet_agent_components.json'))
17
+ end
18
+
19
+ let(:puppet_version) { Puppet.version }
20
+
21
+ context 'when the component JSON file does not exist' do
22
+ before(:each) do
23
+ allow(File).to receive(:file?).with(component_json_path).and_return(false)
24
+ allow(described_class).to receive(:warning)
25
+ end
26
+
27
+ it 'defaults to the currently installed Facter version' do
28
+ expect(facter_version).to eq(Facter.version)
29
+ end
30
+
31
+ it 'warns the user' do
32
+ msg = /does not exist or is not readable, defaulting to/i
33
+ expect(described_class).to receive(:warning).with(a_string_matching(msg))
34
+ facter_version
35
+ end
36
+ end
37
+
38
+ context 'when the component JSON file is unreadable' do
39
+ before(:each) do
40
+ allow(File).to receive(:readable?).with(component_json_path).and_return(false)
41
+ allow(described_class).to receive(:warning)
42
+ end
43
+
44
+ it 'defaults to the currently installed Facter version' do
45
+ expect(facter_version).to eq(Facter.version)
46
+ end
47
+
48
+ it 'warns the user' do
49
+ msg = /does not exist or is not readable, defaulting to/i
50
+ expect(described_class).to receive(:warning).with(a_string_matching(msg))
51
+ facter_version
52
+ end
53
+ end
54
+
55
+ context 'when the component JSON file is unparseable' do
56
+ before(:each) do
57
+ io = StringIO.new('this is not JSON!')
58
+ allow(File).to receive(:open).with(component_json_path, anything).and_return(io)
59
+ allow(described_class).to receive(:warning)
60
+ end
61
+
62
+ it 'defaults to the currently installed Facter version' do
63
+ expect(facter_version).to eq(Facter.version)
64
+ end
65
+
66
+ it 'warns the user' do
67
+ msg = /contains invalid json, defaulting to/i
68
+ expect(described_class).to receive(:warning).with(a_string_matching(msg))
69
+ facter_version
70
+ end
71
+ end
72
+
73
+ context 'when the passed puppet_version is nil' do
74
+ let(:puppet_version) { nil }
75
+
76
+ it 'defaults to the currently installed Facter version' do
77
+ expect(facter_version).to eq(Facter.version)
78
+ end
79
+ end
80
+
81
+ context 'when passed a Puppet version greater than any known version' do
82
+ let(:puppet_version) { '999.0.0' }
83
+
84
+ it 'returns the Facter version for the highest known Puppet version' do
85
+ known_facter_versions = JSON.parse(File.read(component_json_path)).map do |_, r|
86
+ r['facter']
87
+ end
88
+ sorted_facter_versions = known_facter_versions.compact.sort do |a, b|
89
+ Gem::Version.new(b) <=> Gem::Version.new(a)
90
+ end
91
+
92
+ expect(facter_version).to eq(sorted_facter_versions.first)
93
+ end
94
+ end
95
+
96
+ context 'when passed a known Puppet version' do
97
+ let(:puppet_version) { '5.2.0' }
98
+
99
+ it 'returns the Facter version for that Puppet version' do
100
+ expect(facter_version).to eq('3.9.0')
101
+ end
102
+ end
103
+
104
+ context 'when passed a Puppet version between two known versions' do
105
+ let(:puppet_version) { '5.2.5' }
106
+
107
+ it 'returns the Facter version for the lower Puppet version' do
108
+ expect(facter_version).to eq('3.9.0')
109
+ end
110
+ end
111
+
112
+ context 'when passed a Puppet version lower than any known version' do
113
+ let(:puppet_version) { '1.0.0' }
114
+
115
+ before(:each) do
116
+ allow(described_class).to receive(:warning)
117
+ end
118
+
119
+ it 'returns the currently installed Facter version' do
120
+ expect(facter_version).to eq(Facter.version)
121
+ end
122
+
123
+ it 'warns the user' do
124
+ msg = /unable to find puppet #{Regexp.escape(puppet_version)}.+?, defaulting to/i
125
+ expect(described_class).to receive(:warning).with(a_string_matching(msg))
126
+ facter_version
127
+ end
128
+ end
129
+ end
130
+
9
131
  describe '#on_supported_os' do
132
+ context 'With RSpec.configuration.facterdb_string_keys' do
133
+ subject(:result) do
134
+ on_supported_os(
135
+ {
136
+ :supported_os => [
137
+ {
138
+ "operatingsystem" => "Debian",
139
+ "operatingsystemrelease" => ['7'],
140
+ },
141
+ ],
142
+ }
143
+ )
144
+ end
145
+
146
+ let(:get_keys) do
147
+ proc { |r| r.keys + r.select { |_,v| v.is_a?(Hash) }.map { |_,v| get_keys.call(v) }.flatten }
148
+ end
149
+
150
+ context 'set to true' do
151
+ before(:each) do
152
+ RSpec.configuration.facterdb_string_keys = true
153
+ end
154
+
155
+ after(:each) do
156
+ RSpec.configuration.facterdb_string_keys = false
157
+ end
158
+
159
+ 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))
161
+ end
162
+ end
163
+
164
+ context 'set to false' do
165
+ before(:each) do
166
+ RSpec.configuration.facterdb_string_keys = false
167
+ end
168
+
169
+ 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)))
171
+ end
172
+ end
173
+ end
10
174
 
11
175
  context 'Without specifying supported_os' do
12
176
  subject { on_supported_os }
@@ -142,6 +306,30 @@ describe RspecPuppetFacts do
142
306
  end
143
307
  end
144
308
 
309
+ context 'When specifying a supported_os with a single release as a String' do
310
+ subject(:factsets) do
311
+ on_supported_os(
312
+ {
313
+ :supported_os => [
314
+ { 'operatingsystem' => 'RedHat', 'operatingsystemrelease' => '7' },
315
+ ]
316
+ }
317
+ )
318
+ end
319
+
320
+ it 'returns a Hash' do
321
+ expect(factsets).to be_a(Hash)
322
+ end
323
+
324
+ it 'returns a single fact set' do
325
+ expect(factsets.size).to eq(1)
326
+ end
327
+
328
+ it 'returns a fact set for the specified release' do
329
+ expect(factsets).to include('redhat-7-x86_64' => include(:operatingsystemmajrelease => '7'))
330
+ end
331
+ end
332
+
145
333
  context 'When testing Ubuntu' do
146
334
  subject {
147
335
  on_supported_os(
@@ -161,17 +349,13 @@ describe RspecPuppetFacts do
161
349
  }
162
350
 
163
351
  let(:expected_fact_sets) do
164
- if Facter.version.to_f < 1.7
165
- ['ubuntu-12.04-x86_64', 'ubuntu-14.04-x86_64']
166
- else
167
- ['ubuntu-12.04-x86_64', 'ubuntu-14.04-x86_64', 'ubuntu-16.04-x86_64']
168
- end
352
+ ['ubuntu-12.04-x86_64', 'ubuntu-14.04-x86_64', 'ubuntu-16.04-x86_64']
169
353
  end
170
354
 
171
355
  it 'should return a hash' do
172
356
  expect(subject.class).to eq Hash
173
357
  end
174
- it 'should have 4 elements' do
358
+ it 'should have 3 elements' do
175
359
  expect(subject.size).to eq(expected_fact_sets.size)
176
360
  end
177
361
  it 'should return supported OS' do
@@ -265,6 +449,37 @@ describe RspecPuppetFacts do
265
449
  end
266
450
  end
267
451
 
452
+ context 'When testing AIX 7.1' do
453
+ subject {
454
+ on_supported_os(
455
+ {
456
+ :supported_os => [
457
+ {
458
+ "operatingsystem" => "AIX",
459
+ "operatingsystemrelease" => [
460
+ "7.1", "7100"
461
+ ],
462
+ },
463
+ ],
464
+ :facterversion => '3.9'
465
+ }
466
+ )
467
+ }
468
+ it 'should return a hash' do
469
+ expect(subject.class).to eq Hash
470
+ end
471
+ it 'should have 1 elements' do
472
+ expect(subject.size).to eq 1
473
+ end
474
+ it 'should return supported OS' do
475
+ # NOTE: See FACT-1827 for details on the IBM,8284-22A part
476
+ # That has to match whatever hardware generated the facts file.
477
+ expect(subject.keys.sort).to eq %w(
478
+ aix-7100-IBM,8284-22A
479
+ )
480
+ end
481
+ end
482
+
268
483
  context 'When testing Windows', :if => Facter.version.to_f >= 2.4 do
269
484
  subject do
270
485
  on_supported_os(
@@ -275,16 +490,19 @@ describe RspecPuppetFacts do
275
490
  'operatingsystemrelease' => release,
276
491
  }
277
492
  ],
493
+ :facterversion => facterversion,
278
494
  }
279
495
  )
280
496
  end
281
497
 
498
+ let(:facterversion) { '3.8.0' }
499
+
282
500
  context 'with a standard release' do
283
501
  let(:release) { ['7'] }
284
502
 
285
503
  it { is_expected.to be_a(Hash) }
286
504
  it { is_expected.to have_attributes(:size => 1) }
287
- it { is_expected.to include('windows-7-x64' => an_instance_of(Hash)) }
505
+ it { is_expected.to include('windows-7-x86_64' => an_instance_of(Hash)) }
288
506
  end
289
507
 
290
508
  context 'with a revision release' do
@@ -292,7 +510,7 @@ describe RspecPuppetFacts do
292
510
 
293
511
  it { is_expected.to be_a(Hash) }
294
512
  it { is_expected.to have_attributes(:size => 1) }
295
- it { is_expected.to include('windows-2012 R2-x64' => an_instance_of(Hash)) }
513
+ it { is_expected.to include('windows-2012 R2-x86_64' => an_instance_of(Hash)) }
296
514
  end
297
515
 
298
516
  context 'with a Server prefixed release' do
@@ -300,7 +518,7 @@ describe RspecPuppetFacts do
300
518
 
301
519
  it { is_expected.to be_a(Hash) }
302
520
  it { is_expected.to have_attributes(:size => 1) }
303
- it { is_expected.to include('windows-2012-x64' => an_instance_of(Hash)) }
521
+ it { is_expected.to include('windows-2012-x86_64' => an_instance_of(Hash)) }
304
522
  end
305
523
 
306
524
  context 'with a 2016 release' do
@@ -308,7 +526,18 @@ describe RspecPuppetFacts do
308
526
 
309
527
  it { is_expected.to be_a(Hash) }
310
528
  it { is_expected.to have_attributes(:size => 1) }
311
- it { is_expected.to include('windows-2016-x64' => an_instance_of(Hash)) }
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
312
541
  end
313
542
  end
314
543
 
@@ -389,17 +618,56 @@ describe RspecPuppetFacts do
389
618
  expect(subject.size).to eq 2
390
619
  end
391
620
  it 'should return supported OS' do
392
- expect(subject.keys.sort).to eq %w(
393
- archlinux-3-x86_64
394
- debian-8-x86_64
621
+ expect(subject.keys.sort).to include(a_string_matching(/\Aarchlinux-\d+-x86_64/), 'debian-8-x86_64')
622
+ end
623
+ end
624
+
625
+ context 'When the operatingsystemrelease contains parens' do
626
+ subject do
627
+ on_supported_os(
628
+ {
629
+ :supported_os => [
630
+ {
631
+ 'operatingsystem' => 'IOS',
632
+ 'operatingsystemrelease' => ['12.2(25)EWA9'],
633
+ }
634
+ ],
635
+ }
395
636
  )
396
637
  end
638
+
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
642
+ end
643
+
644
+ 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
+ ]
652
+
653
+ expect(FacterDB).to receive(:get_facts).with(filter)
654
+ subject
655
+ end
656
+
657
+ it 'does not raise an error' do
658
+ expect { subject }.not_to raise_error
659
+ end
397
660
  end
398
661
 
399
- context 'Without a custom facterversion in the options hash' do
662
+ context 'With a default Facter version specified in the RSpec configuration' do
400
663
  before(:each) do
401
- allow(Facter).to receive(:version).and_return('2.4.5')
664
+ RSpec.configuration.default_facter_version = '3.1.0'
402
665
  end
666
+
667
+ after(:each) do
668
+ RSpec.configuration.default_facter_version = Facter.version
669
+ end
670
+
403
671
  subject do
404
672
  on_supported_os(
405
673
  supported_os: [
@@ -408,10 +676,10 @@ describe RspecPuppetFacts do
408
676
  )
409
677
  end
410
678
 
411
- it 'returns facts from the loaded facter version' do
679
+ it 'returns facts from the specified default Facter version' do
412
680
  is_expected.to match(
413
681
  'centos-7-x86_64' => include(
414
- facterversion: /\A2\.4\./
682
+ :facterversion => /\A3\.1\./
415
683
  )
416
684
  )
417
685
  end
@@ -435,7 +703,7 @@ describe RspecPuppetFacts do
435
703
  major, minor = Facter.version.split('.')
436
704
  is_expected.to match(
437
705
  'centos-7-x86_64' => include(
438
- facterversion: /\A#{major}\.[#{minor}#{minor.to_i + 1}]\./
706
+ :facterversion => /\A#{major}\.[#{minor}#{minor.to_i + 1}]\./
439
707
  )
440
708
  )
441
709
  end
@@ -462,7 +730,7 @@ describe RspecPuppetFacts do
462
730
 
463
731
  it 'returns facts from a facter version matching 3.1' do
464
732
  is_expected.to match(
465
- 'centos-7-x86_64' => include(facterversion: '3.1.6')
733
+ 'centos-7-x86_64' => include(:facterversion => '3.1.6')
466
734
  )
467
735
  end
468
736
  end
@@ -479,7 +747,7 @@ describe RspecPuppetFacts do
479
747
 
480
748
  it 'returns facts from a facter version matching 3.1' do
481
749
  is_expected.to match(
482
- 'centos-7-x86_64' => include(facterversion: '3.1.6')
750
+ 'centos-7-x86_64' => include(:facterversion => '3.1.6')
483
751
  )
484
752
  end
485
753
  end
@@ -496,7 +764,7 @@ describe RspecPuppetFacts do
496
764
 
497
765
  it 'returns facts from a facter version matching 3.3' do
498
766
  is_expected.to match(
499
- 'centos-7-x86_64' => include(facterversion: '3.3.0')
767
+ 'centos-7-x86_64' => include(:facterversion => '3.3.0')
500
768
  )
501
769
  end
502
770
  end
@@ -513,7 +781,24 @@ describe RspecPuppetFacts do
513
781
 
514
782
  it 'returns facts from a facter version matching 3.3' do
515
783
  is_expected.to match(
516
- 'centos-7-x86_64' => include(facterversion: '3.3.0')
784
+ 'centos-7-x86_64' => include(:facterversion => '3.3.0')
785
+ )
786
+ end
787
+ end
788
+
789
+ context 'When querying a fact set that does not have an operatingsystemmajrelease fact' do
790
+ subject do
791
+ on_supported_os(
792
+ supported_os: [
793
+ { 'operatingsystem' => 'SLES', 'operatingsystemrelease' => ['11'] }
794
+ ],
795
+ facterversion: '2.1.0'
796
+ )
797
+ end
798
+
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')
517
802
  )
518
803
  end
519
804
  end
@@ -533,6 +818,36 @@ describe RspecPuppetFacts do
533
818
  /:facterversion must be in the /)
534
819
  end
535
820
  end
821
+
822
+ context 'Downgrades to a facter version with facts per OS' do
823
+ subject do
824
+ on_supported_os(
825
+ supported_os: [
826
+ { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] },
827
+ { 'operatingsystem' => 'OpenSuSE', 'operatingsystemrelease' => %w[42] }
828
+ ],
829
+ facterversion: '3.9.5'
830
+ )
831
+ end
832
+
833
+ before(:each) do
834
+ allow(FacterDB).to receive(:get_facts).and_call_original
835
+ allow(FacterDB).to receive(:get_facts).with(
836
+ a_hash_including(facterversion: "/\\A3\\.9\\./", operatingsystem: 'CentOS')
837
+ ).and_return([])
838
+ end
839
+
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
+ )
844
+ 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
+ )
849
+ end
850
+ end
536
851
  end
537
852
 
538
853
  context '#add_custom_fact' do
@@ -587,9 +902,9 @@ describe RspecPuppetFacts do
587
902
  end
588
903
 
589
904
  it 'should not add "augeasversion" if Augeas is supported' do
590
- allow(Puppet.features).to receive(:augeas?).and_return(false)
905
+ allow(described_class).to receive(:augeas?).and_return(false)
591
906
  RspecPuppetFacts.reset
592
- expect(subject.common_facts).not_to include :augeasversion
907
+ expect(subject.common_facts).not_to include(:augeasversion)
593
908
  end
594
909
 
595
910
  it 'should determine the Augeas version if Augeas is supported' do
@@ -603,14 +918,37 @@ describe RspecPuppetFacts do
603
918
  end
604
919
  end
605
920
 
606
- allow(Puppet.features).to receive(:augeas?).and_return(true)
921
+ allow(described_class).to receive(:augeas?).and_return(true)
607
922
  stub_const('Augeas', Augeas_stub)
608
923
  RspecPuppetFacts.reset
609
924
  expect(subject.common_facts[:augeasversion]).to eq 'my_version'
610
925
  end
611
926
 
612
- it 'can output a warning message' do
613
- expect { RspecPuppetFacts.warning('test') }.to output(/test/).to_stderr_from_any_process
927
+ context 'when mcollective is available' do
928
+ module MCollective_stub
929
+ VERSION = 'my_version'
930
+ end
931
+
932
+ before(:each) do
933
+ allow(described_class).to receive(:mcollective?).and_return(true)
934
+ stub_const('MCollective', MCollective_stub)
935
+ described_class.reset
936
+ end
937
+
938
+ it 'includes an "mco_version" fact' do
939
+ expect(subject.common_facts).to include(:mco_version => 'my_version')
940
+ end
941
+ end
942
+
943
+ context 'when mcollective is not available' do
944
+ before(:each) do
945
+ allow(described_class).to receive(:mcollective?).and_return(false)
946
+ described_class.reset
947
+ end
948
+
949
+ it 'does not include an "mco_version" fact' do
950
+ expect(subject.common_facts).not_to include(:mco_version)
951
+ end
614
952
  end
615
953
  end
616
954
 
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,7 @@
1
- require 'coveralls'
2
- Coveralls.wear!
1
+ if ENV['COVERAGE']
2
+ require 'coveralls'
3
+ Coveralls.wear!
4
+ end
3
5
 
4
6
  require 'rspec'
5
7
  require 'rspec-puppet-facts'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-puppet-facts
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mickaël Canévet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-01 00:00:00.000000000 Z
11
+ date: 2020-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types
@@ -94,20 +94,6 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: json
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :runtime
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
97
  - !ruby/object:Gem::Dependency
112
98
  name: facter
113
99
  requirement: !ruby/object:Gem::Requirement
@@ -136,20 +122,6 @@ dependencies:
136
122
  - - ">="
137
123
  - !ruby/object:Gem::Version
138
124
  version: 0.5.0
139
- - !ruby/object:Gem::Dependency
140
- name: mcollective-client
141
- requirement: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - ">="
144
- - !ruby/object:Gem::Version
145
- version: '0'
146
- type: :runtime
147
- prerelease: false
148
- version_requirements: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - ">="
151
- - !ruby/object:Gem::Version
152
- version: '0'
153
125
  description: Contains facts from many Facter version on many Operating Systems
154
126
  email:
155
127
  - mickael.canevet@camptocamp.com
@@ -165,6 +137,7 @@ files:
165
137
  - LICENSE
166
138
  - README.md
167
139
  - Rakefile
140
+ - ext/puppet_agent_components.json
168
141
  - lib/rspec-puppet-facts.rb
169
142
  - lib/rspec-puppet-facts/version.rb
170
143
  - rspec-puppet-facts.gemspec
@@ -184,15 +157,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
184
157
  requirements:
185
158
  - - ">="
186
159
  - !ruby/object:Gem::Version
187
- version: '0'
160
+ version: 2.4.0
188
161
  required_rubygems_version: !ruby/object:Gem::Requirement
189
162
  requirements:
190
163
  - - ">="
191
164
  - !ruby/object:Gem::Version
192
165
  version: '0'
193
166
  requirements: []
194
- rubyforge_project:
195
- rubygems_version: 2.5.2.2
167
+ rubygems_version: 3.1.2
196
168
  signing_key:
197
169
  specification_version: 4
198
170
  summary: Standard facts fixtures for Puppet