summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2005-09-16 18:13:42 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2005-09-16 18:13:42 +0000
commit80a2808faa434de7d984409de53227b476a9da1b (patch)
treed62efe3d3cfb84b533d90afaa64ef0d4c36de4bb /test
parente25e8c685f733310e6da0489634288b5e81d2c41 (diff)
downloadpuppet-80a2808faa434de7d984409de53227b476a9da1b.tar.gz
puppet-80a2808faa434de7d984409de53227b476a9da1b.tar.xz
puppet-80a2808faa434de7d984409de53227b476a9da1b.zip
Cron is now fully functional and tested on 3 platforms. In order to make it work, I had to do some modifications to TransObject#to_type and Type.create, but all tests pass now. Type.create is now handling errors on creating objects, so if you try to create an invalid object you will just get nil returned, rather than receiving an error.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@680 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'test')
-rwxr-xr-xtest/types/tc_cron.rb135
-rwxr-xr-xtest/types/tc_exec.rb3
-rw-r--r--test/types/tc_service.rb73
-rw-r--r--test/types/tc_type.rb5
4 files changed, 160 insertions, 56 deletions
diff --git a/test/types/tc_cron.rb b/test/types/tc_cron.rb
index a33ee992d..4389e80ae 100755
--- a/test/types/tc_cron.rb
+++ b/test/types/tc_cron.rb
@@ -1,3 +1,5 @@
+# Test cron job creation, modification, and destruction
+
if __FILE__ == $0
$:.unshift '..'
$:.unshift '../../lib'
@@ -10,10 +12,9 @@ require 'puppet/type/cron'
require 'test/unit'
require 'facter'
-# $Id$
-
class TestExec < TestPuppet
def setup
+ # retrieve the user name
id = %x{id}.chomp
if id =~ /uid=\d+\(([^\)]+)\)/
@me = $1
@@ -23,36 +24,42 @@ class TestExec < TestPuppet
unless defined? @me
raise "Could not retrieve user name; 'id' did not work"
end
- tab = Puppet::Type::Cron.crontype.read(@me)
-
- if $? == 0
- @currenttab = tab
- else
- @currenttab = nil
- end
+ # god i'm lazy
+ @crontype = Puppet::Type::Cron
super
end
- def teardown
- if @currenttab
- Puppet::Type::Cron.crontype.write(@me, @currenttab)
+ # Back up the user's existing cron tab if they have one.
+ def cronback
+ tab = nil
+ assert_nothing_raised {
+ tab = Puppet::Type::Cron.crontype.read(@me)
+ }
+
+ if $? == 0
+ @currenttab = tab
else
- Puppet::Type::Cron.crontype.remove(@me)
+ @currenttab = nil
end
- super
end
- def test_load
+ # Restore the cron tab to its original form.
+ def cronrestore
assert_nothing_raised {
- Puppet::Type::Cron.retrieve(@me)
+ if @currenttab
+ @crontype.crontype.write(@me, @currenttab)
+ else
+ @crontype.crontype.remove(@me)
+ end
}
end
+ # Create a cron job with all fields filled in.
def mkcron(name)
cron = nil
assert_nothing_raised {
- cron = Puppet::Type::Cron.create(
+ cron = @crontype.create(
:command => "date > %s/crontest%s" % [tmpdir(), name],
:name => name,
:user => @me,
@@ -66,6 +73,7 @@ class TestExec < TestPuppet
return cron
end
+ # Run the cron through its paces -- install it then remove it.
def cyclecron(cron)
name = cron.name
comp = newcomp(name, cron)
@@ -76,16 +84,94 @@ class TestExec < TestPuppet
trans = assert_events(comp, [], name)
cron[:command] = :notfound
trans = assert_events(comp, [:cron_deleted], name)
+ # the cron should no longer exist, not even in the comp
+ trans = assert_events(comp, [], name)
+
+ assert(!comp.include?(cron),
+ "Cron is still a member of comp, after being deleted")
+ end
+
+ # A simple test to see if we can load the cron from disk.
+ def test_load
+ assert_nothing_raised {
+ @crontype.retrieve(@me)
+ }
+ 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",
+ :user => @me
+ )
+ }
+ str = nil
+ # generate the text
+ assert_nothing_raised {
+ str = cron.to_cron
+ }
+ assert_equal(str, "# Puppet Name: #{name}\n* * * * * date",
+ "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"
+ assert_nothing_raised {
+ @crontype.parse(user, str)
+ }
+
+ assert_nothing_raised {
+ newstr = @crontype.tab(user)
+ assert_equal(str, newstr, "Cron comments were changed or 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 {
+ @crontype.parse(@me, str)
+ }
+
+ assert_nothing_raised {
+ cron = @crontype.create(
+ :name => "yaycron",
+ :minute => [0, 30],
+ :command => "date",
+ :user => @me
+ )
+ }
+
+ modstr = "# Puppet Name: yaycron\n%s" % str
+
+ assert_nothing_raised {
+ newstr = @crontype.tab(@me)
+ assert_equal(modstr, newstr, "Cron was not correctly matched")
+ }
+ end
+
+ # Test adding a cron when there is currently no file.
def test_mkcronwithnotab
+ cronback
Puppet::Type::Cron.crontype.remove(@me)
cron = mkcron("crontest")
cyclecron(cron)
+ cronrestore
end
def test_mkcronwithtab
+ cronback
Puppet::Type::Cron.crontype.remove(@me)
Puppet::Type::Cron.crontype.write(@me,
"1 1 1 1 * date > %s/crontesting\n" % testdir()
@@ -93,9 +179,11 @@ class TestExec < TestPuppet
cron = mkcron("crontest")
cyclecron(cron)
+ cronrestore
end
def test_makeandretrievecron
+ cronback
Puppet::Type::Cron.crontype.remove(@me)
name = "storeandretrieve"
@@ -110,17 +198,20 @@ class TestExec < TestPuppet
assert(cron = Puppet::Type::Cron[name], "Could not retrieve named cron")
assert_instance_of(Puppet::Type::Cron, cron)
+ cronrestore
end
+ # Do input validation testing on all of the parameters.
def test_arguments
values = {
:monthday => {
- :valid => [ 1, 13, ],
+ :valid => [ 1, 13, "1,30" ],
:invalid => [ -1, 0, 32 ]
},
:weekday => {
- :valid => [ 0, 3, 6, "tue", "wed", "Wed", "MOnday", "SaTurday" ],
- :invalid => [ -1, 7, "tues", "teusday", "thurs" ]
+ :valid => [ 0, 3, 6, "1,2", "tue", "wed",
+ "Wed", "MOnday", "SaTurday" ],
+ :invalid => [ -1, 7, "1, 3", "tues", "teusday", "thurs" ]
},
:hour => {
:valid => [ 0, 21, 23 ],
@@ -147,7 +238,7 @@ class TestExec < TestPuppet
}
if value.is_a?(Integer)
- assert_equal(value, cron[param],
+ assert_equal([value], cron[param],
"Cron value was not set correctly")
end
when :invalid:
@@ -166,3 +257,5 @@ class TestExec < TestPuppet
}
end
end
+
+# $Id$
diff --git a/test/types/tc_exec.rb b/test/types/tc_exec.rb
index 7e1f5f161..0e4de4d9d 100755
--- a/test/types/tc_exec.rb
+++ b/test/types/tc_exec.rb
@@ -62,10 +62,11 @@ class TestExec < TestPuppet
def test_path_or_qualified
command = nil
output = nil
- assert_raise(TypeError) {
+ assert_nothing_raised {
command = Puppet::Type::Exec.create(
:command => "echo"
)
+ assert_nil(command)
}
Puppet::Type::Exec.clear
assert_nothing_raised {
diff --git a/test/types/tc_service.rb b/test/types/tc_service.rb
index a1633aec7..ecacc77d7 100644
--- a/test/types/tc_service.rb
+++ b/test/types/tc_service.rb
@@ -1,87 +1,94 @@
if __FILE__ == $0
$:.unshift '..'
$:.unshift '../../lib'
- $puppetbase = "../../../../language/trunk"
+ $puppetbase = "../.."
end
require 'puppet'
+require 'puppettest'
require 'test/unit'
# $Id$
-class TestService < Test::Unit::TestCase
+class TestService < TestPuppet
# hmmm
# this is complicated, because we store references to the created
# objects in a central store
def setup
- @sleeper = nil
+ sleeper = nil
script = File.join($puppetbase,"examples/root/etc/init.d/sleeper")
@status = script + " status"
- Puppet[:loglevel] = :debug if __FILE__ == $0
- assert_nothing_raised() {
- unless Puppet::Type::Service.has_key?("sleeper")
- Puppet::Type::Service.create(
- :name => "sleeper",
- :path => File.join($puppetbase,"examples/root/etc/init.d"),
- :running => 1
- )
- end
- @sleeper = Puppet::Type::Service["sleeper"]
- }
+
+ super
end
def teardown
- Puppet::Type.allclear
- Kernel.system("pkill sleeper")
+ stopservices
+ super
+ end
+
+ def mksleeper
+ assert_nothing_raised() {
+ return Puppet::Type::Service.create(
+ :name => "sleeper",
+ :path => File.join($puppetbase,"examples/root/etc/init.d"),
+ :running => 1
+ )
+ }
end
def test_process_start
+ sleeper = mksleeper
# start it
assert_nothing_raised() {
- @sleeper[:running] = 1
+ sleeper[:running] = 1
}
assert_nothing_raised() {
- @sleeper.retrieve
+ sleeper.retrieve
}
- assert(!@sleeper.insync?())
+ assert(!sleeper.insync?())
assert_nothing_raised() {
- @sleeper.sync
+ sleeper.sync
}
assert_nothing_raised() {
- @sleeper.retrieve
+ sleeper.retrieve
}
- assert(@sleeper.insync?)
+ assert(sleeper.insync?)
# test refreshing it
assert_nothing_raised() {
- @sleeper.refresh
+ sleeper.refresh
}
- assert(@sleeper.respond_to?(:refresh))
+ assert(sleeper.respond_to?(:refresh))
# now stop it
assert_nothing_raised() {
- @sleeper[:running] = 0
+ sleeper[:running] = 0
}
assert_nothing_raised() {
- @sleeper.retrieve
+ sleeper.retrieve
}
- assert(!@sleeper.insync?())
+ assert(!sleeper.insync?())
assert_nothing_raised() {
- @sleeper.sync
+ sleeper.sync
}
assert_nothing_raised() {
- @sleeper.retrieve
+ sleeper.retrieve
}
- assert(@sleeper.insync?)
+ assert(sleeper.insync?)
end
- def testFailOnNoPath
- assert_raise(Puppet::Error) {
- Puppet::Type::Service.create(
+ def test_FailOnNoPath
+ serv = nil
+ assert_nothing_raised {
+ serv = Puppet::Type::Service.create(
:name => "sleeper"
)
}
+
+ assert_nil(serv)
+ assert_nil(Puppet::Type::Service["sleeper"])
end
end
diff --git a/test/types/tc_type.rb b/test/types/tc_type.rb
index 56eb7071b..f746f287b 100644
--- a/test/types/tc_type.rb
+++ b/test/types/tc_type.rb
@@ -96,7 +96,10 @@ class TestType < TestPuppet
}
end
- def test_nameasstate
+ # This was supposed to test objects whose name was a state, but that
+ # fundamentally doesn't make much sense, and we now don't have any such
+ # types.
+ def disabled_test_nameasstate
# currently groups are the only objects with the namevar as a state
group = nil
assert_nothing_raised {