Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedup tests: execute OS independent code once #486

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you can introduce a helper method default_os_facts. I also wouldn't mind knowing which OS is being tested on.

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
Loading