Skip to content

Commit

Permalink
Speedup tests: execute OS independent code once
Browse files Browse the repository at this point in the history
  • Loading branch information
deric committed Aug 27, 2024
1 parent 7e2e187 commit ae60576
Show file tree
Hide file tree
Showing 13 changed files with 1,534 additions and 1,593 deletions.
1 change: 1 addition & 0 deletions spec/default_module_facts.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
ipaddress: "172.16.254.254"
systemd_internal_services:
systemd-fsck-root.service: enabled-runtime
systemd-journal-gatewayd.service: indirect
Expand Down
349 changes: 172 additions & 177 deletions spec/defines/dropin_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,187 +3,182 @@
require 'spec_helper'

describe 'systemd::dropin_file' do
context 'supported operating systems' do
on_supported_os.each do |os, facts|
context "on #{os}" do
let(:facts) { facts }

let(:title) { 'test.conf' }

let(:params) do
{
unit: 'test.service',
content: 'random stuff',
}
end

it { is_expected.to compile.with_all_deps }

it {
expect(subject).to create_file("/etc/systemd/system/#{params[:unit]}.d").with(
ensure: 'directory',
recurse: 'true',
purge: 'true',
selinux_ignore_defaults: false
)
}
_, facts = on_supported_os.first
let(:facts) { facts }

it {
expect(subject).to create_systemd__daemon_reload(params[:unit])
}
let(:title) { 'test.conf' }

it {
expect(subject).to create_file("/etc/systemd/system/#{params[:unit]}.d/#{title}").with(
ensure: 'file',
content: %r{#{params[:content]}},
mode: '0444',
selinux_ignore_defaults: false
).
that_notifies("Systemd::Daemon_reload[#{params[:unit]}]")
}
let(:params) do
{
unit: 'test.service',
content: 'random stuff',
}
end

it { is_expected.to compile.with_all_deps }

it {
expect(subject).to create_file("/etc/systemd/system/#{params[:unit]}.d").with(
ensure: 'directory',
recurse: 'true',
purge: 'true',
selinux_ignore_defaults: false
)
}

it {
expect(subject).to create_systemd__daemon_reload(params[:unit])
}

it {
expect(subject).to create_file("/etc/systemd/system/#{params[:unit]}.d/#{title}").with(
ensure: 'file',
content: %r{#{params[:content]}},
mode: '0444',
selinux_ignore_defaults: false
).
that_notifies("Systemd::Daemon_reload[#{params[:unit]}]")
}

context 'notifies services' do
let(:params) do
super().merge(notify_service: true)
end
let(:filename) { "/etc/systemd/system/#{params[:unit]}.d/#{title}" }
let(:pre_condition) do
<<-PUPPET
service { ['test', 'test.service']:
}
PUPPET
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_service('test').that_subscribes_to("File[#{filename}]") }
it { is_expected.to contain_service('test.service').that_subscribes_to("File[#{filename}]") }

context 'notifies services' do
let(:params) do
super().merge(notify_service: true)
end
let(:filename) { "/etc/systemd/system/#{params[:unit]}.d/#{title}" }
let(:pre_condition) do
<<-PUPPET
service { ['test', 'test.service']:
}
PUPPET
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_service('test').that_subscribes_to("File[#{filename}]") }
it { is_expected.to contain_service('test.service').that_subscribes_to("File[#{filename}]") }

context 'with overridden name' do
let(:pre_condition) do
<<-PUPPET
service { 'myservice':
name => 'test',
}
PUPPET
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_service('myservice').that_subscribes_to("File[#{filename}]") }
it { is_expected.not_to contain_systemd__daemon_reload(params[:unit]).that_notifies('Service[myservice]') }
it { is_expected.to contain_systemd__daemon_reload(params[:unit]).that_comes_before('Service[myservice]') }
end
end

context 'doesn\'t notify services' do
let(:params) do
super().merge(notify_service: false)
end
let(:filename) { "/etc/systemd/system/#{params[:unit]}.d/#{title}" }
let(:pre_condition) do
<<-PUPPET
service { ['test', 'test.service']:
}
PUPPET
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_service('test') }
it { is_expected.not_to contain_service('test').that_subscribes_to("File[#{filename}]") }
it { is_expected.not_to contain_service('test').that_subscribes_to("Systemd::Daemon_reload[#{params[:unit]}]") }
it { is_expected.to contain_service('test.service') }
it { is_expected.not_to contain_service('test.service').that_subscribes_to("File[#{filename}]") }
it { is_expected.not_to contain_service('test.service').that_subscribes_to("Systemd::Daemon_reload[#{params[:unit]}]") }
end

context 'with selinux_ignore_defaults set to true' do
let(:params) do
super().merge(selinux_ignore_defaults: true)
end

it { is_expected.to create_file("/etc/systemd/system/#{params[:unit]}.d").with_selinux_ignore_defaults(true) }
it { is_expected.to create_file("/etc/systemd/system/#{params[:unit]}.d/#{title}").with_selinux_ignore_defaults(true) }
end

context 'with a bad unit type' do
let(:title) { 'test.badtype' }

it {
expect do
expect(subject).to compile.with_all_deps
end.to raise_error(%r{expects a match for Systemd::Dropin})
}
end

context 'with a bad unit type containing a slash' do
let(:title) { 'test/bad.conf' }

it {
expect do
expect(subject).to compile.with_all_deps
end.to raise_error(%r{expects a match for Systemd::Dropin})
}
end

context 'with another drop-in file with the same filename (and content)' do
let(:default_params) do
{
filename: 'longer-timeout.conf',
content: 'random stuff',
}
end
# Create drop-in file longer-timeout.conf for unit httpd.service
let(:pre_condition) do
"systemd::dropin_file { 'httpd_longer-timeout':
filename => '#{default_params[:filename]}',
unit => 'httpd.service',
content => '#{default_params[:context]}',
}"
end
#
# Create drop-in file longer-timeout.conf for unit ftp.service
let(:title) { 'ftp_longer-timeout' }
let(:params) do
default_params.merge(unit: 'ftp.service')
end

it {
expect(subject).to create_file("/etc/systemd/system/#{params[:unit]}.d/#{params[:filename]}").with(
ensure: 'file',
content: %r{#{params[:content]}},
mode: '0444'
)
}
end

context 'with sensitive content' do
let(:title) { 'sensitive.conf' }
let(:params) do
{
unit: 'sensitive.service',
content: RSpec::Puppet::RawString.new("Sensitive('TEST_CONTENT')"),
}
end

it {
expect(subject).to create_file("/etc/systemd/system/#{params[:unit]}.d/#{title}").with(
ensure: 'file',
content: sensitive('TEST_CONTENT')
)
}
end

context 'with daemon_reload = false' do
let(:params) do
super().merge(daemon_reload: false)
end

it { is_expected.to compile.with_all_deps }

it {
expect(subject).not_to create_systemd__daemon_reload(params[:unit])
}
end
context 'with overridden name' do
let(:pre_condition) do
<<-PUPPET
service { 'myservice':
name => 'test',
}
PUPPET
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_service('myservice').that_subscribes_to("File[#{filename}]") }
it { is_expected.not_to contain_systemd__daemon_reload(params[:unit]).that_notifies('Service[myservice]') }
it { is_expected.to contain_systemd__daemon_reload(params[:unit]).that_comes_before('Service[myservice]') }
end
end

context 'doesn\'t notify services' do
let(:params) do
super().merge(notify_service: false)
end
let(:filename) { "/etc/systemd/system/#{params[:unit]}.d/#{title}" }
let(:pre_condition) do
<<-PUPPET
service { ['test', 'test.service']:
}
PUPPET
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_service('test') }
it { is_expected.not_to contain_service('test').that_subscribes_to("File[#{filename}]") }
it { is_expected.not_to contain_service('test').that_subscribes_to("Systemd::Daemon_reload[#{params[:unit]}]") }
it { is_expected.to contain_service('test.service') }
it { is_expected.not_to contain_service('test.service').that_subscribes_to("File[#{filename}]") }
it { is_expected.not_to contain_service('test.service').that_subscribes_to("Systemd::Daemon_reload[#{params[:unit]}]") }
end

context 'with selinux_ignore_defaults set to true' do
let(:params) do
super().merge(selinux_ignore_defaults: true)
end

it { is_expected.to create_file("/etc/systemd/system/#{params[:unit]}.d").with_selinux_ignore_defaults(true) }
it { is_expected.to create_file("/etc/systemd/system/#{params[:unit]}.d/#{title}").with_selinux_ignore_defaults(true) }
end

context 'with a bad unit type' do
let(:title) { 'test.badtype' }

it {
expect do
expect(subject).to compile.with_all_deps
end.to raise_error(%r{expects a match for Systemd::Dropin})
}
end

context 'with a bad unit type containing a slash' do
let(:title) { 'test/bad.conf' }

it {
expect do
expect(subject).to compile.with_all_deps
end.to raise_error(%r{expects a match for Systemd::Dropin})
}
end

context 'with another drop-in file with the same filename (and content)' do
let(:default_params) do
{
filename: 'longer-timeout.conf',
content: 'random stuff',
}
end
# Create drop-in file longer-timeout.conf for unit httpd.service
let(:pre_condition) do
"systemd::dropin_file { 'httpd_longer-timeout':
filename => '#{default_params[:filename]}',
unit => 'httpd.service',
content => '#{default_params[:context]}',
}"
end
#
# Create drop-in file longer-timeout.conf for unit ftp.service
let(:title) { 'ftp_longer-timeout' }
let(:params) do
default_params.merge(unit: 'ftp.service')
end

it {
expect(subject).to create_file("/etc/systemd/system/#{params[:unit]}.d/#{params[:filename]}").with(
ensure: 'file',
content: %r{#{params[:content]}},
mode: '0444'
)
}
end

context 'with sensitive content' do
let(:title) { 'sensitive.conf' }
let(:params) do
{
unit: 'sensitive.service',
content: RSpec::Puppet::RawString.new("Sensitive('TEST_CONTENT')"),
}
end

it {
expect(subject).to create_file("/etc/systemd/system/#{params[:unit]}.d/#{title}").with(
ensure: 'file',
content: sensitive('TEST_CONTENT')
)
}
end

context 'with daemon_reload = false' do
let(:params) do
super().merge(daemon_reload: false)
end

it { is_expected.to compile.with_all_deps }

it {
expect(subject).not_to create_systemd__daemon_reload(params[:unit])
}
end
end
Loading

0 comments on commit ae60576

Please sign in to comment.