Skip to content

Commit d4c2c72

Browse files
committed
Speedup tests: execute OS independent code once
1 parent 2b232ef commit d4c2c72

12 files changed

+1550
-1610
lines changed

spec/defines/dropin_file_spec.rb

+172-177
Original file line numberDiff line numberDiff line change
@@ -3,187 +3,182 @@
33
require 'spec_helper'
44

55
describe 'systemd::dropin_file' do
6-
context 'supported operating systems' do
7-
on_supported_os.each do |os, facts|
8-
context "on #{os}" do
9-
let(:facts) { facts }
10-
11-
let(:title) { 'test.conf' }
12-
13-
let(:params) do
14-
{
15-
unit: 'test.service',
16-
content: 'random stuff',
17-
}
18-
end
19-
20-
it { is_expected.to compile.with_all_deps }
21-
22-
it {
23-
expect(subject).to create_file("/etc/systemd/system/#{params[:unit]}.d").with(
24-
ensure: 'directory',
25-
recurse: 'true',
26-
purge: 'true',
27-
selinux_ignore_defaults: false
28-
)
29-
}
6+
_, facts = on_supported_os.first
7+
let(:facts) { facts }
308

31-
it {
32-
expect(subject).to create_systemd__daemon_reload(params[:unit])
33-
}
9+
let(:title) { 'test.conf' }
3410

35-
it {
36-
expect(subject).to create_file("/etc/systemd/system/#{params[:unit]}.d/#{title}").with(
37-
ensure: 'file',
38-
content: %r{#{params[:content]}},
39-
mode: '0444',
40-
selinux_ignore_defaults: false
41-
).
42-
that_notifies("Systemd::Daemon_reload[#{params[:unit]}]")
43-
}
11+
let(:params) do
12+
{
13+
unit: 'test.service',
14+
content: 'random stuff',
15+
}
16+
end
17+
18+
it { is_expected.to compile.with_all_deps }
19+
20+
it {
21+
expect(subject).to create_file("/etc/systemd/system/#{params[:unit]}.d").with(
22+
ensure: 'directory',
23+
recurse: 'true',
24+
purge: 'true',
25+
selinux_ignore_defaults: false
26+
)
27+
}
28+
29+
it {
30+
expect(subject).to create_systemd__daemon_reload(params[:unit])
31+
}
32+
33+
it {
34+
expect(subject).to create_file("/etc/systemd/system/#{params[:unit]}.d/#{title}").with(
35+
ensure: 'file',
36+
content: %r{#{params[:content]}},
37+
mode: '0444',
38+
selinux_ignore_defaults: false
39+
).
40+
that_notifies("Systemd::Daemon_reload[#{params[:unit]}]")
41+
}
42+
43+
context 'notifies services' do
44+
let(:params) do
45+
super().merge(notify_service: true)
46+
end
47+
let(:filename) { "/etc/systemd/system/#{params[:unit]}.d/#{title}" }
48+
let(:pre_condition) do
49+
<<-PUPPET
50+
service { ['test', 'test.service']:
51+
}
52+
PUPPET
53+
end
54+
55+
it { is_expected.to compile.with_all_deps }
56+
it { is_expected.to contain_service('test').that_subscribes_to("File[#{filename}]") }
57+
it { is_expected.to contain_service('test.service').that_subscribes_to("File[#{filename}]") }
4458

45-
context 'notifies services' do
46-
let(:params) do
47-
super().merge(notify_service: true)
48-
end
49-
let(:filename) { "/etc/systemd/system/#{params[:unit]}.d/#{title}" }
50-
let(:pre_condition) do
51-
<<-PUPPET
52-
service { ['test', 'test.service']:
53-
}
54-
PUPPET
55-
end
56-
57-
it { is_expected.to compile.with_all_deps }
58-
it { is_expected.to contain_service('test').that_subscribes_to("File[#{filename}]") }
59-
it { is_expected.to contain_service('test.service').that_subscribes_to("File[#{filename}]") }
60-
61-
context 'with overridden name' do
62-
let(:pre_condition) do
63-
<<-PUPPET
64-
service { 'myservice':
65-
name => 'test',
66-
}
67-
PUPPET
68-
end
69-
70-
it { is_expected.to compile.with_all_deps }
71-
it { is_expected.to contain_service('myservice').that_subscribes_to("File[#{filename}]") }
72-
it { is_expected.not_to contain_systemd__daemon_reload(params[:unit]).that_notifies('Service[myservice]') }
73-
it { is_expected.to contain_systemd__daemon_reload(params[:unit]).that_comes_before('Service[myservice]') }
74-
end
75-
end
76-
77-
context 'doesn\'t notify services' do
78-
let(:params) do
79-
super().merge(notify_service: false)
80-
end
81-
let(:filename) { "/etc/systemd/system/#{params[:unit]}.d/#{title}" }
82-
let(:pre_condition) do
83-
<<-PUPPET
84-
service { ['test', 'test.service']:
85-
}
86-
PUPPET
87-
end
88-
89-
it { is_expected.to compile.with_all_deps }
90-
it { is_expected.to contain_service('test') }
91-
it { is_expected.not_to contain_service('test').that_subscribes_to("File[#{filename}]") }
92-
it { is_expected.not_to contain_service('test').that_subscribes_to("Systemd::Daemon_reload[#{params[:unit]}]") }
93-
it { is_expected.to contain_service('test.service') }
94-
it { is_expected.not_to contain_service('test.service').that_subscribes_to("File[#{filename}]") }
95-
it { is_expected.not_to contain_service('test.service').that_subscribes_to("Systemd::Daemon_reload[#{params[:unit]}]") }
96-
end
97-
98-
context 'with selinux_ignore_defaults set to true' do
99-
let(:params) do
100-
super().merge(selinux_ignore_defaults: true)
101-
end
102-
103-
it { is_expected.to create_file("/etc/systemd/system/#{params[:unit]}.d").with_selinux_ignore_defaults(true) }
104-
it { is_expected.to create_file("/etc/systemd/system/#{params[:unit]}.d/#{title}").with_selinux_ignore_defaults(true) }
105-
end
106-
107-
context 'with a bad unit type' do
108-
let(:title) { 'test.badtype' }
109-
110-
it {
111-
expect do
112-
expect(subject).to compile.with_all_deps
113-
end.to raise_error(%r{expects a match for Systemd::Dropin})
114-
}
115-
end
116-
117-
context 'with a bad unit type containing a slash' do
118-
let(:title) { 'test/bad.conf' }
119-
120-
it {
121-
expect do
122-
expect(subject).to compile.with_all_deps
123-
end.to raise_error(%r{expects a match for Systemd::Dropin})
124-
}
125-
end
126-
127-
context 'with another drop-in file with the same filename (and content)' do
128-
let(:default_params) do
129-
{
130-
filename: 'longer-timeout.conf',
131-
content: 'random stuff',
132-
}
133-
end
134-
# Create drop-in file longer-timeout.conf for unit httpd.service
135-
let(:pre_condition) do
136-
"systemd::dropin_file { 'httpd_longer-timeout':
137-
filename => '#{default_params[:filename]}',
138-
unit => 'httpd.service',
139-
content => '#{default_params[:context]}',
140-
}"
141-
end
142-
#
143-
# Create drop-in file longer-timeout.conf for unit ftp.service
144-
let(:title) { 'ftp_longer-timeout' }
145-
let(:params) do
146-
default_params.merge(unit: 'ftp.service')
147-
end
148-
149-
it {
150-
expect(subject).to create_file("/etc/systemd/system/#{params[:unit]}.d/#{params[:filename]}").with(
151-
ensure: 'file',
152-
content: %r{#{params[:content]}},
153-
mode: '0444'
154-
)
155-
}
156-
end
157-
158-
context 'with sensitive content' do
159-
let(:title) { 'sensitive.conf' }
160-
let(:params) do
161-
{
162-
unit: 'sensitive.service',
163-
content: RSpec::Puppet::RawString.new("Sensitive('TEST_CONTENT')"),
164-
}
165-
end
166-
167-
it {
168-
expect(subject).to create_file("/etc/systemd/system/#{params[:unit]}.d/#{title}").with(
169-
ensure: 'file',
170-
content: sensitive('TEST_CONTENT')
171-
)
172-
}
173-
end
174-
175-
context 'with daemon_reload = false' do
176-
let(:params) do
177-
super().merge(daemon_reload: false)
178-
end
179-
180-
it { is_expected.to compile.with_all_deps }
181-
182-
it {
183-
expect(subject).not_to create_systemd__daemon_reload(params[:unit])
184-
}
185-
end
59+
context 'with overridden name' do
60+
let(:pre_condition) do
61+
<<-PUPPET
62+
service { 'myservice':
63+
name => 'test',
64+
}
65+
PUPPET
18666
end
67+
68+
it { is_expected.to compile.with_all_deps }
69+
it { is_expected.to contain_service('myservice').that_subscribes_to("File[#{filename}]") }
70+
it { is_expected.not_to contain_systemd__daemon_reload(params[:unit]).that_notifies('Service[myservice]') }
71+
it { is_expected.to contain_systemd__daemon_reload(params[:unit]).that_comes_before('Service[myservice]') }
72+
end
73+
end
74+
75+
context 'doesn\'t notify services' do
76+
let(:params) do
77+
super().merge(notify_service: false)
78+
end
79+
let(:filename) { "/etc/systemd/system/#{params[:unit]}.d/#{title}" }
80+
let(:pre_condition) do
81+
<<-PUPPET
82+
service { ['test', 'test.service']:
83+
}
84+
PUPPET
85+
end
86+
87+
it { is_expected.to compile.with_all_deps }
88+
it { is_expected.to contain_service('test') }
89+
it { is_expected.not_to contain_service('test').that_subscribes_to("File[#{filename}]") }
90+
it { is_expected.not_to contain_service('test').that_subscribes_to("Systemd::Daemon_reload[#{params[:unit]}]") }
91+
it { is_expected.to contain_service('test.service') }
92+
it { is_expected.not_to contain_service('test.service').that_subscribes_to("File[#{filename}]") }
93+
it { is_expected.not_to contain_service('test.service').that_subscribes_to("Systemd::Daemon_reload[#{params[:unit]}]") }
94+
end
95+
96+
context 'with selinux_ignore_defaults set to true' do
97+
let(:params) do
98+
super().merge(selinux_ignore_defaults: true)
99+
end
100+
101+
it { is_expected.to create_file("/etc/systemd/system/#{params[:unit]}.d").with_selinux_ignore_defaults(true) }
102+
it { is_expected.to create_file("/etc/systemd/system/#{params[:unit]}.d/#{title}").with_selinux_ignore_defaults(true) }
103+
end
104+
105+
context 'with a bad unit type' do
106+
let(:title) { 'test.badtype' }
107+
108+
it {
109+
expect do
110+
expect(subject).to compile.with_all_deps
111+
end.to raise_error(%r{expects a match for Systemd::Dropin})
112+
}
113+
end
114+
115+
context 'with a bad unit type containing a slash' do
116+
let(:title) { 'test/bad.conf' }
117+
118+
it {
119+
expect do
120+
expect(subject).to compile.with_all_deps
121+
end.to raise_error(%r{expects a match for Systemd::Dropin})
122+
}
123+
end
124+
125+
context 'with another drop-in file with the same filename (and content)' do
126+
let(:default_params) do
127+
{
128+
filename: 'longer-timeout.conf',
129+
content: 'random stuff',
130+
}
131+
end
132+
# Create drop-in file longer-timeout.conf for unit httpd.service
133+
let(:pre_condition) do
134+
"systemd::dropin_file { 'httpd_longer-timeout':
135+
filename => '#{default_params[:filename]}',
136+
unit => 'httpd.service',
137+
content => '#{default_params[:context]}',
138+
}"
139+
end
140+
#
141+
# Create drop-in file longer-timeout.conf for unit ftp.service
142+
let(:title) { 'ftp_longer-timeout' }
143+
let(:params) do
144+
default_params.merge(unit: 'ftp.service')
187145
end
146+
147+
it {
148+
expect(subject).to create_file("/etc/systemd/system/#{params[:unit]}.d/#{params[:filename]}").with(
149+
ensure: 'file',
150+
content: %r{#{params[:content]}},
151+
mode: '0444'
152+
)
153+
}
154+
end
155+
156+
context 'with sensitive content' do
157+
let(:title) { 'sensitive.conf' }
158+
let(:params) do
159+
{
160+
unit: 'sensitive.service',
161+
content: RSpec::Puppet::RawString.new("Sensitive('TEST_CONTENT')"),
162+
}
163+
end
164+
165+
it {
166+
expect(subject).to create_file("/etc/systemd/system/#{params[:unit]}.d/#{title}").with(
167+
ensure: 'file',
168+
content: sensitive('TEST_CONTENT')
169+
)
170+
}
171+
end
172+
173+
context 'with daemon_reload = false' do
174+
let(:params) do
175+
super().merge(daemon_reload: false)
176+
end
177+
178+
it { is_expected.to compile.with_all_deps }
179+
180+
it {
181+
expect(subject).not_to create_systemd__daemon_reload(params[:unit])
182+
}
188183
end
189184
end

0 commit comments

Comments
 (0)