diff --git a/spec/defines/dropin_file_spec.rb b/spec/defines/dropin_file_spec.rb index 544e4102..1a34d6bd 100644 --- a/spec/defines/dropin_file_spec.rb +++ b/spec/defines/dropin_file_spec.rb @@ -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 diff --git a/spec/defines/manage_dropin_spec.rb b/spec/defines/manage_dropin_spec.rb index fcf333c9..688c04fc 100644 --- a/spec/defines/manage_dropin_spec.rb +++ b/spec/defines/manage_dropin_spec.rb @@ -3,224 +3,219 @@ require 'spec_helper' describe 'systemd::manage_dropin' do - context 'supported operating systems' do - on_supported_os.each do |os, facts| - context "on #{os}" do - let(:facts) { facts } - - let(:title) { 'foobar.conf' } - - context 'on a service' do - let(:params) do - { - unit: 'special.service', - } - end - - it { is_expected.to compile.with_all_deps } - it { is_expected.to contain_systemd__dropin_file('foobar.conf').with_content(%r{^# Deployed with puppet$}) } - - context 'with empty sections' do - let(:params) do - super().merge( - unit_entry: {}, - service_entry: {} - ) - end - - it { is_expected.to compile.with_all_deps } - - it { - is_expected.to contain_systemd__dropin_file('foobar.conf'). - with_content(%r{^\[Unit\]$}). - with_content(%r{^\[Service\]$}). - without_content(%r{^\[Slice\]$}) - } - end - - context 'setting some parameters simply' do - let(:params) do - super().merge( - unit_entry: { - DefaultDependencies: true - }, - service_entry: { - SyslogIdentifier: 'simple', - LimitCORE: 'infinity', - IODeviceWeight: - [ - ['/dev/weight', 10], - ['/dev/weight2', 20], - ], - IOReadBandwidthMax: ['/dev/weight3', '50K'], - } - ) - end - - it { - is_expected.to contain_systemd__dropin_file('foobar.conf'). - with_content(%r{^LimitCORE=infinity$}). - with_content(%r{^DefaultDependencies=true$}). - with_content(%r{^SyslogIdentifier=simple$}). - with_content(%r{^IODeviceWeight=/dev/weight 10$}). - with_content(%r{^IODeviceWeight=/dev/weight2 20$}). - with_content(%r{^IOReadBandwidthMax=/dev/weight3 50K$}). - without_content(%r{^\[Slice\]$}) - } - end - - context 'drop file chaning Type and resetting ExecStart' do - let(:params) do - super().merge( - service_entry: { - Type: 'oneshot', - ExecStart: ['', '/usr/bin/doit.sh'], - } - ) - end - - it { is_expected.to compile.with_all_deps } - - it { - is_expected.to contain_systemd__dropin_file('foobar.conf'). - with_content(%r{^\[Service\]$}). - without_content(%r{^\[Unit\]$}). - without_content(%r{^\[Install\]$}). - with_content(%r{^ExecStart=$}). - with_content(%r{^ExecStart=/usr/bin/doit.sh$}). - with_content(%r{^Type=oneshot$}) - } - end - - context 'with an instance to instance relation' do - let(:params) do - super().merge( - unit_entry: { - 'After' => ['user-runtime-dir@%i.service'], - 'Requires' => ['user-runtime-dir@%i.service'], - } - ) - end - - it { - is_expected.to contain_systemd__dropin_file('foobar.conf'). - with_content(%r{^After=user-runtime-dir@%i.service$}). - with_content(%r{^Requires=user-runtime-dir@%i.service$}) - } - end - - context 'with a timer entry' do - let(:params) do - super().merge( - timer_entry: { - 'OnCalendar' => 'soon', - } - ) - end - - it { is_expected.to compile.and_raise_error(%r{timer_entry is only valid for timer units}) } - end - - context 'with a slice entry' do - let(:params) do - super().merge( - slice_entry: { - 'MemoryMax' => '100G', - } - ) - end - - it { is_expected.to compile.and_raise_error(%r{slice_entry is only valid for slice units}) } - end - end - - context 'on a timer' do - let(:params) do - { - unit: 'special.timer', - timer_entry: { - 'OnCalendar' => 'soon', - } - } - end - - it { is_expected.to compile.with_all_deps } - - it { - is_expected.to contain_systemd__dropin_file('foobar.conf'). - with_unit('special.timer'). - with_content(%r{^OnCalendar=soon$}) + _, facts = on_supported_os.first + let(:facts) { facts } + + let(:title) { 'foobar.conf' } + + context 'on a service' do + let(:params) do + { + unit: 'special.service', + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_systemd__dropin_file('foobar.conf').with_content(%r{^# Deployed with puppet$}) } + + context 'with empty sections' do + let(:params) do + super().merge( + unit_entry: {}, + service_entry: {} + ) + end + + it { is_expected.to compile.with_all_deps } + + it { + is_expected.to contain_systemd__dropin_file('foobar.conf'). + with_content(%r{^\[Unit\]$}). + with_content(%r{^\[Service\]$}). + without_content(%r{^\[Slice\]$}) + } + end + + context 'setting some parameters simply' do + let(:params) do + super().merge( + unit_entry: { + DefaultDependencies: true + }, + service_entry: { + SyslogIdentifier: 'simple', + LimitCORE: 'infinity', + IODeviceWeight: + [ + ['/dev/weight', 10], + ['/dev/weight2', 20], + ], + IOReadBandwidthMax: ['/dev/weight3', '50K'], } - end - - context 'on a slice' do - let(:params) do - { - unit: 'user-.slice', - slice_entry: { - 'MemoryMax' => '10G', - 'MemoryAccounting' => true, - 'IOWriteBandwidthMax' => [ - ['/dev/afs', '50P'], - ['/dev/gluster', 50], - ] - } - } - end - - it { is_expected.to compile.with_all_deps } - - it { - is_expected.to contain_systemd__dropin_file('foobar.conf'). - with_unit('user-.slice'). - with_content(%r{^IOWriteBandwidthMax=/dev/afs 50P$}). - with_content(%r{^IOWriteBandwidthMax=/dev/gluster 50$}). - with_content(%r{^MemoryMax=10G$}). - with_content(%r{^MemoryAccounting=true$}). - without_content(%r{^\[Service\]$}) + ) + end + + it { + is_expected.to contain_systemd__dropin_file('foobar.conf'). + with_content(%r{^LimitCORE=infinity$}). + with_content(%r{^DefaultDependencies=true$}). + with_content(%r{^SyslogIdentifier=simple$}). + with_content(%r{^IODeviceWeight=/dev/weight 10$}). + with_content(%r{^IODeviceWeight=/dev/weight2 20$}). + with_content(%r{^IOReadBandwidthMax=/dev/weight3 50K$}). + without_content(%r{^\[Slice\]$}) + } + end + + context 'drop file chaning Type and resetting ExecStart' do + let(:params) do + super().merge( + service_entry: { + Type: 'oneshot', + ExecStart: ['', '/usr/bin/doit.sh'], } - end - - context 'on a path unit' do - let(:params) do - { - unit: 'special.path', - path_entry: { - 'PathExists' => '/etc/hosts', - } - } - end - - it { is_expected.to compile.with_all_deps } - - it { - is_expected.to contain_systemd__dropin_file('foobar.conf'). - with_unit('special.path'). - with_content(%r{^\[Path\]$}). - with_content(%r{^PathExists=/etc/hosts$}) + ) + end + + it { is_expected.to compile.with_all_deps } + + it { + is_expected.to contain_systemd__dropin_file('foobar.conf'). + with_content(%r{^\[Service\]$}). + without_content(%r{^\[Unit\]$}). + without_content(%r{^\[Install\]$}). + with_content(%r{^ExecStart=$}). + with_content(%r{^ExecStart=/usr/bin/doit.sh$}). + with_content(%r{^Type=oneshot$}) + } + end + + context 'with an instance to instance relation' do + let(:params) do + super().merge( + unit_entry: { + 'After' => ['user-runtime-dir@%i.service'], + 'Requires' => ['user-runtime-dir@%i.service'], } - end - - context 'on a socket unit' do - let(:params) do - { - unit: 'special.socket', - socket_entry: { - 'ListenMessageQueue' => '/panic', - } - } - end - - it { is_expected.to compile.with_all_deps } - - it { - is_expected.to contain_systemd__dropin_file('foobar.conf'). - with_unit('special.socket'). - with_content(%r{^\[Socket\]$}). - with_content(%r{^ListenMessageQueue=/panic$}) + ) + end + + it { + is_expected.to contain_systemd__dropin_file('foobar.conf'). + with_content(%r{^After=user-runtime-dir@%i.service$}). + with_content(%r{^Requires=user-runtime-dir@%i.service$}) + } + end + + context 'with a timer entry' do + let(:params) do + super().merge( + timer_entry: { + 'OnCalendar' => 'soon', } - end + ) end + + it { is_expected.to compile.and_raise_error(%r{timer_entry is only valid for timer units}) } end + + context 'with a slice entry' do + let(:params) do + super().merge( + slice_entry: { + 'MemoryMax' => '100G', + } + ) + end + + it { is_expected.to compile.and_raise_error(%r{slice_entry is only valid for slice units}) } + end + end + + context 'on a timer' do + let(:params) do + { + unit: 'special.timer', + timer_entry: { + 'OnCalendar' => 'soon', + } + } + end + + it { is_expected.to compile.with_all_deps } + + it { + is_expected.to contain_systemd__dropin_file('foobar.conf'). + with_unit('special.timer'). + with_content(%r{^OnCalendar=soon$}) + } + end + + context 'on a slice' do + let(:params) do + { + unit: 'user-.slice', + slice_entry: { + 'MemoryMax' => '10G', + 'MemoryAccounting' => true, + 'IOWriteBandwidthMax' => [ + ['/dev/afs', '50P'], + ['/dev/gluster', 50], + ] + } + } + end + + it { is_expected.to compile.with_all_deps } + + it { + is_expected.to contain_systemd__dropin_file('foobar.conf'). + with_unit('user-.slice'). + with_content(%r{^IOWriteBandwidthMax=/dev/afs 50P$}). + with_content(%r{^IOWriteBandwidthMax=/dev/gluster 50$}). + with_content(%r{^MemoryMax=10G$}). + with_content(%r{^MemoryAccounting=true$}). + without_content(%r{^\[Service\]$}) + } + end + + context 'on a path unit' do + let(:params) do + { + unit: 'special.path', + path_entry: { + 'PathExists' => '/etc/hosts', + } + } + end + + it { is_expected.to compile.with_all_deps } + + it { + is_expected.to contain_systemd__dropin_file('foobar.conf'). + with_unit('special.path'). + with_content(%r{^\[Path\]$}). + with_content(%r{^PathExists=/etc/hosts$}) + } + end + + context 'on a socket unit' do + let(:params) do + { + unit: 'special.socket', + socket_entry: { + 'ListenMessageQueue' => '/panic', + } + } + end + + it { is_expected.to compile.with_all_deps } + + it { + is_expected.to contain_systemd__dropin_file('foobar.conf'). + with_unit('special.socket'). + with_content(%r{^\[Socket\]$}). + with_content(%r{^ListenMessageQueue=/panic$}) + } end end diff --git a/spec/defines/manage_unit_spec.rb b/spec/defines/manage_unit_spec.rb index fb404b37..9828ce35 100644 --- a/spec/defines/manage_unit_spec.rb +++ b/spec/defines/manage_unit_spec.rb @@ -3,249 +3,244 @@ require 'spec_helper' describe 'systemd::manage_unit' do - context 'supported operating systems' do - on_supported_os.each do |os, facts| - context "on #{os}" do - let(:facts) { facts } - - let(:title) { 'foobar.service' } - - context 'with an arrayed description and simple parameters set' do - let(:params) do - { - unit_entry: { - Description: ['My great service', 'has two lines of description'], - DefaultDependencies: true, - }, - service_entry: { - Type: 'oneshot', - ExecStart: '/usr/bin/doit.sh', - SyslogIdentifier: 'doit-backwards.sh', - Environment: ['bla=foo', 'foo=bla'], - IOReadIOPSMax: ['/dev/afs', '1K'], - }, - install_entry: { - WantedBy: 'multi-user.target', - } - } - end - - it { is_expected.to compile.with_all_deps } - - it { - is_expected.to contain_systemd__unit_file('foobar.service'). - with_content(%r{^\[Unit\]$}). - with_content(%r{^DefaultDependencies=true$}). - with_content(%r{^\[Service\]$}). - with_content(%r{^SyslogIdentifier=doit-backwards\.sh$}). - with_content(%r{^Environment=bla=foo$}). - with_content(%r{^Environment=foo=bla$}). - with_content(%r{^\[Install\]$}). - with_content(%r{^Description=My great service$}). - with_content(%r{^Description=has two lines of description$}). - with_content(%r{^Type=oneshot$}). - with_content(%r{^IOReadIOPSMax=/dev/afs 1K$}). - without_content(%r{^\[Slice\]$}) - } - - context 'with no service_entry' do - let(:params) do - { - ensure: 'present', - } - end - - it { is_expected.to compile.and_raise_error(%r{service_entry is required for service units}) } - - context 'with ensure absent' do - let(:params) do - super().merge(ensure: 'absent') - end - - it { is_expected.to contain_systemd__unit_file('foobar.service').with_ensure('absent') } - end - end - - context 'with a timer entry' do - let(:params) do - super().merge(timer_entry: { 'OnCalendar' => 'something' }) - end - - it { is_expected.to compile.and_raise_error(%r{timer_entry is only valid for timer units}) } - end - - context 'with a slice entry' do - let(:params) do - super().merge(slice_entry: { 'IOWeight' => 100 }) - end - - it { is_expected.to compile.and_raise_error(%r{slice_entry is only valid for slice units}) } - end - - context 'with a path entry' do - let(:params) do - super().merge(path_entry: { 'PathExists' => '/etc/passwd' }) - end - - it { is_expected.to compile.and_raise_error(%r{path_entry is only valid for path units}) } - end - - context 'with a socket entry' do - let(:params) do - super().merge(socket_entry: { 'ListenStream' => '1337' }) - end - - it { is_expected.to compile.and_raise_error(%r{socket_entry is only valid for socket units}) } - end - end + _, facts = on_supported_os.first + let(:facts) { facts } + + let(:title) { 'foobar.service' } + + context 'with an arrayed description and simple parameters set' do + let(:params) do + { + unit_entry: { + Description: ['My great service', 'has two lines of description'], + DefaultDependencies: true, + }, + service_entry: { + Type: 'oneshot', + ExecStart: '/usr/bin/doit.sh', + SyslogIdentifier: 'doit-backwards.sh', + Environment: ['bla=foo', 'foo=bla'], + IOReadIOPSMax: ['/dev/afs', '1K'], + }, + install_entry: { + WantedBy: 'multi-user.target', + } + } + end - context 'on a timer' do - let(:title) { 'winter.timer' } - - let(:params) do - { - unit_entry: { - Description: 'Winter is coming', - }, - timer_entry: { - 'OnActiveSec' => '5min', - 'OnBootSec' => ['', '1min 5s'], - 'OnStartUpSec' => 10, - 'OnUnitActiveSec' => '5s', - 'OnUnitInactiveSec' => ['', 10], - 'OnCalendar' => 'soon', - 'AccuracySec' => '24h', - 'RandomizedDelaySec' => '4min 20s', - 'FixedRandomDelay' => true, - 'OnClockChange' => false, - 'OnTimezoneChange' => true, - 'Unit' => 'summer.service', - } - } - end - - it { is_expected.to compile.with_all_deps } - - it { - is_expected.to contain_systemd__unit_file('winter.timer'). - with_content(%r{^\[Timer\]$}). - with_content(%r{^OnActiveSec=5min$}). - with_content(%r{^OnBootSec=$}). - with_content(%r{^OnBootSec=1min 5s$}). - with_content(%r{^OnStartUpSec=10$}). - with_content(%r{^OnUnitActiveSec=5s$}). - with_content(%r{^OnUnitInactiveSec=$}). - with_content(%r{^OnUnitInactiveSec=10$}). - with_content(%r{^OnCalendar=soon$}). - with_content(%r{^AccuracySec=24h$}). - with_content(%r{^RandomizedDelaySec=4min 20s$}). - with_content(%r{^FixedRandomDelay=true$}). - with_content(%r{^OnClockChange=false$}). - with_content(%r{^OnTimezoneChange=true$}). - with_content(%r{^Unit=summer.service$}) - } - end + it { is_expected.to compile.with_all_deps } + + it { + is_expected.to contain_systemd__unit_file('foobar.service'). + with_content(%r{^\[Unit\]$}). + with_content(%r{^DefaultDependencies=true$}). + with_content(%r{^\[Service\]$}). + with_content(%r{^SyslogIdentifier=doit-backwards\.sh$}). + with_content(%r{^Environment=bla=foo$}). + with_content(%r{^Environment=foo=bla$}). + with_content(%r{^\[Install\]$}). + with_content(%r{^Description=My great service$}). + with_content(%r{^Description=has two lines of description$}). + with_content(%r{^Type=oneshot$}). + with_content(%r{^IOReadIOPSMax=/dev/afs 1K$}). + without_content(%r{^\[Slice\]$}) + } + + context 'with no service_entry' do + let(:params) do + { + ensure: 'present', + } + end - context 'on a socket unit' do - let(:title) { 'arcd.socket' } - let(:params) do - { - unit_entry: { - Description: 'A crazy socket', - }, - socket_entry: { - 'ListenStream' => 4241, - 'Accept' => true, - 'BindIPv6Only' => 'both' - } - } - end - - it { is_expected.to compile.with_all_deps } - - it { - is_expected.to contain_systemd__unit_file('arcd.socket'). - with_content(%r{^\[Socket\]$}). - with_content(%r{^ListenStream=4241$}). - with_content(%r{^Accept=true$}). - with_content(%r{^BindIPv6Only=both$}) - } - end + it { is_expected.to compile.and_raise_error(%r{service_entry is required for service units}) } - context 'on a slice unit' do - let(:title) { 'myslice.slice' } - let(:params) do - { - unit_entry: { - Description: 'A crazy slice', - }, - slice_entry: { - 'MemoryMax' => '10G', - 'IOAccounting' => true, - 'IOWriteIOPSMax' => [ - ['/dev/gluster', 20], - ['/dev/afs', '50K'], - ], - }, - } - end - - it { is_expected.to compile.with_all_deps } - - it { - is_expected.to contain_systemd__unit_file('myslice.slice'). - with_content(%r{^\[Slice\]$}). - with_content(%r{^MemoryMax=10G$}). - with_content(%r{^IOAccounting=true$}). - with_content(%r{^IOWriteIOPSMax=/dev/gluster 20$}). - with_content(%r{^IOWriteIOPSMax=/dev/afs 50K$}). - without_content(%r{^\[Service\]$}) - } + context 'with ensure absent' do + let(:params) do + super().merge(ensure: 'absent') end - context 'on a path unit' do - let(:title) { 'etc-passwd.path' } - - let(:params) do - { - unit_entry: { - Description: 'Watch that passwd like a hawk', - }, - path_entry: { - 'PathExists' => '/etc/passwd', - 'PathExistsGlob' => '/etc/krb5.conf.d/*.conf', - 'PathChanged' => '', - 'PathModified' => ['', '/etc/httpd/conf.d/*.conf'], - 'DirectoryNotEmpty' => '/tmp', - 'Unit' => 'my.service', - 'MakeDirectory' => true, - 'DirectoryMode' => '0777', - 'TriggerLimitIntervalSec' => '10s', - 'TriggerLimitBurst' => 100, - } - } - end - - it { is_expected.to compile.with_all_deps } - - it { - is_expected.to contain_systemd__unit_file('etc-passwd.path'). - without_content(%r{^\[Service\]$}). - with_content(%r{^\[Path\]$}). - with_content(%r{^PathExists=/etc/passwd$}). - with_content(%r{^PathExistsGlob=/etc/krb5.conf.d/\*.conf$}). - with_content(%r{^PathChanged=$}). - with_content(%r{^PathModified=$}). - with_content(%r{^PathModified=/etc/httpd/conf.d/\*.conf$}). - with_content(%r{^DirectoryNotEmpty=/tmp$}). - with_content(%r{^Unit=my.service$}). - with_content(%r{^MakeDirectory=true$}). - with_content(%r{^DirectoryMode=0777$}). - with_content(%r{^TriggerLimitIntervalSec=10s$}). - with_content(%r{^TriggerLimitBurst=100$}) - } - end + it { is_expected.to contain_systemd__unit_file('foobar.service').with_ensure('absent') } + end + end + + context 'with a timer entry' do + let(:params) do + super().merge(timer_entry: { 'OnCalendar' => 'something' }) + end + + it { is_expected.to compile.and_raise_error(%r{timer_entry is only valid for timer units}) } + end + + context 'with a slice entry' do + let(:params) do + super().merge(slice_entry: { 'IOWeight' => 100 }) + end + + it { is_expected.to compile.and_raise_error(%r{slice_entry is only valid for slice units}) } + end + + context 'with a path entry' do + let(:params) do + super().merge(path_entry: { 'PathExists' => '/etc/passwd' }) end + + it { is_expected.to compile.and_raise_error(%r{path_entry is only valid for path units}) } end + + context 'with a socket entry' do + let(:params) do + super().merge(socket_entry: { 'ListenStream' => '1337' }) + end + + it { is_expected.to compile.and_raise_error(%r{socket_entry is only valid for socket units}) } + end + end + + context 'on a timer' do + let(:title) { 'winter.timer' } + + let(:params) do + { + unit_entry: { + Description: 'Winter is coming', + }, + timer_entry: { + 'OnActiveSec' => '5min', + 'OnBootSec' => ['', '1min 5s'], + 'OnStartUpSec' => 10, + 'OnUnitActiveSec' => '5s', + 'OnUnitInactiveSec' => ['', 10], + 'OnCalendar' => 'soon', + 'AccuracySec' => '24h', + 'RandomizedDelaySec' => '4min 20s', + 'FixedRandomDelay' => true, + 'OnClockChange' => false, + 'OnTimezoneChange' => true, + 'Unit' => 'summer.service', + } + } + end + + it { is_expected.to compile.with_all_deps } + + it { + is_expected.to contain_systemd__unit_file('winter.timer'). + with_content(%r{^\[Timer\]$}). + with_content(%r{^OnActiveSec=5min$}). + with_content(%r{^OnBootSec=$}). + with_content(%r{^OnBootSec=1min 5s$}). + with_content(%r{^OnStartUpSec=10$}). + with_content(%r{^OnUnitActiveSec=5s$}). + with_content(%r{^OnUnitInactiveSec=$}). + with_content(%r{^OnUnitInactiveSec=10$}). + with_content(%r{^OnCalendar=soon$}). + with_content(%r{^AccuracySec=24h$}). + with_content(%r{^RandomizedDelaySec=4min 20s$}). + with_content(%r{^FixedRandomDelay=true$}). + with_content(%r{^OnClockChange=false$}). + with_content(%r{^OnTimezoneChange=true$}). + with_content(%r{^Unit=summer.service$}) + } + end + + context 'on a socket unit' do + let(:title) { 'arcd.socket' } + let(:params) do + { + unit_entry: { + Description: 'A crazy socket', + }, + socket_entry: { + 'ListenStream' => 4241, + 'Accept' => true, + 'BindIPv6Only' => 'both' + } + } + end + + it { is_expected.to compile.with_all_deps } + + it { + is_expected.to contain_systemd__unit_file('arcd.socket'). + with_content(%r{^\[Socket\]$}). + with_content(%r{^ListenStream=4241$}). + with_content(%r{^Accept=true$}). + with_content(%r{^BindIPv6Only=both$}) + } + end + + context 'on a slice unit' do + let(:title) { 'myslice.slice' } + let(:params) do + { + unit_entry: { + Description: 'A crazy slice', + }, + slice_entry: { + 'MemoryMax' => '10G', + 'IOAccounting' => true, + 'IOWriteIOPSMax' => [ + ['/dev/gluster', 20], + ['/dev/afs', '50K'], + ], + }, + } + end + + it { is_expected.to compile.with_all_deps } + + it { + is_expected.to contain_systemd__unit_file('myslice.slice'). + with_content(%r{^\[Slice\]$}). + with_content(%r{^MemoryMax=10G$}). + with_content(%r{^IOAccounting=true$}). + with_content(%r{^IOWriteIOPSMax=/dev/gluster 20$}). + with_content(%r{^IOWriteIOPSMax=/dev/afs 50K$}). + without_content(%r{^\[Service\]$}) + } + end + + context 'on a path unit' do + let(:title) { 'etc-passwd.path' } + + let(:params) do + { + unit_entry: { + Description: 'Watch that passwd like a hawk', + }, + path_entry: { + 'PathExists' => '/etc/passwd', + 'PathExistsGlob' => '/etc/krb5.conf.d/*.conf', + 'PathChanged' => '', + 'PathModified' => ['', '/etc/httpd/conf.d/*.conf'], + 'DirectoryNotEmpty' => '/tmp', + 'Unit' => 'my.service', + 'MakeDirectory' => true, + 'DirectoryMode' => '0777', + 'TriggerLimitIntervalSec' => '10s', + 'TriggerLimitBurst' => 100, + } + } + end + + it { is_expected.to compile.with_all_deps } + + it { + is_expected.to contain_systemd__unit_file('etc-passwd.path'). + without_content(%r{^\[Service\]$}). + with_content(%r{^\[Path\]$}). + with_content(%r{^PathExists=/etc/passwd$}). + with_content(%r{^PathExistsGlob=/etc/krb5.conf.d/\*.conf$}). + with_content(%r{^PathChanged=$}). + with_content(%r{^PathModified=$}). + with_content(%r{^PathModified=/etc/httpd/conf.d/\*.conf$}). + with_content(%r{^DirectoryNotEmpty=/tmp$}). + with_content(%r{^Unit=my.service$}). + with_content(%r{^MakeDirectory=true$}). + with_content(%r{^DirectoryMode=0777$}). + with_content(%r{^TriggerLimitIntervalSec=10s$}). + with_content(%r{^TriggerLimitBurst=100$}) + } end end diff --git a/spec/defines/modules_load_spec.rb b/spec/defines/modules_load_spec.rb index e5c21d0f..311a67c2 100644 --- a/spec/defines/modules_load_spec.rb +++ b/spec/defines/modules_load_spec.rb @@ -3,39 +3,34 @@ require 'spec_helper' describe 'systemd::modules_load' do - context 'supported operating systems' do - on_supported_os.each do |os, os_facts| - context "on #{os}" do - let(:facts) { os_facts } - let(:title) { 'random_module.conf' } - let(:params) { { content: 'random stuff' } } - - it { is_expected.to compile.with_all_deps } - it { is_expected.to contain_systemd__modules_load('random_module.conf') } - - it { is_expected.to contain_file('/etc/modules-load.d/random_module.conf') } - - it { - is_expected.to contain_file('/etc/modules-load.d/random_module.conf').with - - { - ensure: 'file', - content: 'random stuff', - mode: '0444' - } - } - - it { is_expected.to contain_class('systemd::modules_loads') } - it { is_expected.to contain_exec('systemd-modules-load').with_command('systemctl start systemd-modules-load.service') } - - context 'with a bad modules-load name' do - let(:title) { 'test.badtype' } - - it { - is_expected.to compile.and_raise_error(%r{expects a match for Systemd::Dropin}) - } - end - end - end + _, os_facts = on_supported_os.first + let(:facts) { os_facts } + let(:title) { 'random_module.conf' } + let(:params) { { content: 'random stuff' } } + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_systemd__modules_load('random_module.conf') } + + it { is_expected.to contain_file('/etc/modules-load.d/random_module.conf') } + + it { + is_expected.to contain_file('/etc/modules-load.d/random_module.conf').with + + { + ensure: 'file', + content: 'random stuff', + mode: '0444' + } + } + + it { is_expected.to contain_class('systemd::modules_loads') } + it { is_expected.to contain_exec('systemd-modules-load').with_command('systemctl start systemd-modules-load.service') } + + context 'with a bad modules-load name' do + let(:title) { 'test.badtype' } + + it { + is_expected.to compile.and_raise_error(%r{expects a match for Systemd::Dropin}) + } end end diff --git a/spec/defines/network_spec.rb b/spec/defines/network_spec.rb index b4f3df45..84b6da69 100644 --- a/spec/defines/network_spec.rb +++ b/spec/defines/network_spec.rb @@ -3,86 +3,81 @@ require 'spec_helper' describe 'systemd::network' do - context 'supported operating systems' do - on_supported_os.each do |os, facts| - context "on #{os}" do - # manage systemd-networkd service - let :pre_condition do - "class { 'systemd': - manage_networkd => true, - }" - end - - let(:facts) { facts } - - let(:title) { 'eth0.network' } - - let(:params) do - { - content: 'random stuff', - restart_service: true, - } - end - - it { is_expected.to compile.with_all_deps } - - it { - expect(subject).to create_file("/etc/systemd/network/#{title}").with( - ensure: 'file', - content: %r{#{params[:content]}}, - mode: '0444' - ) - } - - it { is_expected.to create_file("/etc/systemd/network/#{title}").that_notifies('Service[systemd-networkd]') } - - context 'with group => systemd-network, mode => 0640 and show_diff => false' do - let(:title) { 'wg0.netdev' } - - let(:params) do - { - content: 'secret string', - group: 'systemd-network', - mode: '0640', - show_diff: false, - restart_service: true, - } - end - - it { is_expected.to compile.with_all_deps } - - it { - expect(subject).to create_file("/etc/systemd/network/#{title}").with( - ensure: 'file', - content: %r{#{params[:content]}}, - group: 'systemd-network', - mode: '0640', - show_diff: false - ) - } - - it { is_expected.to create_file("/etc/systemd/network/#{title}").that_notifies('Service[systemd-networkd]') } - end - - context 'without content and without source' do - let :params do - {} - end - - it { is_expected.to compile.and_raise_error(%r{Either content or source must be set}) } - end - - context 'with content and source' do - let :params do - { - content: 'bla', - source: 'foo' - } - end - - it { is_expected.to compile.and_raise_error(%r{Either content or source must be set but not both}) } - end - end + _, facts = on_supported_os.first + # manage systemd-networkd service + let :pre_condition do + "class { 'systemd': + manage_networkd => true, + }" + end + + let(:facts) { facts } + + let(:title) { 'eth0.network' } + + let(:params) do + { + content: 'random stuff', + restart_service: true, + } + end + + it { is_expected.to compile.with_all_deps } + + it { + expect(subject).to create_file("/etc/systemd/network/#{title}").with( + ensure: 'file', + content: %r{#{params[:content]}}, + mode: '0444' + ) + } + + it { is_expected.to create_file("/etc/systemd/network/#{title}").that_notifies('Service[systemd-networkd]') } + + context 'with group => systemd-network, mode => 0640 and show_diff => false' do + let(:title) { 'wg0.netdev' } + + let(:params) do + { + content: 'secret string', + group: 'systemd-network', + mode: '0640', + show_diff: false, + restart_service: true, + } end + + it { is_expected.to compile.with_all_deps } + + it { + expect(subject).to create_file("/etc/systemd/network/#{title}").with( + ensure: 'file', + content: %r{#{params[:content]}}, + group: 'systemd-network', + mode: '0640', + show_diff: false + ) + } + + it { is_expected.to create_file("/etc/systemd/network/#{title}").that_notifies('Service[systemd-networkd]') } + end + + context 'without content and without source' do + let :params do + {} + end + + it { is_expected.to compile.and_raise_error(%r{Either content or source must be set}) } + end + + context 'with content and source' do + let :params do + { + content: 'bla', + source: 'foo' + } + end + + it { is_expected.to compile.and_raise_error(%r{Either content or source must be set but not both}) } end end diff --git a/spec/defines/service_limits_spec.rb b/spec/defines/service_limits_spec.rb index 52dd5f69..039c6310 100644 --- a/spec/defines/service_limits_spec.rb +++ b/spec/defines/service_limits_spec.rb @@ -3,95 +3,90 @@ require 'spec_helper' describe 'systemd::service_limits' do - context 'supported operating systems' do - on_supported_os.each do |os, facts| - context "on #{os}" do - let(:facts) { facts } + _, facts = on_supported_os.first + let(:facts) { facts } - let(:title) { 'test.service' } + let(:title) { 'test.service' } - # Whole type is deprecated but check it still works. - before do - Puppet.settings[:strict] = :warning - end + # Whole type is deprecated but check it still works. + before do + Puppet.settings[:strict] = :warning + end - describe 'with limits and present' do - let(:params) do - { - limits: { - 'LimitCPU' => '10m', - 'LimitFSIZE' => 'infinity', - 'LimitDATA' => '10K', - 'LimitNOFILE' => '20:infinity', - 'LimitNICE' => '-10', - 'LimitRTPRIO' => 50, - 'MemorySwapMax' => '0', - 'CPUQuota' => '125%', - 'IODeviceWeight' => [ - { '/dev/weight' => 10 }, - { '/dev/weight2' => 20 }, - ], - 'IOReadBandwidthMax' => [ - { '/bw/max' => '10K' }, - ], - }, - } - end + describe 'with limits and present' do + let(:params) do + { + limits: { + 'LimitCPU' => '10m', + 'LimitFSIZE' => 'infinity', + 'LimitDATA' => '10K', + 'LimitNOFILE' => '20:infinity', + 'LimitNICE' => '-10', + 'LimitRTPRIO' => 50, + 'MemorySwapMax' => '0', + 'CPUQuota' => '125%', + 'IODeviceWeight' => [ + { '/dev/weight' => 10 }, + { '/dev/weight2' => 20 }, + ], + 'IOReadBandwidthMax' => [ + { '/bw/max' => '10K' }, + ], + }, + } + end - it { is_expected.to compile.with_all_deps } - it { is_expected.to contain_systemd__manage_dropin("#{title}-90-limits.conf").with_ensure('present') } + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_systemd__manage_dropin("#{title}-90-limits.conf").with_ensure('present') } - it { - expect(subject).to create_file("/etc/systemd/system/#{title}.d/90-limits.conf"). - with(ensure: 'file', mode: '0444'). - with_content(%r{LimitCPU=10m}). - with_content(%r{LimitFSIZE=infinity}). - with_content(%r{LimitDATA=10K}). - with_content(%r{LimitNOFILE=20:infinity}). - with_content(%r{LimitNICE=-10}). - with_content(%r{LimitRTPRIO=50}). - with_content(%r{MemorySwapMax=0}). - with_content(%r{CPUQuota=125%}). - with_content(%r{IODeviceWeight=/dev/weight 10}). - with_content(%r{IODeviceWeight=/dev/weight2 20}). - with_content(%r{IOReadBandwidthMax=/bw/max 10K}) - } + it { + expect(subject).to create_file("/etc/systemd/system/#{title}.d/90-limits.conf"). + with(ensure: 'file', mode: '0444'). + with_content(%r{LimitCPU=10m}). + with_content(%r{LimitFSIZE=infinity}). + with_content(%r{LimitDATA=10K}). + with_content(%r{LimitNOFILE=20:infinity}). + with_content(%r{LimitNICE=-10}). + with_content(%r{LimitRTPRIO=50}). + with_content(%r{MemorySwapMax=0}). + with_content(%r{CPUQuota=125%}). + with_content(%r{IODeviceWeight=/dev/weight 10}). + with_content(%r{IODeviceWeight=/dev/weight2 20}). + with_content(%r{IOReadBandwidthMax=/bw/max 10K}) + } - describe 'with service managed' do - let(:pre_condition) do - <<-PUPPET - service { 'test': - } - PUPPET - end + describe 'with service managed' do + let(:pre_condition) do + <<-PUPPET + service { 'test': + } + PUPPET + end - it { is_expected.to compile.with_all_deps } + it { is_expected.to compile.with_all_deps } - it do - is_expected.to create_file("/etc/systemd/system/#{title}.d/90-limits.conf"). - that_notifies('Service[test]') - end - end - end + it do + is_expected.to create_file("/etc/systemd/system/#{title}.d/90-limits.conf"). + that_notifies('Service[test]') + end + end + end - describe 'source specified' do - let(:params) { { source: 'puppet:///mine/content' } } + describe 'source specified' do + let(:params) { { source: 'puppet:///mine/content' } } - it { is_expected.to compile.with_all_deps } - it { is_expected.to contain_systemd__dropin_file("#{title}-90-limits.conf").with_ensure('present') } - end + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_systemd__dropin_file("#{title}-90-limits.conf").with_ensure('present') } + end - describe 'ensured absent' do - let(:params) { { ensure: 'absent' } } + describe 'ensured absent' do + let(:params) { { ensure: 'absent' } } - it { is_expected.to compile.with_all_deps } + it { is_expected.to compile.with_all_deps } - it do - expect(subject).to create_file("/etc/systemd/system/#{title}.d/90-limits.conf"). - with_ensure('absent') - end - end - end + it do + expect(subject).to create_file("/etc/systemd/system/#{title}.d/90-limits.conf"). + with_ensure('absent') end end end diff --git a/spec/defines/timer_spec.rb b/spec/defines/timer_spec.rb index 9e24efb1..73667187 100644 --- a/spec/defines/timer_spec.rb +++ b/spec/defines/timer_spec.rb @@ -3,99 +3,94 @@ require 'spec_helper' describe 'systemd::timer' do - context 'supported operating systems' do - on_supported_os.each do |os, facts| - context "on #{os}" do - let(:facts) { facts } - - let(:title) { 'foobar.timer' } - - context('with timer_content and service_content') do - let(:params) do - { - timer_content: "[Timer]\nOnCalendar=weekly", - service_content: "[Service]\nExecStart=/bin/touch /tmp/foobar", - } - end - - it { is_expected.to compile.with_all_deps } - - it { - expect(subject).to contain_systemd__unit_file('foobar.timer').with( - content: "[Timer]\nOnCalendar=weekly" - ) - } - - it { - expect(subject).to contain_systemd__unit_file('foobar.service').with( - content: "[Service]\nExecStart=/bin/touch /tmp/foobar" - ).that_comes_before('Systemd::Unit_file[foobar.timer]') - } - end - - context('with timer_source and service_source') do - let(:params) do - { - timer_source: 'puppet:///timer', - service_source: 'puppet:///source', - } - end - - it { is_expected.to compile.with_all_deps } - it { is_expected.to contain_systemd__unit_file('foobar.timer').with_source('puppet:///timer') } - it { is_expected.to contain_systemd__unit_file('foobar.service').with_source('puppet:///source').that_comes_before('Systemd::Unit_file[foobar.timer]') } - end - - context('with timer_source only set') do - let(:params) do - { - timer_source: 'puppet:///timer', - } - end - - it { is_expected.to compile.with_all_deps } - it { is_expected.to contain_systemd__unit_file('foobar.timer').with_source('puppet:///timer') } - it { is_expected.not_to contain_systemd__unit_file('foobar.service') } - end - - context 'with service_unit specified' do - let(:params) do - { - timer_content: "[Timer]\nOnCalendar=weekly", - service_content: "[Service]\nExecStart=/bin/touch /tmp/foobar", - service_unit: 'gamma.service', - } - end - - it { is_expected.to contain_systemd__unit_file('foobar.timer').with_content("[Timer]\nOnCalendar=weekly") } - - it { is_expected.to contain_systemd__unit_file('gamma.service').with_content("[Service]\nExecStart=/bin/touch /tmp/foobar").that_comes_before('Systemd::Unit_file[foobar.timer]') } - end - - context 'with timer activated service' do - let(:params) do - { - active: true, - enable: true, - timer_content: "[Timer]\nOnCalendar=hourly", - service_content: "[Service]\nExecStart=/bin/echo timer-fired", - } - end - - it { is_expected.to contain_systemd__unit_file('foobar.timer').with_content("[Timer]\nOnCalendar=hourly") } - it { is_expected.to contain_systemd__unit_file('foobar.service').with_content("[Service]\nExecStart=/bin/echo timer-fired").that_comes_before('Systemd::Unit_file[foobar.timer]') } - end - - context 'with a bad timer name' do - let(:title) { 'foobar' } - - it { - expect do - expect(subject).to compile.with_all_deps - end.to raise_error(%r{expects a match for}) - } - end - end + _, facts = on_supported_os.first + let(:facts) { facts } + + let(:title) { 'foobar.timer' } + + context('with timer_content and service_content') do + let(:params) do + { + timer_content: "[Timer]\nOnCalendar=weekly", + service_content: "[Service]\nExecStart=/bin/touch /tmp/foobar", + } end + + it { is_expected.to compile.with_all_deps } + + it { + expect(subject).to contain_systemd__unit_file('foobar.timer').with( + content: "[Timer]\nOnCalendar=weekly" + ) + } + + it { + expect(subject).to contain_systemd__unit_file('foobar.service').with( + content: "[Service]\nExecStart=/bin/touch /tmp/foobar" + ).that_comes_before('Systemd::Unit_file[foobar.timer]') + } + end + + context('with timer_source and service_source') do + let(:params) do + { + timer_source: 'puppet:///timer', + service_source: 'puppet:///source', + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_systemd__unit_file('foobar.timer').with_source('puppet:///timer') } + it { is_expected.to contain_systemd__unit_file('foobar.service').with_source('puppet:///source').that_comes_before('Systemd::Unit_file[foobar.timer]') } + end + + context('with timer_source only set') do + let(:params) do + { + timer_source: 'puppet:///timer', + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_systemd__unit_file('foobar.timer').with_source('puppet:///timer') } + it { is_expected.not_to contain_systemd__unit_file('foobar.service') } + end + + context 'with service_unit specified' do + let(:params) do + { + timer_content: "[Timer]\nOnCalendar=weekly", + service_content: "[Service]\nExecStart=/bin/touch /tmp/foobar", + service_unit: 'gamma.service', + } + end + + it { is_expected.to contain_systemd__unit_file('foobar.timer').with_content("[Timer]\nOnCalendar=weekly") } + + it { is_expected.to contain_systemd__unit_file('gamma.service').with_content("[Service]\nExecStart=/bin/touch /tmp/foobar").that_comes_before('Systemd::Unit_file[foobar.timer]') } + end + + context 'with timer activated service' do + let(:params) do + { + active: true, + enable: true, + timer_content: "[Timer]\nOnCalendar=hourly", + service_content: "[Service]\nExecStart=/bin/echo timer-fired", + } + end + + it { is_expected.to contain_systemd__unit_file('foobar.timer').with_content("[Timer]\nOnCalendar=hourly") } + it { is_expected.to contain_systemd__unit_file('foobar.service').with_content("[Service]\nExecStart=/bin/echo timer-fired").that_comes_before('Systemd::Unit_file[foobar.timer]') } + end + + context 'with a bad timer name' do + let(:title) { 'foobar' } + + it { + expect do + expect(subject).to compile.with_all_deps + end.to raise_error(%r{expects a match for}) + } end end diff --git a/spec/defines/timer_wrapper_spec.rb b/spec/defines/timer_wrapper_spec.rb index 2054c67f..fa1f43d5 100644 --- a/spec/defines/timer_wrapper_spec.rb +++ b/spec/defines/timer_wrapper_spec.rb @@ -3,169 +3,164 @@ require 'spec_helper' describe 'systemd::timer_wrapper' do - context 'supported operating systems' do - on_supported_os.each do |os, facts| - context "on #{os}" do - let(:facts) { facts } - let(:title) { 'timer_name' } - - context 'simple usage' do - let :params do - { - ensure: 'present', - on_calendar: '*:0/10', - command: '/bin/date', - user: 'root', - } - end - - it do - is_expected.to compile.with_all_deps - is_expected.to contain_file("/etc/systemd/system/#{title}.service"). - with_content(%r{# Deployed with puppet}). - with_content(%r{Type=oneshot}). - with_content(%r{ExecStart=/bin/date}). - with_content(%r{Type=oneshot}). - with_content(%r{User=root}) - is_expected.to contain_file("/etc/systemd/system/#{title}.timer"). - with_content(%r{OnCalendar=\*:0/10}). - with_content(%r{WantedBy=timers.target}) - is_expected.to contain_Systemd__Unit_file("#{title}.service"). - that_comes_before("Systemd::Unit_file[#{title}.timer]") - is_expected.to contain_Systemd__Unit_file("#{title}.service"). - that_comes_before("Systemd::Unit_file[#{title}.timer]") - is_expected.to contain_Exec("systemd-#{title}.service-systemctl-daemon-reload") - is_expected.to contain_Exec("systemd-#{title}.timer-systemctl-daemon-reload") - is_expected.to contain_Service("#{title}.timer") - is_expected.to contain_Systemd__Daemon_reload("#{title}.service") - is_expected.to contain_Systemd__Daemon_reload("#{title}.timer") - is_expected.to contain_Systemd__Manage_unit("#{title}.service") - is_expected.to contain_Systemd__Manage_unit("#{title}.timer") - is_expected.to contain_Systemd__Unit_file("#{title}.timer") - end - end - - context 'failue when not passing calendar spec' do - let :params do - { - ensure: 'present', - command: '/bin/date', - user: 'root', - } - end - - it do - is_expected.to compile.and_raise_error(%r{At least one of on_active_sec}) - end - end - - context 'with / in title' do - let :title do - 't/i/t/l/e' - end - let :params do - { - ensure: 'present', - on_calendar: '*:0/10', - command: '/bin/true', - user: 'root', - } - end - - it { - is_expected.to compile.and_raise_error(%r{expects a match for Systemd::Unit = Pattern}) - } - end - - context 'ensure absent' do - let :params do - { - ensure: 'absent', - } - end - - it { - is_expected.to contain_Systemd__Manage_unit("#{title}.timer"). - with_ensure('absent') - is_expected.to contain_Systemd__Manage_unit("#{title}.service"). - with_ensure('absent') - is_expected.to contain_Service("#{title}.timer"). - that_comes_before("Systemd::Unit_file[#{title}.timer]"). - with_ensure(false) - is_expected.to contain_Systemd__Unit_file("#{title}.timer"). - that_comes_before("Systemd::Unit_file[#{title}.service]"). - with_ensure('absent') - } - end - - context 'applies service_overrides' do - let :params do - { - ensure: 'present', - command: 'date', - service_overrides: { 'Group' => 'bob' }, - on_boot_sec: 100, - user: 'root', - } - end - - it { - is_expected.to contain_file("/etc/systemd/system/#{title}.service"). - with_content(%r{Group=bob}) - } - end - - context 'applies service_unit_overrides' do - let :params do - { - ensure: 'present', - command: 'date', - service_unit_overrides: { 'Wants' => 'network-online.target' }, - on_boot_sec: 100, - user: 'root', - } - end - - it { - is_expected.to contain_file("/etc/systemd/system/#{title}.service"). - with_content(%r{Wants=network-online.target}) - } - end - - context 'applies timer_overrides' do - let :params do - { - ensure: 'present', - command: 'date', - timer_overrides: { 'OnBootSec' => '200' }, - on_boot_sec: 100, - user: 'root', - } - end - - it { - is_expected.to contain_file("/etc/systemd/system/#{title}.timer"). - with_content(%r{OnBootSec=200}) - } - end - - context 'applies timer_unit_overrides' do - let :params do - { - ensure: 'present', - command: 'date', - timer_unit_overrides: { 'Wants' => 'network-online.target' }, - on_boot_sec: 100, - user: 'root', - } - end - - it { - is_expected.to contain_file("/etc/systemd/system/#{title}.timer"). - with_content(%r{Wants=network-online.target}) - } - end - end + _, facts = on_supported_os.first + let(:facts) { facts } + let(:title) { 'timer_name' } + + context 'simple usage' do + let :params do + { + ensure: 'present', + on_calendar: '*:0/10', + command: '/bin/date', + user: 'root', + } end + + it do + is_expected.to compile.with_all_deps + is_expected.to contain_file("/etc/systemd/system/#{title}.service"). + with_content(%r{# Deployed with puppet}). + with_content(%r{Type=oneshot}). + with_content(%r{ExecStart=/bin/date}). + with_content(%r{Type=oneshot}). + with_content(%r{User=root}) + is_expected.to contain_file("/etc/systemd/system/#{title}.timer"). + with_content(%r{OnCalendar=\*:0/10}). + with_content(%r{WantedBy=timers.target}) + is_expected.to contain_Systemd__Unit_file("#{title}.service"). + that_comes_before("Systemd::Unit_file[#{title}.timer]") + is_expected.to contain_Systemd__Unit_file("#{title}.service"). + that_comes_before("Systemd::Unit_file[#{title}.timer]") + is_expected.to contain_Exec("systemd-#{title}.service-systemctl-daemon-reload") + is_expected.to contain_Exec("systemd-#{title}.timer-systemctl-daemon-reload") + is_expected.to contain_Service("#{title}.timer") + is_expected.to contain_Systemd__Daemon_reload("#{title}.service") + is_expected.to contain_Systemd__Daemon_reload("#{title}.timer") + is_expected.to contain_Systemd__Manage_unit("#{title}.service") + is_expected.to contain_Systemd__Manage_unit("#{title}.timer") + is_expected.to contain_Systemd__Unit_file("#{title}.timer") + end + end + + context 'failue when not passing calendar spec' do + let :params do + { + ensure: 'present', + command: '/bin/date', + user: 'root', + } + end + + it do + is_expected.to compile.and_raise_error(%r{At least one of on_active_sec}) + end + end + + context 'with / in title' do + let :title do + 't/i/t/l/e' + end + let :params do + { + ensure: 'present', + on_calendar: '*:0/10', + command: '/bin/true', + user: 'root', + } + end + + it { + is_expected.to compile.and_raise_error(%r{expects a match for Systemd::Unit = Pattern}) + } + end + + context 'ensure absent' do + let :params do + { + ensure: 'absent', + } + end + + it { + is_expected.to contain_Systemd__Manage_unit("#{title}.timer"). + with_ensure('absent') + is_expected.to contain_Systemd__Manage_unit("#{title}.service"). + with_ensure('absent') + is_expected.to contain_Service("#{title}.timer"). + that_comes_before("Systemd::Unit_file[#{title}.timer]"). + with_ensure(false) + is_expected.to contain_Systemd__Unit_file("#{title}.timer"). + that_comes_before("Systemd::Unit_file[#{title}.service]"). + with_ensure('absent') + } + end + + context 'applies service_overrides' do + let :params do + { + ensure: 'present', + command: 'date', + service_overrides: { 'Group' => 'bob' }, + on_boot_sec: 100, + user: 'root', + } + end + + it { + is_expected.to contain_file("/etc/systemd/system/#{title}.service"). + with_content(%r{Group=bob}) + } + end + + context 'applies service_unit_overrides' do + let :params do + { + ensure: 'present', + command: 'date', + service_unit_overrides: { 'Wants' => 'network-online.target' }, + on_boot_sec: 100, + user: 'root', + } + end + + it { + is_expected.to contain_file("/etc/systemd/system/#{title}.service"). + with_content(%r{Wants=network-online.target}) + } + end + + context 'applies timer_overrides' do + let :params do + { + ensure: 'present', + command: 'date', + timer_overrides: { 'OnBootSec' => '200' }, + on_boot_sec: 100, + user: 'root', + } + end + + it { + is_expected.to contain_file("/etc/systemd/system/#{title}.timer"). + with_content(%r{OnBootSec=200}) + } + end + + context 'applies timer_unit_overrides' do + let :params do + { + ensure: 'present', + command: 'date', + timer_unit_overrides: { 'Wants' => 'network-online.target' }, + on_boot_sec: 100, + user: 'root', + } + end + + it { + is_expected.to contain_file("/etc/systemd/system/#{title}.timer"). + with_content(%r{Wants=network-online.target}) + } end end diff --git a/spec/defines/tmpfile_spec.rb b/spec/defines/tmpfile_spec.rb index defe125c..7321c6a5 100644 --- a/spec/defines/tmpfile_spec.rb +++ b/spec/defines/tmpfile_spec.rb @@ -3,61 +3,56 @@ require 'spec_helper' describe 'systemd::tmpfile' do - context 'supported operating systems' do - on_supported_os.each do |os, facts| - context "on #{os}" do - let(:facts) { facts } - let(:title) { 'random_tmpfile.conf' } - let(:params) { { content: 'random stuff' } } - - it { is_expected.to compile.with_all_deps } - - it { - expect(subject).to create_file("/etc/tmpfiles.d/#{title}").with( - ensure: 'file', - content: %r{#{params[:content]}}, - mode: '0444' - ) - } - - context 'with a bad tmpfile name' 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 tmpfile name with slash' do - let(:title) { 'test/foo.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 a tmpfile name specified with filename' do - let(:title) { 'test.badtype' } - let(:params) do - { - filename: 'goodname.conf', - content: 'random stuff', - } - end - - it { - expect(subject).to create_file('/etc/tmpfiles.d/goodname.conf').with( - ensure: 'file', - content: %r{#{params[:content]}}, - mode: '0444' - ) - } - end - end + _, facts = on_supported_os.first + let(:facts) { facts } + let(:title) { 'random_tmpfile.conf' } + let(:params) { { content: 'random stuff' } } + + it { is_expected.to compile.with_all_deps } + + it { + expect(subject).to create_file("/etc/tmpfiles.d/#{title}").with( + ensure: 'file', + content: %r{#{params[:content]}}, + mode: '0444' + ) + } + + context 'with a bad tmpfile name' 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 tmpfile name with slash' do + let(:title) { 'test/foo.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 a tmpfile name specified with filename' do + let(:title) { 'test.badtype' } + let(:params) do + { + filename: 'goodname.conf', + content: 'random stuff', + } end + + it { + expect(subject).to create_file('/etc/tmpfiles.d/goodname.conf').with( + ensure: 'file', + content: %r{#{params[:content]}}, + mode: '0444' + ) + } end end diff --git a/spec/defines/udev_rules_spec.rb b/spec/defines/udev_rules_spec.rb index 4b37125c..6c7956a8 100644 --- a/spec/defines/udev_rules_spec.rb +++ b/spec/defines/udev_rules_spec.rb @@ -3,120 +3,115 @@ require 'spec_helper' describe 'systemd::udev::rule' do - context 'supported operating systems' do - on_supported_os.each do |os, facts| - context "on #{os}" do - let(:facts) { facts } - - let(:title) { 'test.rules' } - - let(:pre_condition) do - <<-PUPPET - class { 'systemd': manage_udevd => true, manage_journald => false } - service { 'foo': } - PUPPET - end - - describe 'with all options (one notify)' do - let(:params) do - { - ensure: 'file', - path: '/etc/udev/rules.d', - selinux_ignore_defaults: false, - notify_services: 'Service[systemd-udevd]', - rules: [ - '# I am a comment', - 'ACTION=="add", KERNEL=="sda", RUN+="/bin/raw /dev/raw/raw1 %N"', - 'ACTION=="add", KERNEL=="sdb", RUN+="/bin/raw /dev/raw/raw2 %N"', - ], - } - end - - it { is_expected.to compile.with_all_deps } - - it { - is_expected.to create_file("/etc/udev/rules.d/#{title}"). - with(ensure: 'file', mode: '0444', owner: 'root', group: 'root'). - with_content(%r{^# This file managed by Puppet - DO NOT EDIT$}). - with_content(%r{^# I am a comment$}). - with_content(%r{^ACTION=="add", KERNEL=="sda", RUN\+="/bin/raw /dev/raw/raw1 %N"$}). - with_content(%r{^ACTION=="add", KERNEL=="sdb", RUN\+="/bin/raw /dev/raw/raw2 %N"$}). - that_notifies('Service[systemd-udevd]') - } - - it { is_expected.to contain_class('systemd') } - it { is_expected.to contain_class('systemd::install') } - it { is_expected.to contain_class('systemd::udevd') } - it { is_expected.to contain_service('systemd-udevd') } - it { is_expected.to contain_file('/etc/udev/udev.conf') } - end - - describe 'with all options (array notify)' do - let(:params) do - { - ensure: 'file', - path: '/etc/udev/rules.d', - selinux_ignore_defaults: false, - notify_services: ['Service[systemd-udevd]', 'Service[foo]'], - rules: [ - '# I am a comment', - 'ACTION=="add", KERNEL=="sda", RUN+="/bin/raw /dev/raw/raw1 %N"', - 'ACTION=="add", KERNEL=="sdb", RUN+="/bin/raw /dev/raw/raw2 %N"', - ], - } - end - - it { is_expected.to compile.with_all_deps } - - it { - is_expected.to create_file("/etc/udev/rules.d/#{title}"). - with(ensure: 'file', mode: '0444', owner: 'root', group: 'root'). - with_content(%r{^# This file managed by Puppet - DO NOT EDIT$}). - with_content(%r{^# I am a comment$}). - with_content(%r{^ACTION=="add", KERNEL=="sda", RUN\+="/bin/raw /dev/raw/raw1 %N"$}). - with_content(%r{^ACTION=="add", KERNEL=="sdb", RUN\+="/bin/raw /dev/raw/raw2 %N"$}). - that_notifies('Service[systemd-udevd]'). - that_notifies('Service[foo]') - } - end - - describe 'ensured absent without notify' do - let(:params) { { ensure: 'absent', } } - - it { is_expected.to compile.with_all_deps } - - it { is_expected.to contain_file("/etc/udev/rules.d/#{title}").with_ensure('absent').with_notify([]) } - end - - describe 'ensured absent with automatic reload' do - let(:pre_condition) do - <<-PUPPET - class { 'systemd': manage_udevd => true, manage_journald => false, udev_reload => true } - PUPPET - end - let(:params) do - { - ensure: 'absent', - } - end - - it { is_expected.to compile.with_all_deps } - - it { is_expected.to contain_file("/etc/udev/rules.d/#{title}").with_ensure('absent').with_notify(['Exec[systemd-udev_reload]']) } - end - - describe 'ensured absent with custom notify' do - let(:params) { { ensure: 'absent', notify_services: 'Service[systemd-udevd]', } } - - it { is_expected.to compile.with_all_deps } - - it do - is_expected.to create_file("/etc/udev/rules.d/#{title}"). - with_ensure('absent'). - that_notifies('Service[systemd-udevd]') - end - end - end + _, facts = on_supported_os.first + let(:facts) { facts } + + let(:title) { 'test.rules' } + + let(:pre_condition) do + <<-PUPPET + class { 'systemd': manage_udevd => true, manage_journald => false } + service { 'foo': } + PUPPET + end + + describe 'with all options (one notify)' do + let(:params) do + { + ensure: 'file', + path: '/etc/udev/rules.d', + selinux_ignore_defaults: false, + notify_services: 'Service[systemd-udevd]', + rules: [ + '# I am a comment', + 'ACTION=="add", KERNEL=="sda", RUN+="/bin/raw /dev/raw/raw1 %N"', + 'ACTION=="add", KERNEL=="sdb", RUN+="/bin/raw /dev/raw/raw2 %N"', + ], + } + end + + it { is_expected.to compile.with_all_deps } + + it { + is_expected.to create_file("/etc/udev/rules.d/#{title}"). + with(ensure: 'file', mode: '0444', owner: 'root', group: 'root'). + with_content(%r{^# This file managed by Puppet - DO NOT EDIT$}). + with_content(%r{^# I am a comment$}). + with_content(%r{^ACTION=="add", KERNEL=="sda", RUN\+="/bin/raw /dev/raw/raw1 %N"$}). + with_content(%r{^ACTION=="add", KERNEL=="sdb", RUN\+="/bin/raw /dev/raw/raw2 %N"$}). + that_notifies('Service[systemd-udevd]') + } + + it { is_expected.to contain_class('systemd') } + it { is_expected.to contain_class('systemd::install') } + it { is_expected.to contain_class('systemd::udevd') } + it { is_expected.to contain_service('systemd-udevd') } + it { is_expected.to contain_file('/etc/udev/udev.conf') } + end + + describe 'with all options (array notify)' do + let(:params) do + { + ensure: 'file', + path: '/etc/udev/rules.d', + selinux_ignore_defaults: false, + notify_services: ['Service[systemd-udevd]', 'Service[foo]'], + rules: [ + '# I am a comment', + 'ACTION=="add", KERNEL=="sda", RUN+="/bin/raw /dev/raw/raw1 %N"', + 'ACTION=="add", KERNEL=="sdb", RUN+="/bin/raw /dev/raw/raw2 %N"', + ], + } + end + + it { is_expected.to compile.with_all_deps } + + it { + is_expected.to create_file("/etc/udev/rules.d/#{title}"). + with(ensure: 'file', mode: '0444', owner: 'root', group: 'root'). + with_content(%r{^# This file managed by Puppet - DO NOT EDIT$}). + with_content(%r{^# I am a comment$}). + with_content(%r{^ACTION=="add", KERNEL=="sda", RUN\+="/bin/raw /dev/raw/raw1 %N"$}). + with_content(%r{^ACTION=="add", KERNEL=="sdb", RUN\+="/bin/raw /dev/raw/raw2 %N"$}). + that_notifies('Service[systemd-udevd]'). + that_notifies('Service[foo]') + } + end + + describe 'ensured absent without notify' do + let(:params) { { ensure: 'absent', } } + + it { is_expected.to compile.with_all_deps } + + it { is_expected.to contain_file("/etc/udev/rules.d/#{title}").with_ensure('absent').with_notify([]) } + end + + describe 'ensured absent with automatic reload' do + let(:pre_condition) do + <<-PUPPET + class { 'systemd': manage_udevd => true, manage_journald => false, udev_reload => true } + PUPPET + end + let(:params) do + { + ensure: 'absent', + } + end + + it { is_expected.to compile.with_all_deps } + + it { is_expected.to contain_file("/etc/udev/rules.d/#{title}").with_ensure('absent').with_notify(['Exec[systemd-udev_reload]']) } + end + + describe 'ensured absent with custom notify' do + let(:params) { { ensure: 'absent', notify_services: 'Service[systemd-udevd]' } } + + it { is_expected.to compile.with_all_deps } + + it do + is_expected.to create_file("/etc/udev/rules.d/#{title}"). + with_ensure('absent'). + that_notifies('Service[systemd-udevd]') end end end diff --git a/spec/defines/unit_file_spec.rb b/spec/defines/unit_file_spec.rb index 6c66c0da..24c25ce7 100644 --- a/spec/defines/unit_file_spec.rb +++ b/spec/defines/unit_file_spec.rb @@ -3,230 +3,225 @@ require 'spec_helper' describe 'systemd::unit_file' do - context 'supported operating systems' do - on_supported_os.each do |os, facts| - context "on #{os}" do - let(:facts) { facts } - let(:title) { 'test.service' } - let(:params) { { content: 'random stuff' } } - - it { is_expected.to compile.with_all_deps } - - context 'with defaults' do - it do - expect(subject).to create_systemd__daemon_reload(title) - end - - it do - expect(subject).to contain_file("/etc/systemd/system/#{title}"). - with_selinux_ignore_defaults(false). - that_notifies("Systemd::Daemon_reload[#{title}]") - end - end - - context 'selinux_ignore_defaults => false' do - let(:params) { { selinux_ignore_defaults: false } } - - it do - expect(subject).to contain_file("/etc/systemd/system/#{title}"). - with_selinux_ignore_defaults(false) - end - end - - context 'selinux_ignore_defaults => true' do - let(:params) { { selinux_ignore_defaults: true } } - - it do - expect(subject).to contain_file("/etc/systemd/system/#{title}"). - with_selinux_ignore_defaults(true) - end - end - - context 'with non-sensitive Content' do - let(:params) { { content: 'non-sensitive Content' } } - - it do - expect(subject).to create_file("/etc/systemd/system/#{title}"). - with_ensure('file'). - with_content(params[:content]). - with_mode('0444') - end - end - - context 'with sensitive Content' do - let(:params) { { content: sensitive('sensitive Content') } } - - it do - resource = catalogue.resource("File[/etc/systemd/system/#{title}]") - expect(resource[:content]).to eq(params[:content].unwrap) - - expect(subject).to contain_file("/etc/systemd/system/#{title}"). - with({ content: sensitive('sensitive Content') }) - end - end - - context 'with a bad unit type' do - let(:title) { 'test.badtype' } - - it { is_expected.to compile.and_raise_error(%r{expects a match for Systemd::Unit}) } - end - - context 'with a bad unit type containing a slash' do - let(:title) { 'test/unit.service' } - - it { is_expected.to compile.and_raise_error(%r{expects a match for Systemd::Unit}) } - end - - context 'with enable => true and active => true' do - let(:params) do - super().merge( - enable: true, - active: true - ) - end - - it { is_expected.to compile.with_all_deps } - - it do - expect(subject).to contain_service('test.service'). - with_ensure(true). - with_enable(true). - with_provider('systemd'). - without_hasstatus. - without_hasrestart. - without_flags. - without_timeout. - that_subscribes_to("File[/etc/systemd/system/#{title}]"). - that_subscribes_to("Systemd::Daemon_reload[#{title}]") - end - end - - context 'with enable => true and active => true and service_parameters' do - let(:params) do - super().merge( - enable: true, - active: true, - service_parameters: { - flags: '--awesome', - timeout: 1337 - } - ) - end - - it { is_expected.to compile.with_all_deps } - - it do - expect(subject).to contain_service('test.service'). - with_ensure(true). - with_enable(true). - with_provider('systemd'). - without_hasstatus. - without_hasrestart. - with_flags('--awesome'). - with_timeout(1337). - that_subscribes_to("File[/etc/systemd/system/#{title}]") - end - end - - context 'with enable => true and active => true and service_restart => false' do - let(:params) do - super().merge( - enable: true, - active: true, - service_restart: false - ) - end - - it { is_expected.to compile.with_all_deps } - - it do - expect(subject).to contain_service('test.service'). - with_ensure(true). - with_enable(true). - with_provider('systemd'). - without_hasstatus. - without_hasrestart. - without_flags. - without_timeout - end - - it do - expect(subject).not_to contain_service('test.service'). - that_subscribes_to("Systemd::Daemon_reload[#{title}]") - end - end - - context 'ensure => absent' do - let(:params) { super().merge(ensure: 'absent') } - - context 'with enable => true' do - let(:params) { super().merge(enable: true) } - - it { is_expected.to compile.and_raise_error(%r{Can't ensure the unit file is absent and activate}) } - end - - context 'with active => true' do - let(:params) { super().merge(active: true) } - - it { is_expected.to compile.and_raise_error(%r{Can't ensure the unit file is absent and activate}) } - end - - context 'with enable => false and active => false' do - let(:params) do - super().merge( - enable: false, - active: false - ) - end - - it { is_expected.to compile.with_all_deps } - - it do - expect(subject).to contain_service('test.service'). - with_ensure(false). - with_enable(false). - with_provider('systemd'). - that_comes_before("File[/etc/systemd/system/#{title}]") - end - end - end - - context 'enable => mask' do - let(:params) { { enable: 'mask' } } - - it do - expect(subject).to create_file("/etc/systemd/system/#{title}"). - with_ensure('link'). - with_target('/dev/null') - end - 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(title) - } - end - - context 'with target => "/tmp/service-target" and ensure => absent' do - let(:params) do - { ensure: 'absent', target: '/tmp/service-target' } - end - - it { is_expected.to compile.with_all_deps } - - it do - expect(subject).to create_file("/etc/systemd/system/#{title}"). - with_ensure('absent'). - with_target('/tmp/service-target') - end - end + _, facts = on_supported_os.first + let(:facts) { facts } + let(:title) { 'test.service' } + let(:params) { { content: 'random stuff' } } + + it { is_expected.to compile.with_all_deps } + + context 'with defaults' do + it do + expect(subject).to create_systemd__daemon_reload(title) + end + + it do + expect(subject).to contain_file("/etc/systemd/system/#{title}"). + with_selinux_ignore_defaults(false). + that_notifies("Systemd::Daemon_reload[#{title}]") + end + end + + context 'selinux_ignore_defaults => false' do + let(:params) { { selinux_ignore_defaults: false } } + + it do + expect(subject).to contain_file("/etc/systemd/system/#{title}"). + with_selinux_ignore_defaults(false) + end + end + + context 'selinux_ignore_defaults => true' do + let(:params) { { selinux_ignore_defaults: true } } + + it do + expect(subject).to contain_file("/etc/systemd/system/#{title}"). + with_selinux_ignore_defaults(true) + end + end + + context 'with non-sensitive Content' do + let(:params) { { content: 'non-sensitive Content' } } + + it do + expect(subject).to create_file("/etc/systemd/system/#{title}"). + with_ensure('file'). + with_content(params[:content]). + with_mode('0444') + end + end + + context 'with sensitive Content' do + let(:params) { { content: sensitive('sensitive Content') } } + + it do + resource = catalogue.resource("File[/etc/systemd/system/#{title}]") + expect(resource[:content]).to eq(params[:content].unwrap) + + expect(subject).to contain_file("/etc/systemd/system/#{title}"). + with({ content: sensitive('sensitive Content') }) + end + end + + context 'with a bad unit type' do + let(:title) { 'test.badtype' } + + it { is_expected.to compile.and_raise_error(%r{expects a match for Systemd::Unit}) } + end + + context 'with a bad unit type containing a slash' do + let(:title) { 'test/unit.service' } + + it { is_expected.to compile.and_raise_error(%r{expects a match for Systemd::Unit}) } + end + + context 'with enable => true and active => true' do + let(:params) do + super().merge( + enable: true, + active: true + ) + end + + it { is_expected.to compile.with_all_deps } + + it do + expect(subject).to contain_service('test.service'). + with_ensure(true). + with_enable(true). + with_provider('systemd'). + without_hasstatus. + without_hasrestart. + without_flags. + without_timeout. + that_subscribes_to("File[/etc/systemd/system/#{title}]"). + that_subscribes_to("Systemd::Daemon_reload[#{title}]") + end + end + + context 'with enable => true and active => true and service_parameters' do + let(:params) do + super().merge( + enable: true, + active: true, + service_parameters: { + flags: '--awesome', + timeout: 1337 + } + ) + end + + it { is_expected.to compile.with_all_deps } + + it do + expect(subject).to contain_service('test.service'). + with_ensure(true). + with_enable(true). + with_provider('systemd'). + without_hasstatus. + without_hasrestart. + with_flags('--awesome'). + with_timeout(1337). + that_subscribes_to("File[/etc/systemd/system/#{title}]") + end + end + + context 'with enable => true and active => true and service_restart => false' do + let(:params) do + super().merge( + enable: true, + active: true, + service_restart: false + ) + end + + it { is_expected.to compile.with_all_deps } + + it do + expect(subject).to contain_service('test.service'). + with_ensure(true). + with_enable(true). + with_provider('systemd'). + without_hasstatus. + without_hasrestart. + without_flags. + without_timeout + end + + it do + expect(subject).not_to contain_service('test.service'). + that_subscribes_to("Systemd::Daemon_reload[#{title}]") + end + end + + context 'ensure => absent' do + let(:params) { super().merge(ensure: 'absent') } + + context 'with enable => true' do + let(:params) { super().merge(enable: true) } + + it { is_expected.to compile.and_raise_error(%r{Can't ensure the unit file is absent and activate}) } + end + + context 'with active => true' do + let(:params) { super().merge(active: true) } + + it { is_expected.to compile.and_raise_error(%r{Can't ensure the unit file is absent and activate}) } + end + + context 'with enable => false and active => false' do + let(:params) do + super().merge( + enable: false, + active: false + ) end + + it { is_expected.to compile.with_all_deps } + + it do + expect(subject).to contain_service('test.service'). + with_ensure(false). + with_enable(false). + with_provider('systemd'). + that_comes_before("File[/etc/systemd/system/#{title}]") + end + end + end + + context 'enable => mask' do + let(:params) { { enable: 'mask' } } + + it do + expect(subject).to create_file("/etc/systemd/system/#{title}"). + with_ensure('link'). + with_target('/dev/null') + end + 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(title) + } + end + + context 'with target => "/tmp/service-target" and ensure => absent' do + let(:params) do + { ensure: 'absent', target: '/tmp/service-target' } + end + + it { is_expected.to compile.with_all_deps } + + it do + expect(subject).to create_file("/etc/systemd/system/#{title}"). + with_ensure('absent'). + with_target('/tmp/service-target') end end end diff --git a/spec/defines/user_service_spec.rb b/spec/defines/user_service_spec.rb index 81e2f70c..e44f848b 100644 --- a/spec/defines/user_service_spec.rb +++ b/spec/defines/user_service_spec.rb @@ -3,152 +3,147 @@ require 'spec_helper' describe 'systemd::user_service' do - context 'supported operating systems' do - on_supported_os.each do |os, facts| - context "on #{os}" do - let(:facts) { facts } - let(:title) { 'mine.timer' } - - context 'with defaults' do - it { is_expected.to compile.and_raise_error(%r{"user" or "global"}) } - end - - context 'with user and global set' do - let(:params) do - { global: true, user: 'steve' } - end - - it { is_expected.to compile.and_raise_error(%r{"user" or "global"}) } - end - - context 'with global and ensure' do - let(:params) do - { global: true, ensure: 'running' } - end - - it { is_expected.to compile.and_raise_error(%r{Cannot ensure a service is running for all users globally}) } - end - - context 'with global enable' do - let(:params) do - { global: true, enable: true } - end - - it { - is_expected.to contain_exec('Enable user service mine.timer globally') - } - - it { - is_expected.to contain_exec('Enable user service mine.timer globally'). - with_command(['systemctl', '--global', 'enable', 'mine.timer']). - with_unless([['systemctl', '--global', 'is-enabled', 'mine.timer']]). - without_onlyif - } - end - - context 'with global disable' do - let(:params) do - { global: true, enable: false } - end - - it { - is_expected.to contain_exec('Disable user service mine.timer globally'). - with_command(['systemctl', '--global', 'disable', 'mine.timer']). - without_unless. - with_onlyif([['systemctl', '--global', 'is-enabled', 'mine.timer']]) - } - end - - context 'with a user specified' do - let(:params) do - { user: 'steve' } - end - - it { is_expected.to contain_exec('try-reload-or-restart-steve-mine.timer') } - - context 'with enable and ensure false' do - let(:params) do - super().merge(enable: false, ensure: 'stopped') - end - - it { - is_expected.to contain_exec('Stop user service mine.timer for user steve'). - with_command([ - 'systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', - 'systemctl', '--user', 'stop', 'mine.timer', - ]). - with_onlyif([[ - 'systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', - 'systemctl', '--user', 'is-active', 'mine.timer', - ]]). - without_unless - } - - it { - is_expected.to contain_exec('Disable user service mine.timer for user steve'). - with_command([ - 'systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', - 'systemctl', '--user', 'disable', 'mine.timer', - ]). - with_onlyif([[ - 'systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', - 'systemctl', '--user', 'is-enabled', 'mine.timer', - ]]). - without_unless - } - end - - context 'with enable and ensure true' do - let(:params) do - super().merge(enable: true, ensure: 'running') - end - - it { - is_expected.to contain_exec('Start user service mine.timer for user steve'). - with_command([ - 'systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', - 'systemctl', '--user', 'start', 'mine.timer', - ]). - without_onlyif. - with_unless([[ - 'systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', - 'systemctl', '--user', 'is-active', 'mine.timer', - ]]) - } - - it { - is_expected.to contain_exec('Enable user service mine.timer for user steve'). - with_command([ - 'systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', - 'systemctl', '--user', 'enable', 'mine.timer', - ]). - without_onlif. - with_unless([[ - 'systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', - 'systemctl', '--user', 'is-enabled', 'mine.timer', - ]]) - } - end - - context 'with enable true and ensure false' do - let(:params) do - super().merge(enable: true, ensure: false) - end - - it { is_expected.to contain_exec('Stop user service mine.timer for user steve') } - it { is_expected.to contain_exec('Enable user service mine.timer for user steve') } - end - - context 'with enable false and ensure true' do - let(:params) do - super().merge(enable: false, ensure: true) - end - - it { is_expected.to contain_exec('Start user service mine.timer for user steve') } - it { is_expected.to contain_exec('Disable user service mine.timer for user steve') } - end - end + _, facts = on_supported_os.first + let(:facts) { facts } + let(:title) { 'mine.timer' } + + context 'with defaults' do + it { is_expected.to compile.and_raise_error(%r{"user" or "global"}) } + end + + context 'with user and global set' do + let(:params) do + { global: true, user: 'steve' } + end + + it { is_expected.to compile.and_raise_error(%r{"user" or "global"}) } + end + + context 'with global and ensure' do + let(:params) do + { global: true, ensure: 'running' } + end + + it { is_expected.to compile.and_raise_error(%r{Cannot ensure a service is running for all users globally}) } + end + + context 'with global enable' do + let(:params) do + { global: true, enable: true } + end + + it { + is_expected.to contain_exec('Enable user service mine.timer globally') + } + + it { + is_expected.to contain_exec('Enable user service mine.timer globally'). + with_command(['systemctl', '--global', 'enable', 'mine.timer']). + with_unless([['systemctl', '--global', 'is-enabled', 'mine.timer']]). + without_onlyif + } + end + + context 'with global disable' do + let(:params) do + { global: true, enable: false } + end + + it { + is_expected.to contain_exec('Disable user service mine.timer globally'). + with_command(['systemctl', '--global', 'disable', 'mine.timer']). + without_unless. + with_onlyif([['systemctl', '--global', 'is-enabled', 'mine.timer']]) + } + end + + context 'with a user specified' do + let(:params) do + { user: 'steve' } + end + + it { is_expected.to contain_exec('try-reload-or-restart-steve-mine.timer') } + + context 'with enable and ensure false' do + let(:params) do + super().merge(enable: false, ensure: 'stopped') + end + + it { + is_expected.to contain_exec('Stop user service mine.timer for user steve'). + with_command([ + 'systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', + 'systemctl', '--user', 'stop', 'mine.timer', + ]). + with_onlyif([[ + 'systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', + 'systemctl', '--user', 'is-active', 'mine.timer', + ]]). + without_unless + } + + it { + is_expected.to contain_exec('Disable user service mine.timer for user steve'). + with_command([ + 'systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', + 'systemctl', '--user', 'disable', 'mine.timer', + ]). + with_onlyif([[ + 'systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', + 'systemctl', '--user', 'is-enabled', 'mine.timer', + ]]). + without_unless + } + end + + context 'with enable and ensure true' do + let(:params) do + super().merge(enable: true, ensure: 'running') end + + it { + is_expected.to contain_exec('Start user service mine.timer for user steve'). + with_command([ + 'systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', + 'systemctl', '--user', 'start', 'mine.timer', + ]). + without_onlyif. + with_unless([[ + 'systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', + 'systemctl', '--user', 'is-active', 'mine.timer', + ]]) + } + + it { + is_expected.to contain_exec('Enable user service mine.timer for user steve'). + with_command([ + 'systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', + 'systemctl', '--user', 'enable', 'mine.timer', + ]). + without_onlif. + with_unless([[ + 'systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', + 'systemctl', '--user', 'is-enabled', 'mine.timer', + ]]) + } + end + + context 'with enable true and ensure false' do + let(:params) do + super().merge(enable: true, ensure: false) + end + + it { is_expected.to contain_exec('Stop user service mine.timer for user steve') } + it { is_expected.to contain_exec('Enable user service mine.timer for user steve') } + end + + context 'with enable false and ensure true' do + let(:params) do + super().merge(enable: false, ensure: true) + end + + it { is_expected.to contain_exec('Start user service mine.timer for user steve') } + it { is_expected.to contain_exec('Disable user service mine.timer for user steve') } end end end