diff options
author | Jorie Tappa <jorie@jorietappa.com> | 2018-07-31 14:56:46 -0500 |
---|---|---|
committer | Jorie Tappa <jorie@jorietappa.com> | 2018-07-31 16:07:37 -0500 |
commit | a2af7dd0b9713f279724d2c7e6f17bfd8ce2d95b (patch) | |
tree | 6a90efd716578c7449ec69051279150308884414 /spec/unit | |
parent | e74dce11298298889a40879aad1e2fcc27fa0559 (diff) | |
download | puppet-cron_core-a2af7dd0b9713f279724d2c7e6f17bfd8ce2d95b.tar.gz puppet-cron_core-a2af7dd0b9713f279724d2c7e6f17bfd8ce2d95b.tar.bz2 |
Initial cron import from puppet 7a4c5f07bdf61a7bc7aa32a50e99489a604eac52
Diffstat (limited to 'spec/unit')
-rw-r--r-- | spec/unit/provider/cron/crontab_spec.rb | 207 | ||||
-rw-r--r-- | spec/unit/provider/cron/parsed_spec.rb | 358 | ||||
-rw-r--r-- | spec/unit/type/cron_spec.rb | 543 |
3 files changed, 1108 insertions, 0 deletions
diff --git a/spec/unit/provider/cron/crontab_spec.rb b/spec/unit/provider/cron/crontab_spec.rb new file mode 100644 index 0000000..98ae589 --- /dev/null +++ b/spec/unit/provider/cron/crontab_spec.rb @@ -0,0 +1,207 @@ +#! /usr/bin/env ruby +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 =~ /^# / } + expect(have.lines.to_a[4..-1].join('')).to eq(want) + end + + context "with the simple samples" do + FIELDS = { + :crontab => %w{command minute hour month monthday weekday}.collect { |o| o.intern }, + :environment => [:line], + :blank => [:line], + :comment => [:line], + } + + 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'))) + + 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 "should parse all sample records at once" do + subject.parse(text).zip(records).each do |round| + compare_crontab_record(*round) + end + end + + it "should reconstitute 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] or 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.gsub('_', ' ') do + it "should regenerate the text from the record" do + compare_crontab_text subject.to_file(records), text + end + + it "should parse the records from the text" do + subject.parse(text).zip(records).each do |round| + compare_crontab_record(*round) + end + end + end + end + + it "should parse 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 "should regenerate 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 "should 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 "should 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 "should try and use the match method to find a more fitting record" do + subject.expects(:match).with(record, resources) + subject.prefetch(resources) + end + + it "should not match a provider to the resource" do + resource.expects(:provider=).never + subject.prefetch(resources) + end + + it "should 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..6cec867 --- /dev/null +++ b/spec/unit/provider/cron/parsed_spec.rb @@ -0,0 +1,358 @@ +#!/usr/bin/env ruby + +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 => %w{0 15 30 45}, + :hour => %w{8-18 20-22}, + :monthday => %w{31}, + :month => %w{12}, + :weekday => %w{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 => %w{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 => %w{0 15 30 45}, + :hour => %w{8-18 20-22}, + :monthday => %w{31}, + :month => %w{12}, + :weekday => %w{7}, + :special => :absent, + :command => '/bin/true', + :on_disk => true, + :target => 'root' + } + end + + describe "when determining the correct filetype" do + it "should use 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 "should use 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 "should use 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 "should fallback 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 ] + %w{foo bar} } + before do + File.expects(:readable?).returns true + File.stubs(:file?).returns true + File.stubs(:writable?).returns true + end + after do + File.unstub :readable?, :file?, :writable? + Dir.unstub :foreach + end + it "should add 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 "should parse a comment" do + expect(described_class.parse_line("# This is a test")).to eq({ + :record_type => :comment, + :line => "# This is a test", + }) + end + + it "should get 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 "should ignore 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 "should extract 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 "should extract 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 => %w{0 15 30 45}, + :hour => %w{8-18 20-22}, + :monthday => %w{31}, + :month => %w{12}, + :weekday => %w{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 => %w{0}, + :hour => %w{22}, + :monthday => :absent, + :month => :absent, + :weekday => %w{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 do + Facter.stubs(:value).with(:osfamily).returns 'Linux' + Facter.stubs(:value).with(:operatingsystem) + end + + it "should contain no resources for a user who has no crontab" 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. + described_class.target_object('foobar').expects(:`).with('crontab -u foobar -l 2>/dev/null').returns "" + expect(described_class.instances.select { |resource| + resource.get('target') == 'foobar' + }).to be_empty + end + + it "should contain no resources for a user who is absent" do + # `crontab...` does only capture stdout. On vixie-cron-4.1 + # 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 { |resource| + resource.get('target') == 'foobar' + }).to be_empty + end + + it "should be 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 "should be 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 "should match when all fields are the same" do + expect(described_class.match(record,{resource[:name] => resource})).to eq(resource) + end + + { + :minute => %w{0 15 31 45}, + :hour => %w{8-18}, + :monthday => %w{30 31}, + :month => %w{12 23}, + :weekday => %w{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 "should match 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 "should not raise an error" do + expect { described_class.match(record,{resource_sparse[:name] => resource_sparse}) }.to_not 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..4241e51 --- /dev/null +++ b/spec/unit/type/cron_spec.rb @@ -0,0 +1,543 @@ +#! /usr/bin/env ruby + +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 @provider_class + end + + after :each do + described_class.unprovide(:simple) + end + + it "should have :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 "should support present as a value for ensure" do + expect { described_class.new(:name => 'foo', :ensure => :present) }.to_not raise_error + end + + it "should support absent as a value for ensure" do + expect { described_class.new(:name => 'foo', :ensure => :present) }.to_not raise_error + end + + it "should not support other values" do + expect { described_class.new(:name => 'foo', :ensure => :foo) }.to raise_error(Puppet::Error, /Invalid value/) + end + end + + describe "command" do + it "should discard leading spaces" do + expect(described_class.new(:name => 'foo', :command => " /bin/true")[:command]).not_to match Regexp.new(" ") + end + it "should discard trailing spaces" do + expect(described_class.new(:name => 'foo', :command => "/bin/true ")[:command]).not_to match Regexp.new(" ") + end + end + + describe "minute" do + it "should support absent" do + expect { described_class.new(:name => 'foo', :minute => 'absent') }.to_not raise_error + end + + it "should support *" do + expect { described_class.new(:name => 'foo', :minute => '*') }.to_not raise_error + end + + it "should translate absent to :absent" do + expect(described_class.new(:name => 'foo', :minute => 'absent')[:minute]).to eq(:absent) + end + + it "should translate * to :absent" do + expect(described_class.new(:name => 'foo', :minute => '*')[:minute]).to eq(:absent) + end + + it "should support valid single values" do + expect { described_class.new(:name => 'foo', :minute => '0') }.to_not raise_error + expect { described_class.new(:name => 'foo', :minute => '1') }.to_not raise_error + expect { described_class.new(:name => 'foo', :minute => '59') }.to_not raise_error + end + + it "should not support non numeric characters" do + expect { described_class.new(:name => 'foo', :minute => 'z59') }.to raise_error(Puppet::Error, /z59 is not a valid minute/) + expect { described_class.new(:name => 'foo', :minute => '5z9') }.to raise_error(Puppet::Error, /5z9 is not a valid minute/) + expect { described_class.new(:name => 'foo', :minute => '59z') }.to raise_error(Puppet::Error, /59z is not a valid minute/) + end + + it "should not support single values out of range" do + + expect { described_class.new(:name => 'foo', :minute => '-1') }.to raise_error(Puppet::Error, /-1 is not a valid minute/) + expect { described_class.new(:name => 'foo', :minute => '60') }.to raise_error(Puppet::Error, /60 is not a valid minute/) + expect { described_class.new(:name => 'foo', :minute => '61') }.to raise_error(Puppet::Error, /61 is not a valid minute/) + expect { described_class.new(:name => 'foo', :minute => '120') }.to raise_error(Puppet::Error, /120 is not a valid minute/) + end + + it "should support valid multiple values" do + expect { described_class.new(:name => 'foo', :minute => ['0','1','59'] ) }.to_not raise_error + expect { described_class.new(:name => 'foo', :minute => ['40','30','20'] ) }.to_not raise_error + expect { described_class.new(:name => 'foo', :minute => ['10','30','20'] ) }.to_not raise_error + end + + it "should 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, /60 is not a valid minute/) + expect { described_class.new(:name => 'foo', :minute => ['0','120','59'] ) }.to raise_error(Puppet::Error, /120 is not a valid minute/) + expect { described_class.new(:name => 'foo', :minute => ['-1','1','59'] ) }.to raise_error(Puppet::Error, /-1 is not a valid minute/) + # two invalid + expect { described_class.new(:name => 'foo', :minute => ['0','61','62'] ) }.to raise_error(Puppet::Error, /(61|62) is not a valid minute/) + # all invalid + expect { described_class.new(:name => 'foo', :minute => ['-1','61','62'] ) }.to raise_error(Puppet::Error, /(-1|61|62) is not a valid minute/) + end + + it "should support valid step syntax" do + expect { described_class.new(:name => 'foo', :minute => '*/2' ) }.to_not raise_error + expect { described_class.new(:name => 'foo', :minute => '10-16/2' ) }.to_not raise_error + end + + it "should not support invalid steps" do + expect { described_class.new(:name => 'foo', :minute => '*/A' ) }.to raise_error(Puppet::Error, /\*\/A is not a valid minute/) + expect { described_class.new(:name => 'foo', :minute => '*/2A' ) }.to raise_error(Puppet::Error, /\*\/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 "should support absent" do + expect { described_class.new(:name => 'foo', :hour => 'absent') }.to_not raise_error + end + + it "should support *" do + expect { described_class.new(:name => 'foo', :hour => '*') }.to_not raise_error + end + + it "should translate absent to :absent" do + expect(described_class.new(:name => 'foo', :hour => 'absent')[:hour]).to eq(:absent) + end + + it "should translate * to :absent" do + expect(described_class.new(:name => 'foo', :hour => '*')[:hour]).to eq(:absent) + end + + it "should support valid single values" do + expect { described_class.new(:name => 'foo', :hour => '0') }.to_not raise_error + expect { described_class.new(:name => 'foo', :hour => '11') }.to_not raise_error + expect { described_class.new(:name => 'foo', :hour => '12') }.to_not raise_error + expect { described_class.new(:name => 'foo', :hour => '13') }.to_not raise_error + expect { described_class.new(:name => 'foo', :hour => '23') }.to_not raise_error + end + + it "should not support non numeric characters" do + expect { described_class.new(:name => 'foo', :hour => 'z15') }.to raise_error(Puppet::Error, /z15 is not a valid hour/) + expect { described_class.new(:name => 'foo', :hour => '1z5') }.to raise_error(Puppet::Error, /1z5 is not a valid hour/) + expect { described_class.new(:name => 'foo', :hour => '15z') }.to raise_error(Puppet::Error, /15z is not a valid hour/) + end + + it "should not support single values out of range" do + expect { described_class.new(:name => 'foo', :hour => '-1') }.to raise_error(Puppet::Error, /-1 is not a valid hour/) + expect { described_class.new(:name => 'foo', :hour => '24') }.to raise_error(Puppet::Error, /24 is not a valid hour/) + expect { described_class.new(:name => 'foo', :hour => '120') }.to raise_error(Puppet::Error, /120 is not a valid hour/) + end + + it "should support valid multiple values" do + expect { described_class.new(:name => 'foo', :hour => ['0','1','23'] ) }.to_not raise_error + expect { described_class.new(:name => 'foo', :hour => ['5','16','14'] ) }.to_not raise_error + expect { described_class.new(:name => 'foo', :hour => ['16','13','9'] ) }.to_not raise_error + end + + it "should 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, /24 is not a valid hour/) + expect { described_class.new(:name => 'foo', :hour => ['0','-1','5'] ) }.to raise_error(Puppet::Error, /-1 is not a valid hour/) + expect { described_class.new(:name => 'foo', :hour => ['-1','1','23'] ) }.to raise_error(Puppet::Error, /-1 is not a valid hour/) + # two invalid + expect { described_class.new(:name => 'foo', :hour => ['0','25','26'] ) }.to raise_error(Puppet::Error, /(25|26) is not a valid hour/) + # all invalid + expect { described_class.new(:name => 'foo', :hour => ['-1','24','120'] ) }.to raise_error(Puppet::Error, /(-1|24|120) is not a valid hour/) + end + + it "should support valid step syntax" do + expect { described_class.new(:name => 'foo', :hour => '*/2' ) }.to_not raise_error + expect { described_class.new(:name => 'foo', :hour => '10-18/4' ) }.to_not raise_error + end + + it "should not support invalid steps" do + expect { described_class.new(:name => 'foo', :hour => '*/A' ) }.to raise_error(Puppet::Error, /\*\/A is not a valid hour/) + expect { described_class.new(:name => 'foo', :hour => '*/2A' ) }.to raise_error(Puppet::Error, /\*\/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 "should support absent" do + expect { described_class.new(:name => 'foo', :weekday => 'absent') }.to_not raise_error + end + + it "should support *" do + expect { described_class.new(:name => 'foo', :weekday => '*') }.to_not raise_error + end + + it "should translate absent to :absent" do + expect(described_class.new(:name => 'foo', :weekday => 'absent')[:weekday]).to eq(:absent) + end + + it "should translate * to :absent" do + expect(described_class.new(:name => 'foo', :weekday => '*')[:weekday]).to eq(:absent) + end + + it "should support valid numeric weekdays" do + expect { described_class.new(:name => 'foo', :weekday => '0') }.to_not raise_error + expect { described_class.new(:name => 'foo', :weekday => '1') }.to_not raise_error + expect { described_class.new(:name => 'foo', :weekday => '6') }.to_not raise_error + # According to http://www.manpagez.com/man/5/crontab 7 is also valid (Sunday) + expect { described_class.new(:name => 'foo', :weekday => '7') }.to_not raise_error + end + + it "should support valid weekdays as words (long version)" do + expect { described_class.new(:name => 'foo', :weekday => 'Monday') }.to_not raise_error + expect { described_class.new(:name => 'foo', :weekday => 'Tuesday') }.to_not raise_error + expect { described_class.new(:name => 'foo', :weekday => 'Wednesday') }.to_not raise_error + expect { described_class.new(:name => 'foo', :weekday => 'Thursday') }.to_not raise_error + expect { described_class.new(:name => 'foo', :weekday => 'Friday') }.to_not raise_error + expect { described_class.new(:name => 'foo', :weekday => 'Saturday') }.to_not raise_error + expect { described_class.new(:name => 'foo', :weekday => 'Sunday') }.to_not raise_error + end + + it "should support valid weekdays as words (3 character version)" do + expect { described_class.new(:name => 'foo', :weekday => 'Mon') }.to_not raise_error + expect { described_class.new(:name => 'foo', :weekday => 'Tue') }.to_not raise_error + expect { described_class.new(:name => 'foo', :weekday => 'Wed') }.to_not raise_error + expect { described_class.new(:name => 'foo', :weekday => 'Thu') }.to_not raise_error + expect { described_class.new(:name => 'foo', :weekday => 'Fri') }.to_not raise_error + expect { described_class.new(:name => 'foo', :weekday => 'Sat') }.to_not raise_error + expect { described_class.new(:name => 'foo', :weekday => 'Sun') }.to_not raise_error + end + + it "should not support numeric values out of range" do + expect { described_class.new(:name => 'foo', :weekday => '-1') }.to raise_error(Puppet::Error, /-1 is not a valid weekday/) + expect { described_class.new(:name => 'foo', :weekday => '8') }.to raise_error(Puppet::Error, /8 is not a valid weekday/) + end + + it "should not support invalid weekday names" do + expect { described_class.new(:name => 'foo', :weekday => 'Sar') }.to raise_error(Puppet::Error, /Sar is not a valid weekday/) + end + + it "should support valid multiple values" do + expect { described_class.new(:name => 'foo', :weekday => ['0','1','6'] ) }.to_not raise_error + expect { described_class.new(:name => 'foo', :weekday => ['Mon','Wed','Friday'] ) }.to_not raise_error + end + + it "should 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, /8 is not a valid weekday/) + expect { described_class.new(:name => 'foo', :weekday => ['Mon','Fii','Sat'] ) }.to raise_error(Puppet::Error, /Fii is not a valid weekday/) + # two invalid + expect { described_class.new(:name => 'foo', :weekday => ['Mos','Fii','Sat'] ) }.to raise_error(Puppet::Error, /(Mos|Fii) is not a valid weekday/) + # all invalid + expect { described_class.new(:name => 'foo', :weekday => ['Mos','Fii','Saa'] ) }.to raise_error(Puppet::Error, /(Mos|Fii|Saa) is not a valid weekday/) + expect { described_class.new(:name => 'foo', :weekday => ['-1','8','11'] ) }.to raise_error(Puppet::Error, /(-1|8|11) is not a valid weekday/) + end + + it "should support valid step syntax" do + expect { described_class.new(:name => 'foo', :weekday => '*/2' ) }.to_not raise_error + expect { described_class.new(:name => 'foo', :weekday => '0-4/2' ) }.to_not raise_error + end + + it "should not support invalid steps" do + expect { described_class.new(:name => 'foo', :weekday => '*/A' ) }.to raise_error(Puppet::Error, /\*\/A is not a valid weekday/) + expect { described_class.new(:name => 'foo', :weekday => '*/2A' ) }.to raise_error(Puppet::Error, /\*\/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 "should support absent" do + expect { described_class.new(:name => 'foo', :month => 'absent') }.to_not raise_error + end + + it "should support *" do + expect { described_class.new(:name => 'foo', :month => '*') }.to_not raise_error + end + + it "should translate absent to :absent" do + expect(described_class.new(:name => 'foo', :month => 'absent')[:month]).to eq(:absent) + end + + it "should translate * to :absent" do + expect(described_class.new(:name => 'foo', :month => '*')[:month]).to eq(:absent) + end + + it "should support valid numeric values" do + expect { described_class.new(:name => 'foo', :month => '1') }.to_not raise_error + expect { described_class.new(:name => 'foo', :month => '12') }.to_not raise_error + end + + it "should support 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 "should support 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 "should not support numeric values out of range" do + expect { described_class.new(:name => 'foo', :month => '-1') }.to raise_error(Puppet::Error, /-1 is not a valid month/) + expect { described_class.new(:name => 'foo', :month => '0') }.to raise_error(Puppet::Error, /0 is not a valid month/) + expect { described_class.new(:name => 'foo', :month => '13') }.to raise_error(Puppet::Error, /13 is not a valid month/) + end + + it "should not support words that are not valid months" do + expect { described_class.new(:name => 'foo', :month => 'Jal') }.to raise_error(Puppet::Error, /Jal is not a valid month/) + end + + it "should not support single values out of range" do + + expect { described_class.new(:name => 'foo', :month => '-1') }.to raise_error(Puppet::Error, /-1 is not a valid month/) + expect { described_class.new(:name => 'foo', :month => '60') }.to raise_error(Puppet::Error, /60 is not a valid month/) + expect { described_class.new(:name => 'foo', :month => '61') }.to raise_error(Puppet::Error, /61 is not a valid month/) + expect { described_class.new(:name => 'foo', :month => '120') }.to raise_error(Puppet::Error, /120 is not a valid month/) + end + + it "should support valid multiple values" do + expect { described_class.new(:name => 'foo', :month => ['1','9','12'] ) }.to_not raise_error + expect { described_class.new(:name => 'foo', :month => ['Jan','March','Jul'] ) }.to_not raise_error + end + + it "should 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, /0 is not a valid month/) + expect { described_class.new(:name => 'foo', :month => ['1','13','10'] ) }.to raise_error(Puppet::Error, /13 is not a valid month/) + expect { described_class.new(:name => 'foo', :month => ['Jan','Feb','Jxx'] ) }.to raise_error(Puppet::Error, /Jxx is not a valid month/) + # two invalid + expect { described_class.new(:name => 'foo', :month => ['Jan','Fex','Jux'] ) }.to raise_error(Puppet::Error, /(Fex|Jux) is not a valid month/) + # all invalid + expect { described_class.new(:name => 'foo', :month => ['-1','0','13'] ) }.to raise_error(Puppet::Error, /(-1|0|13) is not a valid month/) + expect { described_class.new(:name => 'foo', :month => ['Jax','Fex','Aux'] ) }.to raise_error(Puppet::Error, /(Jax|Fex|Aux) is not a valid month/) + end + + it "should support valid step syntax" do + expect { described_class.new(:name => 'foo', :month => '*/2' ) }.to_not raise_error + expect { described_class.new(:name => 'foo', :month => '1-12/3' ) }.to_not raise_error + end + + it "should not support invalid steps" do + expect { described_class.new(:name => 'foo', :month => '*/A' ) }.to raise_error(Puppet::Error, /\*\/A is not a valid month/) + expect { described_class.new(:name => 'foo', :month => '*/2A' ) }.to raise_error(Puppet::Error, /\*\/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 "should support absent" do + expect { described_class.new(:name => 'foo', :monthday => 'absent') }.to_not raise_error + end + + it "should support *" do + expect { described_class.new(:name => 'foo', :monthday => '*') }.to_not raise_error + end + + it "should translate absent to :absent" do + expect(described_class.new(:name => 'foo', :monthday => 'absent')[:monthday]).to eq(:absent) + end + + it "should translate * to :absent" do + expect(described_class.new(:name => 'foo', :monthday => '*')[:monthday]).to eq(:absent) + end + + it "should support valid single values" do + expect { described_class.new(:name => 'foo', :monthday => '1') }.to_not raise_error + expect { described_class.new(:name => 'foo', :monthday => '30') }.to_not raise_error + expect { described_class.new(:name => 'foo', :monthday => '31') }.to_not raise_error + end + + it "should not support non numeric characters" do + expect { described_class.new(:name => 'foo', :monthday => 'z23') }.to raise_error(Puppet::Error, /z23 is not a valid monthday/) + expect { described_class.new(:name => 'foo', :monthday => '2z3') }.to raise_error(Puppet::Error, /2z3 is not a valid monthday/) + expect { described_class.new(:name => 'foo', :monthday => '23z') }.to raise_error(Puppet::Error, /23z is not a valid monthday/) + end + + it "should not support single values out of range" do + expect { described_class.new(:name => 'foo', :monthday => '-1') }.to raise_error(Puppet::Error, /-1 is not a valid monthday/) + expect { described_class.new(:name => 'foo', :monthday => '0') }.to raise_error(Puppet::Error, /0 is not a valid monthday/) + expect { described_class.new(:name => 'foo', :monthday => '32') }.to raise_error(Puppet::Error, /32 is not a valid monthday/) + end + + it "should support valid multiple values" do + expect { described_class.new(:name => 'foo', :monthday => ['1','23','31'] ) }.to_not raise_error + expect { described_class.new(:name => 'foo', :monthday => ['31','23','1'] ) }.to_not raise_error + expect { described_class.new(:name => 'foo', :monthday => ['1','31','23'] ) }.to_not raise_error + end + + it "should 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, /32 is not a valid monthday/) + expect { described_class.new(:name => 'foo', :monthday => ['-1','12','23'] ) }.to raise_error(Puppet::Error, /-1 is not a valid monthday/) + expect { described_class.new(:name => 'foo', :monthday => ['13','32','30'] ) }.to raise_error(Puppet::Error, /32 is not a valid monthday/) + # two invalid + expect { described_class.new(:name => 'foo', :monthday => ['-1','0','23'] ) }.to raise_error(Puppet::Error, /(-1|0) is not a valid monthday/) + # all invalid + expect { described_class.new(:name => 'foo', :monthday => ['-1','0','32'] ) }.to raise_error(Puppet::Error, /(-1|0|32) is not a valid monthday/) + end + + it "should support valid step syntax" do + expect { described_class.new(:name => 'foo', :monthday => '*/2' ) }.to_not raise_error + expect { described_class.new(:name => 'foo', :monthday => '10-16/2' ) }.to_not raise_error + end + + it "should not support invalid steps" do + expect { described_class.new(:name => 'foo', :monthday => '*/A' ) }.to raise_error(Puppet::Error, /\*\/A is not a valid monthday/) + expect { described_class.new(:name => 'foo', :monthday => '*/2A' ) }.to raise_error(Puppet::Error, /\*\/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 + %w(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 ) }.to_not raise_error + end + end + + context "when combined with numeric schedule fields" do + context "which are 'absent'" do + [ %w(reboot yearly annually monthly weekly daily midnight hourly), :absent ].flatten.each { |value| + it "should accept the value '#{value}' for special" do + expect { + described_class.new(:name => 'foo', :minute => :absent, :special => value ) + }.to_not raise_error + end + } + end + context "which are not absent" do + %w(reboot yearly annually monthly weekly daily midnight hourly).each { |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, /cannot specify both a special schedule and a value/) + end + } + it "should accept the 'absent' value for special" do + expect { + described_class.new(:name => 'foo', :minute => "1", :special => :absent ) + }.to_not raise_error + end + end + end + end + + describe "environment" do + it "it should accept an :environment that looks like a path" do + expect do + described_class.new(:name => 'foo',:environment => 'PATH=/bin:/usr/bin:/usr/sbin') + end.to_not raise_error + end + + it "should not accept environment variables that do not contain '='" do + expect do + described_class.new(:name => 'foo',:environment => 'INVALID') + end.to raise_error(Puppet::Error, /Invalid environment setting "INVALID"/) + end + + it "should accept empty environment variables that do not contain '='" do + expect do + described_class.new(:name => 'foo',:environment => 'MAILTO=') + end.to_not raise_error + end + + it "should accept 'absent'" do + expect do + described_class.new(:name => 'foo',:environment => 'absent') + end.to_not raise_error + end + + end + end + + describe "when autorequiring resources" do + + before :each do + @user_bob = Puppet::Type.type(:user).new(:name => 'bob', :ensure => :present) + @user_alice = Puppet::Type.type(:user).new(:name => 'alice', :ensure => :present) + @catalog = Puppet::Resource::Catalog.new + @catalog.add_resource @user_bob, @user_alice + end + + it "should autorequire 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 "should 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 "should default 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 |