aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorJosh Cooper <josh@puppet.com>2018-08-27 14:37:09 -0700
committerGitHub <noreply@github.com>2018-08-27 14:37:09 -0700
commit74f80e8d72f8b646ea206c8481f15e08aa469198 (patch)
treeacf0e4d48682859f94a3925f0e76dc5199b62e19 /spec
parent2142feac49c20972e39ed0e11a017fbbf15cc51f (diff)
parentf5d3f1058d52fc851ea42b09a2661554df48e694 (diff)
downloadpuppet-cron_core-74f80e8d72f8b646ea206c8481f15e08aa469198.tar.gz
puppet-cron_core-74f80e8d72f8b646ea206c8481f15e08aa469198.tar.bz2
Merge pull request #1 from jtappa/extract
Import the module
Diffstat (limited to 'spec')
-rw-r--r--spec/acceptance/nodesets/default.yml19
-rw-r--r--spec/acceptance/tests/resource/cron/should_allow_changing_parameters_spec.rb63
-rw-r--r--spec/acceptance/tests/resource/cron/should_be_idempotent_spec.rb32
-rw-r--r--spec/acceptance/tests/resource/cron/should_create_cron_spec.rb31
-rw-r--r--spec/acceptance/tests/resource/cron/should_match_existing_spec.rb33
-rw-r--r--spec/acceptance/tests/resource/cron/should_remove_cron_spec.rb35
-rw-r--r--spec/acceptance/tests/resource/cron/should_remove_leading_and_trailing_whitespace_spec.rb41
-rw-r--r--spec/acceptance/tests/resource/cron/should_remove_matching_spec.rb34
-rw-r--r--spec/acceptance/tests/resource/cron/should_update_existing_spec.rb39
-rw-r--r--spec/default_facts.yml8
-rw-r--r--spec/fixtures/integration/provider/cron/crontab/create_normal_entry19
-rw-r--r--spec/fixtures/integration/provider/cron/crontab/create_special_entry18
-rw-r--r--spec/fixtures/integration/provider/cron/crontab/crontab_user115
-rw-r--r--spec/fixtures/integration/provider/cron/crontab/crontab_user24
-rw-r--r--spec/fixtures/integration/provider/cron/crontab/crontab_user317
-rw-r--r--spec/fixtures/integration/provider/cron/crontab/modify_entry13
-rw-r--r--spec/fixtures/integration/provider/cron/crontab/moved_cronjob_input115
-rw-r--r--spec/fixtures/integration/provider/cron/crontab/moved_cronjob_input26
-rw-r--r--spec/fixtures/integration/provider/cron/crontab/purged8
-rw-r--r--spec/fixtures/integration/provider/cron/crontab/remove_named_resource12
-rw-r--r--spec/fixtures/integration/provider/cron/crontab/remove_unnamed_resource14
-rw-r--r--spec/fixtures/integration/provider/cron/crontab/unspecialized15
-rw-r--r--spec/fixtures/unit/provider/cron/crontab/single_line.yaml272
-rw-r--r--spec/fixtures/unit/provider/cron/crontab/vixie_header.txt3
-rw-r--r--spec/fixtures/unit/provider/cron/parsed/managed6
-rw-r--r--spec/fixtures/unit/provider/cron/parsed/simple9
-rw-r--r--spec/integration/provider/cron/crontab_spec.rb237
-rw-r--r--spec/lib/puppet_spec/compiler.rb112
-rw-r--r--spec/lib/puppet_spec/files.rb112
-rw-r--r--spec/spec_helper.rb44
-rw-r--r--spec/spec_helper_acceptance.rb40
-rw-r--r--spec/spec_helper_local.rb17
-rw-r--r--spec/unit/provider/cron/crontab_spec.rb205
-rw-r--r--spec/unit/provider/cron/parsed_spec.rb335
-rw-r--r--spec/unit/type/cron_spec.rb536
35 files changed, 2419 insertions, 0 deletions
diff --git a/spec/acceptance/nodesets/default.yml b/spec/acceptance/nodesets/default.yml
new file mode 100644
index 0000000..2cd2823
--- /dev/null
+++ b/spec/acceptance/nodesets/default.yml
@@ -0,0 +1,19 @@
+---
+HOSTS:
+ centos7-64-1:
+ pe_dir:
+ pe_ver:
+ pe_upgrade_dir:
+ pe_upgrade_ver:
+ hypervisor: vmpooler
+ platform: centos-7-x86_64
+ packaging_platform: el-7-x86_64
+ template: centos-7-x86_64
+ roles:
+ - agent
+ - default
+CONFIG:
+ type: agent
+ nfs_server: none
+ consoleport: 443
+ pooling_api: http://vmpooler.delivery.puppetlabs.net/ \ No newline at end of file
diff --git a/spec/acceptance/tests/resource/cron/should_allow_changing_parameters_spec.rb b/spec/acceptance/tests/resource/cron/should_allow_changing_parameters_spec.rb
new file mode 100644
index 0000000..615d617
--- /dev/null
+++ b/spec/acceptance/tests/resource/cron/should_allow_changing_parameters_spec.rb
@@ -0,0 +1,63 @@
+require 'spec_helper_acceptance'
+
+RSpec.context 'when changing parameters' do
+ before(:each) do
+ compatible_agents.each do |agent|
+ step 'ensure the user exists via puppet'
+ setup agent
+ end
+ end
+
+ after(:each) do
+ compatible_agents.each do |agent|
+ step 'Cron: cleanup'
+ clean agent
+ end
+ end
+
+ compatible_agents.each do |agent|
+ it "manages cron entries on #{agent}" do
+ step 'Cron: basic - verify that it can be created'
+ result = apply_manifest_on(agent, 'cron { "myjob": command => "/bin/false", user => "tstuser", hour => "*", minute => [1], ensure => present,}')
+ expect(result.stdout).to match(%r{ensure: created})
+ result = run_cron_on(agent, :list, 'tstuser')
+ expect(result.stdout).to match(%r{.bin.false})
+
+ step 'Cron: allow changing command'
+ result = apply_manifest_on(agent, 'cron { "myjob": command => "/bin/true", user => "tstuser", hour => "*", minute => [1], ensure => present,}')
+ expect(result.stdout).to match(%r{command changed '.bin.false'.* to '.bin.true'})
+
+ result = run_cron_on(agent, :list, 'tstuser')
+ expect(result.stdout).to match(%r{1 . . . . .bin.true})
+
+ step 'Cron: allow changing time'
+ result = apply_manifest_on(agent, 'cron { "myjob": command => "/bin/true", user => "tstuser", hour => "1", minute => [1], ensure => present,}')
+ expect(result.stdout).to match(%r{hour: defined 'hour' as \['1'\]})
+
+ result = run_cron_on(agent, :list, 'tstuser')
+ expect(result.stdout).to match(%r{1 1 . . . .bin.true})
+
+ step 'Cron: allow changing time(array)'
+ result = apply_manifest_on(agent, 'cron { "myjob": command => "/bin/true", user => "tstuser", hour => ["1","2"], minute => [1], ensure => present,}')
+ expect(result.stdout).to match(%r{hour: hour changed \['1'\].* to \['1', '2'\]})
+
+ result = run_cron_on(agent, :list, 'tstuser')
+ expect(result.stdout).to match(%r{1 1,2 . . . .bin.true})
+
+ step 'Cron: allow changing time(array modification)'
+ result = apply_manifest_on(agent, 'cron { "myjob": command => "/bin/true", user => "tstuser", hour => ["3","2"], minute => [1], ensure => present,}')
+ expect(result.stdout).to match(%r{hour: hour changed \['1', '2'\].* to \['3', '2'\]})
+
+ result = run_cron_on(agent, :list, 'tstuser')
+ expect(result.stdout).to match(%r{1 3,2 . . . .bin.true})
+
+ step 'Cron: allow changing time(array modification to *)'
+ result = apply_manifest_on(agent, 'cron { "myjob": command => "/bin/true", user => "tstuser", hour => "*", minute => "*", ensure => present,}')
+ expect(result.stdout).to match(%r{minute: undefined 'minute' from \['1'\]})
+ expect(result.stdout).to match(%r{hour: undefined 'hour' from \['3', '2'\]})
+
+ result = run_cron_on(agent, :list, 'tstuser')
+ expect(result.stdout).to match(%r{\* \* . . . .bin.true})
+ end
+ end
+end
diff --git a/spec/acceptance/tests/resource/cron/should_be_idempotent_spec.rb b/spec/acceptance/tests/resource/cron/should_be_idempotent_spec.rb
new file mode 100644
index 0000000..35a91b7
--- /dev/null
+++ b/spec/acceptance/tests/resource/cron/should_be_idempotent_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper_acceptance'
+
+RSpec.context 'when checking idempotency' do
+ before(:each) do
+ compatible_agents.each do |agent|
+ step 'ensure the user exists via puppet'
+ setup(agent)
+ end
+ end
+
+ after(:each) do
+ compatible_agents.each do |agent|
+ step 'Cron: cleanup'
+ clean(agent)
+ end
+ end
+
+ compatible_agents.each do |agent|
+ it "ensures idempotency on #{agent}" do
+ step 'Cron: basic - verify that it can be created'
+ result = apply_manifest_on(agent, 'cron { "myjob": command => "/bin/true", user => "tstuser", hour => "*", minute => [1], ensure => present,}')
+ expect(result.stdout).to match(%r{ensure: created})
+
+ result = run_cron_on(agent, :list, 'tstuser')
+ expect(result.stdout).to match(%r{. . . . . .bin.true})
+
+ step 'Cron: basic - should not create again'
+ result = apply_manifest_on(agent, 'cron { "myjob": command => "/bin/true", user => "tstuser", hour => "*", minute => [1], ensure => present,}')
+ expect(result.stdout).not_to match(%r{ensure: created})
+ end
+ end
+end
diff --git a/spec/acceptance/tests/resource/cron/should_create_cron_spec.rb b/spec/acceptance/tests/resource/cron/should_create_cron_spec.rb
new file mode 100644
index 0000000..05ae5e5
--- /dev/null
+++ b/spec/acceptance/tests/resource/cron/should_create_cron_spec.rb
@@ -0,0 +1,31 @@
+require 'spec_helper_acceptance'
+
+RSpec.context 'when creating cron' do
+ before(:each) do
+ compatible_agents.each do |agent|
+ step 'ensure the user exists via puppet'
+ setup(agent)
+ end
+ end
+
+ after(:each) do
+ compatible_agents.each do |agent|
+ step 'Cron: cleanup'
+ clean(agent)
+ end
+ end
+
+ compatible_agents.each do |host|
+ it 'creates a new cron job' do
+ step 'apply the resource on the host using puppet resource'
+ on(host, puppet_resource('cron', 'crontest', 'user=tstuser', 'command=/bin/true', 'ensure=present')) do
+ expect(stdout).to match(%r{created})
+ end
+
+ step 'verify that crontab -l contains what you expected'
+ run_cron_on(host, :list, 'tstuser') do
+ expect(stdout).to match(%r{\* \* \* \* \* /bin/true})
+ end
+ end
+ end
+end
diff --git a/spec/acceptance/tests/resource/cron/should_match_existing_spec.rb b/spec/acceptance/tests/resource/cron/should_match_existing_spec.rb
new file mode 100644
index 0000000..ce25be7
--- /dev/null
+++ b/spec/acceptance/tests/resource/cron/should_match_existing_spec.rb
@@ -0,0 +1,33 @@
+require 'spec_helper_acceptance'
+
+RSpec.context 'when matching cron' do
+ before(:each) do
+ compatible_agents.each do |agent|
+ step 'ensure the user exists via puppet'
+ setup(agent)
+ step 'Create the existing cron job by hand...'
+ run_cron_on(agent, :add, 'tstuser', '* * * * * /bin/true')
+ end
+ end
+
+ after(:each) do
+ compatible_agents.each do |agent|
+ step 'Cron: cleanup'
+ clean(agent)
+ end
+ end
+
+ compatible_agents.each do |host|
+ it 'matches existing cron jobs' do
+ step 'Apply the resource on the host using puppet resource'
+ on(host, puppet_resource('cron', 'crontest', 'user=tstuser', 'command=/bin/true', 'ensure=present')) do
+ expect(stdout).to match(%r{present})
+ end
+
+ step 'Verify that crontab -l contains what you expected'
+ run_cron_on(host, :list, 'tstuser') do
+ expect(stdout).to match(%r{\* \* \* \* \* /bin/true})
+ end
+ end
+ end
+end
diff --git a/spec/acceptance/tests/resource/cron/should_remove_cron_spec.rb b/spec/acceptance/tests/resource/cron/should_remove_cron_spec.rb
new file mode 100644
index 0000000..d23cded
--- /dev/null
+++ b/spec/acceptance/tests/resource/cron/should_remove_cron_spec.rb
@@ -0,0 +1,35 @@
+require 'spec_helper_acceptance'
+
+RSpec.context 'when removing crontab' do
+ before(:each) do
+ compatible_agents.each do |agent|
+ step 'ensure the user exists via puppet'
+ setup(agent)
+ end
+ end
+
+ after(:each) do
+ compatible_agents.each do |agent|
+ step 'Cron: cleanup'
+ clean(agent)
+ end
+ end
+
+ compatible_agents.each do |host|
+ it 'removes existing crontabs' do
+ step 'create the existing job by hand...'
+ run_cron_on(host, :add, 'tstuser', '* * * * * /bin/true')
+
+ step 'apply the resource on the host using puppet resource'
+ on(host, puppet_resource('cron', 'crontest', 'user=tstuser',
+ 'command=/bin/true', 'ensure=absent')) do
+ expect(stdout).to match(%r{crontest\D+ensure:\s+removed})
+ end
+
+ step ' contains what you expected'
+ run_cron_on(host, :list, 'tstuser') do
+ expect(stderr).not_to match(%r{/bin/true})
+ end
+ end
+ end
+end
diff --git a/spec/acceptance/tests/resource/cron/should_remove_leading_and_trailing_whitespace_spec.rb b/spec/acceptance/tests/resource/cron/should_remove_leading_and_trailing_whitespace_spec.rb
new file mode 100644
index 0000000..da04daa
--- /dev/null
+++ b/spec/acceptance/tests/resource/cron/should_remove_leading_and_trailing_whitespace_spec.rb
@@ -0,0 +1,41 @@
+require 'spec_helper_acceptance'
+
+RSpec.context 'when stripping whitespace from cron jobs' do
+ before(:each) do
+ compatible_agents.each do |agent|
+ step 'ensure the user exists via puppet'
+ setup(agent)
+ end
+ end
+
+ after(:each) do
+ compatible_agents.each do |agent|
+ step 'Cron: cleanup'
+ clean(agent)
+ end
+ end
+
+ agents.each do |host|
+ it 'removes leading and trailing whitespace from cron jobs' do
+ step 'apply the resource on the host using puppet resource'
+ on(host, puppet_resource('cron', 'crontest', 'user=tstuser', "command=' date > /dev/null '", 'ensure=present')) do
+ expect(stdout).to match(%r{created})
+ end
+
+ step 'verify the added crontab entry has stripped whitespace'
+ run_cron_on(host, :list, 'tstuser') do
+ expect(stdout).to match(%r{\* \* \* \* \* date > .dev.null})
+ end
+
+ step 'apply the resource with trailing whitespace and check nothing happened'
+ on(host, puppet_resource('cron', 'crontest', 'user=tstuser', "command='date > /dev/null '", 'ensure=present')) do
+ expect(stdout).not_to match(%r{ensure: created})
+ end
+
+ step 'apply the resource with leading whitespace and check nothing happened'
+ on(host, puppet_resource('cron', 'crontest', 'user=tstuser', "command=' date > /dev/null'", 'ensure=present')) do
+ expect(stdout).not_to match(%r{ensure: created})
+ end
+ end
+ end
+end
diff --git a/spec/acceptance/tests/resource/cron/should_remove_matching_spec.rb b/spec/acceptance/tests/resource/cron/should_remove_matching_spec.rb
new file mode 100644
index 0000000..652d8c5
--- /dev/null
+++ b/spec/acceptance/tests/resource/cron/should_remove_matching_spec.rb
@@ -0,0 +1,34 @@
+require 'spec_helper_acceptance'
+
+RSpec.context 'when removing crontabs' do
+ before(:each) do
+ compatible_agents.each do |agent|
+ step 'ensure the user exists via puppet'
+ setup(agent)
+
+ step 'create the existing job by hand...'
+ run_cron_on(agent, :add, 'tstuser', '* * * * * /bin/true')
+ end
+ end
+
+ after(:each) do
+ compatible_agents.each do |agent|
+ step 'Cron: cleanup'
+ clean(agent)
+ end
+ end
+
+ compatible_agents.each do |host|
+ it 'removes crontabs based on matching' do
+ step 'Remove cron resource'
+ on(host, puppet_resource('cron', 'bogus', 'user=tstuser', 'command=/bin/true', 'ensure=absent')) do
+ expect(stdout).to match(%r{bogus\D+ensure: removed})
+ end
+
+ step 'verify that crontab -l contains what you expected'
+ run_cron_on(host, :list, 'tstuser') do
+ expect(stdout.scan('/bin/true').length).to eq(0)
+ end
+ end
+ end
+end
diff --git a/spec/acceptance/tests/resource/cron/should_update_existing_spec.rb b/spec/acceptance/tests/resource/cron/should_update_existing_spec.rb
new file mode 100644
index 0000000..3b226e7
--- /dev/null
+++ b/spec/acceptance/tests/resource/cron/should_update_existing_spec.rb
@@ -0,0 +1,39 @@
+require 'spec_helper_acceptance'
+
+RSpec.context 'when updating cron jobs' do
+ before(:each) do
+ compatible_agents.each do |agent|
+ step 'ensure the user exists via puppet'
+ setup(agent)
+
+ step 'create the existing job by hand...'
+ run_cron_on(agent, :add, 'tstuser', '* * * * * /bin/true')
+ end
+ end
+
+ after(:each) do
+ compatible_agents.each do |agent|
+ step 'Cron: cleanup'
+ clean(agent)
+ end
+ end
+
+ compatible_agents.each do |host|
+ it 'updates existing cron entries' do
+ step 'verify that crontab -l contains what you expected'
+ run_cron_on(host, :list, 'tstuser') do
+ expect(stdout).to match(%r{\* \* \* \* \* /bin/true})
+ end
+
+ step 'apply the resource change on the host'
+ on(host, puppet_resource('cron', 'crontest', 'user=tstuser', 'command=/bin/true', 'ensure=present', "hour='0-6'")) do
+ expect(stdout).to match(%r{hour\s+=>\s+\['0-6'\]})
+ end
+
+ step 'verify that crontab -l contains what you expected'
+ run_cron_on(host, :list, 'tstuser') do
+ expect(stdout).to match(%r{\* 0-6 \* \* \* /bin/true})
+ end
+ end
+ end
+end
diff --git a/spec/default_facts.yml b/spec/default_facts.yml
new file mode 100644
index 0000000..3248be5
--- /dev/null
+++ b/spec/default_facts.yml
@@ -0,0 +1,8 @@
+# Use default_module_facts.yml for module specific facts.
+#
+# Facts specified here will override the values provided by rspec-puppet-facts.
+---
+concat_basedir: "/tmp"
+ipaddress: "172.16.254.254"
+is_pe: false
+macaddress: "AA:AA:AA:AA:AA:AA"
diff --git a/spec/fixtures/integration/provider/cron/crontab/create_normal_entry b/spec/fixtures/integration/provider/cron/crontab/create_normal_entry
new file mode 100644
index 0000000..e3e2c04
--- /dev/null
+++ b/spec/fixtures/integration/provider/cron/crontab/create_normal_entry
@@ -0,0 +1,19 @@
+# HEADER: some simple
+# HEADER: header
+@daily /bin/unnamed_special_command >> /dev/null 2>&1
+
+# commend with blankline above and below
+
+17-19,22 0-23/2 * * 2 /bin/unnamed_regular_command
+
+# Puppet Name: My daily failure
+MAILTO=""
+@daily /bin/false
+# Puppet Name: Monthly job
+SHELL=/bin/sh
+MAILTO=mail@company.com
+15 14 1 * * $HOME/bin/monthly
+# Puppet Name: new entry
+MAILTO=""
+SHELL=/bin/bash
+12 * * * 2 /bin/new
diff --git a/spec/fixtures/integration/provider/cron/crontab/create_special_entry b/spec/fixtures/integration/provider/cron/crontab/create_special_entry
new file mode 100644
index 0000000..ee25954
--- /dev/null
+++ b/spec/fixtures/integration/provider/cron/crontab/create_special_entry
@@ -0,0 +1,18 @@
+# HEADER: some simple
+# HEADER: header
+@daily /bin/unnamed_special_command >> /dev/null 2>&1
+
+# commend with blankline above and below
+
+17-19,22 0-23/2 * * 2 /bin/unnamed_regular_command
+
+# Puppet Name: My daily failure
+MAILTO=""
+@daily /bin/false
+# Puppet Name: Monthly job
+SHELL=/bin/sh
+MAILTO=mail@company.com
+15 14 1 * * $HOME/bin/monthly
+# Puppet Name: new special entry
+MAILTO=bob@company.com
+@reboot echo "Booted" 1>&2
diff --git a/spec/fixtures/integration/provider/cron/crontab/crontab_user1 b/spec/fixtures/integration/provider/cron/crontab/crontab_user1
new file mode 100644
index 0000000..2c7d542
--- /dev/null
+++ b/spec/fixtures/integration/provider/cron/crontab/crontab_user1
@@ -0,0 +1,15 @@
+# HEADER: some simple
+# HEADER: header
+@daily /bin/unnamed_special_command >> /dev/null 2>&1
+
+# commend with blankline above and below
+
+17-19,22 0-23/2 * * 2 /bin/unnamed_regular_command
+
+# Puppet Name: My daily failure
+MAILTO=""
+@daily /bin/false
+# Puppet Name: Monthly job
+SHELL=/bin/sh
+MAILTO=mail@company.com
+15 14 1 * * $HOME/bin/monthly
diff --git a/spec/fixtures/integration/provider/cron/crontab/crontab_user2 b/spec/fixtures/integration/provider/cron/crontab/crontab_user2
new file mode 100644
index 0000000..267e643
--- /dev/null
+++ b/spec/fixtures/integration/provider/cron/crontab/crontab_user2
@@ -0,0 +1,4 @@
+# HEADER: some simple
+# HEADER: header
+# Puppet Name: some_unrelevant job
+* * * * * /bin/true
diff --git a/spec/fixtures/integration/provider/cron/crontab/crontab_user3 b/spec/fixtures/integration/provider/cron/crontab/crontab_user3
new file mode 100644
index 0000000..ae314ae
--- /dev/null
+++ b/spec/fixtures/integration/provider/cron/crontab/crontab_user3
@@ -0,0 +1,17 @@
+# HEADER: some simple
+# HEADER: header
+@daily /bin/unnamed_special_command >> /dev/null 2>&1
+
+# commend with blankline above and below
+
+17-19,22 0-23/2 * * 2 /bin/unnamed_regular_command
+
+# Puppet Name: My daily failure
+MAILTO=""
+@daily /bin/false
+# Puppet Name: Monthly job
+SHELL=/bin/sh
+MAILTO=mail@company.com
+15 14 1 * * $HOME/bin/monthly
+# Puppet Name: My weekly failure
+@weekly /bin/false
diff --git a/spec/fixtures/integration/provider/cron/crontab/modify_entry b/spec/fixtures/integration/provider/cron/crontab/modify_entry
new file mode 100644
index 0000000..ed06fd4
--- /dev/null
+++ b/spec/fixtures/integration/provider/cron/crontab/modify_entry
@@ -0,0 +1,13 @@
+# HEADER: some simple
+# HEADER: header
+@daily /bin/unnamed_special_command >> /dev/null 2>&1
+
+# commend with blankline above and below
+
+17-19,22 0-23/2 * * 2 /bin/unnamed_regular_command
+
+# Puppet Name: My daily failure
+MAILTO=""
+@daily /bin/false
+# Puppet Name: Monthly job
+@monthly /usr/bin/monthly
diff --git a/spec/fixtures/integration/provider/cron/crontab/moved_cronjob_input1 b/spec/fixtures/integration/provider/cron/crontab/moved_cronjob_input1
new file mode 100644
index 0000000..2c7d542
--- /dev/null
+++ b/spec/fixtures/integration/provider/cron/crontab/moved_cronjob_input1
@@ -0,0 +1,15 @@
+# HEADER: some simple
+# HEADER: header
+@daily /bin/unnamed_special_command >> /dev/null 2>&1
+
+# commend with blankline above and below
+
+17-19,22 0-23/2 * * 2 /bin/unnamed_regular_command
+
+# Puppet Name: My daily failure
+MAILTO=""
+@daily /bin/false
+# Puppet Name: Monthly job
+SHELL=/bin/sh
+MAILTO=mail@company.com
+15 14 1 * * $HOME/bin/monthly
diff --git a/spec/fixtures/integration/provider/cron/crontab/moved_cronjob_input2 b/spec/fixtures/integration/provider/cron/crontab/moved_cronjob_input2
new file mode 100644
index 0000000..0b68287
--- /dev/null
+++ b/spec/fixtures/integration/provider/cron/crontab/moved_cronjob_input2
@@ -0,0 +1,6 @@
+# HEADER: some simple
+# HEADER: header
+# Puppet Name: some_unrelevant job
+* * * * * /bin/true
+# Puppet Name: My daily failure
+@daily /bin/false
diff --git a/spec/fixtures/integration/provider/cron/crontab/purged b/spec/fixtures/integration/provider/cron/crontab/purged
new file mode 100644
index 0000000..b302836
--- /dev/null
+++ b/spec/fixtures/integration/provider/cron/crontab/purged
@@ -0,0 +1,8 @@
+# HEADER: some simple
+# HEADER: header
+
+# commend with blankline above and below
+
+
+# Puppet Name: only managed entry
+* * * * * /bin/true
diff --git a/spec/fixtures/integration/provider/cron/crontab/remove_named_resource b/spec/fixtures/integration/provider/cron/crontab/remove_named_resource
new file mode 100644
index 0000000..e1c1716
--- /dev/null
+++ b/spec/fixtures/integration/provider/cron/crontab/remove_named_resource
@@ -0,0 +1,12 @@
+# HEADER: some simple
+# HEADER: header
+@daily /bin/unnamed_special_command >> /dev/null 2>&1
+
+# commend with blankline above and below
+
+17-19,22 0-23/2 * * 2 /bin/unnamed_regular_command
+
+# Puppet Name: Monthly job
+SHELL=/bin/sh
+MAILTO=mail@company.com
+15 14 1 * * $HOME/bin/monthly
diff --git a/spec/fixtures/integration/provider/cron/crontab/remove_unnamed_resource b/spec/fixtures/integration/provider/cron/crontab/remove_unnamed_resource
new file mode 100644
index 0000000..2dcbfe2
--- /dev/null
+++ b/spec/fixtures/integration/provider/cron/crontab/remove_unnamed_resource
@@ -0,0 +1,14 @@
+# HEADER: some simple
+# HEADER: header
+@daily /bin/unnamed_special_command >> /dev/null 2>&1
+
+# commend with blankline above and below
+
+
+# Puppet Name: My daily failure
+MAILTO=""
+@daily /bin/false
+# Puppet Name: Monthly job
+SHELL=/bin/sh
+MAILTO=mail@company.com
+15 14 1 * * $HOME/bin/monthly
diff --git a/spec/fixtures/integration/provider/cron/crontab/unspecialized b/spec/fixtures/integration/provider/cron/crontab/unspecialized
new file mode 100644
index 0000000..e6a4082
--- /dev/null
+++ b/spec/fixtures/integration/provider/cron/crontab/unspecialized
@@ -0,0 +1,15 @@
+# HEADER: some simple
+# HEADER: header
+@daily /bin/unnamed_special_command >> /dev/null 2>&1
+
+# commend with blankline above and below
+
+17-19,22 0-23/2 * * 2 /bin/unnamed_regular_command
+
+# Puppet Name: My daily failure
+MAILTO=""
+* * * * * /bin/false
+# Puppet Name: Monthly job
+SHELL=/bin/sh
+MAILTO=mail@company.com
+15 14 1 * * $HOME/bin/monthly
diff --git a/spec/fixtures/unit/provider/cron/crontab/single_line.yaml b/spec/fixtures/unit/provider/cron/crontab/single_line.yaml
new file mode 100644
index 0000000..da2853e
--- /dev/null
+++ b/spec/fixtures/unit/provider/cron/crontab/single_line.yaml
@@ -0,0 +1,272 @@
+---
+:longcommment:
+ :text: "# This is a comment"
+ :record:
+ :line: "# This is a comment"
+ :record_type: :comment
+:special:
+ :text: "@hourly /bin/date"
+ :record:
+ :special: hourly
+ :command: /bin/date
+ :record_type: :crontab
+:long_name:
+ :text: "# Puppet Name: long_name"
+ :record:
+ :line: "# Puppet Name: long_name"
+ :name: long_name
+ :record_type: :comment
+:multiple_minutes:
+ :text: 5,15 * * * * /bin/date
+ :record:
+ :minute:
+ - "5"
+ - "15"
+ :command: /bin/date
+ :record_type: :crontab
+:environment:
+ :text: ONE=TWO
+ :record:
+ :line: ONE=TWO
+ :record_type: :environment
+:empty:
+ :text: ""
+ :record:
+ :line: ""
+ :record_type: :blank
+:simple:
+ :text: "* * * * * /bin/date"
+ :record:
+ :command: /bin/date
+ :record_type: :crontab
+:whitespace:
+ :text: " "
+ :record:
+ :line: " "
+ :record_type: :blank
+:minute_and_hour:
+ :text: 5 15 * * * /bin/date
+ :record:
+ :minute:
+ - "5"
+ :hour:
+ - "15"
+ :command: /bin/date
+ :record_type: :crontab
+:lowercase_environment:
+ :text: a=b
+ :record:
+ :line: a=b
+ :record_type: :environment
+:special_with_spaces:
+ :text: "@daily /bin/echo testing"
+ :record:
+ :special: daily
+ :command: /bin/echo testing
+ :record_type: :crontab
+:tabs:
+ :text: !binary |
+ CQ==
+
+ :record:
+ :line: !binary |
+ CQ==
+
+ :record_type: :blank
+:multiple_minute_and_hour:
+ :text: 5,10 15,20 * * * /bin/date
+ :record:
+ :minute:
+ - "5"
+ - "10"
+ :hour:
+ - "15"
+ - "20"
+ :command: /bin/date
+ :record_type: :crontab
+:name:
+ :text: "# Puppet Name: testing"
+ :record:
+ :line: "# Puppet Name: testing"
+ :name: testing
+ :record_type: :comment
+:another_env:
+ :text: Testing=True
+ :record:
+ :line: Testing=True
+ :record_type: :environment
+:shortcomment:
+ :text: "#"
+ :record:
+ :line: "#"
+ :record_type: :comment
+:spaces_in_command:
+ :text: "* * * * * /bin/echo testing"
+ :record:
+ :command: /bin/echo testing
+ :record_type: :crontab
+:fourth_env:
+ :text: True=False
+ :record:
+ :line: True=False
+ :record_type: :environment
+:simple_with_minute:
+ :text: 5 * * * * /bin/date
+ :record:
+ :minute:
+ - "5"
+ :command: /bin/date
+ :record_type: :crontab
+:spaces_in_command_with_times:
+ :text: 5,10 15,20 * * * /bin/echo testing
+ :record:
+ :minute:
+ - "5"
+ - "10"
+ :hour:
+ - "15"
+ - "20"
+ :command: /bin/echo testing
+ :record_type: :crontab
+:name_with_spaces:
+ :text: "# Puppet Name: another name"
+ :record:
+ :line: "# Puppet Name: another name"
+ :name: another name
+ :record_type: :comment
+---
+:longcommment:
+ :text: "# This is a comment"
+ :record:
+ :line: "# This is a comment"
+ :record_type: :comment
+:special:
+ :text: "@hourly /bin/date"
+ :record:
+ :special: hourly
+ :command: /bin/date
+ :record_type: :crontab
+:long_name:
+ :text: "# Puppet Name: long_name"
+ :record:
+ :line: "# Puppet Name: long_name"
+ :name: long_name
+ :record_type: :comment
+:multiple_minutes:
+ :text: 5,15 * * * * /bin/date
+ :record:
+ :minute:
+ - "5"
+ - "15"
+ :command: /bin/date
+ :record_type: :crontab
+:environment:
+ :text: ONE=TWO
+ :record:
+ :line: ONE=TWO
+ :record_type: :environment
+:empty:
+ :text: ""
+ :record:
+ :line: ""
+ :record_type: :blank
+:simple:
+ :text: "* * * * * /bin/date"
+ :record:
+ :command: /bin/date
+ :record_type: :crontab
+:whitespace:
+ :text: " "
+ :record:
+ :line: " "
+ :record_type: :blank
+:minute_and_hour:
+ :text: 5 15 * * * /bin/date
+ :record:
+ :minute:
+ - "5"
+ :hour:
+ - "15"
+ :command: /bin/date
+ :record_type: :crontab
+:lowercase_environment:
+ :text: a=b
+ :record:
+ :line: a=b
+ :record_type: :environment
+:special_with_spaces:
+ :text: "@daily /bin/echo testing"
+ :record:
+ :special: daily
+ :command: /bin/echo testing
+ :record_type: :crontab
+:tabs:
+ :text: !binary |
+ CQ==
+
+ :record:
+ :line: !binary |
+ CQ==
+
+ :record_type: :blank
+:multiple_minute_and_hour:
+ :text: 5,10 15,20 * * * /bin/date
+ :record:
+ :minute:
+ - "5"
+ - "10"
+ :hour:
+ - "15"
+ - "20"
+ :command: /bin/date
+ :record_type: :crontab
+:name:
+ :text: "# Puppet Name: testing"
+ :record:
+ :line: "# Puppet Name: testing"
+ :name: testing
+ :record_type: :comment
+:another_env:
+ :text: Testing=True
+ :record:
+ :line: Testing=True
+ :record_type: :environment
+:shortcomment:
+ :text: "#"
+ :record:
+ :line: "#"
+ :record_type: :comment
+:spaces_in_command:
+ :text: "* * * * * /bin/echo testing"
+ :record:
+ :command: /bin/echo testing
+ :record_type: :crontab
+:fourth_env:
+ :text: True=False
+ :record:
+ :line: True=False
+ :record_type: :environment
+:simple_with_minute:
+ :text: 5 * * * * /bin/date
+ :record:
+ :minute:
+ - "5"
+ :command: /bin/date
+ :record_type: :crontab
+:spaces_in_command_with_times:
+ :text: 5,10 15,20 * * * /bin/echo testing
+ :record:
+ :minute:
+ - "5"
+ - "10"
+ :hour:
+ - "15"
+ - "20"
+ :command: /bin/echo testing
+ :record_type: :crontab
+:name_with_spaces:
+ :text: "# Puppet Name: another name"
+ :record:
+ :line: "# Puppet Name: another name"
+ :name: another name
+ :record_type: :comment
diff --git a/spec/fixtures/unit/provider/cron/crontab/vixie_header.txt b/spec/fixtures/unit/provider/cron/crontab/vixie_header.txt
new file mode 100644
index 0000000..7ccfc37
--- /dev/null
+++ b/spec/fixtures/unit/provider/cron/crontab/vixie_header.txt
@@ -0,0 +1,3 @@
+# DO NOT EDIT THIS FILE - edit the master and reinstall.
+# (- installed on Thu Apr 12 12:16:01 2007)
+# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
diff --git a/spec/fixtures/unit/provider/cron/parsed/managed b/spec/fixtures/unit/provider/cron/parsed/managed
new file mode 100644
index 0000000..c48d20a
--- /dev/null
+++ b/spec/fixtures/unit/provider/cron/parsed/managed
@@ -0,0 +1,6 @@
+# Puppet Name: real_job
+* * * * * /bin/true
+# Puppet Name: complex_job
+MAILTO=foo@example.com
+SHELL=/bin/sh
+@reboot /bin/true >> /dev/null 2>&1
diff --git a/spec/fixtures/unit/provider/cron/parsed/simple b/spec/fixtures/unit/provider/cron/parsed/simple
new file mode 100644
index 0000000..477553e
--- /dev/null
+++ b/spec/fixtures/unit/provider/cron/parsed/simple
@@ -0,0 +1,9 @@
+# use /bin/sh to run commands, no matter what /etc/passwd says
+SHELL=/bin/sh
+# mail any output to `paul', no matter whose crontab this is
+MAILTO=paul
+#
+# run five minutes after midnight, every day
+5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
+# run at 2:15pm on the first of every month -- output mailed to paul
+15 14 1 * * $HOME/bin/monthly
diff --git a/spec/integration/provider/cron/crontab_spec.rb b/spec/integration/provider/cron/crontab_spec.rb
new file mode 100644
index 0000000..989eae6
--- /dev/null
+++ b/spec/integration/provider/cron/crontab_spec.rb
@@ -0,0 +1,237 @@
+require 'spec_helper'
+require 'puppet/file_bucket/dipper'
+require 'puppet_spec/compiler'
+
+describe Puppet::Type.type(:cron).provider(:crontab), unless: Puppet.features.microsoft_windows? do
+ include PuppetSpec::Files
+ include PuppetSpec::Compiler
+
+ before :each do
+ Puppet::Type.type(:cron).stubs(:defaultprovider).returns described_class
+ described_class.stubs(:suitable?).returns true
+ Puppet::FileBucket::Dipper.any_instance.stubs(:backup) # rubocop:disable RSpec/AnyInstance
+
+ # I don't want to execute anything
+ described_class.stubs(:filetype).returns Puppet::Util::FileType::FileTypeFlat
+ described_class.stubs(:default_target).returns crontab_user1
+
+ # I don't want to stub Time.now to get a static header because I don't know
+ # where Time.now is used elsewhere, so just go with a very simple header
+ described_class.stubs(:header).returns "# HEADER: some simple\n# HEADER: header\n"
+ FileUtils.cp(my_fixture('crontab_user1'), crontab_user1)
+ FileUtils.cp(my_fixture('crontab_user2'), crontab_user2)
+ end
+
+ after :each do
+ described_class.clear
+ end
+
+ let :crontab_user1 do
+ tmpfile('cron_integration_specs')
+ end
+
+ let :crontab_user2 do
+ tmpfile('cron_integration_specs')
+ end
+
+ def expect_output(fixture_name)
+ expect(File.read(crontab_user1)).to eq(File.read(my_fixture(fixture_name)))
+ end
+
+ describe 'when managing a cron entry' do
+ it 'is able to purge unmanaged entries' do
+ apply_with_error_check(<<-MANIFEST)
+ cron {
+ 'only managed entry':
+ ensure => 'present',
+ command => '/bin/true',
+ target => '#{crontab_user1}',
+ }
+ resources { 'cron': purge => 'true' }
+ MANIFEST
+ expect_output('purged')
+ end
+
+ describe 'with ensure absent' do
+ it 'does nothing if entry already absent' do
+ apply_with_error_check(<<-MANIFEST)
+ cron {
+ 'no_such_entry':
+ ensure => 'absent',
+ target => '#{crontab_user1}',
+ }
+ MANIFEST
+ expect_output('crontab_user1')
+ end
+
+ it 'removes the resource from crontab if present' do
+ apply_with_error_check(<<-MANIFEST)
+ cron {
+ 'My daily failure':
+ ensure => 'absent',
+ target => '#{crontab_user1}',
+ }
+ MANIFEST
+ expect_output('remove_named_resource')
+ end
+
+ it 'removes a matching cronentry if present' do
+ apply_with_error_check(<<-MANIFEST)
+ cron {
+ 'no_such_named_resource_in_crontab':
+ ensure => absent,
+ minute => [ '17-19', '22' ],
+ hour => [ '0-23/2' ],
+ weekday => 'Tue',
+ command => '/bin/unnamed_regular_command',
+ target => '#{crontab_user1}',
+ }
+ MANIFEST
+ expect_output('remove_unnamed_resource')
+ end
+ end
+
+ describe 'with ensure present' do
+ context 'and no command specified' do
+ it 'works if the resource is already present' do
+ apply_with_error_check(<<-MANIFEST)
+ cron {
+ 'My daily failure':
+ special => 'daily',
+ target => '#{crontab_user1}',
+ }
+ MANIFEST
+ expect_output('crontab_user1')
+ end
+ it 'fails if the resource needs creating' do
+ manifest = <<-MANIFEST
+ cron {
+ 'Entirely new resource':
+ special => 'daily',
+ target => '#{crontab_user1}',
+ }
+ MANIFEST
+ apply_compiled_manifest(manifest) do |res|
+ if res.ref == 'Cron[Entirely new resource]'
+ res.expects(:err).with(regexp_matches(%r{no command}))
+ else
+ res.expects(:err).never
+ end
+ end
+ end
+ end
+
+ it 'does nothing if entry already present' do
+ apply_with_error_check(<<-MANIFEST)
+ cron {
+ 'My daily failure':
+ special => 'daily',
+ command => '/bin/false',
+ target => '#{crontab_user1}',
+ }
+ MANIFEST
+ expect_output('crontab_user1')
+ end
+
+ it "works correctly when managing 'target' but not 'user'" do
+ apply_with_error_check(<<-MANIFEST)
+ cron {
+ 'My weekly failure':
+ special => 'weekly',
+ command => '/bin/false',
+ target => '#{crontab_user1}',
+ }
+ MANIFEST
+ expect_output('crontab_user3')
+ end
+
+ it 'does nothing if a matching entry already present' do
+ apply_with_error_check(<<-MANIFEST)
+ cron {
+ 'no_such_named_resource_in_crontab':
+ ensure => present,
+ minute => [ '17-19', '22' ],
+ hour => [ '0-23/2' ],
+ command => '/bin/unnamed_regular_command',
+ target => '#{crontab_user1}',
+ }
+ MANIFEST
+ expect_output('crontab_user1')
+ end
+
+ it 'adds a new normal entry if currently absent' do
+ apply_with_error_check(<<-MANIFEST)
+ cron {
+ 'new entry':
+ ensure => present,
+ minute => '12',
+ weekday => 'Tue',
+ command => '/bin/new',
+ environment => [
+ 'MAILTO=""',
+ 'SHELL=/bin/bash'
+ ],
+ target => '#{crontab_user1}',
+ }
+ MANIFEST
+ expect_output('create_normal_entry')
+ end
+
+ it 'adds a new special entry if currently absent' do
+ apply_with_error_check(<<-MANIFEST)
+ cron {
+ 'new special entry':
+ ensure => present,
+ special => 'reboot',
+ command => 'echo "Booted" 1>&2',
+ environment => 'MAILTO=bob@company.com',
+ target => '#{crontab_user1}',
+ }
+ MANIFEST
+ expect_output('create_special_entry')
+ end
+
+ it 'changes existing entry if out of sync' do
+ apply_with_error_check(<<-MANIFEST)
+ cron {
+ 'Monthly job':
+ ensure => present,
+ special => 'monthly',
+ #minute => ['22'],
+ command => '/usr/bin/monthly',
+ environment => [],
+ target => '#{crontab_user1}',
+ }
+ MANIFEST
+ expect_output('modify_entry')
+ end
+ it 'changes a special schedule to numeric if requested' do
+ apply_with_error_check(<<-MANIFEST)
+ cron {
+ 'My daily failure':
+ special => 'absent',
+ command => '/bin/false',
+ target => '#{crontab_user1}',
+ }
+ MANIFEST
+ expect_output('unspecialized')
+ end
+ it 'does not try to move an entry from one file to another' do
+ # force the parsedfile provider to also parse user1's crontab
+ apply_with_error_check(<<-MANIFEST)
+ cron {
+ 'foo':
+ ensure => absent,
+ target => '#{crontab_user1}';
+ 'My daily failure':
+ special => 'daily',
+ command => "/bin/false",
+ target => '#{crontab_user2}',
+ }
+ MANIFEST
+ expect(File.read(crontab_user1)).to eq(File.read(my_fixture('moved_cronjob_input1')))
+ expect(File.read(crontab_user2)).to eq(File.read(my_fixture('moved_cronjob_input2')))
+ end
+ end
+ end
+end
diff --git a/spec/lib/puppet_spec/compiler.rb b/spec/lib/puppet_spec/compiler.rb
new file mode 100644
index 0000000..89c97a5
--- /dev/null
+++ b/spec/lib/puppet_spec/compiler.rb
@@ -0,0 +1,112 @@
+module PuppetSpec::Compiler
+ module_function
+
+ def compile_to_catalog(string, node = Puppet::Node.new('test'))
+ Puppet[:code] = string
+ # see lib/puppet/indirector/catalog/compiler.rb#filter
+ Puppet::Parser::Compiler.compile(node).filter { |r| r.virtual? }
+ end
+
+ # Does not removed virtual resources in compiled catalog (i.e. keeps unrealized)
+ def compile_to_catalog_unfiltered(string, node = Puppet::Node.new('test'))
+ Puppet[:code] = string
+ # see lib/puppet/indirector/catalog/compiler.rb#filter
+ Puppet::Parser::Compiler.compile(node)
+ end
+
+ def compile_to_ral(manifest, node = Puppet::Node.new('test'))
+ catalog = compile_to_catalog(manifest, node)
+ ral = catalog.to_ral
+ ral.finalize
+ ral
+ end
+
+ def compile_to_relationship_graph(manifest, prioritizer = Puppet::Graph::SequentialPrioritizer.new)
+ ral = compile_to_ral(manifest)
+ graph = Puppet::Graph::RelationshipGraph.new(prioritizer)
+ graph.populate_from(ral)
+ graph
+ end
+
+ def apply_compiled_manifest(manifest, prioritizer = Puppet::Graph::SequentialPrioritizer.new)
+ catalog = compile_to_ral(manifest)
+ if block_given?
+ catalog.resources.each { |res| yield res }
+ end
+ transaction = Puppet::Transaction.new(catalog,
+ Puppet::Transaction::Report.new,
+ prioritizer)
+ transaction.evaluate
+ transaction.report.finalize_report
+
+ transaction
+ end
+
+ def apply_with_error_check(manifest)
+ apply_compiled_manifest(manifest) do |res|
+ res.expects(:err).never
+ end
+ end
+
+ def order_resources_traversed_in(relationships)
+ order_seen = []
+ relationships.traverse { |resource| order_seen << resource.ref }
+ order_seen
+ end
+
+ def collect_notices(code, node = Puppet::Node.new('foonode'))
+ Puppet[:code] = code
+ compiler = Puppet::Parser::Compiler.new(node)
+ node.environment.check_for_reparse
+ logs = []
+ Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
+ yield(compiler)
+ end
+ logs = logs.select { |log| log.level == :notice }.map { |log| log.message }
+ logs
+ end
+
+ def eval_and_collect_notices(code, node = Puppet::Node.new('foonode'), topscope_vars = {})
+ collect_notices(code, node) do |compiler|
+ unless topscope_vars.empty?
+ scope = compiler.topscope
+ topscope_vars.each { |k, v| scope.setvar(k, v) }
+ end
+ if block_given?
+ compiler.compile do |catalog|
+ yield(compiler.topscope, catalog)
+ catalog
+ end
+ else
+ compiler.compile
+ end
+ end
+ end
+
+ # Compiles a catalog, and if source is given evaluates it and returns its result.
+ # The catalog is returned if no source is given.
+ # Topscope variables are set before compilation
+ # Uses a created node 'testnode' if none is given.
+ # (Parameters given by name)
+ #
+ def evaluate(code: 'undef', source: nil, node: Puppet::Node.new('testnode'), variables: {})
+ source_location = caller(0..0)
+ Puppet[:code] = code
+ compiler = Puppet::Parser::Compiler.new(node)
+ unless variables.empty?
+ scope = compiler.topscope
+ variables.each { |k, v| scope.setvar(k, v) }
+ end
+
+ if source.nil?
+ compiler.compile
+ # see lib/puppet/indirector/catalog/compiler.rb#filter
+ return compiler.filter { |r| r.virtual? }
+ end
+
+ # evaluate given source is the context of the compiled state and return its result
+ compiler.compile do |_catalog|
+ Puppet::Pops::Parser::EvaluatingParser.singleton.evaluate_string(compiler.topscope, source, source_location)
+ end
+ end
+end
diff --git a/spec/lib/puppet_spec/files.rb b/spec/lib/puppet_spec/files.rb
new file mode 100644
index 0000000..c8315c9
--- /dev/null
+++ b/spec/lib/puppet_spec/files.rb
@@ -0,0 +1,112 @@
+require 'fileutils'
+require 'tempfile'
+require 'tmpdir'
+require 'pathname'
+
+# A support module for testing files.
+module PuppetSpec::Files
+ def self.cleanup
+ # rubocop:disable Style/GlobalVars
+ $global_tempfiles ||= []
+ $global_tempfiles.each do |path|
+ begin
+ Dir.unstub(:entries)
+ FileUtils.rm_rf path, secure: true
+ rescue Errno::ENOENT # rubocop:disable Lint/HandleExceptions
+ # nothing to do
+ end
+ end
+ $global_tempfiles = []
+ # rubocop:enable Style/GlobalVars
+ end
+
+ module_function
+
+ def make_absolute(path)
+ path = File.expand_path(path)
+ path[0] = 'c' if Puppet.features.microsoft_windows?
+ path
+ end
+
+ def tmpfile(name, dir = nil)
+ dir ||= Dir.tmpdir
+ path = Puppet::FileSystem.expand_path(make_tmpname(name, nil).encode(Encoding::UTF_8), dir)
+ record_tmp(File.expand_path(path))
+
+ path
+ end
+
+ def file_containing(name, contents)
+ file = tmpfile(name)
+ File.open(file, 'wb') { |f| f.write(contents) }
+ file
+ end
+
+ def script_containing(name, contents)
+ file = tmpfile(name)
+ if Puppet.features.microsoft_windows?
+ file += '.bat'
+ text = contents[:windows]
+ else
+ text = contents[:posix]
+ end
+ File.open(file, 'wb') { |f| f.write(text) }
+ Puppet::FileSystem.chmod(0o755, file)
+ file
+ end
+
+ def tmpdir(name)
+ dir = Puppet::FileSystem.expand_path(Dir.mktmpdir(name).encode!(Encoding::UTF_8))
+
+ record_tmp(dir)
+
+ dir
+ end
+
+ # Copied from ruby 2.4 source
+ def make_tmpname((prefix, suffix), n)
+ prefix = (String.try_convert(prefix) ||
+ raise(ArgumentError, "unexpected prefix: #{prefix.inspect}"))
+ suffix &&= (String.try_convert(suffix) ||
+ raise(ArgumentError, "unexpected suffix: #{suffix.inspect}"))
+ t = Time.now.strftime('%Y%m%d')
+ path = "#{prefix}#{t}-#{$PROCESS_ID}-#{rand(0x100000000).to_s(36)}".dup
+ path << "-#{n}" if n
+ path << suffix if suffix
+ path
+ end
+
+ def dir_containing(name, contents_hash)
+ dir_contained_in(tmpdir(name), contents_hash)
+ end
+
+ def dir_contained_in(dir, contents_hash)
+ contents_hash.each do |k, v|
+ if v.is_a?(Hash)
+ Dir.mkdir(tmp = File.join(dir, k))
+ dir_contained_in(tmp, v)
+ else
+ file = File.join(dir, k)
+ File.open(file, 'wb') { |f| f.write(v) }
+ end
+ end
+ dir
+ end
+
+ def record_tmp(tmp)
+ # rubocop:disable Style/GlobalVars
+ $global_tempfiles ||= []
+ $global_tempfiles << tmp
+ # rubocop:enable Style/GlobalVars
+ end
+
+ def expect_file_mode(file, mode)
+ actual_mode = '%o' % Puppet::FileSystem.stat(file).mode
+ target_mode = if Puppet.features.microsoft_windows?
+ mode
+ else
+ '10' + '%04i' % mode.to_i
+ end
+ expect(actual_mode).to eq(target_mode)
+ end
+end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
new file mode 100644
index 0000000..5e721b7
--- /dev/null
+++ b/spec/spec_helper.rb
@@ -0,0 +1,44 @@
+require 'puppetlabs_spec_helper/module_spec_helper'
+require 'rspec-puppet-facts'
+
+begin
+ require 'spec_helper_local' if File.file?(File.join(File.dirname(__FILE__), 'spec_helper_local.rb'))
+rescue LoadError => loaderror
+ warn "Could not require spec_helper_local: #{loaderror.message}"
+end
+
+include RspecPuppetFacts
+
+default_facts = {
+ puppetversion: Puppet.version,
+ facterversion: Facter.version,
+}
+
+default_facts_path = File.expand_path(File.join(File.dirname(__FILE__), 'default_facts.yml'))
+default_module_facts_path = File.expand_path(File.join(File.dirname(__FILE__), 'default_module_facts.yml'))
+
+if File.exist?(default_facts_path) && File.readable?(default_facts_path)
+ default_facts.merge!(YAML.safe_load(File.read(default_facts_path)))
+end
+
+if File.exist?(default_module_facts_path) && File.readable?(default_module_facts_path)
+ default_facts.merge!(YAML.safe_load(File.read(default_module_facts_path)))
+end
+
+RSpec.configure do |c|
+ c.default_facts = default_facts
+ c.before :each do
+ # set to strictest setting for testing
+ # by default Puppet runs at warning level
+ Puppet.settings[:strict] = :warning
+ end
+end
+
+def ensure_module_defined(module_name)
+ module_name.split('::').reduce(Object) do |last_module, next_module|
+ last_module.const_set(next_module, Module.new) unless last_module.const_defined?(next_module)
+ last_module.const_get(next_module)
+ end
+end
+
+# 'spec_overrides' from sync.yml will appear below this line
diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb
new file mode 100644
index 0000000..4759227
--- /dev/null
+++ b/spec/spec_helper_acceptance.rb
@@ -0,0 +1,40 @@
+require 'beaker-rspec'
+require 'beaker/module_install_helper'
+require 'beaker/puppet_install_helper'
+
+$LOAD_PATH << File.join(__dir__, 'acceptance/lib')
+
+def beaker_opts
+ { debug: true, trace: true, expect_failures: true, acceptable_exit_codes: (0...256) }
+ # { expect_failures: true, acceptable_exit_codes: (0...256) }
+end
+
+def compatible_agents
+ agents.reject { |agent| agent['platform'].include?('windows') || agent['platform'].include?('eos-') || agent['platform'].include?('fedora-28') }
+end
+
+def clean(agent, o = {})
+ o = { user: 'tstuser' }.merge(o)
+ run_cron_on(agent, :remove, o[:user])
+ apply_manifest_on(agent, %([user{'%s': ensure => absent, managehome => false }]) % o[:user])
+end
+
+def setup(agent, o = {})
+ o = { user: 'tstuser' }.merge(o)
+ apply_manifest_on(agent, %(user { '%s': ensure => present, managehome => false }) % o[:user])
+ apply_manifest_on(agent, %(case $operatingsystem {
+ centos, redhat: {$cron = 'cronie'}
+ solaris: { $cron = 'core-os' }
+ default: {$cron ='cron'} }
+ package {'cron': name=> $cron, ensure=>present, }))
+end
+
+RSpec.configure do |c|
+ c.before :suite do
+ unless ENV['BEAKER_provision'] == 'no'
+ run_puppet_install_helper
+ install_module_on(hosts_as('default'))
+ install_module_dependencies_on(hosts)
+ end
+ end
+end
diff --git a/spec/spec_helper_local.rb b/spec/spec_helper_local.rb
new file mode 100644
index 0000000..f06b4bb
--- /dev/null
+++ b/spec/spec_helper_local.rb
@@ -0,0 +1,17 @@
+dir = File.expand_path(File.dirname(__FILE__))
+$LOAD_PATH.unshift File.join(dir, 'lib')
+
+# Container for various Puppet-specific RSpec helpers.
+module PuppetSpec
+end
+
+require 'puppet_spec/files'
+
+RSpec.configure do |config|
+ config.before :each do |_test|
+ base = PuppetSpec::Files.tmpdir('tmp_settings')
+ Puppet[:vardir] = File.join(base, 'var')
+
+ FileUtils.mkdir_p Puppet[:statedir]
+ end
+end
diff --git a/spec/unit/provider/cron/crontab_spec.rb b/spec/unit/provider/cron/crontab_spec.rb
new file mode 100644
index 0000000..031b3ae
--- /dev/null
+++ b/spec/unit/provider/cron/crontab_spec.rb
@@ -0,0 +1,205 @@
+require 'spec_helper'
+
+describe Puppet::Type.type(:cron).provider(:crontab) do
+ subject do
+ provider = Puppet::Type.type(:cron).provider(:crontab)
+ provider.initvars
+ provider
+ end
+
+ def compare_crontab_text(have, want)
+ # We should have four header lines, and then the text...
+ expect(have.lines.to_a[0..3]).to(be_all { |x| x =~ %r{^# } })
+ expect(have.lines.to_a[4..-1].join('')).to eq(want)
+ end
+
+ context 'with the simple samples' do
+ FIELDS = {
+ crontab: ['command', 'minute', 'hour', 'month', 'monthday', 'weekday'].map { |o| o.to_sym },
+ environment: [:line],
+ blank: [:line],
+ comment: [:line],
+ }.freeze
+
+ def compare_crontab_record(have, want)
+ want.each do |param, value|
+ expect(have).to be_key param
+ expect(have[param]).to eq(value)
+ end
+
+ (FIELDS[have[:record_type]] - want.keys).each do |name|
+ expect(have[name]).to eq(:absent)
+ end
+ end
+
+ ########################################################################
+ # Simple input fixtures for testing.
+ samples = YAML.load(File.read(my_fixture('single_line.yaml'))) # rubocop:disable Security/YAMLLoad
+
+ samples.each do |name, data|
+ it "should parse crontab line #{name} correctly" do
+ compare_crontab_record subject.parse_line(data[:text]), data[:record]
+ end
+
+ it "should reconstruct the crontab line #{name} from the record" do
+ expect(subject.to_line(data[:record])).to eq(data[:text])
+ end
+ end
+
+ records = []
+ text = ''
+
+ # Sorting is from the original, and avoids :empty being the last line,
+ # since the provider will ignore that and cause this to fail.
+ samples.sort_by { |x| x.first.to_s }.each do |_name, data|
+ records << data[:record]
+ text << data[:text] + "\n"
+ end
+
+ it 'parses all sample records at once' do
+ subject.parse(text).zip(records).each do |round|
+ compare_crontab_record(*round)
+ end
+ end
+
+ it 'reconstitutes the file from the records' do
+ compare_crontab_text subject.to_file(records), text
+ end
+
+ context 'multi-line crontabs' do
+ tests = { simple: [:spaces_in_command_with_times],
+ with_name: [:name, :spaces_in_command_with_times],
+ with_env: [:environment, :spaces_in_command_with_times],
+ with_multiple_envs: [:environment, :lowercase_environment, :spaces_in_command_with_times],
+ with_name_and_env: [:name_with_spaces, :another_env, :spaces_in_command_with_times],
+ with_name_and_multiple_envs: [:long_name, :another_env, :fourth_env, :spaces_in_command_with_times] }
+
+ all_records = []
+ all_text = ''
+
+ tests.each do |name, content|
+ data = content.map { |x| samples[x] || raise("missing sample data #{x}") }
+ text = data.map { |x| x[:text] }.join("\n") + "\n"
+ records = data.map { |x| x[:record] }
+
+ # Capture the whole thing for later, too...
+ all_records += records
+ all_text += text
+
+ context name.to_s.tr('_', ' ') do
+ it 'regenerates the text from the record' do
+ compare_crontab_text subject.to_file(records), text
+ end
+
+ it 'parses the records from the text' do
+ subject.parse(text).zip(records).each do |round|
+ compare_crontab_record(*round)
+ end
+ end
+ end
+ end
+
+ it 'parses the whole set of records from the text' do
+ subject.parse(all_text).zip(all_records).each do |round|
+ compare_crontab_record(*round)
+ end
+ end
+
+ it 'regenerates the whole text from the set of all records' do
+ compare_crontab_text subject.to_file(all_records), all_text
+ end
+ end
+ end
+
+ context 'when receiving a vixie cron header from the cron interface' do
+ it 'does not write that header back to disk' do
+ vixie_header = File.read(my_fixture('vixie_header.txt'))
+ vixie_records = subject.parse(vixie_header)
+ compare_crontab_text subject.to_file(vixie_records), ''
+ end
+ end
+
+ context 'when adding a cronjob with the same command as an existing job' do
+ let(:record) { { name: 'existing', user: 'root', command: '/bin/true', record_type: :crontab } }
+ let(:resource) { Puppet::Type::Cron.new(name: 'test', user: 'root', command: '/bin/true') }
+ let(:resources) { { 'test' => resource } }
+
+ before :each do
+ subject.stubs(:prefetch_all_targets).returns([record])
+ end
+
+ # this would be a more fitting test, but I haven't yet
+ # figured out how to get it working
+ # it "should include both jobs in the output" do
+ # subject.prefetch(resources)
+ # class Puppet::Provider::ParsedFile
+ # def self.records
+ # @records
+ # end
+ # end
+ # subject.to_file(subject.records).should match /Puppet name: test/
+ # end
+
+ it "does not base the new resource's provider on the existing record" do
+ subject.expects(:new).with(record).never
+ subject.stubs(:new)
+ subject.prefetch(resources)
+ end
+ end
+
+ context 'when prefetching an entry now managed for another user' do
+ let(:resource) do
+ s = stub(:resource)
+ s.stubs(:[]).with(:user).returns 'root'
+ s.stubs(:[]).with(:target).returns 'root'
+ s
+ end
+
+ let(:record) { { name: 'test', user: 'nobody', command: '/bin/true', record_type: :crontab } }
+ let(:resources) { { 'test' => resource } }
+
+ before :each do
+ subject.stubs(:prefetch_all_targets).returns([record])
+ end
+
+ it 'tries and use the match method to find a more fitting record' do
+ subject.expects(:match).with(record, resources)
+ subject.prefetch(resources)
+ end
+
+ it 'does not match a provider to the resource' do
+ resource.expects(:provider=).never
+ subject.prefetch(resources)
+ end
+
+ it 'does not find the resource when looking up the on-disk record' do
+ subject.prefetch(resources)
+ expect(subject.resource_for_record(record, resources)).to be_nil
+ end
+ end
+
+ context 'when matching resources to existing crontab entries' do
+ let(:first_resource) { Puppet::Type::Cron.new(name: :one, user: 'root', command: '/bin/true') }
+ let(:second_resource) { Puppet::Type::Cron.new(name: :two, user: 'nobody', command: '/bin/false') }
+
+ let(:resources) { { one: first_resource, two: second_resource } }
+
+ describe 'with a record with a matching name and mismatching user (#2251)' do
+ # Puppet::Resource objects have #should defined on them, so in these
+ # examples we have to use the monkey patched `must` alias for the rspec
+ # `should` method.
+
+ it "doesn't match the record to the resource" do
+ record = { name: :one, user: 'notroot', record_type: :crontab }
+ expect(subject.resource_for_record(record, resources)).to be_nil
+ end
+ end
+
+ describe 'with a record with a matching name and matching user' do
+ it 'matches the record to the resource' do
+ record = { name: :two, target: 'nobody', command: '/bin/false' }
+ expect(subject.resource_for_record(record, resources)).to eq(second_resource)
+ end
+ end
+ end
+end
diff --git a/spec/unit/provider/cron/parsed_spec.rb b/spec/unit/provider/cron/parsed_spec.rb
new file mode 100644
index 0000000..d4460f7
--- /dev/null
+++ b/spec/unit/provider/cron/parsed_spec.rb
@@ -0,0 +1,335 @@
+require 'spec_helper'
+
+describe Puppet::Type.type(:cron).provider(:crontab) do
+ let :provider do
+ described_class.new(command: '/bin/true')
+ end
+
+ let :resource do
+ Puppet::Type.type(:cron).new(
+ minute: ['0', '15', '30', '45'],
+ hour: ['8-18', '20-22'],
+ monthday: ['31'],
+ month: ['12'],
+ weekday: ['7'],
+ name: 'basic',
+ command: '/bin/true',
+ target: 'root',
+ provider: provider,
+ )
+ end
+
+ let :resource_special do
+ Puppet::Type.type(:cron).new(
+ special: 'reboot',
+ name: 'special',
+ command: '/bin/true',
+ target: 'nobody',
+ )
+ end
+
+ let :resource_sparse do
+ Puppet::Type.type(:cron).new(
+ minute: ['42'],
+ target: 'root',
+ name: 'sparse',
+ )
+ end
+
+ let :record_special do
+ {
+ record_type: :crontab,
+ special: 'reboot',
+ command: '/bin/true',
+ on_disk: true,
+ target: 'nobody',
+ }
+ end
+
+ let :record do
+ {
+ record_type: :crontab,
+ minute: ['0', '15', '30', '45'],
+ hour: ['8-18', '20-22'],
+ monthday: ['31'],
+ month: ['12'],
+ weekday: ['7'],
+ special: :absent,
+ command: '/bin/true',
+ on_disk: true,
+ target: 'root',
+ }
+ end
+
+ describe 'when determining the correct filetype' do
+ it 'uses the suntab filetype on Solaris' do
+ Facter.stubs(:value).with(:osfamily).returns 'Solaris'
+ expect(described_class.filetype).to eq(Puppet::Util::FileType::FileTypeSuntab)
+ end
+
+ it 'uses the aixtab filetype on AIX' do
+ Facter.stubs(:value).with(:osfamily).returns 'AIX'
+ expect(described_class.filetype).to eq(Puppet::Util::FileType::FileTypeAixtab)
+ end
+
+ it 'uses the crontab filetype on other platforms' do
+ Facter.stubs(:value).with(:osfamily).returns 'Not a real operating system family'
+ expect(described_class.filetype).to eq(Puppet::Util::FileType::FileTypeCrontab)
+ end
+ end
+
+ # I'd use ENV.expects(:[]).with('USER') but this does not work because
+ # ENV["USER"] is evaluated at load time.
+ describe 'when determining the default target' do
+ it "should use the current user #{ENV['USER']}", if: ENV['USER'] do
+ expect(described_class.default_target).to eq(ENV['USER'])
+ end
+
+ it 'fallbacks to root', unless: ENV['USER'] do
+ expect(described_class.default_target).to eq('root')
+ end
+ end
+
+ describe '.targets' do
+ let(:tabs) { [described_class.default_target] + ['foo', 'bar'] }
+
+ before(:each) do
+ File.expects(:readable?).returns true
+ File.stubs(:file?).returns true
+ File.stubs(:writable?).returns true
+ end
+ after(:each) do
+ File.unstub :readable?, :file?, :writable?
+ Dir.unstub :foreach
+ end
+ it 'adds all crontabs as targets' do
+ Dir.expects(:foreach).multiple_yields(*tabs)
+ expect(described_class.targets).to eq(tabs)
+ end
+ end
+
+ describe 'when parsing a record' do
+ it 'parses a comment' do
+ expect(described_class.parse_line('# This is a test')).to eq(record_type: :comment,
+ line: '# This is a test')
+ end
+
+ it 'gets the resource name of a PUPPET NAME comment' do
+ expect(described_class.parse_line('# Puppet Name: My Fancy Cronjob')).to eq(record_type: :comment,
+ name: 'My Fancy Cronjob',
+ line: '# Puppet Name: My Fancy Cronjob')
+ end
+
+ it 'ignores blank lines' do
+ expect(described_class.parse_line('')).to eq(record_type: :blank, line: '')
+ expect(described_class.parse_line(' ')).to eq(record_type: :blank, line: ' ')
+ expect(described_class.parse_line("\t")).to eq(record_type: :blank, line: "\t")
+ expect(described_class.parse_line(" \t ")).to eq(record_type: :blank, line: " \t ")
+ end
+
+ it 'extracts environment assignments' do
+ # man 5 crontab: MAILTO="" with no value can be used to surpress sending
+ # mails at all
+ expect(described_class.parse_line('MAILTO=""')).to eq(record_type: :environment, line: 'MAILTO=""')
+ expect(described_class.parse_line('FOO=BAR')).to eq(record_type: :environment, line: 'FOO=BAR')
+ expect(described_class.parse_line('FOO_BAR=BAR')).to eq(record_type: :environment, line: 'FOO_BAR=BAR')
+ expect(described_class.parse_line('SPACE = BAR')).to eq(record_type: :environment, line: 'SPACE = BAR')
+ end
+
+ it 'extracts a cron entry' do
+ expect(described_class.parse_line('* * * * * /bin/true')).to eq(record_type: :crontab,
+ hour: :absent,
+ minute: :absent,
+ month: :absent,
+ weekday: :absent,
+ monthday: :absent,
+ special: :absent,
+ command: '/bin/true')
+ expect(described_class.parse_line('0,15,30,45 8-18,20-22 31 12 7 /bin/true')).to eq(record_type: :crontab,
+ minute: ['0', '15', '30', '45'],
+ hour: ['8-18', '20-22'],
+ monthday: ['31'],
+ month: ['12'],
+ weekday: ['7'],
+ special: :absent,
+ command: '/bin/true')
+ # A percent sign will cause the rest of the string to be passed as
+ # standard input and will also act as a newline character. Not sure
+ # if puppet should convert % to a \n as the command property so the
+ # test covers the current behaviour: Do not do any conversions
+ expect(described_class.parse_line('0 22 * * 1-5 mail -s "It\'s 10pm" joe%Joe,%%Where are your kids?%')).to eq(record_type: :crontab,
+ minute: ['0'],
+ hour: ['22'],
+ monthday: :absent,
+ month: :absent,
+ weekday: ['1-5'],
+ special: :absent,
+ command: 'mail -s "It\'s 10pm" joe%Joe,%%Where are your kids?%')
+ end
+
+ describe 'it should support special strings' do
+ ['reboot', 'yearly', 'anually', 'monthly', 'weekly', 'daily', 'midnight', 'hourly'].each do |special|
+ it "should support @#{special}" do
+ expect(described_class.parse_line("@#{special} /bin/true")).to eq(record_type: :crontab,
+ hour: :absent,
+ minute: :absent,
+ month: :absent,
+ weekday: :absent,
+ monthday: :absent,
+ special: special,
+ command: '/bin/true')
+ end
+ end
+ end
+ end
+
+ describe '.instances' do
+ before :each do
+ described_class.stubs(:default_target).returns 'foobar'
+ end
+
+ describe 'on linux' do
+ before(:each) do
+ Facter.stubs(:value).with(:osfamily).returns 'Linux'
+ Facter.stubs(:value).with(:operatingsystem)
+ end
+
+ it 'contains no resources for a user who has no crontab, or for a user that is absent' do
+ # `crontab...` does only capture stdout here. On vixie-cron-4.1
+ # STDERR shows "no crontab for foobar" but stderr is ignored as
+ # well as the exitcode.
+ # STDERR shows "crontab: user `foobar' unknown" but stderr is
+ # ignored as well as the exitcode
+ described_class.target_object('foobar').expects(:`).with('crontab -u foobar -l 2>/dev/null').returns ''
+ expect(described_class.instances.select do |resource|
+ resource.get('target') == 'foobar'
+ end).to be_empty
+ end
+
+ it 'is able to create records from not-managed records' do
+ described_class.stubs(:target_object).returns File.new(my_fixture('simple'))
+ parameters = described_class.instances.map do |p|
+ h = { name: p.get(:name) }
+ Puppet::Type.type(:cron).validproperties.each do |property|
+ h[property] = p.get(property)
+ end
+ h
+ end
+
+ expect(parameters[0][:name]).to match(%r{unmanaged:\$HOME/bin/daily.job_>>_\$HOME/tmp/out_2>&1-\d+})
+ expect(parameters[0][:minute]).to eq(['5'])
+ expect(parameters[0][:hour]).to eq(['0'])
+ expect(parameters[0][:weekday]).to eq(:absent)
+ expect(parameters[0][:month]).to eq(:absent)
+ expect(parameters[0][:monthday]).to eq(:absent)
+ expect(parameters[0][:special]).to eq(:absent)
+ expect(parameters[0][:command]).to match(%r{\$HOME/bin/daily.job >> \$HOME/tmp/out 2>&1})
+ expect(parameters[0][:ensure]).to eq(:present)
+ expect(parameters[0][:environment]).to eq(:absent)
+ expect(parameters[0][:user]).to eq(:absent)
+
+ expect(parameters[1][:name]).to match(%r{unmanaged:\$HOME/bin/monthly-\d+})
+ expect(parameters[1][:minute]).to eq(['15'])
+ expect(parameters[1][:hour]).to eq(['14'])
+ expect(parameters[1][:weekday]).to eq(:absent)
+ expect(parameters[1][:month]).to eq(:absent)
+ expect(parameters[1][:monthday]).to eq(['1'])
+ expect(parameters[1][:special]).to eq(:absent)
+ expect(parameters[1][:command]).to match(%r{\$HOME/bin/monthly})
+ expect(parameters[1][:ensure]).to eq(:present)
+ expect(parameters[1][:environment]).to eq(:absent)
+ expect(parameters[1][:user]).to eq(:absent)
+ expect(parameters[1][:target]).to eq('foobar')
+ end
+
+ it 'is able to parse puppet managed cronjobs' do
+ described_class.stubs(:target_object).returns File.new(my_fixture('managed'))
+ expect(described_class.instances.map do |p|
+ h = { name: p.get(:name) }
+ Puppet::Type.type(:cron).validproperties.each do |property|
+ h[property] = p.get(property)
+ end
+ h
+ end).to eq([
+ {
+ name: 'real_job',
+ minute: :absent,
+ hour: :absent,
+ weekday: :absent,
+ month: :absent,
+ monthday: :absent,
+ special: :absent,
+ command: '/bin/true',
+ ensure: :present,
+ environment: :absent,
+ user: :absent,
+ target: 'foobar',
+ },
+ {
+ name: 'complex_job',
+ minute: :absent,
+ hour: :absent,
+ weekday: :absent,
+ month: :absent,
+ monthday: :absent,
+ special: 'reboot',
+ command: '/bin/true >> /dev/null 2>&1',
+ ensure: :present,
+ environment: [
+ 'MAILTO=foo@example.com',
+ 'SHELL=/bin/sh',
+ ],
+ user: :absent,
+ target: 'foobar',
+ },
+ ])
+ end
+ end
+ end
+
+ describe '.match' do
+ describe 'normal records' do
+ it 'matches when all fields are the same' do
+ expect(described_class.match(record, resource[:name] => resource)).to eq(resource)
+ end
+
+ {
+ minute: ['0', '15', '31', '45'],
+ hour: ['8-18'],
+ monthday: ['30', '31'],
+ month: ['12', '23'],
+ weekday: ['4'],
+ command: '/bin/false',
+ target: 'nobody',
+ }.each_pair do |field, new_value|
+ it "should not match a record when #{field} does not match" do
+ record[field] = new_value
+ expect(described_class.match(record, resource[:name] => resource)).to be_falsey
+ end
+ end
+ end
+
+ describe 'special records' do
+ it 'matches when all fields are the same' do
+ expect(described_class.match(record_special, resource_special[:name] => resource_special)).to eq(resource_special)
+ end
+
+ {
+ special: 'monthly',
+ command: '/bin/false',
+ target: 'root',
+ }.each_pair do |field, new_value|
+ it "should not match a record when #{field} does not match" do
+ record_special[field] = new_value
+ expect(described_class.match(record_special, resource_special[:name] => resource_special)).to be_falsey
+ end
+ end
+ end
+
+ describe 'with a resource without a command' do
+ it 'does not raise an error' do
+ expect { described_class.match(record, resource_sparse[:name] => resource_sparse) }.not_to raise_error
+ end
+ end
+ end
+end
diff --git a/spec/unit/type/cron_spec.rb b/spec/unit/type/cron_spec.rb
new file mode 100644
index 0000000..32bde11
--- /dev/null
+++ b/spec/unit/type/cron_spec.rb
@@ -0,0 +1,536 @@
+require 'spec_helper'
+
+describe Puppet::Type.type(:cron), unless: Puppet.features.microsoft_windows? do
+ let(:simple_provider) do
+ provider_class = described_class.provide(:simple) { mk_resource_methods }
+ provider_class.stubs(:suitable?).returns true
+ provider_class
+ end
+
+ before :each do
+ described_class.stubs(:defaultprovider).returns simple_provider
+ end
+
+ after :each do
+ described_class.unprovide(:simple)
+ end
+
+ it 'has :name be its namevar' do
+ expect(described_class.key_attributes).to eq([:name])
+ end
+
+ describe 'when validating attributes' do
+ [:name, :provider].each do |param|
+ it "should have a #{param} parameter" do
+ expect(described_class.attrtype(param)).to eq(:param)
+ end
+ end
+
+ [:command, :special, :minute, :hour, :weekday, :month, :monthday, :environment, :user, :target].each do |property|
+ it "should have a #{property} property" do
+ expect(described_class.attrtype(property)).to eq(:property)
+ end
+ end
+
+ [:command, :minute, :hour, :weekday, :month, :monthday].each do |cronparam|
+ it "should have #{cronparam} of type CronParam" do
+ expect(described_class.attrclass(cronparam).ancestors).to include CronParam
+ end
+ end
+ end
+
+ describe 'when validating values' do
+ describe 'ensure' do
+ it 'supports present as a value for ensure' do
+ expect { described_class.new(name: 'foo', ensure: :present) }.not_to raise_error
+ end
+
+ it 'supports absent as a value for ensure' do
+ expect { described_class.new(name: 'foo', ensure: :absent) }.not_to raise_error
+ end
+
+ it 'does not support other values' do
+ expect { described_class.new(name: 'foo', ensure: :foo) }.to raise_error(Puppet::Error, %r{Invalid value})
+ end
+ end
+
+ describe 'command' do
+ it 'discards leading spaces' do
+ expect(described_class.new(name: 'foo', command: ' /bin/true')[:command]).not_to match Regexp.new(' ')
+ end
+ it 'discards trailing spaces' do
+ expect(described_class.new(name: 'foo', command: '/bin/true ')[:command]).not_to match Regexp.new(' ')
+ end
+ end
+
+ describe 'minute' do
+ it 'supports absent' do
+ expect { described_class.new(name: 'foo', minute: 'absent') }.not_to raise_error
+ end
+
+ it 'supports *' do
+ expect { described_class.new(name: 'foo', minute: '*') }.not_to raise_error
+ end
+
+ it 'translates absent to :absent' do
+ expect(described_class.new(name: 'foo', minute: 'absent')[:minute]).to eq(:absent)
+ end
+
+ it 'translates * to :absent' do
+ expect(described_class.new(name: 'foo', minute: '*')[:minute]).to eq(:absent)
+ end
+
+ it 'supports valid single values' do
+ expect { described_class.new(name: 'foo', minute: '0') }.not_to raise_error
+ expect { described_class.new(name: 'foo', minute: '1') }.not_to raise_error
+ expect { described_class.new(name: 'foo', minute: '59') }.not_to raise_error
+ end
+
+ it 'does not support non numeric characters' do
+ expect { described_class.new(name: 'foo', minute: 'z59') }.to raise_error(Puppet::Error, %r{z59 is not a valid minute})
+ expect { described_class.new(name: 'foo', minute: '5z9') }.to raise_error(Puppet::Error, %r{5z9 is not a valid minute})
+ expect { described_class.new(name: 'foo', minute: '59z') }.to raise_error(Puppet::Error, %r{59z is not a valid minute})
+ end
+
+ it 'does not support single values out of range' do
+ expect { described_class.new(name: 'foo', minute: '-1') }.to raise_error(Puppet::Error, %r{-1 is not a valid minute})
+ expect { described_class.new(name: 'foo', minute: '60') }.to raise_error(Puppet::Error, %r{60 is not a valid minute})
+ expect { described_class.new(name: 'foo', minute: '61') }.to raise_error(Puppet::Error, %r{61 is not a valid minute})
+ expect { described_class.new(name: 'foo', minute: '120') }.to raise_error(Puppet::Error, %r{120 is not a valid minute})
+ end
+
+ it 'supports valid multiple values' do
+ expect { described_class.new(name: 'foo', minute: ['0', '1', '59']) }.not_to raise_error
+ expect { described_class.new(name: 'foo', minute: ['40', '30', '20']) }.not_to raise_error
+ expect { described_class.new(name: 'foo', minute: ['10', '30', '20']) }.not_to raise_error
+ end
+
+ it 'does not support multiple values if at least one is invalid' do
+ # one invalid
+ expect { described_class.new(name: 'foo', minute: ['0', '1', '60']) }.to raise_error(Puppet::Error, %r{60 is not a valid minute})
+ expect { described_class.new(name: 'foo', minute: ['0', '120', '59']) }.to raise_error(Puppet::Error, %r{120 is not a valid minute})
+ expect { described_class.new(name: 'foo', minute: ['-1', '1', '59']) }.to raise_error(Puppet::Error, %r{-1 is not a valid minute})
+ # two invalid
+ expect { described_class.new(name: 'foo', minute: ['0', '61', '62']) }.to raise_error(Puppet::Error, %r{(61|62) is not a valid minute})
+ # all invalid
+ expect { described_class.new(name: 'foo', minute: ['-1', '61', '62']) }.to raise_error(Puppet::Error, %r{(-1|61|62) is not a valid minute})
+ end
+
+ it 'supports valid step syntax' do
+ expect { described_class.new(name: 'foo', minute: '*/2') }.not_to raise_error
+ expect { described_class.new(name: 'foo', minute: '10-16/2') }.not_to raise_error
+ end
+
+ it 'does not support invalid steps' do
+ expect { described_class.new(name: 'foo', minute: '*/A') }.to raise_error(Puppet::Error, %r{\*/A is not a valid minute})
+ expect { described_class.new(name: 'foo', minute: '*/2A') }.to raise_error(Puppet::Error, %r{\*/2A is not a valid minute})
+ # As it turns out cron does not complaining about steps that exceed the valid range
+ # expect { described_class.new(:name => 'foo', :minute => '*/120' ) }.to raise_error(Puppet::Error, /is not a valid minute/)
+ end
+ end
+
+ describe 'hour' do
+ it 'supports absent' do
+ expect { described_class.new(name: 'foo', hour: 'absent') }.not_to raise_error
+ end
+
+ it 'supports *' do
+ expect { described_class.new(name: 'foo', hour: '*') }.not_to raise_error
+ end
+
+ it 'translates absent to :absent' do
+ expect(described_class.new(name: 'foo', hour: 'absent')[:hour]).to eq(:absent)
+ end
+
+ it 'translates * to :absent' do
+ expect(described_class.new(name: 'foo', hour: '*')[:hour]).to eq(:absent)
+ end
+
+ it 'supports valid single values' do
+ expect { described_class.new(name: 'foo', hour: '0') }.not_to raise_error
+ expect { described_class.new(name: 'foo', hour: '11') }.not_to raise_error
+ expect { described_class.new(name: 'foo', hour: '12') }.not_to raise_error
+ expect { described_class.new(name: 'foo', hour: '13') }.not_to raise_error
+ expect { described_class.new(name: 'foo', hour: '23') }.not_to raise_error
+ end
+
+ it 'does not support non numeric characters' do
+ expect { described_class.new(name: 'foo', hour: 'z15') }.to raise_error(Puppet::Error, %r{z15 is not a valid hour})
+ expect { described_class.new(name: 'foo', hour: '1z5') }.to raise_error(Puppet::Error, %r{1z5 is not a valid hour})
+ expect { described_class.new(name: 'foo', hour: '15z') }.to raise_error(Puppet::Error, %r{15z is not a valid hour})
+ end
+
+ it 'does not support single values out of range' do
+ expect { described_class.new(name: 'foo', hour: '-1') }.to raise_error(Puppet::Error, %r{-1 is not a valid hour})
+ expect { described_class.new(name: 'foo', hour: '24') }.to raise_error(Puppet::Error, %r{24 is not a valid hour})
+ expect { described_class.new(name: 'foo', hour: '120') }.to raise_error(Puppet::Error, %r{120 is not a valid hour})
+ end
+
+ it 'supports valid multiple values' do
+ expect { described_class.new(name: 'foo', hour: ['0', '1', '23']) }.not_to raise_error
+ expect { described_class.new(name: 'foo', hour: ['5', '16', '14']) }.not_to raise_error
+ expect { described_class.new(name: 'foo', hour: ['16', '13', '9']) }.not_to raise_error
+ end
+
+ it 'does not support multiple values if at least one is invalid' do
+ # one invalid
+ expect { described_class.new(name: 'foo', hour: ['0', '1', '24']) }.to raise_error(Puppet::Error, %r{24 is not a valid hour})
+ expect { described_class.new(name: 'foo', hour: ['0', '-1', '5']) }.to raise_error(Puppet::Error, %r{-1 is not a valid hour})
+ expect { described_class.new(name: 'foo', hour: ['-1', '1', '23']) }.to raise_error(Puppet::Error, %r{-1 is not a valid hour})
+ # two invalid
+ expect { described_class.new(name: 'foo', hour: ['0', '25', '26']) }.to raise_error(Puppet::Error, %r{(25|26) is not a valid hour})
+ # all invalid
+ expect { described_class.new(name: 'foo', hour: ['-1', '24', '120']) }.to raise_error(Puppet::Error, %r{(-1|24|120) is not a valid hour})
+ end
+
+ it 'supports valid step syntax' do
+ expect { described_class.new(name: 'foo', hour: '*/2') }.not_to raise_error
+ expect { described_class.new(name: 'foo', hour: '10-18/4') }.not_to raise_error
+ end
+
+ it 'does not support invalid steps' do
+ expect { described_class.new(name: 'foo', hour: '*/A') }.to raise_error(Puppet::Error, %r{\*/A is not a valid hour})
+ expect { described_class.new(name: 'foo', hour: '*/2A') }.to raise_error(Puppet::Error, %r{\*/2A is not a valid hour})
+ # As it turns out cron does not complaining about steps that exceed the valid range
+ # expect { described_class.new(:name => 'foo', :hour => '*/26' ) }.to raise_error(Puppet::Error, /is not a valid hour/)
+ end
+ end
+
+ describe 'weekday' do
+ it 'supports absent' do
+ expect { described_class.new(name: 'foo', weekday: 'absent') }.not_to raise_error
+ end
+
+ it 'supports *' do
+ expect { described_class.new(name: 'foo', weekday: '*') }.not_to raise_error
+ end
+
+ it 'translates absent to :absent' do
+ expect(described_class.new(name: 'foo', weekday: 'absent')[:weekday]).to eq(:absent)
+ end
+
+ it 'translates * to :absent' do
+ expect(described_class.new(name: 'foo', weekday: '*')[:weekday]).to eq(:absent)
+ end
+
+ it 'supports valid numeric weekdays' do
+ expect { described_class.new(name: 'foo', weekday: '0') }.not_to raise_error
+ expect { described_class.new(name: 'foo', weekday: '1') }.not_to raise_error
+ expect { described_class.new(name: 'foo', weekday: '6') }.not_to raise_error
+ # According to http://www.manpagez.com/man/5/crontab 7 is also valid (Sunday)
+ expect { described_class.new(name: 'foo', weekday: '7') }.not_to raise_error
+ end
+
+ it 'supports valid weekdays as words (long version)' do
+ expect { described_class.new(name: 'foo', weekday: 'Monday') }.not_to raise_error
+ expect { described_class.new(name: 'foo', weekday: 'Tuesday') }.not_to raise_error
+ expect { described_class.new(name: 'foo', weekday: 'Wednesday') }.not_to raise_error
+ expect { described_class.new(name: 'foo', weekday: 'Thursday') }.not_to raise_error
+ expect { described_class.new(name: 'foo', weekday: 'Friday') }.not_to raise_error
+ expect { described_class.new(name: 'foo', weekday: 'Saturday') }.not_to raise_error
+ expect { described_class.new(name: 'foo', weekday: 'Sunday') }.not_to raise_error
+ end
+
+ it 'supports valid weekdays as words (3 character version)' do
+ expect { described_class.new(name: 'foo', weekday: 'Mon') }.not_to raise_error
+ expect { described_class.new(name: 'foo', weekday: 'Tue') }.not_to raise_error
+ expect { described_class.new(name: 'foo', weekday: 'Wed') }.not_to raise_error
+ expect { described_class.new(name: 'foo', weekday: 'Thu') }.not_to raise_error
+ expect { described_class.new(name: 'foo', weekday: 'Fri') }.not_to raise_error
+ expect { described_class.new(name: 'foo', weekday: 'Sat') }.not_to raise_error
+ expect { described_class.new(name: 'foo', weekday: 'Sun') }.not_to raise_error
+ end
+
+ it 'does not support numeric values out of range' do
+ expect { described_class.new(name: 'foo', weekday: '-1') }.to raise_error(Puppet::Error, %r{-1 is not a valid weekday})
+ expect { described_class.new(name: 'foo', weekday: '8') }.to raise_error(Puppet::Error, %r{8 is not a valid weekday})
+ end
+
+ it 'does not support invalid weekday names' do
+ expect { described_class.new(name: 'foo', weekday: 'Sar') }.to raise_error(Puppet::Error, %r{Sar is not a valid weekday})
+ end
+
+ it 'supports valid multiple values' do
+ expect { described_class.new(name: 'foo', weekday: ['0', '1', '6']) }.not_to raise_error
+ expect { described_class.new(name: 'foo', weekday: ['Mon', 'Wed', 'Friday']) }.not_to raise_error
+ end
+
+ it 'does not support multiple values if at least one is invalid' do
+ # one invalid
+ expect { described_class.new(name: 'foo', weekday: ['0', '1', '8']) }.to raise_error(Puppet::Error, %r{8 is not a valid weekday})
+ expect { described_class.new(name: 'foo', weekday: ['Mon', 'Fii', 'Sat']) }.to raise_error(Puppet::Error, %r{Fii is not a valid weekday})
+ # two invalid
+ expect { described_class.new(name: 'foo', weekday: ['Mos', 'Fii', 'Sat']) }.to raise_error(Puppet::Error, %r{(Mos|Fii) is not a valid weekday})
+ # all invalid
+ expect { described_class.new(name: 'foo', weekday: ['Mos', 'Fii', 'Saa']) }.to raise_error(Puppet::Error, %r{(Mos|Fii|Saa) is not a valid weekday})
+ expect { described_class.new(name: 'foo', weekday: ['-1', '8', '11']) }.to raise_error(Puppet::Error, %r{(-1|8|11) is not a valid weekday})
+ end
+
+ it 'supports valid step syntax' do
+ expect { described_class.new(name: 'foo', weekday: '*/2') }.not_to raise_error
+ expect { described_class.new(name: 'foo', weekday: '0-4/2') }.not_to raise_error
+ end
+
+ it 'does not support invalid steps' do
+ expect { described_class.new(name: 'foo', weekday: '*/A') }.to raise_error(Puppet::Error, %r{\*/A is not a valid weekday})
+ expect { described_class.new(name: 'foo', weekday: '*/2A') }.to raise_error(Puppet::Error, %r{\*/2A is not a valid weekday})
+ # As it turns out cron does not complaining about steps that exceed the valid range
+ # expect { described_class.new(:name => 'foo', :weekday => '*/9' ) }.to raise_error(Puppet::Error, /is not a valid weekday/)
+ end
+ end
+
+ describe 'month' do
+ it 'supports absent' do
+ expect { described_class.new(name: 'foo', month: 'absent') }.not_to raise_error
+ end
+
+ it 'supports *' do
+ expect { described_class.new(name: 'foo', month: '*') }.not_to raise_error
+ end
+
+ it 'translates absent to :absent' do
+ expect(described_class.new(name: 'foo', month: 'absent')[:month]).to eq(:absent)
+ end
+
+ it 'translates * to :absent' do
+ expect(described_class.new(name: 'foo', month: '*')[:month]).to eq(:absent)
+ end
+
+ it 'supports valid numeric values' do
+ expect { described_class.new(name: 'foo', month: '1') }.not_to raise_error
+ expect { described_class.new(name: 'foo', month: '12') }.not_to raise_error
+ end
+
+ it 'supports valid months as words' do
+ expect(described_class.new(name: 'foo', month: 'January')[:month]).to eq(['1'])
+ expect(described_class.new(name: 'foo', month: 'February')[:month]).to eq(['2'])
+ expect(described_class.new(name: 'foo', month: 'March')[:month]).to eq(['3'])
+ expect(described_class.new(name: 'foo', month: 'April')[:month]).to eq(['4'])
+ expect(described_class.new(name: 'foo', month: 'May')[:month]).to eq(['5'])
+ expect(described_class.new(name: 'foo', month: 'June')[:month]).to eq(['6'])
+ expect(described_class.new(name: 'foo', month: 'July')[:month]).to eq(['7'])
+ expect(described_class.new(name: 'foo', month: 'August')[:month]).to eq(['8'])
+ expect(described_class.new(name: 'foo', month: 'September')[:month]).to eq(['9'])
+ expect(described_class.new(name: 'foo', month: 'October')[:month]).to eq(['10'])
+ expect(described_class.new(name: 'foo', month: 'November')[:month]).to eq(['11'])
+ expect(described_class.new(name: 'foo', month: 'December')[:month]).to eq(['12'])
+ end
+
+ it 'supports valid months as words (3 character short version)' do
+ expect(described_class.new(name: 'foo', month: 'Jan')[:month]).to eq(['1'])
+ expect(described_class.new(name: 'foo', month: 'Feb')[:month]).to eq(['2'])
+ expect(described_class.new(name: 'foo', month: 'Mar')[:month]).to eq(['3'])
+ expect(described_class.new(name: 'foo', month: 'Apr')[:month]).to eq(['4'])
+ expect(described_class.new(name: 'foo', month: 'May')[:month]).to eq(['5'])
+ expect(described_class.new(name: 'foo', month: 'Jun')[:month]).to eq(['6'])
+ expect(described_class.new(name: 'foo', month: 'Jul')[:month]).to eq(['7'])
+ expect(described_class.new(name: 'foo', month: 'Aug')[:month]).to eq(['8'])
+ expect(described_class.new(name: 'foo', month: 'Sep')[:month]).to eq(['9'])
+ expect(described_class.new(name: 'foo', month: 'Oct')[:month]).to eq(['10'])
+ expect(described_class.new(name: 'foo', month: 'Nov')[:month]).to eq(['11'])
+ expect(described_class.new(name: 'foo', month: 'Dec')[:month]).to eq(['12'])
+ end
+
+ it 'does not support numeric values out of range' do
+ expect { described_class.new(name: 'foo', month: '-1') }.to raise_error(Puppet::Error, %r{-1 is not a valid month})
+ expect { described_class.new(name: 'foo', month: '0') }.to raise_error(Puppet::Error, %r{0 is not a valid month})
+ expect { described_class.new(name: 'foo', month: '13') }.to raise_error(Puppet::Error, %r{13 is not a valid month})
+ end
+
+ it 'does not support words that are not valid months' do
+ expect { described_class.new(name: 'foo', month: 'Jal') }.to raise_error(Puppet::Error, %r{Jal is not a valid month})
+ end
+
+ it 'does not support single values out of range' do
+ expect { described_class.new(name: 'foo', month: '-1') }.to raise_error(Puppet::Error, %r{-1 is not a valid month})
+ expect { described_class.new(name: 'foo', month: '60') }.to raise_error(Puppet::Error, %r{60 is not a valid month})
+ expect { described_class.new(name: 'foo', month: '61') }.to raise_error(Puppet::Error, %r{61 is not a valid month})
+ expect { described_class.new(name: 'foo', month: '120') }.to raise_error(Puppet::Error, %r{120 is not a valid month})
+ end
+
+ it 'supports valid multiple values' do
+ expect { described_class.new(name: 'foo', month: ['1', '9', '12']) }.not_to raise_error
+ expect { described_class.new(name: 'foo', month: ['Jan', 'March', 'Jul']) }.not_to raise_error
+ end
+
+ it 'does not support multiple values if at least one is invalid' do
+ # one invalid
+ expect { described_class.new(name: 'foo', month: ['0', '1', '12']) }.to raise_error(Puppet::Error, %r{0 is not a valid month})
+ expect { described_class.new(name: 'foo', month: ['1', '13', '10']) }.to raise_error(Puppet::Error, %r{13 is not a valid month})
+ expect { described_class.new(name: 'foo', month: ['Jan', 'Feb', 'Jxx']) }.to raise_error(Puppet::Error, %r{Jxx is not a valid month})
+ # two invalid
+ expect { described_class.new(name: 'foo', month: ['Jan', 'Fex', 'Jux']) }.to raise_error(Puppet::Error, %r{(Fex|Jux) is not a valid month})
+ # all invalid
+ expect { described_class.new(name: 'foo', month: ['-1', '0', '13']) }.to raise_error(Puppet::Error, %r{(-1|0|13) is not a valid month})
+ expect { described_class.new(name: 'foo', month: ['Jax', 'Fex', 'Aux']) }.to raise_error(Puppet::Error, %r{(Jax|Fex|Aux) is not a valid month})
+ end
+
+ it 'supports valid step syntax' do
+ expect { described_class.new(name: 'foo', month: '*/2') }.not_to raise_error
+ expect { described_class.new(name: 'foo', month: '1-12/3') }.not_to raise_error
+ end
+
+ it 'does not support invalid steps' do
+ expect { described_class.new(name: 'foo', month: '*/A') }.to raise_error(Puppet::Error, %r{\*/A is not a valid month})
+ expect { described_class.new(name: 'foo', month: '*/2A') }.to raise_error(Puppet::Error, %r{\*/2A is not a valid month})
+ # As it turns out cron does not complaining about steps that exceed the valid range
+ # expect { described_class.new(:name => 'foo', :month => '*/13' ) }.to raise_error(Puppet::Error, /is not a valid month/)
+ end
+ end
+
+ describe 'monthday' do
+ it 'supports absent' do
+ expect { described_class.new(name: 'foo', monthday: 'absent') }.not_to raise_error
+ end
+
+ it 'supports *' do
+ expect { described_class.new(name: 'foo', monthday: '*') }.not_to raise_error
+ end
+
+ it 'translates absent to :absent' do
+ expect(described_class.new(name: 'foo', monthday: 'absent')[:monthday]).to eq(:absent)
+ end
+
+ it 'translates * to :absent' do
+ expect(described_class.new(name: 'foo', monthday: '*')[:monthday]).to eq(:absent)
+ end
+
+ it 'supports valid single values' do
+ expect { described_class.new(name: 'foo', monthday: '1') }.not_to raise_error
+ expect { described_class.new(name: 'foo', monthday: '30') }.not_to raise_error
+ expect { described_class.new(name: 'foo', monthday: '31') }.not_to raise_error
+ end
+
+ it 'does not support non numeric characters' do
+ expect { described_class.new(name: 'foo', monthday: 'z23') }.to raise_error(Puppet::Error, %r{z23 is not a valid monthday})
+ expect { described_class.new(name: 'foo', monthday: '2z3') }.to raise_error(Puppet::Error, %r{2z3 is not a valid monthday})
+ expect { described_class.new(name: 'foo', monthday: '23z') }.to raise_error(Puppet::Error, %r{23z is not a valid monthday})
+ end
+
+ it 'does not support single values out of range' do
+ expect { described_class.new(name: 'foo', monthday: '-1') }.to raise_error(Puppet::Error, %r{-1 is not a valid monthday})
+ expect { described_class.new(name: 'foo', monthday: '0') }.to raise_error(Puppet::Error, %r{0 is not a valid monthday})
+ expect { described_class.new(name: 'foo', monthday: '32') }.to raise_error(Puppet::Error, %r{32 is not a valid monthday})
+ end
+
+ it 'supports valid multiple values' do
+ expect { described_class.new(name: 'foo', monthday: ['1', '23', '31']) }.not_to raise_error
+ expect { described_class.new(name: 'foo', monthday: ['31', '23', '1']) }.not_to raise_error
+ expect { described_class.new(name: 'foo', monthday: ['1', '31', '23']) }.not_to raise_error
+ end
+
+ it 'does not support multiple values if at least one is invalid' do
+ # one invalid
+ expect { described_class.new(name: 'foo', monthday: ['1', '23', '32']) }.to raise_error(Puppet::Error, %r{32 is not a valid monthday})
+ expect { described_class.new(name: 'foo', monthday: ['-1', '12', '23']) }.to raise_error(Puppet::Error, %r{-1 is not a valid monthday})
+ expect { described_class.new(name: 'foo', monthday: ['13', '32', '30']) }.to raise_error(Puppet::Error, %r{32 is not a valid monthday})
+ # two invalid
+ expect { described_class.new(name: 'foo', monthday: ['-1', '0', '23']) }.to raise_error(Puppet::Error, %r{(-1|0) is not a valid monthday})
+ # all invalid
+ expect { described_class.new(name: 'foo', monthday: ['-1', '0', '32']) }.to raise_error(Puppet::Error, %r{(-1|0|32) is not a valid monthday})
+ end
+
+ it 'supports valid step syntax' do
+ expect { described_class.new(name: 'foo', monthday: '*/2') }.not_to raise_error
+ expect { described_class.new(name: 'foo', monthday: '10-16/2') }.not_to raise_error
+ end
+
+ it 'does not support invalid steps' do
+ expect { described_class.new(name: 'foo', monthday: '*/A') }.to raise_error(Puppet::Error, %r{\*/A is not a valid monthday})
+ expect { described_class.new(name: 'foo', monthday: '*/2A') }.to raise_error(Puppet::Error, %r{\*/2A is not a valid monthday})
+ # As it turns out cron does not complaining about steps that exceed the valid range
+ # expect { described_class.new(:name => 'foo', :monthday => '*/32' ) }.to raise_error(Puppet::Error, /is not a valid monthday/)
+ end
+ end
+
+ describe 'special' do
+ ['reboot', 'yearly', 'annually', 'monthly', 'weekly', 'daily', 'midnight', 'hourly'].each do |value|
+ it "should support the value '#{value}'" do
+ expect { described_class.new(name: 'foo', special: value) }.not_to raise_error
+ end
+ end
+
+ context 'when combined with numeric schedule fields' do
+ context "which are 'absent'" do
+ [['reboot', 'yearly', 'annually', 'monthly', 'weekly', 'daily', 'midnight', 'hourly'], :absent].flatten.each do |value|
+ it "should accept the value '#{value}' for special" do
+ expect {
+ described_class.new(name: 'foo', minute: :absent, special: value)
+ }.not_to raise_error
+ end
+ end
+ end
+ context 'which are not absent' do
+ ['reboot', 'yearly', 'annually', 'monthly', 'weekly', 'daily', 'midnight', 'hourly'].each do |value|
+ it "should not accept the value '#{value}' for special" do
+ expect {
+ described_class.new(name: 'foo', minute: '1', special: value)
+ }.to raise_error(Puppet::Error, %r{cannot specify both a special schedule and a value})
+ end
+ end
+ it "accepts the 'absent' value for special" do
+ expect {
+ described_class.new(name: 'foo', minute: '1', special: :absent)
+ }.not_to raise_error
+ end
+ end
+ end
+ end
+
+ describe 'environment' do
+ it 'accepts an :environment that looks like a path' do
+ expect {
+ described_class.new(name: 'foo', environment: 'PATH=/bin:/usr/bin:/usr/sbin')
+ }.not_to raise_error
+ end
+
+ it "does not accept environment variables that do not contain '='" do
+ expect {
+ described_class.new(name: 'foo', environment: 'INVALID')
+ }.to raise_error(Puppet::Error, %r{Invalid environment setting "INVALID"})
+ end
+
+ it "accepts empty environment variables that do not contain '='" do
+ expect {
+ described_class.new(name: 'foo', environment: 'MAILTO=')
+ }.not_to raise_error
+ end
+
+ it "accepts 'absent'" do
+ expect {
+ described_class.new(name: 'foo', environment: 'absent')
+ }.not_to raise_error
+ end
+ end
+ end
+
+ describe 'when autorequiring resources' do
+ let(:user_bob) { Puppet::Type.type(:user).new(name: 'bob', ensure: :present) }
+ let(:user_alice) { Puppet::Type.type(:user).new(name: 'alice', ensure: :present) }
+ let(:catalog) { Puppet::Resource::Catalog.new }
+
+ before :each do
+ catalog.add_resource user_bob, user_alice
+ end
+
+ it 'autorequires the user' do
+ resource = described_class.new(name: 'dummy', command: '/usr/bin/uptime', user: 'alice')
+ catalog.add_resource resource
+ req = resource.autorequire
+ expect(req.size).to eq(1)
+ expect(req[0].target).to eq(resource)
+ expect(req[0].source).to eq(user_alice)
+ end
+ end
+
+ it 'does not require a command when removing an entry' do
+ entry = described_class.new(name: 'test_entry', ensure: :absent)
+ expect(entry.value(:command)).to eq(nil)
+ end
+
+ it 'defaults to user => root if Etc.getpwuid(Process.uid) returns nil (#12357)' do
+ Etc.expects(:getpwuid).returns(nil)
+ entry = described_class.new(name: 'test_entry', ensure: :present)
+ expect(entry.value(:user)).to eql 'root'
+ end
+end