diff --git a/spec/defines/timer_spec.rb b/spec/defines/timer_spec.rb index 55c0a2e..28d4117 100644 --- a/spec/defines/timer_spec.rb +++ b/spec/defines/timer_spec.rb @@ -1,85 +1,106 @@ 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 { is_expected.to contain_systemd__unit_file('foobar.timer').with( content: "[Timer]\nOnCalendar=weekly", ) } it { is_expected.to contain_systemd__unit_file('foobar.service').with( content: "[Service]\nExecStart=/bin/touch /tmp/foobar", ) } 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') } 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") } 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") } + + it { + is_expected.to create_exec('foobar.service-systemctl-daemon-reload').with( + command: 'systemctl daemon-reload', + refreshonly: true + ) + } + end + context 'with a bad timer name' do let(:title) { 'foobar' } it { expect { is_expected.to compile.with_all_deps }.to raise_error(%r{expects a match for}) } end end end end end diff --git a/spec/defines/unit_file_spec.rb b/spec/defines/unit_file_spec.rb index 23e0e74..dc8ce13 100644 --- a/spec/defines/unit_file_spec.rb +++ b/spec/defines/unit_file_spec.rb @@ -1,86 +1,95 @@ 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 } it do is_expected.to create_file("/etc/systemd/system/#{title}") .with_ensure('file') .with_content(%r{#{params[:content]}}) .with_mode('0444') 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 is_expected.to contain_service('test.service') .with_ensure(true) .with_enable(true) .with_provider('systemd') .that_subscribes_to("File[/etc/systemd/system/#{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 is_expected.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 'when using default values for enable and active' do + it { + is_expected.to create_exec("#{title}-systemctl-daemon-reload").with( + command: 'systemctl daemon-reload', + refreshonly: true + ) + } + end end end end end