rspec-puppet-facts 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 25dba2e8315cd00eabd5d471bfae20a617ac3cdb
4
- data.tar.gz: 1362031f653c28534a8858edfb330218eb05d97a
3
+ metadata.gz: 67e267fa84f829a97dd7a211cd499b7916038eda
4
+ data.tar.gz: 1cae575348cca8a0483c935f68d58d1c315b7c84
5
5
  SHA512:
6
- metadata.gz: 4f5f2ad99655433291c78064abe45919123be877124f9ec8f204cb5b08ca5bd698fd4cc482b3cdc186057e366d890ff253b3557449efba526defb6927f07ba90
7
- data.tar.gz: 3ace83756a933a7d4d09e573dd0387d5cc80dcf1c4b12d2fcb1cea41ab06567d24edc8e020b948e2fecd5cf0168920b09f185c459fd8ecb46dc312393cf6ea45
6
+ metadata.gz: 817f91d0427e96c4d57a4d3524adc8374308c23fd805384718f803a13853c71b29383f146797654f168aa93a83483ee13efa83f89cdbbc707597978f1b6d23bf
7
+ data.tar.gz: 65220d4078595dad750393f9e3ea2725b638da129d6cabd71e823d2b03c73700ffdabb1defa5e5c250551b5a6b3a69bd24f38bed35154e9ac9f0ab6f96bf418d
data/.gitignore CHANGED
@@ -5,3 +5,5 @@ Gemfile.lock
5
5
  .bundle/
6
6
  coverage/
7
7
  .idea
8
+ doc
9
+ .yardoc
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ##2016-05-17 - Release 1.6.0
2
+ - Cleanup and refactor methods
3
+ - Add YARD documentation
4
+ - Refactor and fix rspec
5
+ - Add the OS filter support
6
+
1
7
  ##2016-03-29 - Release 1.5.0
2
8
  - Add some Microsft Windows support
3
9
 
data/README.md CHANGED
@@ -277,6 +277,18 @@ require 'rspec-puppet-facts'
277
277
  include RspecPuppetFacts
278
278
  ```
279
279
 
280
+ Run the tests:
281
+
282
+ ```bash
283
+ rake spec
284
+ ```
285
+
286
+ Run the tests only on some of the facts sets:
287
+
288
+ ```bash
289
+ SPEC_FACTS_OS='ubuntu-14' rake spec
290
+ ```
291
+
280
292
  Finaly, Add some `facter` version to test in your .travis.yml
281
293
 
282
294
  ```yaml
data/Rakefile CHANGED
@@ -1,5 +1,7 @@
1
1
  begin
2
2
  require 'rspec/core/rake_task'
3
- RSpec::Core::RakeTask.new(:spec)
3
+ require 'yard'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ YARD::Rake::YardocTask.new
4
6
  rescue LoadError
5
7
  end
@@ -4,10 +4,30 @@ require 'facterdb'
4
4
  require 'json'
5
5
  require 'mcollective'
6
6
 
7
+ # The purpose of this module is to simplify the Puppet
8
+ # module's RSpec tests by looping through all supported
9
+ # OS'es and their facts data which is received from the FacterDB.
7
10
  module RspecPuppetFacts
8
-
9
- def on_supported_os( opts = {} )
11
+ # Use the provided options or the data from the metadata.json file
12
+ # to find a set of matching facts in the FacterDB.
13
+ # OS names and facts can be used in the Puppet RSpec tests
14
+ # to run the examples against all supported facts combinations.
15
+ #
16
+ # The list of received OS facts can also be filtered by the SPEC_FACTS_OS
17
+ # environment variable. For example, if the variable is set to "debian"
18
+ # only the OS names which start with "debian" will be returned. It allows a
19
+ # user to quickly run the tests only on a single facts set without any
20
+ # file modifications.
21
+ #
22
+ # @return [Hash <String => Hash>]
23
+ # @param [Hash] opts
24
+ # @option opts [String,Array<String>] :hardwaremodels The OS architecture names, i.e. x86_64
25
+ # @option opts [Array<Hash>] :supported_os If this options is provided the data
26
+ # will be used instead of the "operatingsystem_support" section if the metadata file
27
+ # even if the file is missing.
28
+ def on_supported_os(opts = {})
10
29
  opts[:hardwaremodels] ||= ['x86_64']
30
+ opts[:hardwaremodels] = [opts[:hardwaremodels]] unless opts[:hardwaremodels].is_a? Array
11
31
  opts[:supported_os] ||= RspecPuppetFacts.meta_supported_os
12
32
 
13
33
  filter = []
@@ -25,57 +45,116 @@ module RspecPuppetFacts
25
45
  end
26
46
 
27
47
  filter << {
28
- :facterversion => "/^#{Facter.version[0..2]}/",
29
- :operatingsystem => os_sup['operatingsystem'],
30
- :operatingsystemrelease => "/^#{operatingsystemmajrelease.split(" ")[0]}/",
31
- :hardwaremodel => hardwaremodel,
48
+ :facterversion => "/^#{Facter.version[0..2]}/",
49
+ :operatingsystem => os_sup['operatingsystem'],
50
+ :operatingsystemrelease => "/^#{operatingsystemmajrelease.split(' ')[0]}/",
51
+ :hardwaremodel => hardwaremodel,
32
52
  }
33
53
  end
34
54
  end
35
55
  else
36
56
  opts[:hardwaremodels].each do |hardwaremodel|
37
57
  filter << {
38
- :facterversion => "/^#{Facter.version[0..2]}/",
39
- :operatingsystem => os_sup['operatingsystem'],
40
- :hardwaremodel => hardwaremodel,
58
+ :facterversion => "/^#{Facter.version[0..2]}/",
59
+ :operatingsystem => os_sup['operatingsystem'],
60
+ :hardwaremodel => hardwaremodel,
41
61
  }
42
62
  end
43
63
  end
44
64
  end
45
65
 
46
- h = {}
47
- FacterDB::get_facts(filter).map do |facts|
48
- facts.merge!({
66
+ received_facts = FacterDB::get_facts(filter)
67
+ unless received_facts.any?
68
+ RspecPuppetFacts.warning "No facts were found in the FacterDB for: #{filter.inspect}"
69
+ return {}
70
+ end
71
+
72
+ os_facts_hash = {}
73
+ received_facts.map do |facts|
74
+ os = "#{facts[:operatingsystem].downcase}-#{facts[:operatingsystemrelease].split('.')[0]}-#{facts[:hardwaremodel]}"
75
+ next unless os.start_with? RspecPuppetFacts.spec_facts_os_filter if RspecPuppetFacts.spec_facts_os_filter
76
+ facts.merge! RspecPuppetFacts.common_facts
77
+ os_facts_hash[os] = facts
78
+ end
79
+ os_facts_hash
80
+ end
81
+
82
+ # If provided this filter can be used to limit the set
83
+ # of retrieved facts only to the matched OS names.
84
+ # The value is being taken from the SPEC_FACTS_OS environment
85
+ # variable and
86
+ # @return [nil,String]
87
+ # @api private
88
+ def self.spec_facts_os_filter
89
+ ENV['SPEC_FACTS_OS']
90
+ end
91
+
92
+ # These facts are common for all OS'es and will be
93
+ # added to the facts retrieved from the FacterDB
94
+ # @api private
95
+ # @return [Hash <Symbol => String>]
96
+ def self.common_facts
97
+ return @common_facts if @common_facts
98
+ @common_facts = {
49
99
  :mco_version => MCollective::VERSION,
50
100
  :puppetversion => Puppet.version,
51
- :rubysitedir => RbConfig::CONFIG["sitelibdir"],
101
+ :rubysitedir => RbConfig::CONFIG['sitelibdir'],
52
102
  :rubyversion => RUBY_VERSION,
53
- })
54
- facts[:augeasversion] = Augeas.open(nil, nil, Augeas::NO_MODL_AUTOLOAD).get('/augeas/version') if Puppet.features.augeas?
55
- h["#{facts[:operatingsystem].downcase}-#{facts[:operatingsystemrelease].split('.')[0]}-#{facts[:hardwaremodel]}"] = facts
103
+ }
104
+ if Puppet.features.augeas?
105
+ @common_facts[:augeasversion] = Augeas.open(nil, nil, Augeas::NO_MODL_AUTOLOAD).get('/augeas/version')
56
106
  end
57
- h
107
+ @common_facts
58
108
  end
59
109
 
110
+ # Get the "operatingsystem_support" structure from
111
+ # the parsed metadata.json file
112
+ # @raise [StandardError] if there is no "operatingsystem_support"
113
+ # in the metadata
114
+ # @return [Array<Hash>]
60
115
  # @api private
61
116
  def self.meta_supported_os
62
- @meta_supported_os ||= get_meta_supported_os
63
- end
64
-
65
- # @api private
66
- def self.get_meta_supported_os
67
- metadata = get_metadata
68
- if metadata['operatingsystem_support'].nil?
69
- fail StandardError, "Unknown operatingsystem support"
117
+ unless metadata['operatingsystem_support'].is_a? Array
118
+ fail StandardError, 'Unknown operatingsystem support in the metadata file!'
70
119
  end
71
120
  metadata['operatingsystem_support']
72
121
  end
73
122
 
123
+ # Read the metadata file and parse
124
+ # its JSON content.
125
+ # @raise [StandardError] if the metadata file is missing
126
+ # @return [Hash]
74
127
  # @api private
75
- def self.get_metadata
76
- if ! File.file?('metadata.json')
128
+ def self.metadata
129
+ return @metadata if @metadata
130
+ unless File.file? metadata_file
77
131
  fail StandardError, "Can't find metadata.json... dunno why"
78
132
  end
79
- JSON.parse(File.read('metadata.json'))
133
+ content = File.read metadata_file
134
+ @metadata = JSON.parse content
135
+ end
136
+
137
+ # This file contains the Puppet module's metadata
138
+ # @return [String]
139
+ # @api private
140
+ def self.metadata_file
141
+ 'metadata.json'
80
142
  end
143
+
144
+ # Print a warning message to the console
145
+ # @param message [String]
146
+ # @api private
147
+ def self.warning(message)
148
+ STDERR.puts message
149
+ end
150
+
151
+ # Reset the memoization
152
+ # to make the saved structures
153
+ # be generated again
154
+ # @api private
155
+ def self.reset
156
+ @common_facts = nil
157
+ @metadata = nil
158
+ end
159
+
81
160
  end
@@ -1,5 +1,7 @@
1
1
  module RspecPuppetFacts
2
+ # This module contains the current version constant
2
3
  module Version
3
- STRING = '1.5.0'
4
+ # The current version of this gem
5
+ STRING = '1.6.0'
4
6
  end
5
7
  end
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
  s.add_development_dependency 'coveralls'
25
25
  s.add_development_dependency 'rake'
26
26
  s.add_development_dependency 'rspec'
27
+ s.add_development_dependency 'yard'
27
28
  s.add_runtime_dependency 'puppet'
28
29
  s.add_runtime_dependency 'json'
29
30
  s.add_runtime_dependency 'facter'
@@ -1,58 +1,94 @@
1
1
  require 'spec_helper'
2
+ require 'json'
2
3
 
3
- describe 'RspecPuppetFacts' do
4
+ describe RspecPuppetFacts do
5
+ let(:metadata_file) do
6
+ 'spec/fixtures/metadata.json'
7
+ end
4
8
 
5
9
  describe '#on_supported_os' do
6
10
 
7
- context 'Without parameter' do
8
- subject { on_supported_os() }
11
+ context 'Without specifying supported_os' do
12
+ subject { on_supported_os }
9
13
 
10
14
  context 'Without metadata.json' do
15
+ before(:each) do
16
+ expect(File).to receive(:file?).with('metadata.json').and_return false
17
+ end
18
+
11
19
  it { expect { subject }.to raise_error(StandardError, /Can't find metadata.json/) }
12
20
  end
13
21
 
14
22
  context 'With a metadata.json' do
15
-
16
- context 'With a broken metadata.json' do
17
-
18
- context 'With missing operatingsystem_support section' do
19
- before :each do
20
- fixture = File.read('spec/fixtures/metadata.json_with_missing_operatingsystem_support')
21
- expect(File).to receive(:file?).with('metadata.json').and_return true
22
- expect(File).to receive(:read).with('metadata.json').and_return fixture
23
- end
24
-
25
- it { expect { subject }.to raise_error(StandardError, /Unknown operatingsystem support/) }
26
- end
23
+ it 'can load the metadata file' do
24
+ allow(RspecPuppetFacts).to receive(:metadata_file).and_return(metadata_file)
25
+ RspecPuppetFacts.reset
26
+ expect(RspecPuppetFacts.metadata).to be_a Hash
27
+ expect(RspecPuppetFacts.metadata['name']).to eq 'mcanevet-mymodule'
27
28
  end
28
29
 
29
30
  context 'With a valid metadata.json' do
31
+ let(:metadata) do
32
+ fixture = File.read(metadata_file)
33
+ JSON.parse fixture
34
+ end
35
+
30
36
  before :each do
31
- fixture = File.read('spec/fixtures/metadata.json')
32
- expect(File).to receive(:file?).with('metadata.json').and_return true
33
- expect(File).to receive(:read).and_call_original
34
- expect(File).to receive(:read).with('metadata.json').and_return fixture
37
+ allow(RspecPuppetFacts).to receive(:metadata).and_return(metadata)
35
38
  end
36
39
 
37
40
  it 'should return a hash' do
38
- pending 'FIXME'
39
- expect( on_supported_os().class ).to eq Hash
41
+ is_expected.to be_a Hash
40
42
  end
43
+
41
44
  it 'should have 5 elements' do
42
- pending 'FIXME'
43
45
  expect(subject.size).to eq 5
44
46
  end
47
+
45
48
  it 'should return supported OS' do
46
- pending 'FIXME'
47
- expect(subject.keys.sort).to eq [
48
- 'debian-6-x86_64',
49
- 'debian-7-x86_64',
50
- 'redhat-5-x86_64',
51
- 'redhat-6-x86_64',
52
- 'redhat-7-x86_64',
53
- ]
49
+ expect(subject.keys.sort).to eq %w(
50
+ debian-6-x86_64
51
+ debian-7-x86_64
52
+ redhat-5-x86_64
53
+ redhat-6-x86_64
54
+ redhat-7-x86_64
55
+ )
56
+ end
57
+
58
+ it 'should be able to filter the received OS facts' do
59
+ allow(RspecPuppetFacts).to receive(:spec_facts_os_filter).and_return('redhat')
60
+ expect(subject.keys.sort).to eq %w(
61
+ redhat-5-x86_64
62
+ redhat-6-x86_64
63
+ redhat-7-x86_64
64
+ )
54
65
  end
55
66
  end
67
+
68
+ context 'With a broken metadata.json' do
69
+ before :each do
70
+ allow(RspecPuppetFacts).to receive(:metadata).and_return(metadata)
71
+ end
72
+
73
+ context 'With a missing operatingsystem_support section' do
74
+ let(:metadata) do
75
+ {}
76
+ end
77
+
78
+ it { expect { subject }.to raise_error(StandardError, /Unknown operatingsystem support/) }
79
+ end
80
+
81
+ context 'With a wrong operatingsystem_support section' do
82
+ let(:metadata) do
83
+ {
84
+ 'operatingsystem_support' => 'Ubuntu',
85
+ }
86
+ end
87
+
88
+ it { expect { subject }.to raise_error(StandardError, /Unknown operatingsystem support/) }
89
+ end
90
+ end
91
+
56
92
  end
57
93
  end
58
94
 
@@ -79,19 +115,30 @@ describe 'RspecPuppetFacts' do
79
115
  }
80
116
  )
81
117
  }
118
+
82
119
  it 'should return a hash' do
83
- expect(subject.class).to eq Hash
120
+ is_expected.to be_a Hash
84
121
  end
122
+
85
123
  it 'should have 4 elements' do
86
124
  expect(subject.size).to eq 4
87
125
  end
126
+
88
127
  it 'should return supported OS' do
89
- expect(subject.keys.sort).to eq [
90
- 'debian-6-x86_64',
91
- 'debian-7-x86_64',
92
- 'redhat-5-x86_64',
93
- 'redhat-6-x86_64',
94
- ]
128
+ expect(subject.keys.sort).to eq %w(
129
+ debian-6-x86_64
130
+ debian-7-x86_64
131
+ redhat-5-x86_64
132
+ redhat-6-x86_64
133
+ )
134
+ end
135
+
136
+ it 'should be able to filter the received OS facts' do
137
+ allow(RspecPuppetFacts).to receive(:spec_facts_os_filter).and_return('redhat')
138
+ expect(subject.keys.sort).to eq %w(
139
+ redhat-5-x86_64
140
+ redhat-6-x86_64
141
+ )
95
142
  end
96
143
  end
97
144
 
@@ -151,33 +198,59 @@ describe 'RspecPuppetFacts' do
151
198
  end
152
199
  end
153
200
 
154
- unless Facter.version.to_f < 2.4
155
- context 'When testing Windows 7' do
156
- subject {
157
- on_supported_os(
201
+ context 'When testing Solaris 11', :if => Facter.version.to_f >= 2.0 do
202
+ subject {
203
+ on_supported_os(
158
204
  {
159
- :supported_os => [
160
- {
161
- "operatingsystem" => "windows",
162
- "operatingsystemrelease" => [
163
- "7",
164
- ],
165
- },
166
- ],
205
+ :supported_os => [
206
+ {
207
+ "operatingsystem" => "Solaris",
208
+ "operatingsystemrelease" => [
209
+ "11",
210
+ ],
211
+ },
212
+ ],
167
213
  }
168
- )
169
- }
170
- it 'should return a hash' do
171
- expect(subject.class).to eq Hash
172
- end
173
- it 'should have 1 elements' do
174
- expect(subject.size).to eq 1
175
- end
176
- it 'should return supported OS' do
177
- expect(subject.keys.sort).to eq [
178
- 'windows-7-x64',
179
- ]
180
- end
214
+ )
215
+ }
216
+ it 'should return a hash' do
217
+ expect(subject.class).to eq Hash
218
+ end
219
+ it 'should have 1 elements' do
220
+ expect(subject.size).to eq 1
221
+ end
222
+ it 'should return supported OS' do
223
+ expect(subject.keys.sort).to eq %w(
224
+ solaris-11-i86pc
225
+ )
226
+ end
227
+ end
228
+
229
+ context 'When testing Windows 7', :if => Facter.version.to_f >= 2.4 do
230
+ subject {
231
+ on_supported_os(
232
+ {
233
+ :supported_os => [
234
+ {
235
+ "operatingsystem" => "windows",
236
+ "operatingsystemrelease" => [
237
+ "7",
238
+ ],
239
+ },
240
+ ],
241
+ }
242
+ )
243
+ }
244
+ it 'should return a hash' do
245
+ expect(subject.class).to eq Hash
246
+ end
247
+ it 'should have 1 elements' do
248
+ expect(subject.size).to eq 1
249
+ end
250
+ it 'should return supported OS' do
251
+ expect(subject.keys.sort).to eq [
252
+ 'windows-7-x64',
253
+ ]
181
254
  end
182
255
  end
183
256
 
@@ -226,8 +299,8 @@ describe 'RspecPuppetFacts' do
226
299
  }
227
300
 
228
301
  it 'should output warning message' do
229
- pending "TODO: Show a warning when missing facts in database"
230
- expect { subject }.to output(/Can't find facts for 'debian-4-x86_64'/).to_stderr
302
+ expect(RspecPuppetFacts).to receive(:warning).with(/No facts were found in the FacterDB/)
303
+ subject
231
304
  end
232
305
  end
233
306
 
@@ -257,11 +330,47 @@ describe 'RspecPuppetFacts' do
257
330
  expect(subject.size).to eq 2
258
331
  end
259
332
  it 'should return supported OS' do
260
- expect(subject.keys.sort).to eq [
261
- 'archlinux-3-x86_64',
262
- 'debian-8-x86_64',
263
- ]
333
+ expect(subject.keys.sort).to eq %w(
334
+ archlinux-3-x86_64
335
+ debian-8-x86_64
336
+ )
264
337
  end
265
338
  end
266
339
  end
340
+
341
+ context '#misc' do
342
+ it 'should have a common facts structure' do
343
+ RspecPuppetFacts.reset
344
+ expect(subject.common_facts).to be_a Hash
345
+ expect(subject.common_facts).not_to be_empty
346
+ end
347
+
348
+ it 'should not add "augeasversion" if Augeas is supported' do
349
+ allow(Puppet.features).to receive(:augeas?).and_return(false)
350
+ RspecPuppetFacts.reset
351
+ expect(subject.common_facts).not_to include :augeasversion
352
+ end
353
+
354
+ it 'should determine the Augeas version if Augeas is supported' do
355
+ module Augeas_stub
356
+ NO_MODL_AUTOLOAD = true
357
+ def self.open(*_args)
358
+ self
359
+ end
360
+ def self.get(*_args)
361
+ 'my_version'
362
+ end
363
+ end
364
+
365
+ allow(Puppet.features).to receive(:augeas?).and_return(true)
366
+ stub_const('Augeas', Augeas_stub)
367
+ RspecPuppetFacts.reset
368
+ expect(subject.common_facts[:augeasversion]).to eq 'my_version'
369
+ end
370
+
371
+ it 'can output a warning message' do
372
+ expect { RspecPuppetFacts.warning('test') }.to output(/test/).to_stderr_from_any_process
373
+ end
374
+ end
375
+
267
376
  end
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.5.0
4
+ version: 1.6.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: 2016-03-29 00:00:00.000000000 Z
11
+ date: 2016-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: puppet
71
85
  requirement: !ruby/object:Gem::Requirement