summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xlib/puppet/filetype.rb45
-rwxr-xr-xlib/puppet/type/cron.rb12
-rwxr-xr-xtest/types/cron.rb36
3 files changed, 71 insertions, 22 deletions
diff --git a/lib/puppet/filetype.rb b/lib/puppet/filetype.rb
index 8a1f13ec0..168d8e472 100755
--- a/lib/puppet/filetype.rb
+++ b/lib/puppet/filetype.rb
@@ -112,43 +112,38 @@ module Puppet
# Operate on plain files.
newfiletype(:ram) do
+ @@tabs = {}
+
+ def self.clear
+ @@tabs.clear
+ end
+
def initialize(path)
super
- @text = ""
+ @@tabs[@path] ||= ""
end
# Read the file.
def read
Puppet.info "Reading %s from RAM" % @path
- @text
+ @@tabs[@path]
end
# Remove the file.
def remove
Puppet.info "Removing %s from RAM" % @path
- @text = ""
+ @@tabs[@path] = ""
end
# Overwrite the file.
def write(text)
Puppet.info "Writing %s to RAM" % @path
- @text = text
+ @@tabs[@path] = text
end
end
# Handle Linux-style cron tabs.
newfiletype(:crontab) do
- # Only add the -u flag when the @path is different. Fedora apparently
- # does not think I should be allowed to set the @path to my
- def cmdbase
- cmd = nil
- if @path == Process.uid
- return "crontab"
- else
- return "crontab -u #{@path}"
- end
- end
-
def initialize(user)
begin
uid = Puppet::Util.uid(user)
@@ -178,6 +173,19 @@ module Puppet
p.print text
}
end
+
+ private
+
+ # Only add the -u flag when the @path is different. Fedora apparently
+ # does not think I should be allowed to set the @path to my
+ def cmdbase
+ cmd = nil
+ if @path == Process.uid
+ return "crontab"
+ else
+ return "crontab -u #{@path}"
+ end
+ end
end
# SunOS has completely different cron commands; this class implements
@@ -208,7 +216,8 @@ module Puppet
end
end
- # Treat netinfo tables as a single file, just for simplicity of certain types
+ # Treat netinfo tables as a single file, just for simplicity of certain
+ # types
newfiletype(:netinfo) do
class << self
attr_accessor :format
@@ -230,8 +239,8 @@ module Puppet
end
end
- # Convert our table to an array of hashes. This only works for handling one
- # table at a time.
+ # Convert our table to an array of hashes. This only works for
+ # handling one table at a time.
def to_array(text = nil)
unless text
text = read
diff --git a/lib/puppet/type/cron.rb b/lib/puppet/type/cron.rb
index c87618693..10c362848 100755
--- a/lib/puppet/type/cron.rb
+++ b/lib/puppet/type/cron.rb
@@ -635,18 +635,22 @@ module Puppet
# Store the user's cron tab. Collects the text of the new tab and
# sends it to the +@filetype+ module's +write+ function. Also adds
- # header warning users not to modify the file directly.
+ # header, warning users not to modify the file directly.
def self.store(user)
- unless @instances.include?(user)
+ unless @instances.include?(user) or @objects.find do |n,o|
+ o[:user] == user
+ end
Puppet.notice "No cron instances for %s" % user
- p @instances.keys
return
end
@tabs[user] ||= @filetype.new(user)
self.each do |inst|
- @instances[user] << inst unless @instances[user].include? inst
+ unless (@instances[user] and @instances[user].include? inst)
+ @instances[user] ||= []
+ @instances[user] << inst
+ end
end
@tabs[user].write(self.tab(user))
end
diff --git a/test/types/cron.rb b/test/types/cron.rb
index 3b6384d59..fee09b360 100755
--- a/test/types/cron.rb
+++ b/test/types/cron.rb
@@ -35,6 +35,7 @@ class TestCron < Test::Unit::TestCase
def teardown
@crontype.filetype = @oldfiletype
+ Puppet::FileType.filetype(:ram).clear
super
end
@@ -677,6 +678,41 @@ class TestCron < Test::Unit::TestCase
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
end
# $Id$