3
require File.dirname(__FILE__) + '/../../lib/puppettest'
6
require 'puppettest/support/utils'
8
class TestTidy < Test::Unit::TestCase
9
include PuppetTest::Support::Utils
10
include PuppetTest::FileTesting
12
# because luke's home directory is on nfs, it can't be used for testing
15
File.open(tmpfile, "w") { |f| f.puts rand(100) }
16
@@tmpfiles.push tmpfile
21
dir = File.join(tmpdir(), "puppetlinkdir")
22
unless FileTest.exists?(dir)
31
file = File.join(dir, "file")
32
File.open(file, "w") { |f|
36
tidy = Puppet.type(:tidy).create(
43
assert_events([:file_tidied, :file_tidied], tidy)
45
assert(!FileTest.exists?(file), "Tidied %s still exists" % file)
46
assert(!FileTest.exists?(dir), "Tidied %s still exists" % dir)
50
def disabled_test_recursion
52
FileUtils.cd(source) {
57
assert_nothing_raised {
58
link = newlink(:target => source, :recurse => true)
60
comp = mk_catalog("linktest",link)
64
list = file_list(path)
67
unless FileTest.directory?(file)
68
assert(FileTest.symlink?(file))
69
target = File.readlink(file)
70
assert_equal(target,File.join(source,file.sub(/^\.\//,'')))
76
# Test the different age iterations.
77
def test_age_conversions
78
tidy = Puppet::Type.type(:tidy).create :path => tempfile(), :age => "1m"
85
convertors[:hour] = convertors[:minute] * 60
86
convertors[:day] = convertors[:hour] * 24
87
convertors[:week] = convertors[:day] * 7
89
# First make sure we default to days
90
assert_nothing_raised do
94
assert_equal(2 * convertors[:day], tidy.should(:age),
97
convertors.each do |name, number|
98
init = name.to_s[0..0] # The first letter
99
[0, 1, 5].each do |multi|
100
[init, init.upcase].each do |letter|
101
age = multi.to_s + letter.to_s
102
assert_nothing_raised do
106
assert_equal(multi * convertors[name], tidy.should(:age),
107
"Converted %s wrong" % age)
113
def test_size_conversions
121
tidy = Puppet::Type.type(:tidy).create :path => tempfile(), :age => "1m"
123
# First make sure we default to kb
124
assert_nothing_raised do
128
assert_equal(2048, tidy.should(:size),
131
convertors.each do |name, number|
132
init = name.to_s[0..0] # The first letter
133
[0, 1, 5].each do |multi|
134
[init, init.upcase].each do |letter|
135
size = multi.to_s + letter.to_s
136
assert_nothing_raised do
141
number.times do total *= 1024 end
143
assert_equal(total, tidy.should(:size),
144
"Converted %s wrong" % size)
151
tidy = Puppet::Type.type(:tidy).create :path => tempfile(), :age => "1m"
153
age = tidy.property(:age)
155
# Set it to something that should be fine
156
assert(age.insync?(Time.now.to_i - 5), "Tried to tidy a low age")
158
# Now to something that should fail
159
assert(! age.insync?(Time.now.to_i - 120), "Incorrectly skipped tidy")
163
tidy = Puppet::Type.type(:tidy).create :path => tempfile(), :size => "1k"
165
size = tidy.property(:size)
167
# Set it to something that should be fine
168
assert(size.insync?(50), "Tried to tidy a low size")
170
# Now to something that should fail
171
assert(! size.insync?(2048), "Incorrectly skipped tidy")
174
# Make sure we can remove different types of files
177
tidy = Puppet::Type.type(:tidy).create :path => path, :size => "1b", :age => "1s"
180
File.open(path, "w") { |f| f.puts "this is a test" }
181
assert_events([:file_tidied], tidy)
182
assert(! FileTest.exists?(path), "File was not removed")
186
File.open(dest, "w") { |f| f.puts "this is a test" }
187
File.symlink(dest, path)
188
assert_events([:file_tidied], tidy)
189
assert(! FileTest.exists?(path), "Link was not removed")
190
assert(FileTest.exists?(dest), "Destination was removed")
195
assert_events([:file_tidied], tidy)
196
assert(! FileTest.exists?(path), "File was not removed")
199
# Make sure we can specify either attribute and get appropriate behaviour.
200
# I found that the original implementation of this did not work unless both
201
# size and age were specified.
202
def test_one_attribute
204
File.open(path, "w") { |f| 10.times { f.puts "yayness " } }
205
tidy = Puppet::Type.type(:tidy).create :path => path, :size => "1b"
208
assert(! FileTest.exists?(path), "file did not get tidied")
212
# Now try one with just an age attribute.
214
stat = stub 'stat', :mtime => time, :atime => time, :ftype => "file"
216
File.stubs(:lstat).with(path).returns(stat)
218
File.open(path, "w") { |f| 10.times { f.puts "yayness " } }
219
tidy = Puppet::Type.type(:tidy).create :path => path, :age => "5s"
223
assert(! FileTest.exists?(path), "file did not get tidied")
227
def test_remove_dead_links
229
link = File.join(dir, "link")
232
File.symlink(target, link)
234
tidy = Puppet::Type.type(:tidy).create :path => dir, :size => "1b", :recurse => true
236
assert(! FileTest.symlink?(link), "link was not tidied")
239
def test_glob_matches_single
242
:remove => File.join(dir, "01-foo"),
243
:keep => File.join(dir, "default")
245
files.each do |tag, file|
246
File.open(file, "w") { |f|
251
tidy = Puppet.type(:tidy).create(
260
assert(FileTest.exists?(files[:keep]), "%s was tidied" % files[:keep])
261
assert(!FileTest.exists?(files[:remove]), "Tidied %s still exists" % files[:remove])
264
def test_glob_matches_multiple
267
:remove1 => File.join(dir, "01-foo"),
268
:remove2 => File.join(dir, "02-bar"),
269
:keep1 => File.join(dir, "default")
271
files.each do |tag, file|
272
File.open(file, "w") { |f|
277
tidy = Puppet.type(:tidy).create(
282
:matches => ["01-*", "02-*"]
286
assert(FileTest.exists?(files[:keep1]), "%s was tidied" % files[:keep1])
287
assert(!FileTest.exists?(files[:remove1]), "Tidied %s still exists" % files[:remove1])
288
assert(!FileTest.exists?(files[:remove2]), "Tidied %s still exists" % files[:remove2])