Skip to content

Commit 12543ee

Browse files
committed
Cleanup
* (More) consistently use `.dig` to retrieve facts * Supply missing facts in tests
1 parent 09c9644 commit 12543ee

63 files changed

Lines changed: 1773 additions & 1468 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/puppet/functions/simplib/host_is_me.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def hostlist_contains_me(hosts)
2828
scope = closure_scope
2929

3030
host_identifiers = [
31-
scope['facts']['networking']['fqdn'],
32-
scope['facts']['networking']['hostname'],
31+
scope['facts'].dig('networking', 'fqdn'),
32+
scope['facts'].dig('networking', 'hostname'),
3333
'localhost',
3434
'localhost.localdomain',
3535
]

lib/puppet/functions/simplib/ip_to_cron.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
def ip_to_cron(occurs = 1, max_value = 59, algorithm = 'ip_mod', ip = nil)
5454
if ip.nil?
5555
scope = closure_scope
56-
ipaddr = scope['facts']['networking']['ip']
56+
ipaddr = scope['facts'].dig('networking', 'ip')
5757
else
5858
ipaddr = ip.dup
5959
end

lib/puppet/functions/simplib/join_mount_opts.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def join_mount_opts(system_mount_opts, new_mount_opts)
3434

3535
mount_options = {}
3636
scope = closure_scope
37-
selinux_current_mode = scope['facts']['os'].dig('selinux', 'current_mode')
37+
selinux_current_mode = scope['facts'].dig('os', 'selinux', 'current_mode')
3838

3939
if !selinux_current_mode || (selinux_current_mode == 'disabled')
4040
# SELinux is off, get rid of selinux related items in the options

lib/puppet/provider/reboot_notify/notify.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def self.post_resource_eval
124124
# If the number of seconds between the time that the record was written
125125
# and the current time is greater than the system uptime then we should
126126
# remove the record
127-
(current_time - v['updated']) > Facter.value(:system_uptime)['seconds']
127+
(current_time - v['updated']) > Facter.value(:system_uptime)&.dig('seconds')
128128
end
129129

130130
unless records.empty?

spec/classes/reboot_notify_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
describe 'simplib::reboot_notify' do
44
context 'on supported operating systems' do
5-
on_supported_os.each_key do |os|
5+
on_supported_os.each do |os, os_facts|
66
context "on #{os}" do
7+
let(:facts) { os_facts }
8+
79
it { is_expected.to compile.with_all_deps }
810
it { is_expected.to create_reboot_notify('__simplib_control__').with_log_level('notice') }
911
it { is_expected.to create_reboot_notify('__simplib_control__').with_control_only(true) }
Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
require 'spec_helper'
22

33
describe 'simplib::ldap::domain_to_dn' do
4-
on_supported_os.each_value do |os_facts|
5-
context 'with a regular domain' do
6-
let(:facts) do
7-
os_facts[:networking][:domain] = 'test.domain'
8-
os_facts
9-
end
4+
on_supported_os.each do |os, os_facts|
5+
context "on #{os}" do
6+
let(:facts) { os_facts }
107

11-
it { is_expected.to run.and_return('DC=test,DC=domain') }
12-
end
8+
context 'with a regular domain' do
9+
let(:facts) do
10+
os_facts[:networking][:domain] = 'test.domain'
11+
os_facts
12+
end
1313

14-
context 'with a short domain' do
15-
let(:facts) do
16-
os_facts[:networking][:domain] = 'domain'
17-
os_facts
14+
it { is_expected.to run.and_return('DC=test,DC=domain') }
1815
end
1916

20-
it { is_expected.to run.and_return('DC=domain') }
21-
end
17+
context 'with a short domain' do
18+
let(:facts) do
19+
os_facts[:networking][:domain] = 'domain'
20+
os_facts
21+
end
2222

23-
context 'when passed a domain' do
24-
it { is_expected.to run.with_params('test.domain').and_return('DC=test,DC=domain') }
25-
end
23+
it { is_expected.to run.and_return('DC=domain') }
24+
end
25+
26+
context 'when passed a domain' do
27+
it { is_expected.to run.with_params('test.domain').and_return('DC=test,DC=domain') }
28+
end
2629

27-
context 'when told to downcase the attributes' do
28-
it { is_expected.to run.with_params('test.domain', true).and_return('dc=test,dc=domain') }
30+
context 'when told to downcase the attributes' do
31+
it { is_expected.to run.with_params('test.domain', true).and_return('dc=test,dc=domain') }
32+
end
2933
end
3034
end
3135
end

spec/functions/simplib/dlookup_spec.rb

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,75 @@
22

33
# This just gives us a hook so that we can call the function later on
44
describe 'simplib::stages', type: :class do
5-
let(:pre_condition) do
6-
<<~END
7-
define mydef::test (
8-
$attribute = simplib::dlookup('mydef::test', 'attribute', $title, { 'default_value' => 'lucille2' })
9-
) {
10-
notify { "$title says": message => $attribute }
11-
}
5+
on_supported_os.each do |os, os_facts|
6+
context "on #{os}" do
7+
let(:facts) { os_facts }
8+
let(:pre_condition) do
9+
<<~END
10+
define mydef::test (
11+
$attribute = simplib::dlookup('mydef::test', 'attribute', $title, { 'default_value' => 'lucille2' })
12+
) {
13+
notify { "$title says": message => $attribute }
14+
}
1215
13-
define mydef::othertest (
14-
$attribute = simplib::dlookup('mydef::test', 'attribute', { 'default_value' => 'lucille2' })
15-
) {
16-
notify { "other $title says": message => $attribute }
17-
}
16+
define mydef::othertest (
17+
$attribute = simplib::dlookup('mydef::test', 'attribute', { 'default_value' => 'lucille2' })
18+
) {
19+
notify { "other $title says": message => $attribute }
20+
}
1821
19-
mydef::test { 'gob': }
20-
mydef::test { 'tobias': }
21-
mydef::test { 'michael': attribute => 'bananastand' }
22+
mydef::test { 'gob': }
23+
mydef::test { 'tobias': }
24+
mydef::test { 'michael': attribute => 'bananastand' }
2225
23-
mydef::othertest { 'gob': }
24-
mydef::othertest { 'tobias': }
25-
mydef::othertest { 'michael': attribute => 'bananastand' }
26-
END
27-
end
26+
mydef::othertest { 'gob': }
27+
mydef::othertest { 'tobias': }
28+
mydef::othertest { 'michael': attribute => 'bananastand' }
29+
END
30+
end
2831

29-
let(:gob) { catalogue.resource('Mydef::Test[gob]') }
30-
let(:tobias) { catalogue.resource('Mydef::Test[tobias]') }
31-
let(:michael) { catalogue.resource('Mydef::Test[michael]') }
32+
let(:gob) { catalogue.resource('Mydef::Test[gob]') }
33+
let(:tobias) { catalogue.resource('Mydef::Test[tobias]') }
34+
let(:michael) { catalogue.resource('Mydef::Test[michael]') }
3235

33-
let(:gob_other) { catalogue.resource('Mydef::Othertest[gob]') }
34-
let(:tobias_other) { catalogue.resource('Mydef::Othertest[tobias]') }
35-
let(:michael_other) { catalogue.resource('Mydef::Othertest[michael]') }
36+
let(:gob_other) { catalogue.resource('Mydef::Othertest[gob]') }
37+
let(:tobias_other) { catalogue.resource('Mydef::Othertest[tobias]') }
38+
let(:michael_other) { catalogue.resource('Mydef::Othertest[michael]') }
3639

37-
it { is_expected.to compile.with_all_deps }
40+
it { is_expected.to compile.with_all_deps }
3841

39-
context 'no overrides' do
40-
it { expect(gob[:attribute]).to eq('lucille2') }
41-
it { expect(tobias[:attribute]).to eq('lucille2') }
42-
it { expect(michael[:attribute]).to eq('bananastand') }
43-
it { expect(gob_other[:attribute]).to eq('lucille2') }
44-
it { expect(tobias_other[:attribute]).to eq('lucille2') }
45-
it { expect(michael_other[:attribute]).to eq('bananastand') }
46-
end
42+
context 'no overrides' do
43+
it { expect(gob[:attribute]).to eq('lucille2') }
44+
it { expect(tobias[:attribute]).to eq('lucille2') }
45+
it { expect(michael[:attribute]).to eq('bananastand') }
46+
it { expect(gob_other[:attribute]).to eq('lucille2') }
47+
it { expect(tobias_other[:attribute]).to eq('lucille2') }
48+
it { expect(michael_other[:attribute]).to eq('bananastand') }
49+
end
4750

48-
context 'overrides' do
49-
let(:facts) do
50-
{
51-
cache_bust: Time.now.to_s,
52-
hieradata: 'simplib_dlookup_overrides',
53-
}
54-
end
51+
context 'overrides' do
52+
let(:facts) do
53+
os_facts.merge(
54+
cache_bust: Time.now.to_s,
55+
hieradata: 'simplib_dlookup_overrides',
56+
)
57+
end
5558

56-
let(:hieradata) { 'simplib_dlookup_overrides' }
59+
let(:hieradata) { 'simplib_dlookup_overrides' }
5760

58-
context 'with global overrides' do
59-
it { expect(gob[:attribute]).to eq('illusions') }
60-
it { expect(gob_other[:attribute]).to eq('illusions') }
61-
it { expect(tobias_other[:attribute]).to eq('illusions') }
62-
end
63-
context 'with specific overrides' do
64-
it { expect(tobias[:attribute]).to eq('blueman') }
65-
end
66-
context 'with a static value' do
67-
it { expect(michael[:attribute]).to eq('bananastand') }
68-
it { expect(michael_other[:attribute]).to eq('bananastand') }
61+
context 'with global overrides' do
62+
it { expect(gob[:attribute]).to eq('illusions') }
63+
it { expect(gob_other[:attribute]).to eq('illusions') }
64+
it { expect(tobias_other[:attribute]).to eq('illusions') }
65+
end
66+
context 'with specific overrides' do
67+
it { expect(tobias[:attribute]).to eq('blueman') }
68+
end
69+
context 'with a static value' do
70+
it { expect(michael[:attribute]).to eq('bananastand') }
71+
it { expect(michael_other[:attribute]).to eq('bananastand') }
72+
end
73+
end
6974
end
7075
end
7176
end
Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11
require 'spec_helper'
22

33
describe 'Simplib::Cron::Hour_entry' do
4-
context 'with valid parameters' do
5-
it { is_expected.to allow_value('22') }
6-
it { is_expected.to allow_value('*') }
7-
it { is_expected.to allow_value('*/5') }
8-
it { is_expected.to allow_value('2/5') }
9-
it { is_expected.to allow_value(22) }
10-
it { is_expected.to allow_value('23,20') }
11-
it { is_expected.to allow_value('20-23') }
12-
it { is_expected.to allow_value('0-23/2') }
13-
end
14-
context 'with invalid parameters' do
15-
it { is_expected.not_to allow_value('one') }
16-
it { is_expected.not_to allow_value('-2') }
17-
it { is_expected.not_to allow_value('/3') }
18-
it { is_expected.not_to allow_value('24') }
19-
it { is_expected.not_to allow_value('13/*') }
20-
it { is_expected.not_to allow_value('13-/15') }
21-
end
22-
context 'with silly things' do
23-
it { is_expected.not_to allow_value([]) }
24-
it { is_expected.not_to allow_value('.') }
25-
it { is_expected.not_to allow_value('') }
26-
it { is_expected.not_to allow_value('1 ') }
27-
it { is_expected.not_to allow_value('5 1') }
28-
it { is_expected.not_to allow_value(:undef) }
4+
on_supported_os.each do |os, os_facts|
5+
context "on #{os}" do
6+
let(:facts) { os_facts }
7+
8+
context 'with valid parameters' do
9+
it { is_expected.to allow_value('22') }
10+
it { is_expected.to allow_value('*') }
11+
it { is_expected.to allow_value('*/5') }
12+
it { is_expected.to allow_value('2/5') }
13+
it { is_expected.to allow_value(22) }
14+
it { is_expected.to allow_value('23,20') }
15+
it { is_expected.to allow_value('20-23') }
16+
it { is_expected.to allow_value('0-23/2') }
17+
end
18+
context 'with invalid parameters' do
19+
it { is_expected.not_to allow_value('one') }
20+
it { is_expected.not_to allow_value('-2') }
21+
it { is_expected.not_to allow_value('/3') }
22+
it { is_expected.not_to allow_value('24') }
23+
it { is_expected.not_to allow_value('13/*') }
24+
it { is_expected.not_to allow_value('13-/15') }
25+
end
26+
context 'with silly things' do
27+
it { is_expected.not_to allow_value([]) }
28+
it { is_expected.not_to allow_value('.') }
29+
it { is_expected.not_to allow_value('') }
30+
it { is_expected.not_to allow_value('1 ') }
31+
it { is_expected.not_to allow_value('5 1') }
32+
it { is_expected.not_to allow_value(:undef) }
33+
end
34+
end
2935
end
3036
end
Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
require 'spec_helper'
22

33
describe 'Simplib::Cron::Hour' do
4-
context 'with valid parameters' do
5-
it { is_expected.to allow_value([22]) }
6-
it { is_expected.to allow_value(['22']) }
7-
it { is_expected.to allow_value(['20-23', '10-14/2', 3, 5, '19']) }
8-
it { is_expected.to allow_value(22) }
9-
it { is_expected.to allow_value('22') }
10-
it { is_expected.to allow_value('20-23') }
11-
it { is_expected.to allow_value('*') }
12-
it { is_expected.to allow_value('*/5') }
13-
it { is_expected.to allow_value('0-23/2') }
14-
end
15-
context 'with invalid parameters' do
16-
it { is_expected.not_to allow_value(['20-23', '10-14/2', 3, 24, 5, '19']) }
17-
it { is_expected.not_to allow_value(['0,1,12-19,5']) }
18-
it { is_expected.not_to allow_value(["'0','1','12-19','5'"]) }
4+
on_supported_os.each do |os, os_facts|
5+
context "on #{os}" do
6+
let(:facts) { os_facts }
7+
8+
context 'with valid parameters' do
9+
it { is_expected.to allow_value([22]) }
10+
it { is_expected.to allow_value(['22']) }
11+
it { is_expected.to allow_value(['20-23', '10-14/2', 3, 5, '19']) }
12+
it { is_expected.to allow_value(22) }
13+
it { is_expected.to allow_value('22') }
14+
it { is_expected.to allow_value('20-23') }
15+
it { is_expected.to allow_value('*') }
16+
it { is_expected.to allow_value('*/5') }
17+
it { is_expected.to allow_value('0-23/2') }
18+
end
19+
context 'with invalid parameters' do
20+
it { is_expected.not_to allow_value(['20-23', '10-14/2', 3, 24, 5, '19']) }
21+
it { is_expected.not_to allow_value(['0,1,12-19,5']) }
22+
it { is_expected.not_to allow_value(["'0','1','12-19','5'"]) }
23+
end
24+
end
1925
end
2026
end

0 commit comments

Comments
 (0)