diff options
| author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-03-17 02:50:48 +0000 |
|---|---|---|
| committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-03-17 02:50:48 +0000 |
| commit | 86c63ce2d9e93786cb27f9056b90f6887cbc8826 (patch) | |
| tree | 2457fcfa3098bc1f6da35a6dff7ce1fcc2f52c90 /test | |
| parent | ba23a5ac276e59fdda8186750c6d0fd2cfecdeac (diff) | |
Fixing cron support (I hope). It now uses providers, and seems to work, at least on my os x box.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2284 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'test')
| -rw-r--r-- | test/data/providers/cron/crontab.allthree | 17 | ||||
| -rw-r--r-- | test/data/providers/cron/crontab.envNcomment | 12 | ||||
| -rw-r--r-- | test/data/providers/cron/crontab.envNname | 11 | ||||
| -rw-r--r-- | test/data/providers/cron/crontab.multirecords | 12 | ||||
| -rw-r--r-- | test/data/providers/cron/examples/freebsd (renamed from test/data/types/cron/freebsd) | 0 | ||||
| -rw-r--r-- | test/data/providers/cron/examples/one (renamed from test/data/types/cron/one) | 0 | ||||
| -rwxr-xr-x | test/ral/manager/attributes.rb | 45 | ||||
| -rwxr-xr-x | test/ral/manager/provider.rb | 52 | ||||
| -rwxr-xr-x | test/ral/providers/cron/crontab.rb | 364 | ||||
| -rwxr-xr-x | test/ral/types/cron.rb | 474 | ||||
| -rwxr-xr-x | test/util/fileparsing.rb | 5 |
11 files changed, 528 insertions, 464 deletions
diff --git a/test/data/providers/cron/crontab.allthree b/test/data/providers/cron/crontab.allthree new file mode 100644 index 000000000..dd2a40466 --- /dev/null +++ b/test/data/providers/cron/crontab.allthree @@ -0,0 +1,17 @@ +--- +- | + # comment 1 + # Puppet Name: name2 + env3=val + * * * * * command4 + # comment 5 + +- - :record_type: :comment + :line: "# comment 1" + - :command: command4 + :environment: + - env3=val + :name: name2 + :record_type: :crontab + - :record_type: :comment + :line: "# comment 5" diff --git a/test/data/providers/cron/crontab.envNcomment b/test/data/providers/cron/crontab.envNcomment new file mode 100644 index 000000000..63effe076 --- /dev/null +++ b/test/data/providers/cron/crontab.envNcomment @@ -0,0 +1,12 @@ +--- +- | + # comment 9 + env10=val + * * * * * command11 + +- - :record_type: :comment + :line: "# comment 9" + - :record_type: :environment + :line: env10=val + - :command: command11 + :record_type: :crontab diff --git a/test/data/providers/cron/crontab.envNname b/test/data/providers/cron/crontab.envNname new file mode 100644 index 000000000..cc058248e --- /dev/null +++ b/test/data/providers/cron/crontab.envNname @@ -0,0 +1,11 @@ +--- +- | + env6=val + # Puppet Name: name7 + * * * * * command8 + +- - :record_type: :environment + :line: env6=val + - :command: command8 + :record_type: :crontab + :name: name7 diff --git a/test/data/providers/cron/crontab.multirecords b/test/data/providers/cron/crontab.multirecords new file mode 100644 index 000000000..63effe076 --- /dev/null +++ b/test/data/providers/cron/crontab.multirecords @@ -0,0 +1,12 @@ +--- +- | + # comment 9 + env10=val + * * * * * command11 + +- - :record_type: :comment + :line: "# comment 9" + - :record_type: :environment + :line: env10=val + - :command: command11 + :record_type: :crontab diff --git a/test/data/types/cron/freebsd b/test/data/providers/cron/examples/freebsd index fba1e310b..fba1e310b 100644 --- a/test/data/types/cron/freebsd +++ b/test/data/providers/cron/examples/freebsd diff --git a/test/data/types/cron/one b/test/data/providers/cron/examples/one index 9cddf97e7..9cddf97e7 100644 --- a/test/data/types/cron/one +++ b/test/data/providers/cron/examples/one diff --git a/test/ral/manager/attributes.rb b/test/ral/manager/attributes.rb index 6fd132aef..f27b0513a 100755 --- a/test/ral/manager/attributes.rb +++ b/test/ral/manager/attributes.rb @@ -182,6 +182,51 @@ class TestTypeAttributes < Test::Unit::TestCase end assert_equal(false, inst[:falsetest], "false default was not set") end + + def test_alias_parameter + type = mktype + type.newparam(:name) {} + type.newparam(:one) {} + type.newproperty(:two) {} + + aliases = { + :three => :one, + :four => :two + } + aliases.each do |new, old| + assert_nothing_raised("Could not create alias parameter %s" % new) do + type.set_attr_alias new => old + end + end + + aliases.each do |new, old| + assert_equal(old, type.attr_alias(new), "did not return alias info for %s" % + new) + end + + assert_nil(type.attr_alias(:name), "got invalid alias info for name") + + inst = type.create(:name => "my name") + assert(inst, "could not create instance") + + aliases.each do |new, old| + val = "value %s" % new + assert_nothing_raised do + inst[new] = val + end + + case old + when :one: # param + assert_equal(val, inst[new], + "Incorrect alias value for %s in []" % new) + else + assert_equal(val, inst.should(new), + "Incorrect alias value for %s in should" % new) + end + assert_equal(val, inst.value(new), "Incorrect alias value for %s" % new) + assert_equal(val, inst.value(old), "Incorrect orig value for %s" % old) + end + end end # $Id$ diff --git a/test/ral/manager/provider.rb b/test/ral/manager/provider.rb new file mode 100755 index 000000000..6075697d6 --- /dev/null +++ b/test/ral/manager/provider.rb @@ -0,0 +1,52 @@ +#!/usr/bin/env ruby + +$:.unshift("../../lib") if __FILE__ =~ /\.rb$/ + +require 'puppettest' +require 'mocha' + +class TestTypeProviders < Test::Unit::TestCase + include PuppetTest + + # Make sure default providers behave correctly + def test_defaultproviders + # Make a fake type + type = Puppet::Type.newtype(:defaultprovidertest) do + newparam(:name) do end + end + + basic = type.provide(:basic) do + defaultfor :operatingsystem => :somethingelse, + :operatingsystemrelease => :yayness + end + + assert_equal(basic, type.defaultprovider) + type.defaultprovider = nil + + greater = type.provide(:greater) do + defaultfor :operatingsystem => Facter.value("operatingsystem") + end + + assert_equal(greater, type.defaultprovider) + end + + # Make sure the provider is always the first parameter created. + def test_provider_sorting + type = Puppet::Type.newtype(:sorttest) do + newparam(:name) {} + ensurable + end + cleanup { Puppet::Type.rmtype(:sorttest) } + + should = [:name, :ensure] + assert_equal(should, type.allattrs.reject { |p| ! should.include?(p) }, + "Got wrong order of parameters") + + type.provide(:yay) { } + should = [:name, :provider, :ensure] + assert_equal(should, type.allattrs.reject { |p| ! should.include?(p) }, + "Providify did not reorder parameters") + end +end + +# $Id$ diff --git a/test/ral/providers/cron/crontab.rb b/test/ral/providers/cron/crontab.rb index c4f1031cd..6d0d1e8a9 100755 --- a/test/ral/providers/cron/crontab.rb +++ b/test/ral/providers/cron/crontab.rb @@ -14,7 +14,9 @@ class TestCronParsedProvider < Test::Unit::TestCase def setup super - @provider = Puppet::Type.type(:cron).provider(:crontab) + @type = Puppet::Type.type(:cron) + @provider = @type.provider(:crontab) + @provider.initvars @oldfiletype = @provider.filetype end @@ -30,7 +32,7 @@ class TestCronParsedProvider < Test::Unit::TestCase fields = [:month, :weekday, :monthday, :hour, :command, :minute] { "* * * * * /bin/echo" => {:command => "/bin/echo"}, - "10 * * * * /bin/echo test" => {:minute => "10", + "10 * * * * /bin/echo test" => {:minute => ["10"], :command => "/bin/echo test"} }.each do |line, should| result = nil @@ -50,113 +52,44 @@ class TestCronParsedProvider < Test::Unit::TestCase end end - def test_prefetch_hook - count = 0 - env = Proc.new do - count += 1 - {:record_type => :environment, :line => "env%s=val" % count} - end - comment = Proc.new do |name| - count += 1 - hash = {:record_type => :comment, :line => "comment %s" % count} - if name - hash[:name] = name - end - hash - end - record = Proc.new do - count += 1 - {:record_type => :crontab, :command => "command%s" % count} - end + def test_parse_and_generate + @provider.filetype = :ram + crondir = datadir(File.join(%w{providers cron})) + files = Dir.glob("%s/crontab.*" % crondir) - result = nil - args = [] - # First try it with all three - args << comm = comment.call(false) - args << name = comment.call(true) - args << var = env.call() - args << line = record.call - args << sec = comment.call(false) - assert_nothing_raised do - result = @provider.prefetch_hook(args) - end - assert_equal([comm, line, sec], result, - "Did not remove name and var records") - - assert_equal(name[:name], line[:name], - "did not set name in hook") - assert_equal([var[:line]], line[:environment], - "did not set env") - - # Now try it with an env, a name, and a record - args.clear - args << var = env.call() - args << name = comment.call(true) - args << line = record.call - assert_nothing_raised do - result = @provider.prefetch_hook(args) - end + setme + target = @provider.target_object(@me) + files.each do |file| + str = args = nil + assert_nothing_raised("could not load %s" % file) do + str, args = YAML.load(File.read(file)) + end + target.write(str) + assert_nothing_raised("could not parse %s" % file) do + @provider.prefetch_target(@me) + end + records = @provider.send(:instance_variable_get, "@records") - assert_equal([var, line], result, - "Removed var record") - assert_equal(name[:name], line[:name], - "did not set name in hook") - assert_nil(line[:environment], "incorrectly set env") - - # try it with a comment, an env, and a record - args.clear - args << comm = comment.call(false) - args << var = env.call() - args << line = record.call - assert_nothing_raised do - result = @provider.prefetch_hook(args) - end - assert_equal([comm, var, line], result, - "Removed var record") - assert_nil(line[:name], "name got set somehow") - assert_nil(line[:environment], "env got set somehow") - - # Try it with multiple records - args = [] - should = [] - args << startcom = comment.call(false) - should << startcom - args << startenv = env.call - should << startenv - - args << name1 = comment.call(true) - args << env1s = env.call - args << env1m = env.call - args << line1 = record.call - should << line1 - - args << midcom = comment.call(false) - args << midenv = env.call - should << midcom << midenv - - args << name2 = comment.call(true) - args << env2s = env.call - args << env2m = env.call - args << line2 = record.call - should << line2 - - args << endcom = comment.call(false) - args << endenv = env.call - should << endcom << endenv - - assert_nothing_raised do - result = @provider.prefetch_hook(args) - end - assert_equal(should, result, - "Did not handle records correctly") + args.zip(records) do |should, sis| + # Make the values a bit more equal. + should[:target] = @me + should[:ensure] = :present + should[:on_disk] = true + is = sis.dup + sis.dup.each do |p,v| + is.delete(p) if v == :absent + end + assert_equal(should, is, + "Did not parse %s correctly" % file) + end - assert_equal(line1[:name], line1[:name], "incorrectly set first name") - assert_equal(line1[:environment], line1[:environment], - "incorrectly set first env") + assert_nothing_raised("could not generate %s" % file) do + @provider.flush_target(@me) + end - assert_equal(line2[:name], line2[:name], "incorrectly set second name") - assert_equal(line2[:environment], line2[:environment], - "incorrectly set second env") + assert_equal(str, target.read, "%s changed" % file) + @provider.clear + end end # A simple test to see if we can load the cron from disk. @@ -168,6 +101,227 @@ class TestCronParsedProvider < Test::Unit::TestCase } assert_instance_of(Array, records, "did not get correct response") end + + # Test that a cron job turns out as expected, by creating one and generating + # it directly + def test_simple_to_cron + # make the cron + setme() + + name = "yaytest" + args = {:name => name, + :command => "date > /dev/null", + :minute => "30", + :user => @me, + :record_type => :crontab + } + # generate the text + str = nil + assert_nothing_raised { + str = @provider.to_line(args) + } + + assert_equal("# Puppet Name: #{name}\n30 * * * * date > /dev/null", str, + "Cron did not generate correctly") + end + + # Test that comments are correctly retained + def test_retain_comments + str = "# this is a comment\n#and another comment\n" + user = "fakeuser" + records = nil + target = @provider.filetype = :ram + target = @provider.target_object(user) + target.write(str) + assert_nothing_raised { + @provider.prefetch_target(user) + } + + assert_nothing_raised { + newstr = @provider.flush_target(user) + assert(target.read.include?(str), "Comments were lost") + } + end + + def test_simpleparsing + @provider.filetype = :ram + text = "5 1,2 * 1 0 /bin/echo funtest" + + records = nil + assert_nothing_raised { + records = @provider.parse(text) + } + + should = { + :minute => %w{5}, + :hour => %w{1 2}, + :monthday => :absent, + :month => %w{1}, + :weekday => %w{0}, + :command => "/bin/echo funtest" + } + + is = records.shift + assert(is, "Did not get record") + + should.each do |p, v| + assert_equal(v, is[p], "did not parse %s correctly" % p) + end + end + + # Make sure we can create a cron in an empty tab + def test_mkcron_if_empty + setme + @provider.filetype = @oldfiletype + + records = @provider.retrieve(@me) + + target = @provider.target_object(@me) + + cleanup do + if records.length == 0 + target.remove + else + target.write(@provider.to_file(records)) + end + end + + # Now get rid of it + assert_nothing_raised("Could not remove cron tab") do + target.remove + end + + @provider.flush :target => @me, :command => "/do/something", + :record_type => :crontab + created = @provider.retrieve(@me) + assert(created.detect { |r| r[:command] == "/do/something" }, + "Did not create cron tab") + end + + # Make sure we correctly bidirectionally parse things. + def test_records_and_strings + @provider.filetype = :ram + setme + + target = @provider.target_object(@me) + + [ + "* * * * * /some/command", + "0,30 * * * * /some/command", + "0-30 * * * * /some/command", + "# Puppet Name: name\n0-30 * * * * /some/command", + "# Puppet Name: name\nVAR=VALUE\n0-30 * * * * /some/command", + "# Puppet Name: name\nVAR=VALUE\nC=D\n0-30 * * * * /some/command", + "0 * * * * /some/command" + ].each do |str| + @provider.initvars + str += "\n" + target.write(str) + assert_equal(str, target.read, + "Did not write correctly") + assert_nothing_raised("Could not prefetch with %s" % str.inspect) do + @provider.prefetch_target(@me) + end + assert_nothing_raised("Could not flush with %s" % str.inspect) do + @provider.flush_target(@me) + end + + assert_equal(str, target.read, + "Changed in read/write") + + @provider.clear + end + end + + # Test that a specified cron job will be matched against an existing job + # with no name, as long as all fields match + def test_matchcron + str = "0,30 * * * * date\n" + setme + @provider.filetype = :ram + + cron = nil + assert_nothing_raised { + cron = @type.create( + :name => "yaycron", + :minute => [0, 30], + :command => "date", + :user => @me + ) + } + + hash = @provider.parse_line(str) + hash[:user] = @me + + instance = @provider.match(hash) + assert(instance, "did not match cron") + assert_equal(cron, instance, + "Did not match cron job") + end + + def test_data + setme + @provider.filetype = :ram + target = @provider.target_object(@me) + fakedata("data/providers/cron/examples").each do |file| + text = File.read(file) + target.write(text) + + assert_nothing_raised("Could not parse %s" % file) do + @provider.prefetch_target(@me) + end + # mark the provider modified + @provider.modified(@me) + + # and zero the text + target.write("") + + result = nil + assert_nothing_raised("Could not generate %s" % file) do + @provider.flush_target(@me) + end + + assert_equal(text, target.read, + "File was not rewritten the same") + + @provider.clear + end + end + + # Match freebsd's annoying @daily stuff. + def test_match_freebsd_special + @provider.filetype = :ram + setme + + target = @provider.target_object(@me) + + [ + "@daily /some/command", + "@daily /some/command more" + ].each do |str| + @provider.initvars + str += "\n" + target.write(str) + assert_equal(str, target.read, + "Did not write correctly") + assert_nothing_raised("Could not prefetch with %s" % str.inspect) do + @provider.prefetch_target(@me) + end + records = @provider.send(:instance_variable_get, "@records") + records.each do |r| + assert_equal(:freebsd_special, r[:record_type], + "Did not create lines as freebsd lines") + end + assert_nothing_raised("Could not flush with %s" % str.inspect) do + @provider.flush_target(@me) + end + + assert_equal(str, target.read, + "Changed in read/write") + + @provider.clear + end + end end # $Id$ diff --git a/test/ral/types/cron.rb b/test/ral/types/cron.rb index f4b9719ea..96d7cb3f6 100755 --- a/test/ral/types/cron.rb +++ b/test/ral/types/cron.rb @@ -3,18 +3,38 @@ $:.unshift("../../lib") if __FILE__ =~ /\.rb$/ require 'puppettest' +require 'spec' # Test cron job creation, modification, and destruction class TestCron < Test::Unit::TestCase include PuppetTest + def setup super setme() - # god i'm lazy - @crontype = Puppet.type(:cron) + @crontype = Puppet::Type.type(:cron) + @provider = @crontype.defaultprovider + if @provider.respond_to?(:filetype=) + @oldfiletype = @provider.filetype + @provider.filetype = :ram + end + @crontype = Puppet::Type.type(:cron) + end + + def teardown + @crontype.defaultprovider = nil + if defined? @oldfiletype + @provider.filetype = @oldfiletype + end + end + + def eachprovider + @crontype.suitableprovider.each do |provider| + yield provider + end end # Back up the user's existing cron tab if they have one. @@ -76,6 +96,7 @@ class TestCron < Test::Unit::TestCase comp = newcomp(name, cron) assert_events([:cron_created], comp) + cron.provider.class.prefetch cron.retrieve assert(cron.insync?, "Cron is not in sync") @@ -92,187 +113,47 @@ class TestCron < Test::Unit::TestCase assert_events([:cron_removed], comp) + cron.provider.class.prefetch cron.retrieve assert(cron.insync?, "Cron is not in sync") assert_events([], comp) end - # Test that a cron job turns out as expected, by creating one and generating - # it directly - def test_simple_to_cron - cron = nil - # make the cron - name = "yaytest" - assert_nothing_raised { - cron = @crontype.create( - :name => name, - :command => "date > /dev/null", - :user => @me - ) - } - str = nil - # generate the text - assert_nothing_raised { - str = cron.to_record - } - - assert_equal(str, "# Puppet Name: #{name}\n* * * * * date > /dev/null", - "Cron did not generate correctly") - end - - def test_simpleparsing - @fakefiletype = Puppet::Util::FileType.filetype(:ram) - @crontype.filetype = @fakefiletype - - @crontype.retrieve(@me) - obj = Puppet::Type::Cron.cronobj(@me) - - text = "5 1,2 * 1 0 /bin/echo funtest" - - assert_nothing_raised { - @crontype.parse(@me, text) - } - - @crontype.each do |obj| - assert_equal(["5"], obj.is(:minute), "Minute was not parsed correctly") - assert_equal(["1", "2"], obj.is(:hour), "Hour was not parsed correctly") - assert_equal([:absent], obj.is(:monthday), "Monthday was not parsed correctly") - assert_equal(["1"], obj.is(:month), "Month was not parsed correctly") - assert_equal(["0"], obj.is(:weekday), "Weekday was not parsed correctly") - assert_equal(["/bin/echo funtest"], obj.is(:command), "Command was not parsed correctly") - end - end - - # Test that changing any field results in the cron tab being rewritten. - # it directly - def test_any_field_changes - cron = nil - # make the cron - name = "yaytest" - assert_nothing_raised { - cron = @crontype.create( - :name => name, - :command => "date > /dev/null", - :month => "May", - :user => @me - ) - } - assert(cron, "Cron did not get created") - comp = newcomp(cron) - assert_events([:cron_created], comp) - - assert_nothing_raised { - cron[:month] = "June" - } - - cron.retrieve - - assert_events([:cron_changed], comp) - end - # Test that a cron job with spaces at the end doesn't get rewritten def test_trailingspaces - cron = nil - # make the cron - name = "yaytest" - assert_nothing_raised { - cron = @crontype.create( - :name => name, - :command => "date > /dev/null ", - :month => "May", - :user => @me - ) - } - comp = newcomp(cron) - - assert_events([:cron_created], comp, "did not create cron job") - cron.retrieve - assert_events([], comp, "cron job got rewritten") - end - - # Test that comments are correctly retained - def test_retain_comments - str = "# this is a comment\n#and another comment\n" - user = "fakeuser" - @crontype.retrieve(@me) - assert_nothing_raised { - @crontype.parse(@me, str) - } - - assert_nothing_raised { - newstr = @crontype.tab(@me) - assert(newstr.include?(str), "Comments were lost") - } - end - - # Test that a specified cron job will be matched against an existing job - # with no name, as long as all fields match - def test_matchcron - str = "0,30 * * * * date\n" - - assert_nothing_raised { - cron = @crontype.create( - :name => "yaycron", - :minute => [0, 30], - :command => "date", - :user => @me - ) - } - - assert_nothing_raised { - @crontype.parse(@me, str) - } + eachprovider do |provider| + cron = nil + # make the cron + name = "yaytest" + command = "date > /dev/null " + assert_nothing_raised { + cron = @crontype.create( + :name => name, + :command => "date > /dev/null ", + :month => "May", + :user => @me + ) + } + property = cron.send(:property, :command) + cron.provider.command = command + cron.provider.class.prefetch + cron.retrieve - count = @crontype.inject(0) do |c, obj| - c + 1 + assert(property.insync?, "command parsing removes trailing whitespace") + @crontype.clear end - - assert_equal(1, count, "Did not match cron job") - - modstr = "# Puppet Name: yaycron\n%s" % str - - assert_nothing_raised { - newstr = @crontype.tab(@me) - assert(newstr.include?(modstr), - "Cron was not correctly matched") - } - end - - # Test adding a cron when there is currently no file. - def test_mkcronwithnotab - tab = @fakefiletype.new(@me) - tab.remove - - @crontype.retrieve(@me) - cron = mkcron("testwithnotab") - cyclecron(cron) - end - - def test_mkcronwithtab - @crontype.retrieve(@me) - obj = Puppet::Type::Cron.cronobj(@me) - obj.write( -"1 1 1 1 * date > %s/crontesting\n" % tstdir() - ) - - cron = mkcron("testwithtab") - cyclecron(cron) end def test_makeandretrievecron - tab = @fakefiletype.new(@me) - tab.remove - %w{storeandretrieve a-name another-name more_naming SomeName}.each do |name| cron = mkcron(name) comp = newcomp(name, cron) trans = assert_events([:cron_created], comp, name) + cron.provider.class.prefetch cron = nil - Puppet.type(:cron).retrieve(@me) - assert(cron = Puppet.type(:cron)[name], "Could not retrieve named cron") assert_instance_of(Puppet.type(:cron), cron) end @@ -336,78 +217,80 @@ class TestCron < Test::Unit::TestCase } end - # Test that we can read and write cron tabs - def test_crontab - Puppet.type(:cron).filetype = Puppet.type(:cron).defaulttype - type = nil - unless type = Puppet.type(:cron).filetype - $stderr.puts "No crontab type; skipping test" - end - - obj = nil - assert_nothing_raised { - obj = type.new(Puppet::Util::SUIDManager.uid) - } - - txt = nil - assert_nothing_raised { - txt = obj.read - } - - assert_nothing_raised { - obj.write(txt) - } - end - # Verify that comma-separated numbers are not resulting in rewrites - def test_norewrite - cron = nil - assert_nothing_raised { - cron = Puppet.type(:cron).create( - :command => "/bin/date > /dev/null", - :minute => [0, 30], - :name => "crontest" - ) - } + def test_comma_separated_vals_work + eachprovider do |provider| + cron = nil + assert_nothing_raised { + cron = @crontype.create( + :command => "/bin/date > /dev/null", + :minute => [0, 30], + :name => "crontest", + :provider => provider.name + ) + } - assert_events([:cron_created], cron) - cron.retrieve - assert_events([], cron) + minute = cron.send(:property, :minute) + cron.provider.minute = %w{0 30} + cron.provider.class.prefetch + cron.retrieve + + assert(minute.insync?, "minute is out of sync with %s" % provider.name) + @crontype.clear + end end def test_fieldremoval cron = nil assert_nothing_raised { - cron = Puppet.type(:cron).create( + cron = @crontype.create( :command => "/bin/date > /dev/null", :minute => [0, 30], - :name => "crontest" + :name => "crontest", + :provider => :crontab ) } assert_events([:cron_created], cron) + cron.provider.class.prefetch cron[:minute] = :absent assert_events([:cron_changed], cron) assert_nothing_raised { + cron.provider.class.prefetch cron.retrieve } assert_equal(:absent, cron.is(:minute)) end def test_listing - @crontype.filetype = @oldfiletype + # Make a crontab cron for testing + provider = @crontype.provider(:crontab) + return unless provider.suitable? + + ft = provider.filetype + provider.filetype = :ram + cleanup { provider.filetype = ft } + + setme + cron = @crontype.create(:name => "testing", + :minute => [0, 30], + :command => "/bin/testing", + :user => @me + ) + # Write it to our file + assert_apply(cron) crons = [] assert_nothing_raised { - Puppet::Type.type(:cron).list.each do |cron| + @crontype.list.each do |cron| crons << cron end } crons.each do |cron| - assert(cron, "Did not receive a real cron object") - assert_instance_of(String, cron[:user], + assert_instance_of(@crontype, cron, "Did not receive a real cron object") + assert_instance_of(String, cron.value(:user), "Cron user is not a string") end end @@ -434,61 +317,13 @@ class TestCron < Test::Unit::TestCase end end - # Make sure we don't puke on env settings - def test_envsettings - cron = mkcron("envtst") - - assert_apply(cron) - - obj = Puppet::Type::Cron.cronobj(@me) - - assert(obj) - - text = obj.read - - text = "SHELL = /path/to/some/thing\n" + text - - obj.write(text) - - assert_nothing_raised { - cron.retrieve - } - - cron[:command] = "/some/other/command" - - assert_apply(cron) - - assert(obj.read =~ /SHELL/, "lost env setting") - - env1 = "TEST = /bin/true" - env2 = "YAY = fooness" - assert_nothing_raised { - cron[:environment] = [env1, env2] - } - - assert_apply(cron) - - cron.retrieve - - vals = cron.is(:environment) - assert(vals, "Did not get environment settings") - assert(vals != :absent, "Env is incorrectly absent") - assert_instance_of(Array, vals) - - assert(vals.include?(env1), "Missing first env setting") - assert(vals.include?(env2), "Missing second env setting") - - # Now do it again and make sure there are no changes - assert_events([], cron) - - end - def test_divisionnumbers cron = mkcron("divtest") cron[:minute] = "*/5" assert_apply(cron) + cron.provider.class.prefetch cron.retrieve assert_equal(["*/5"], cron.is(:minute)) @@ -500,49 +335,12 @@ class TestCron < Test::Unit::TestCase assert_apply(cron) + cron.provider.class.prefetch cron.retrieve assert_equal(["2-4"], cron.is(:minute)) end - def test_data - @fakefiletype = Puppet::Util::FileType.filetype(:ram) - @crontype.filetype = @fakefiletype - - @crontype.retrieve(@me) - obj = Puppet::Type::Cron.cronobj(@me) - - fakedata("data/types/cron").each do |file| - names = [] - text = File.read(file) - obj.write(File.read(file)) - - @crontype.retrieve(@me) - - @crontype.each do |cron| - names << cron.name - end - - name = File.basename(file) - cron = mkcron("filetest-#{name}") - - assert_apply(cron) - - @crontype.retrieve(@me) - - names.each do |name| - assert(@crontype[name], "Could not retrieve %s" % name) - end - - tablines = @crontype.tab(@me).split("\n") - - text.split("\n").each do |line| - assert(tablines.include?(line), - "Did not get %s back out" % line.inspect) - end - end - end - def test_value cron = mkcron("valuetesting", false) @@ -591,6 +389,8 @@ class TestCron < Test::Unit::TestCase assert_equal("4,5", cron.value(param)) end + Puppet[:trace] = false + # Now make sure that :command works correctly cron.delete(:command) cron.newattr(:command) @@ -632,71 +432,6 @@ class TestCron < Test::Unit::TestCase assert_equal("4", cron.value(param)) end - # Make sure we can successfully list all cron jobs on all users - def test_cron_listing - crons = [] - %w{fake1 fake2 fake3 fake4 fake5}.each do |user| - crons << @crontype.create( - :name => "#{user}-1", - :command => "/usr/bin/#{user}", - :minute => "0", - :user => user, - :hour => user.sub("fake",'') - ) - - crons << @crontype.create( - :name => "#{user}-2", - :command => "/usr/sbin/#{user}", - :minute => "0", - :user => user, - :weekday => user.sub("fake",'') - ) - - assert_apply(*crons) - end - - list = @crontype.list.collect { |c| c.name } - - crons.each do |cron| - assert(list.include?(cron.name), "Did not match cron %s" % name) - end - end - - # Make sure we can create a cron in an empty tab - def test_mkcron_if_empty - @crontype.filetype = @oldfiletype - - @crontype.retrieve(@me) - - # Backup our tab - text = @crontype.tabobj(@me).read - - cleanup do - if text == "" - @crontype.tabobj(@me).remove - else - @crontype.tabobj(@me).write(text) - end - end - - # Now get rid of it - @crontype.tabobj(@me).remove - @crontype.clear - - cron = mkcron("emptycron") - - assert_apply(cron) - - # Clear the type, but don't clear the filetype - @crontype.clear - - # Get the stuff again - @crontype.retrieve(@me) - - assert(@crontype["emptycron"], - "Did not retrieve cron") - end - def test_multiple_users crons = [] users = ["root", nonrootuser.name] @@ -708,18 +443,43 @@ class TestCron < Test::Unit::TestCase :minute => [0,30] ) end + provider = crons[0].provider.class assert_apply(*crons) users.each do |user| users.each do |other| next if user == other - assert(Puppet::Type.type(:cron).tabobj(other).read !~ /testcron-#{user}/, + text = provider.target_object(other).read + + assert(text !~ /testcron-#{user}/, "%s's cron job is in %s's tab" % [user, other]) end end end + + # Make sure the user stuff defaults correctly. + def test_default_user + crontab = @crontype.provider(:crontab) + if crontab.suitable? + inst = @crontype.create( + :name => "something", :command => "/some/thing", + :provider => :crontab) + assert_equal(ENV["USER"], inst.should(:user), + "user did not default to current user with crontab") + assert_equal(ENV["USER"], inst.should(:target), + "target did not default to current user with crontab") + + # Now make a new cron with a user, and make sure it gets copied + # over + inst = @crontype.create(:name => "yay", :command => "/some/thing", + :user => "bin", :provider => :crontab) + assert_equal("bin", inst.should(:target), + "target did not default to user with crontab") + end + end end + # $Id$ diff --git a/test/util/fileparsing.rb b/test/util/fileparsing.rb index 12343c38b..c81c9e006 100755 --- a/test/util/fileparsing.rb +++ b/test/util/fileparsing.rb @@ -564,7 +564,8 @@ billy three four\n" assert_nothing_raised("Could not set hooks") do record = @parser.record_line :yay, :fields => %w{one two}, :post_parse => proc { |hash| hash[:posted] = true }, - :pre_gen => proc { |hash| hash[:one] = hash[:one].upcase } + :pre_gen => proc { |hash| hash[:one] = hash[:one].upcase }, + :to_line => proc { |hash| "# Line\n" + join(hash) } end assert(record.respond_to?(:post_parse), "did not create method for post-hook") @@ -582,7 +583,7 @@ billy three four\n" assert_nothing_raised("Could not generate line with hooks") do result = @parser.to_line(result) end - assert_equal("ONE two", result, "did not call pre-gen hook") + assert_equal("# Line\nONE two", result, "did not call pre-gen hook") assert_equal("one", old_result[:one], "passed original hash to pre hook") end end |
