Skip to content

Commit ec2da07

Browse files
authored
Merge pull request #35 from Dorin-Pleava/MODULES-7786/Cron_accept_leading_zeros
(MODULES-7786) Allow leading zeroes for cron params
2 parents 7cc67bf + 29925fb commit ec2da07

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

lib/puppet/type/cron.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ def insync?(is)
7373
# in string form to actual integers, and returns the value if it's
7474
# an integer or false if it's just a normal string.
7575
def numfix(num)
76-
if num =~ %r{^\d+$}
77-
num.to_i
78-
elsif num.is_a?(Integer)
76+
if num =~ %r{^\d+$} || num.is_a?(Integer)
7977
num
8078
else
8179
false
@@ -85,7 +83,7 @@ def numfix(num)
8583
# Verify that a number is within the specified limits. Return the
8684
# number if it is, or false if it is not.
8785
def limitcheck(num, lower, upper)
88-
(num >= lower && num <= upper) && num
86+
(num.to_i >= lower && num.to_i <= upper) && num
8987
end
9088

9189
# Verify that a value falls within the specified array. Does case
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'spec_helper_acceptance'
2+
3+
RSpec.context 'when leading zeroes are present' do
4+
before(:each) do
5+
compatible_agents.each do |agent|
6+
step 'ensure the user exists via puppet'
7+
setup(agent)
8+
end
9+
end
10+
11+
after(:each) do
12+
compatible_agents.each do |agent|
13+
step 'Cron: cleanup'
14+
clean(agent)
15+
end
16+
end
17+
18+
compatible_agents.each do |host|
19+
it 'does not ignore leading zeroes' do
20+
step 'apply the resource on the host' do
21+
on(host, puppet_resource('cron', 'crontest', 'user=tstuser', 'command=/bin/true', 'ensure=present', "minute='05'", "hour='007'", "weekday='03'", "month='0011'", "monthday='07'"), acceptable_exit_codes: [0])
22+
end
23+
24+
step 'Verify that crontab -l contains what you expected' do
25+
run_cron_on(host, :list, 'tstuser') do
26+
expect(stdout).to match(%r{05 007 07 0011 03 /bin/true})
27+
end
28+
end
29+
end
30+
end
31+
end

spec/unit/type/cron_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@
127127
# As it turns out cron does not complaining about steps that exceed the valid range
128128
# expect { described_class.new(:name => 'foo', :minute => '*/120' ) }.to raise_error(Puppet::Error, /is not a valid minute/)
129129
end
130+
131+
it 'supports values with leading zeros' do
132+
expect { described_class.new(name: 'foo', minute: ['0', '0011', '044']) }.not_to raise_error
133+
expect { described_class.new(name: 'foo', minute: '022') }.not_to raise_error
134+
end
135+
136+
it 'does not remove leading zeroes' do
137+
expect(described_class.new(name: 'foo', minute: '0045')[:minute]).to eq(['0045'])
138+
end
130139
end
131140

132141
describe 'hour' do
@@ -194,6 +203,15 @@
194203
# As it turns out cron does not complaining about steps that exceed the valid range
195204
# expect { described_class.new(:name => 'foo', :hour => '*/26' ) }.to raise_error(Puppet::Error, /is not a valid hour/)
196205
end
206+
207+
it 'supports values with leading zeros' do
208+
expect { described_class.new(name: 'foo', hour: ['007', '1', '0023']) }.not_to raise_error
209+
expect { described_class.new(name: 'foo', hour: '022') }.not_to raise_error
210+
end
211+
212+
it 'does not remove leading zeroes' do
213+
expect(described_class.new(name: 'foo', hour: '005')[:hour]).to eq(['005'])
214+
end
197215
end
198216

199217
describe 'weekday' do
@@ -277,6 +295,15 @@
277295
# As it turns out cron does not complaining about steps that exceed the valid range
278296
# expect { described_class.new(:name => 'foo', :weekday => '*/9' ) }.to raise_error(Puppet::Error, /is not a valid weekday/)
279297
end
298+
299+
it 'supports values with leading zeros' do
300+
expect { described_class.new(name: 'foo', weekday: ['Mon', 'Wed', '05']) }.not_to raise_error
301+
expect { described_class.new(name: 'foo', weekday: '007') }.not_to raise_error
302+
end
303+
304+
it 'does not remove leading zeroes' do
305+
expect(described_class.new(name: 'foo', weekday: '006')[:weekday]).to eq(['006'])
306+
end
280307
end
281308

282309
describe 'month' do
@@ -376,6 +403,15 @@
376403
# As it turns out cron does not complaining about steps that exceed the valid range
377404
# expect { described_class.new(:name => 'foo', :month => '*/13' ) }.to raise_error(Puppet::Error, /is not a valid month/)
378405
end
406+
407+
it 'supports values with leading zeros' do
408+
expect { described_class.new(name: 'foo', month: ['007', '1', '0012']) }.not_to raise_error
409+
expect { described_class.new(name: 'foo', month: ['Jan', '04', '0009']) }.not_to raise_error
410+
end
411+
412+
it 'does not remove leading zeroes' do
413+
expect(described_class.new(name: 'foo', month: '09')[:month]).to eq(['09'])
414+
end
379415
end
380416

381417
describe 'monthday' do
@@ -441,6 +477,15 @@
441477
# As it turns out cron does not complaining about steps that exceed the valid range
442478
# expect { described_class.new(:name => 'foo', :monthday => '*/32' ) }.to raise_error(Puppet::Error, /is not a valid monthday/)
443479
end
480+
481+
it 'supports values with leading zeros' do
482+
expect { described_class.new(name: 'foo', monthday: ['007', '1', '0023']) }.not_to raise_error
483+
expect { described_class.new(name: 'foo', monthday: '022') }.not_to raise_error
484+
end
485+
486+
it 'does not remove leading zeroes' do
487+
expect(described_class.new(name: 'foo', monthday: '01')[:monthday]).to eq(['01'])
488+
end
444489
end
445490

446491
describe 'special' do

0 commit comments

Comments
 (0)