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.
- checksums.yaml +7 -0
- data/.changelog.yml +20 -0
- data/.github/dependabot.yml +17 -0
- data/.github/workflows/ci.yml +51 -0
- data/.github/workflows/release.yml +20 -0
- data/.github/workflows/release_prep.yml +24 -0
- data/.gitignore +12 -0
- data/.rspec +4 -0
- data/.rubocop.yml +16 -0
- data/.rubocop_todo.yml +94 -0
- data/CHANGELOG.md +274 -0
- data/Gemfile +60 -0
- data/HISTORY.md +490 -0
- data/LICENSE +201 -0
- data/README.md +625 -0
- data/Rakefile +88 -0
- data/ext/puppet_agent_facter_versions.json +74 -0
- data/lib/rspec-puppetlabs-facts/version.rb +9 -0
- data/lib/rspec-puppetlabs-facts.rb +440 -0
- data/rspec-puppetlabs-facts.gemspec +32 -0
- data/scripts/rename-to-puppetlabs.sh +66 -0
- data/spec/acceptance/build_spec.rb +22 -0
- data/spec/acceptance/install_spec.rb +29 -0
- data/spec/acceptance/scripts/facts_check.rb +15 -0
- data/spec/fixtures/metadata.json +41 -0
- data/spec/fixtures/metadata.json_with_missing_operatingsystem_support +25 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/spec_helper_acceptance.rb +54 -0
- data/spec/unit/rspec_puppetlabs_facts_spec.rb +1055 -0
- metadata +197 -0
data/README.md
ADDED
|
@@ -0,0 +1,625 @@
|
|
|
1
|
+
rspec-puppetlabs-facts
|
|
2
|
+
======================
|
|
3
|
+
|
|
4
|
+
[](https://github.com/puppetlabs/rspec-puppetlabs-facts/blob/main/LICENSE)
|
|
5
|
+
[](https://github.com/puppetlabs/rspec-puppetlabs-facts/actions/workflows/ci.yml)
|
|
6
|
+
[](https://github.com/puppetlabs/rspec-puppetlabs-facts/actions/workflows/release.yml)
|
|
7
|
+
[](https://rubygems.org/gems/rspec-puppetlabs-facts)
|
|
8
|
+
[](https://rubygems.org/gems/rspec-puppetlabs-facts)
|
|
9
|
+
|
|
10
|
+
Based on an original idea from [apenney](https://github.com/apenney/puppet_facts/),
|
|
11
|
+
this gem provides a method of running your [rspec-puppet](https://github.com/puppetlabs/rspec-puppet)
|
|
12
|
+
tests against the facts for all your supported operating systems (provided by
|
|
13
|
+
[puppetlabs-facterdb](https://github.com/puppetlabs/puppetlabs-facterdb), built from Puppet Labs facter releases).
|
|
14
|
+
This simplifies unit testing because you don't need to specify the facts yourself.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
If you're using Bundler to manage gems in your module repository, install `rspec-puppetlabs-facts` by adding it to the Gemfile.
|
|
19
|
+
|
|
20
|
+
1. Add the following line to your `Gemfile`:
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
gem 'rspec-puppetlabs-facts', require: false
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
2. Run `bundle install`.
|
|
27
|
+
|
|
28
|
+
If you're not using Bundler, install the `rspec-puppetlabs-facts` manually.
|
|
29
|
+
|
|
30
|
+
1. On the command line, run:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
$ gem install rspec-puppetlabs-facts
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
After the gem is installed (using either method), make the gem available to rspec by adding the following lines in your `spec/spec_helper.rb` file. Place the lines after `require 'rspec-puppet'` and before the `RSpec.configure` block, if one exists.
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
require 'rspec-puppetlabs-facts'
|
|
40
|
+
include RspecPuppetlabsFacts
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Differences from rspec-puppet-facts
|
|
44
|
+
|
|
45
|
+
This gem is a fork of upstream [rspec-puppet-facts](https://github.com/voxpupuli/rspec-puppet-facts) 6.3.1 for use with Puppet Labs facter. From 7.0.0 it uses `facter` and `puppetlabs-facterdb` in place of `openfact` and `facterdb`, and renames the gem, `require` path and module (a breaking change, see below).
|
|
46
|
+
|
|
47
|
+
| | rspec-puppet-facts | rspec-puppetlabs-facts |
|
|
48
|
+
|---|---|---|
|
|
49
|
+
| Gemfile gem name | `rspec-puppet-facts` | `rspec-puppetlabs-facts` |
|
|
50
|
+
| `require` path | `rspec-puppet-facts` | `rspec-puppetlabs-facts` |
|
|
51
|
+
| `include` module | `RspecPuppetFacts` | `RspecPuppetlabsFacts` |
|
|
52
|
+
| Facts dependencies | `openfact`, `facterdb` | `facter` (>= 4, < 5), `puppetlabs-facterdb` |
|
|
53
|
+
| Environment variables | `FACTERDB_*` | `PUPPETLABS_FACTERDB_*` (`FACTERDB_*` has no effect) |
|
|
54
|
+
|
|
55
|
+
`on_supported_os`, `add_custom_fact`, `SPEC_FACTS_OS` and `SPEC_FACTS_STRICT` are unchanged.
|
|
56
|
+
|
|
57
|
+
Facter 4.23.0 is the data ceiling: it is the newest facter version with a fact data set in puppetlabs-facterdb 0.1.x.
|
|
58
|
+
|
|
59
|
+
Both gems can be installed side by side, but require only one of them per project and never both from the same file, because `facter` and `openfact` both define `::Facter`.
|
|
60
|
+
|
|
61
|
+
## Specifying the supported operating systems
|
|
62
|
+
|
|
63
|
+
To determine which facts to run your tests against, `rspec-puppetlabs-facts` checks your module's `metadata.json` to find out what operating systems your module supports. The `metadata.json` file is located in the root of your module. To learn more about this file, see Puppet's [metadata](https://help.puppet.com/core/current/Content/PuppetCore/modules_metadata.htm) documentation.
|
|
64
|
+
|
|
65
|
+
By default, `rspec-puppetlabs-facts` provides the facts only for `x86_64` architecture. However, you can override this default and the supported operating system list by passing a hash to `on_supported_os` in your tests. This hash must contain either or both of the following keys:
|
|
66
|
+
|
|
67
|
+
* `:hardwaremodels` - An array of hardware architecture names, as strings.
|
|
68
|
+
* `:supported_os` - An array of hashes representing the operating systems.
|
|
69
|
+
**Note: the keys of these hashes must be strings**
|
|
70
|
+
* `'operatingsystem'` - The name of the operating system, as a string.
|
|
71
|
+
* `'operatingsystemrelease'` - An array of version numbers, as strings.
|
|
72
|
+
|
|
73
|
+
This is particularly useful if your module is split into operating system subclasses. For example, if you had a class called `myclass::debian` that you wanted to test against Debian 12 and Debian 11 on both `x86_64` _and_ `i386` architectures, you could write the following test:
|
|
74
|
+
|
|
75
|
+
```ruby
|
|
76
|
+
require 'spec_helper'
|
|
77
|
+
|
|
78
|
+
describe 'myclass::raspbian' do
|
|
79
|
+
test_on = {
|
|
80
|
+
hardwaremodels: ['x86_64', 'i386'],
|
|
81
|
+
supported_os: [
|
|
82
|
+
{
|
|
83
|
+
'operatingsystem' => 'Debian',
|
|
84
|
+
'operatingsystemrelease' => ['12', '11'],
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
}
|
|
88
|
+
on_supported_os(test_on).each do |os, os_facts|
|
|
89
|
+
let(:facts) { os_facts }
|
|
90
|
+
it { is_expected.to compile.with_all_deps }
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Specifying a default Facter version
|
|
96
|
+
|
|
97
|
+
By default, `on_supported_os` will return the facts for the version of Facter that it has loaded. It will check for the loaded Puppet version and maps that to the Facter version that Perforce released in the matching AIO.
|
|
98
|
+
The mapping is stored in `ext/puppet_agent_facter_versions.json` (check the [maintenance](#maintenance) section for details) and computated in the `facter_version_for_puppet_version` method.
|
|
99
|
+
This behaviour can be overridden by setting the `default_facter_version` RSpec setting in your `spec/spec_helper.rb` file.
|
|
100
|
+
|
|
101
|
+
```ruby
|
|
102
|
+
RSpec.configure do |c|
|
|
103
|
+
c.default_facter_version = '4.23.0'
|
|
104
|
+
end
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Symbolized vs stringified facts
|
|
108
|
+
|
|
109
|
+
For a long long time, the first level of keys in a factsets were symbols.
|
|
110
|
+
This was fine before there were structured facts, but structured facts ended up as nested hashes that always had strings.
|
|
111
|
+
This doesn't make sense and easy to get wrong.
|
|
112
|
+
The original data contains only strings so conversion actually costs performance.
|
|
113
|
+
|
|
114
|
+
The option to switch between symbolized vs stringified facts was introduced in [25634f4481f20f2fc7444e867928ca607234e33e](https://github.com/voxpupuli/rspec-puppet-facts/commit/25634f4481f20f2fc7444e867928ca607234e33e) (Release [1.9.5](https://github.com/voxpupuli/rspec-puppet-facts/blob/master/CHANGELOG.md#2019-07-29---release-195)), but version 5.0.0 has resolved various implementation bugs.
|
|
115
|
+
|
|
116
|
+
This can be configured like this:
|
|
117
|
+
|
|
118
|
+
```ruby
|
|
119
|
+
RSpec.configure do |c|
|
|
120
|
+
c.facterdb_string_keys = false
|
|
121
|
+
end
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
In the 5.x release series of rspec-puppet-facts the default `false` is deprecated.
|
|
125
|
+
With the 6.0.0 release we will flip it to `true` (https://github.com/voxpupuli/rspec-puppet-facts/pull/189).
|
|
126
|
+
|
|
127
|
+
You may have the following rspec-puppet test using deprecated symbolized facts:
|
|
128
|
+
|
|
129
|
+
```ruby
|
|
130
|
+
on_supported_os.each do |os, os_facts|
|
|
131
|
+
case os_facts[:os]['name']
|
|
132
|
+
when 'Archlinux'
|
|
133
|
+
context 'on Archlinux' do
|
|
134
|
+
it { is_expected.to contain_package('borg') }
|
|
135
|
+
end
|
|
136
|
+
when 'Ubuntu'
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
With the string facts, the same test looks like:
|
|
142
|
+
|
|
143
|
+
```ruby
|
|
144
|
+
on_supported_os.each do |os, os_facts|
|
|
145
|
+
case os_facts['os']['name']
|
|
146
|
+
when 'Archlinux'
|
|
147
|
+
context 'on Archlinux' do
|
|
148
|
+
it { is_expected.to contain_package('borg') }
|
|
149
|
+
end
|
|
150
|
+
when 'Ubuntu'
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Usage
|
|
156
|
+
|
|
157
|
+
Use the `on_supported_os` iterator to loop through all of your module's supported operating systems. This allows you to simplify your tests and remove a lot of duplicate code.
|
|
158
|
+
|
|
159
|
+
Each iteration of `on_supported_os` provides two variables to your tests. (In the code examples below, these variables are specified by the values between the pipe (`|`) characters.)
|
|
160
|
+
|
|
161
|
+
* The first value is the name of the fact set. This is made from the values of the operatingsystem, operatingsystemmajrelease, and architecture facts separated by dashes (for example, 'debian-7-x86_64').
|
|
162
|
+
* The second value is the facts for that combination of operating system, release, and architecture.
|
|
163
|
+
|
|
164
|
+
For example, previously, you might have written a test that specified Debian 7 and Red Hat 6 as the supported modules:
|
|
165
|
+
|
|
166
|
+
```ruby
|
|
167
|
+
require 'spec_helper'
|
|
168
|
+
|
|
169
|
+
describe 'myclass' do
|
|
170
|
+
|
|
171
|
+
context "on debian-7-x86_64" do
|
|
172
|
+
let(:facts) do
|
|
173
|
+
{
|
|
174
|
+
:osfamily => 'Debian',
|
|
175
|
+
:operatingsystem => 'Debian',
|
|
176
|
+
:operatingsystemmajrelease => '7',
|
|
177
|
+
...
|
|
178
|
+
}
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
it { is_expected.to compile.with_all_deps }
|
|
182
|
+
...
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
context "on redhat-6-x86_64" do
|
|
186
|
+
let(:facts) do
|
|
187
|
+
{
|
|
188
|
+
:osfamily => 'RedHat',
|
|
189
|
+
:operatingsystem => 'RedHat',
|
|
190
|
+
:operatingsystemmajrelease => '6',
|
|
191
|
+
...
|
|
192
|
+
}
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
it { is_expected.to compile.with_all_deps }
|
|
196
|
+
...
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
...
|
|
200
|
+
end
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
With `on_supported_os` iteration, you can rewrite this test to loop over each of the supported operating systems without explicitly specifying them:
|
|
204
|
+
|
|
205
|
+
```ruby
|
|
206
|
+
require 'spec_helper'
|
|
207
|
+
|
|
208
|
+
describe 'myclass' do
|
|
209
|
+
|
|
210
|
+
on_supported_os.each do |os, os_facts|
|
|
211
|
+
context "on #{os}" do
|
|
212
|
+
let(:facts) do
|
|
213
|
+
os_facts
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
it { is_expected.to compile.with_all_deps }
|
|
217
|
+
...
|
|
218
|
+
|
|
219
|
+
# If you need any to specify any operating system specific tests
|
|
220
|
+
case os_facts[:osfamily]
|
|
221
|
+
when 'Debian'
|
|
222
|
+
...
|
|
223
|
+
else
|
|
224
|
+
...
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
When using roles and profiles to manage a heterogeneous IT estate, you can test a profile that supports several OSes with many `let(:facts)` as long as each is in its own context:
|
|
232
|
+
```ruby
|
|
233
|
+
require 'spec_helper'
|
|
234
|
+
|
|
235
|
+
describe 'profiles::packagerepos' do
|
|
236
|
+
context 'Raspbian tests' do # We manage hundreds of desk-mounted Pis
|
|
237
|
+
raspbian = {
|
|
238
|
+
hardwaremodels: ['armv7l'],
|
|
239
|
+
supported_os: [
|
|
240
|
+
{
|
|
241
|
+
'operatingsystem' => 'Debian',
|
|
242
|
+
'operatingsystemrelease' => ['10', '9', '8'],
|
|
243
|
+
},
|
|
244
|
+
],
|
|
245
|
+
}
|
|
246
|
+
on_supported_os(raspbian).each do |os, os_facts|
|
|
247
|
+
let(:facts) do
|
|
248
|
+
os_facts
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
context "#{os} with defaults" do
|
|
252
|
+
it { is_expected.to compile }
|
|
253
|
+
# more tests ...
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
context 'Ubuntu tests' do # And also a fleet of Ubuntu desktops
|
|
258
|
+
ubuntu = {
|
|
259
|
+
supported_os: [
|
|
260
|
+
{
|
|
261
|
+
'operatingsystem' => 'Ubuntu',
|
|
262
|
+
'operatingsystemrelease' => ['18.04', '16.04'],
|
|
263
|
+
},
|
|
264
|
+
],
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
on_supported_os(ubuntu).each do |os, os_facts|
|
|
268
|
+
let(:facts) do
|
|
269
|
+
os_facts
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
context "#{os} with defaults" do
|
|
273
|
+
it { is_expected.to compile }
|
|
274
|
+
# more tests ...
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### Testing a type or provider
|
|
282
|
+
|
|
283
|
+
Use `on_supported_os` in the same way for your type and provider unit tests.
|
|
284
|
+
|
|
285
|
+
**Specifying each operating system**:
|
|
286
|
+
|
|
287
|
+
```ruby
|
|
288
|
+
require 'spec_helper'
|
|
289
|
+
|
|
290
|
+
describe 'mytype' do
|
|
291
|
+
|
|
292
|
+
context "on debian-7-x86_64" do
|
|
293
|
+
let(:facts) do
|
|
294
|
+
{
|
|
295
|
+
:osfamily => 'Debian',
|
|
296
|
+
:operatingsystem => 'Debian',
|
|
297
|
+
:operatingsystemmajrelease => '7',
|
|
298
|
+
}
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
it { should be_valid_type }
|
|
302
|
+
...
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
context "on redhat-7-x86_64" do
|
|
306
|
+
let(:facts) do
|
|
307
|
+
{
|
|
308
|
+
:osfamily => 'RedHat',
|
|
309
|
+
:operatingsystem => 'RedHat',
|
|
310
|
+
:operatingsystemmajrelease => '7',
|
|
311
|
+
}
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
it { should be_valid_type }
|
|
315
|
+
...
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
**Looping with `on_supported_os` iterator**:
|
|
322
|
+
|
|
323
|
+
```ruby
|
|
324
|
+
require 'spec_helper'
|
|
325
|
+
|
|
326
|
+
describe 'mytype' do
|
|
327
|
+
|
|
328
|
+
on_supported_os.each do |os, os_facts|
|
|
329
|
+
context "on #{os}" do
|
|
330
|
+
let(:facts) do
|
|
331
|
+
os_facts
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
it { should be_valid_type }
|
|
335
|
+
...
|
|
336
|
+
|
|
337
|
+
# If you need to specify any operating system specific tests
|
|
338
|
+
case os_facts[:osfamily]
|
|
339
|
+
when 'Debian'
|
|
340
|
+
...
|
|
341
|
+
else
|
|
342
|
+
...
|
|
343
|
+
end
|
|
344
|
+
...
|
|
345
|
+
end
|
|
346
|
+
end
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
### Testing a function
|
|
352
|
+
|
|
353
|
+
As with testing manifests, types, or providers, `on_supported_os` iteration simplifies your function unit tests.
|
|
354
|
+
|
|
355
|
+
**Specifying each operating system**:
|
|
356
|
+
|
|
357
|
+
```ruby
|
|
358
|
+
require 'spec_helper'
|
|
359
|
+
|
|
360
|
+
describe 'myfunction' do
|
|
361
|
+
context "on debian-7-x86_64" do
|
|
362
|
+
let(:facts) do
|
|
363
|
+
{
|
|
364
|
+
:osfamily => 'Debian',
|
|
365
|
+
:operatingsystem => 'Debian',
|
|
366
|
+
...
|
|
367
|
+
}
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
it { should run.with_params('something').and_return('a value') }
|
|
371
|
+
...
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
context "on redhat-7-x86_64" do
|
|
375
|
+
let(:facts) do
|
|
376
|
+
{
|
|
377
|
+
:osfamily => 'RedHat',
|
|
378
|
+
:operatingsystem => 'RedHat',
|
|
379
|
+
...
|
|
380
|
+
}
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
it { should run.with_params('something').and_return('a value') }
|
|
384
|
+
...
|
|
385
|
+
end
|
|
386
|
+
end
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
**Looping with `on_supported_os` iterator**:
|
|
390
|
+
|
|
391
|
+
```ruby
|
|
392
|
+
require 'spec_helper'
|
|
393
|
+
|
|
394
|
+
describe 'myfunction' do
|
|
395
|
+
|
|
396
|
+
on_supported_os.each do |os, os_facts|
|
|
397
|
+
context "on #{os}" do
|
|
398
|
+
let(:facts) do
|
|
399
|
+
os_facts
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
it { should run.with_params('something').and_return('a value') }
|
|
403
|
+
...
|
|
404
|
+
end
|
|
405
|
+
end
|
|
406
|
+
end
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
### Adding custom fact values
|
|
410
|
+
|
|
411
|
+
By adding custom fact values, you can:
|
|
412
|
+
|
|
413
|
+
* Override fact values
|
|
414
|
+
* Include additional facts in your tests.
|
|
415
|
+
* Add global custom facts for all of your unit tests
|
|
416
|
+
* Add custom facts to only certain operating systems
|
|
417
|
+
* Add custom facts to all operating systems _except_ specific operating systems
|
|
418
|
+
* Create dynamic values for custom facts by setting a lambda as the value.
|
|
419
|
+
|
|
420
|
+
#### Override and add facts
|
|
421
|
+
|
|
422
|
+
To override fact values and include additional facts in your tests, merge values with the facts hash provided by each iteration of `on_supported_os`.
|
|
423
|
+
|
|
424
|
+
```ruby
|
|
425
|
+
require 'spec_helper'
|
|
426
|
+
|
|
427
|
+
describe 'myclass' do
|
|
428
|
+
on_supported_os.each do |os, os_facts|
|
|
429
|
+
context "on #{os}" do
|
|
430
|
+
|
|
431
|
+
# Add the 'foo' fact with the value 'bar' to the tests
|
|
432
|
+
let(:facts) do
|
|
433
|
+
os_facts.merge({
|
|
434
|
+
:foo => 'bar',
|
|
435
|
+
})
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
it { is_expected.to compile.with_all_deps }
|
|
439
|
+
...
|
|
440
|
+
end
|
|
441
|
+
end
|
|
442
|
+
end
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
#### Set global custom facts
|
|
446
|
+
|
|
447
|
+
Set global custom fact values in your `spec/spec_helper.rb` file so that they are automatically available to all of your unit tests using `on_supported_os`.
|
|
448
|
+
|
|
449
|
+
Pass the fact name and value to the `add_custom_fact` function:
|
|
450
|
+
|
|
451
|
+
```ruby
|
|
452
|
+
require 'rspec-puppet'
|
|
453
|
+
require 'rspec-puppetlabs-facts'
|
|
454
|
+
include RspecPuppetlabsFacts
|
|
455
|
+
|
|
456
|
+
# Add the 'concat_basedir' fact to all tests
|
|
457
|
+
add_custom_fact :concat_basedir, '/doesnotexist'
|
|
458
|
+
|
|
459
|
+
RSpec.configure do |config|
|
|
460
|
+
# normal rspec-puppet configuration
|
|
461
|
+
...
|
|
462
|
+
end
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
#### Confine custom facts
|
|
466
|
+
|
|
467
|
+
To add custom facts for only certain operating systems, set `confine` with the operating system as a string value:
|
|
468
|
+
|
|
469
|
+
```ruby
|
|
470
|
+
add_custom_fact :root_home, '/root', :confine => 'redhat-7-x86_64'
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
To add custom facts for all operating systems _except_ specific ones, set `exclude` with the operating system as a string value:
|
|
474
|
+
|
|
475
|
+
```ruby
|
|
476
|
+
add_custom_fact :root_home, '/root', :exclude => 'redhat-7-x86_64'
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
#### Create dynamic facts
|
|
480
|
+
|
|
481
|
+
In addition to the static fact values shown in the previous examples, you can create dynamic values.
|
|
482
|
+
|
|
483
|
+
To do this, pass a lambda as the value for the custom fact. The lambda is passed the same values for operating system name and fact values that your tests are provided by `on_supported_os`.
|
|
484
|
+
|
|
485
|
+
```ruby
|
|
486
|
+
add_custom_fact :root_home, lambda { |os,facts| "/tmp/#{facts['hostname']}" }
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
#### Merge into existing facts
|
|
490
|
+
|
|
491
|
+
You can also supply an optional input `:merge_facts` to the `add_custom_fact` method.
|
|
492
|
+
|
|
493
|
+
This allows you to merge facts values into a fact, if the fact is already present in the facts hash as oppose to overwriting the fact value.
|
|
494
|
+
|
|
495
|
+
```ruby
|
|
496
|
+
add_custom_fact :identity, { 'user' => 'test_user' }, merge_facts: true
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
Will result in a hash of the identity fact like the below:
|
|
500
|
+
|
|
501
|
+
```ruby
|
|
502
|
+
{
|
|
503
|
+
"gid"=>0,
|
|
504
|
+
"group"=>"root",
|
|
505
|
+
"privileged"=>true,
|
|
506
|
+
"uid"=>0,
|
|
507
|
+
"user"=>"test_user"
|
|
508
|
+
}
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
### Supplying Custom External Facts through FacterDB
|
|
512
|
+
rspec-puppetlabs-facts uses a gem called puppetlabs-facterdb that contains many fact sets of various combinations that are pre generated. rspec-puppetlabs-facts queries
|
|
513
|
+
puppetlabs-facterdb to pull out a specific fact set to use when testing. The upstream `FACTERDB_*` environment variables have no effect; use the `PUPPETLABS_FACTERDB_*` variables below.
|
|
514
|
+
|
|
515
|
+
The default facts are great for many things but there will be times when you need to have custom
|
|
516
|
+
fact sets that only make sense in your environment or might contain sensitive information.
|
|
517
|
+
|
|
518
|
+
To supply external facts to puppetlabs-facterdb just set the `PUPPETLABS_FACTERDB_SEARCH_EXTERNAL_DIR` environment variable with one or more
|
|
519
|
+
paths to your facts.
|
|
520
|
+
|
|
521
|
+
When separating paths please use the default path separator character supported by your OS.
|
|
522
|
+
* Unix/Linux/OSX = `:`
|
|
523
|
+
* Windows = `;`
|
|
524
|
+
|
|
525
|
+
This means you will need to supply your own fact sets in addition to the ones contained in facterdb.
|
|
526
|
+
So each fact set you create must meet the following requirements:
|
|
527
|
+
|
|
528
|
+
1. A JSON serialized file containing a single Hash of all the facts.
|
|
529
|
+
2. The facts file must end in `.facts`
|
|
530
|
+
3. Must be placed inside some directory. You can organize this directory however you like.
|
|
531
|
+
|
|
532
|
+
[Example file](https://github.com/camptocamp/facterdb/blob/master/facts/3.5/oraclelinux-5-i386.facts)
|
|
533
|
+
|
|
534
|
+
Facterdb is smart enough the search your supplied directories for files ending with '.facts'. You can even supply
|
|
535
|
+
multiple directories.
|
|
536
|
+
|
|
537
|
+
Example:
|
|
538
|
+
|
|
539
|
+
`PUPPETLABS_FACTERDB_SEARCH_EXTERNAL_DIR="/var/opt/lib/custom_facts"`
|
|
540
|
+
|
|
541
|
+
or
|
|
542
|
+
|
|
543
|
+
`PUPPETLABS_FACTERDB_SEARCH_EXTERNAL_DIR="/var/opt/lib/custom_facts:/tmp/custom_facts:/home/user1/custom_facts"`
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
You can create these files via many methods.
|
|
547
|
+
|
|
548
|
+
* `puppet facts | jq '.values' > /tmp/custom_facts/datacenter_a/2.4/os_x.facts` # must have jq command
|
|
549
|
+
* Via puppetdb queries
|
|
550
|
+
* hand crafted
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
Additionally you can skip the default FacterDB facts completely by setting the environment variable `PUPPETLABS_FACTERDB_SKIP_DEFAULTDB`.
|
|
554
|
+
This will instruct puppetlabs-facterdb to not look at its built-in facts which can be useful should you need to completely replace which facts are used.
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
Setting the variable `PUPPETLABS_FACTERDB_SKIP_DEFAULTDB` to anything will disable the internal facts used by puppetlabs-facterdb. You would most likely use this in combination
|
|
558
|
+
with the `PUPPETLABS_FACTERDB_SEARCH_EXTERNAL_DIR` environment variable.
|
|
559
|
+
|
|
560
|
+
Example:
|
|
561
|
+
|
|
562
|
+
```
|
|
563
|
+
PUPPETLABS_FACTERDB_SEARCH_EXTERNAL_DIR="/var/opt/lib/custom_facts:/tmp/custom_facts:/home/user1/custom_facts"
|
|
564
|
+
PUPPETLABS_FACTERDB_SKIP_DEFAULTDB='yes'
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
We recommend placing custom external facts under spec/fixtures/facts directory.
|
|
568
|
+
|
|
569
|
+
Additionally, if you plan on using these custom facts everytime you should set the following in your spec helper.
|
|
570
|
+
|
|
571
|
+
```ruby
|
|
572
|
+
module_spec_dir = File.dirname(__FILE__)
|
|
573
|
+
custom_facts = File.join(module_spec_dir, 'fixtures', 'facts')
|
|
574
|
+
ENV['PUPPETLABS_FACTERDB_SEARCH_EXTERNAL_DIR'] = custom_facts
|
|
575
|
+
```
|
|
576
|
+
## Running your tests
|
|
577
|
+
|
|
578
|
+
For most cases, there is no change to how you run your tests. Running `rake spec` will run all the tests against the facts for all the supported operating systems. If you are developing a module using the [Puppet Development Kit](https://puppet.com/docs/pdk/3.x/pdk_install.html), `pdk test unit` will run all your tests against the supported operating systems listed in `metadata.json`.
|
|
579
|
+
|
|
580
|
+
If you want to run the tests against the facts for specific operating systems, you can provide a filter in the `SPEC_FACTS_OS` environment variable and only the supported operating systems whose name starts with the specified filter will be used.
|
|
581
|
+
|
|
582
|
+
```bash
|
|
583
|
+
SPEC_FACTS_OS='ubuntu-14' rake spec
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
When no facts are available for the specific facter/operating system combination, the library will fall back to facts from earlier versions of the requested operating system, to allow testing to continue when new versions of facter are released. Set `SPEC_FACTS_STRICT=yes` to instead trigger a failure.
|
|
587
|
+
|
|
588
|
+
## Maintenance
|
|
589
|
+
|
|
590
|
+
This gem uses information about puppet AIO component versions to build/test.
|
|
591
|
+
They are stored at `ext/puppet_agent_facter_versions.json`.
|
|
592
|
+
|
|
593
|
+
`ext/puppet_agent_facter_versions.json` lists the Puppet Labs puppet-agent releases and the facter version each ships.
|
|
594
|
+
It is maintained by hand from the [Puppet Core platform lifecycle page](https://help.puppet.com/core/current/Content/PuppetCore/platform_lifecycle.htm); the `puppet_versions:update` and `puppet_versions:test` rake tasks are disabled.
|
|
595
|
+
The spec suite checks that every mapped facter version is valid, below 5 and has puppetlabs-facterdb data.
|
|
596
|
+
|
|
597
|
+
### Syncing with upstream
|
|
598
|
+
|
|
599
|
+
1. Add an `upstream` remote if it is absent (`git remote add upstream https://github.com/voxpupuli/rspec-puppet-facts.git`) and fetch the new upstream tag.
|
|
600
|
+
2. Primary path (replay): start from the new upstream tag's tree and run `scripts/rename-to-puppetlabs.sh`. It renames files and rewrites names in `lib`, `spec`, `Rakefile`, the gemspec and the rubocop files, then fails if leftovers remain. It never stages, commits or pushes. Then re-apply the changes the script does not make: the gemspec identity and the `facter` / `puppetlabs-facterdb` dependencies, the Gemfile puppet/facter sources, `ext/puppet_agent_facter_versions.json` and the README edits.
|
|
601
|
+
3. Expected conflict: `ext/puppet_agent_facter_versions.json`. Upstream maps OpenVox/OpenFact releases while this gem maps Puppet Labs releases only. Keep this gem's map, add only new Puppet Labs puppet/facter pairs and never add openvox keys.
|
|
602
|
+
4. Merge case (untested guidance; replay is the recommended path): the script assumes a clean upstream tree. After merging an upstream tag into the renamed branch, old-named files (for example `lib/rspec-puppet-facts.rb`) reappear beside the renamed ones and the script's `move` exits 1 with `move: both ... exist (merge case, see DOC-04)`. The script only guarantees that error. To handle it manually, carry upstream's changes from each reappeared old-named file into its renamed counterpart, remove the old-named file, then rerun the script for the name substitutions.
|
|
603
|
+
5. Verify with `bundle exec rake`.
|
|
604
|
+
|
|
605
|
+
## License
|
|
606
|
+
|
|
607
|
+
This project is licensed under the [Apache-2 license](./LICENSE).
|
|
608
|
+
|
|
609
|
+
## Provenance
|
|
610
|
+
|
|
611
|
+
This plugin was originally authored by [Camptocamp](http://www.camptocamp.com), based on an original idea from [apenney](https://github.com/apenney/puppet_facts/).
|
|
612
|
+
It was later maintained by Vox Pupuli at https://github.com/voxpupuli/rspec-puppet-facts.
|
|
613
|
+
Puppet Labs forked it from upstream 6.3.1 as rspec-puppetlabs-facts.
|
|
614
|
+
|
|
615
|
+
Previously: https://github.com/mcanevet/rspec-puppet-facts
|
|
616
|
+
|
|
617
|
+
## Release information
|
|
618
|
+
|
|
619
|
+
To make a new release, please do:
|
|
620
|
+
|
|
621
|
+
1. Run the `Release Prep` workflow (`release_prep.yml`) from the Actions tab with the new version. It updates `lib/rspec-puppetlabs-facts/version.rb`, regenerates `CHANGELOG.md` with gh-changelog (configured in `.changelog.yml`) and opens a release-prep PR against `main`.
|
|
622
|
+
2. Review and merge that PR.
|
|
623
|
+
3. Run the `release` workflow (`release.yml`). It builds the gem with `rake build`, creates the GitHub release `v<version>` and publishes to RubyGems.org and GitHub Packages.
|
|
624
|
+
|
|
625
|
+
Only maintainers with write access can run these workflows. Publishing uses the repository's `GEM_HOST_API_KEY` secret.
|