Commit 39211d26 authored by Tim Smith's avatar Tim Smith

Convert to inline resources and test create_if_missing

Inline resources greatly simplify the notification issues. This also
fixes requiring a command when you delete a cron entry, which was a
regression in 1.7.1. I've added a serverspec to ensure we're handling
create_if_missing correctly by first creating a cron entry, then
creating it again with create_if_missing.  We can expect to find the
content of the first creation only.
parent 59279d50
......@@ -17,31 +17,26 @@
# limitations under the License.
#
# This pattern is used to make the providers compatible with Chef 10,
# which does not support use_inline_resources.
#
# FIXME: replace when Chef 12 is released.
use_inline_resources
action :delete do
r = create_template
r.action :delete
new_resource.updated_by_last_action(r.updated_by_last_action?)
file "/etc/cron.d/#{new_resource.name}" do
action :delete
notifies :create, 'template[/etc/crontab]', :delayed if node['cron']['emulate_cron.d']
end
end
action :create do
# We should be able to switch emulate_cron.d on for Solaris, but I don't have a Solaris box to verify
fail 'Solaris does not support cron jobs in /etc/cron.d' if node['platform_family'] == 'solaris2'
r = create_template
new_resource.updated_by_last_action(r.updated_by_last_action?)
create_template(:create)
end
action :create_if_missing do
r = create_template
r.action :create_if_missing
new_resource.updated_by_last_action(r.updated_by_last_action?)
create_template(:create_if_missing)
end
def create_template
def create_template(create_action)
template "/etc/cron.d/#{new_resource.name}" do
cookbook new_resource.cookbook
source 'cron.d.erb'
......@@ -63,7 +58,7 @@ def create_template
comment: new_resource.comment,
environment: new_resource.environment
)
action :create
action create_action
notifies :create, 'template[/etc/crontab]', :delayed if node['cron']['emulate_cron.d']
end
end
......@@ -76,9 +76,6 @@ describe 'cron_test::default' do
end
it 'deletes cron_d[delete_cron]' do
expect(chef_run).to delete_cron_d('delete_cron').with(
command: '/bin/true',
user: 'appuser'
)
expect(chef_run).to delete_cron_d('delete_cron')
end
end
......@@ -74,12 +74,19 @@ cron_d 'no_value_check' do
action :create_if_missing
end
cron_d 'test-weekday-usage-report' do
minute '1'
hour '1'
weekday '1'
command '/this/should/never/run'
user 'appuser'
action :create_if_missing
end
file '/etc/cron.d/delete_cron' do
content '* * * * * appuser /bin/true'
end
cron_d 'delete_cron' do
command '/bin/true'
user 'appuser'
action :delete
end
......@@ -30,6 +30,10 @@ if os[:family] == 'freebsd'
its(:content) { should match /\* \* \* \* \* appuser \/bin\/true/ }
end
else
# make sure the :create_if_missing didn't overwrite the :create
describe file('/etc/cron.d/test-weekday-usage-report') do
its(:content) { should match /\/srv\/app\/scripts\/generate_report/ }
end
describe file('/etc/cron.d/nil_value_check') do
its(:content) { should match /\* \* \* \* \* appuser \/bin\/true/ }
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment