summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-10-17 03:06:45 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-10-17 03:06:45 +0000
commitb4ebe76e6aa14efee6c5c56a61c4ee7f5784997e (patch)
treec8869ec0a67a5471ef7d5f9e074509fd09115a85
parent9e5ea8c2c52f3d4b48fa01eb56264b323a2c4581 (diff)
downloadpuppet-b4ebe76e6aa14efee6c5c56a61c4ee7f5784997e.tar.gz
puppet-b4ebe76e6aa14efee6c5c56a61c4ee7f5784997e.tar.xz
puppet-b4ebe76e6aa14efee6c5c56a61c4ee7f5784997e.zip
Rewriting nearly all of the tests for the tidy type, and redoing the internals of the testing.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1794 980ebf18-57e1-0310-9a29-db15c13687c0
-rwxr-xr-xlib/puppet/type/tidy.rb140
-rwxr-xr-xtest/types/tidy.rb156
2 files changed, 238 insertions, 58 deletions
diff --git a/lib/puppet/type/tidy.rb b/lib/puppet/type/tidy.rb
index 0636e3fac..7e0a04353 100755
--- a/lib/puppet/type/tidy.rb
+++ b/lib/puppet/type/tidy.rb
@@ -5,7 +5,9 @@ require 'puppet/type/pfile'
module Puppet
newtype(:tidy, Puppet.type(:file)) do
- @doc = "Remove unwanted files based on specific criteria."
+ @doc = "Remove unwanted files based on specific criteria. Multiple
+ criteria or OR'd together, so a file that is too large but is not
+ old enough will still get tidied."
newparam(:path) do
desc "The path to the file or directory to manage. Must be fully
@@ -17,22 +19,41 @@ module Puppet
newparam(:age) do
desc "Tidy files whose age is equal to or greater than
- the specified number of days."
+ the specified number of days. You can choose seconds, minutes,
+ hours, days, or weeks by specifying the first letter of any
+ of those words (e.g., '1w')."
+
+ @@ageconvertors = {
+ :s => 1,
+ :m => 60
+ }
+
+ @@ageconvertors[:h] = @@ageconvertors[:m] * 60
+ @@ageconvertors[:d] = @@ageconvertors[:h] * 24
+ @@ageconvertors[:w] = @@ageconvertors[:d] * 7
+
+ def convert(unit, multi)
+ if num = @@ageconvertors[unit]
+ return num * multi
+ else
+ self.fail "Invalid age unit '%s'" % unit
+ end
+ end
munge do |age|
+ unit = multi = nil
case age
- when /^[0-9]+$/, /^[0-9]+[dD]/:
- Integer(age.gsub(/[^0-9]+/,'')) *
- 60 * 60 * 24
- when /^[0-9]+$/, /^[0-9]+[hH]/:
- Integer(age.gsub(/[^0-9]+/,'')) * 60 * 60
- when /^[0-9]+[mM]/:
- Integer(age.gsub(/[^0-9]+/,'')) * 60
- when /^[0-9]+[sS]/:
- Integer(age.gsub(/[^0-9]+/,''))
+ when /^([0-9]+)(\w)\w*$/:
+ multi = Integer($1)
+ unit = $2.downcase.intern
+ when /^([0-9]+)$/:
+ multi = Integer($1)
+ unit = :d
else
self.fail "Invalid tidy age %s" % age
end
+
+ convert(unit, multi)
end
end
@@ -42,38 +63,46 @@ module Puppet
*b*, *k*, and *m* can be appended to specify *bytes*, *kilobytes*,
and *megabytes*, respectively. Only the first character is
significant, so the full word can also be used."
+
+ @@sizeconvertors = {
+ :b => 0,
+ :k => 1,
+ :m => 2,
+ :g => 3
+ }
+
+ def convert(unit, multi)
+ if num = @@sizeconvertors[unit]
+ result = multi
+ num.times do result *= 1024 end
+ return result
+ else
+ self.fail "Invalid size unit '%s'" % unit
+ end
+ end
munge do |size|
- if FileTest.directory?(@parent[:path])
- # don't do size comparisons for directories
- return
- end
case size
- when /^[0-9]+$/, /^[0-9]+[kK]/:
- Integer(size.gsub(/[^0-9]+/,'')) * 1024
- when /^[0-9]+[bB]/:
- Integer(size.gsub(/[^0-9]+/,''))
- when /^[0-9]+[mM]/:
- Integer(size.gsub(/[^0-9]+/,'')) *
- 1024 * 1024
+ when /^([0-9]+)(\w)\w*$/:
+ multi = Integer($1)
+ unit = $2.downcase.intern
+ when /^([0-9]+)$/:
+ multi = Integer($1)
+ unit = :k
else
- self.fail "Invalid tidy size %s" % size
+ self.fail "Invalid tidy size %s" % age
end
+
+ convert(unit, multi)
end
end
newparam(:type) do
- desc "Set the mechanism for determining age.
- **atime**/*mtime*/*ctime*."
+ desc "Set the mechanism for determining age."
- munge do |type|
- case type
- when "atime", "mtime", "ctime":
- type.intern
- else
- self.fail "Invalid tidy type %s" % type
- end
- end
+ newvalues(:atime, :mtime, :ctime)
+
+ defaultto :atime
end
newparam(:recurse) do
@@ -83,7 +112,9 @@ module Puppet
newparam(:rmdirs) do
desc "Tidy directories in addition to files; that is, remove
- directories whose age is older than the specified criteria."
+ directories whose age is older than the specified criteria.
+ This will only remove empty directories, so all contained
+ files must also be tidied before a directory gets removed."
end
newstate(:tidyup) do
@@ -100,7 +131,44 @@ module Puppet
type = @parent[:type] || :atime
end
- return Integer(Time.now - stat.send(type))
+ #return Integer(Time.now - stat.send(type))
+ return stat.send(type).to_i
+ end
+
+ def change_to_s
+ start = "Tidying"
+ unless insync_age?
+ start += ", older than %s seconds" % @parent[:age]
+ end
+ unless insync_size?
+ start += ", larger than %s bytes" % @parent[:size]
+ end
+
+ start
+ end
+
+ def insync_age?
+ if num = @parent[:age] and @is[0]
+ if (Time.now.to_i - @is[0]) > num
+ return false
+ end
+ end
+
+ true
+ end
+
+ def insync_size?
+ if num = @parent[:size] and @is[1]
+ if @is[1] > num
+ return false
+ end
+ end
+
+ true
+ end
+
+ def insync?
+ insync_age? and insync_size?
end
def retrieve
@@ -143,7 +211,7 @@ module Puppet
when "file":
@parent.handlebackup(file)
File.unlink(file)
- when "symlink": File.unlink(file)
+ when "link": File.unlink(file)
else
self.fail "Cannot tidy files of type %s" %
File.lstat(file).ftype
diff --git a/test/types/tidy.rb b/test/types/tidy.rb
index 7bbc657cc..20694762f 100755
--- a/test/types/tidy.rb
+++ b/test/types/tidy.rb
@@ -5,8 +5,6 @@ $:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
require 'puppet'
require 'puppettest'
-# $Id$
-
class TestTidy < Test::Unit::TestCase
include PuppetTest::FileTesting
def mktmpfile
@@ -27,26 +25,6 @@ class TestTidy < Test::Unit::TestCase
return dir
end
- def test_simpletidy
- dir = mktmpdir
- file = File.join(dir, "tidytesting")
- File.open(file, "w") { |f|
- f.puts rand(100)
- }
-
- tidy = Puppet.type(:tidy).create(
- :name => dir,
- :size => "1b",
- :recurse => true
- )
- comp = newcomp("tidytesting", tidy)
- comp.finalize
-
- trans = nil
- assert_events([:file_tidied], comp)
- assert(!FileTest.exists?(file), "Tidied file still exists")
- end
-
def test_tidydirs
dir = mktmpdir
file = File.join(dir, "tidytesting")
@@ -96,4 +74,138 @@ class TestTidy < Test::Unit::TestCase
}
}
end
+
+ # Test the different age iterations.
+ def test_age_conversions
+ tidy = Puppet::Type.newtidy :path => tempfile(), :age => "1m"
+
+ convertors = {
+ :second => 1,
+ :minute => 60
+ }
+
+ convertors[:hour] = convertors[:minute] * 60
+ convertors[:day] = convertors[:hour] * 24
+ convertors[:week] = convertors[:day] * 7
+
+ # First make sure we default to days
+ assert_nothing_raised do
+ tidy[:age] = "2"
+ end
+
+ assert_equal(2 * convertors[:day], tidy[:age],
+ "Converted 2 wrong")
+
+ convertors.each do |name, number|
+ init = name.to_s[0..0] # The first letter
+ [0, 1, 5].each do |multi|
+ [init, init.upcase].each do |letter|
+ age = multi.to_s + letter.to_s
+ assert_nothing_raised do
+ tidy[:age] = age
+ end
+
+ assert_equal(multi * convertors[name], tidy[:age],
+ "Converted %s wrong" % age)
+ end
+ end
+ end
+ end
+
+ def test_size_conversions
+ convertors = {
+ :b => 0,
+ :kb => 1,
+ :mb => 2,
+ :gb => 3
+ }
+
+ tidy = Puppet::Type.newtidy :path => tempfile(), :age => "1m"
+
+ # First make sure we default to kb
+ assert_nothing_raised do
+ tidy[:size] = "2"
+ end
+
+ assert_equal(2048, tidy[:size],
+ "Converted 2 wrong")
+
+ convertors.each do |name, number|
+ init = name.to_s[0..0] # The first letter
+ [0, 1, 5].each do |multi|
+ [init, init.upcase].each do |letter|
+ size = multi.to_s + letter.to_s
+ assert_nothing_raised do
+ tidy[:size] = size
+ end
+
+ total = multi
+ number.times do total *= 1024 end
+
+ assert_equal(total, tidy[:size],
+ "Converted %s wrong" % size)
+ end
+ end
+ end
+ end
+
+ def test_agetest
+ tidy = Puppet::Type.newtidy :path => tempfile(), :age => "1m"
+
+ state = tidy.state(:tidyup)
+
+ # Set it to something that should be fine
+ state.is = [Time.now.to_i - 5, 50]
+
+ assert(state.insync?, "Tried to tidy a low age")
+
+ # Now to something that should fail
+ state.is = [Time.now.to_i - 120, 50]
+
+ assert(! state.insync?, "Incorrectly skipped tidy")
+ end
+
+ def test_sizetest
+ tidy = Puppet::Type.newtidy :path => tempfile(), :size => "1k"
+
+ state = tidy.state(:tidyup)
+
+ # Set it to something that should be fine
+ state.is = [5, 50]
+
+ assert(state.insync?, "Tried to tidy a low size")
+
+ # Now to something that should fail
+ state.is = [120, 2048]
+
+ assert(! state.insync?, "Incorrectly skipped tidy")
+ end
+
+ # Make sure we can remove different types of files
+ def test_tidytypes
+ path = tempfile()
+ tidy = Puppet::Type.newtidy :path => path, :size => "1b", :age => "1s"
+
+ # Start with a file
+ File.open(path, "w") { |f| f.puts "this is a test" }
+ assert_events([:file_tidied], tidy)
+ assert(! FileTest.exists?(path), "File was not removed")
+
+ # Then a link
+ dest = tempfile
+ File.open(dest, "w") { |f| f.puts "this is a test" }
+ File.symlink(dest, path)
+ assert_events([:file_tidied], tidy)
+ assert(! FileTest.exists?(path), "Link was not removed")
+ assert(FileTest.exists?(dest), "Destination was removed")
+
+ # And a directory
+ Dir.mkdir(path)
+ tidy.is = [:tidyup, [Time.now - 1024, 1]]
+ tidy[:rmdirs] = true
+ assert_events([:file_tidied], tidy)
+ assert(! FileTest.exists?(path), "File was not removed")
+ end
end
+
+# $Id$