~ubuntu-branches/ubuntu/lucid/puppet/lucid-security

« back to all changes in this revision

Viewing changes to test/ral/type/tidy.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env ruby
2
 
 
3
 
require File.dirname(__FILE__) + '/../../lib/puppettest'
4
 
 
5
 
require 'puppettest'
6
 
require 'puppettest/support/utils'
7
 
 
8
 
class TestTidy < Test::Unit::TestCase
9
 
    include PuppetTest::Support::Utils
10
 
    include PuppetTest::FileTesting
11
 
    def mktmpfile
12
 
        # because luke's home directory is on nfs, it can't be used for testing
13
 
        # as root
14
 
        tmpfile = tempfile()
15
 
        File.open(tmpfile, "w") { |f| f.puts rand(100) }
16
 
        @@tmpfiles.push tmpfile
17
 
        return tmpfile
18
 
    end
19
 
 
20
 
    def mktmpdir
21
 
        dir = File.join(tmpdir(), "puppetlinkdir")
22
 
        unless FileTest.exists?(dir)
23
 
            Dir.mkdir(dir)
24
 
        end
25
 
        @@tmpfiles.push dir
26
 
        return dir
27
 
    end
28
 
 
29
 
    def test_tidydirs
30
 
        dir = mktmpdir
31
 
        file = File.join(dir, "file")
32
 
        File.open(file, "w") { |f|
33
 
            f.puts "some stuff"
34
 
        }
35
 
 
36
 
        tidy = Puppet.type(:tidy).create(
37
 
            :name => dir,
38
 
            :size => "1b",
39
 
            :rmdirs => true,
40
 
            :recurse => true
41
 
        )
42
 
 
43
 
        assert_events([:file_tidied, :file_tidied], tidy)
44
 
 
45
 
        assert(!FileTest.exists?(file), "Tidied %s still exists" % file)
46
 
        assert(!FileTest.exists?(dir), "Tidied %s still exists" % dir)
47
 
 
48
 
    end
49
 
 
50
 
    def disabled_test_recursion
51
 
        source = mktmpdir()
52
 
        FileUtils.cd(source) {
53
 
            mkranddirsandfiles()
54
 
        }
55
 
 
56
 
        link = nil
57
 
        assert_nothing_raised {
58
 
            link = newlink(:target => source, :recurse => true)
59
 
        }
60
 
        comp = mk_catalog("linktest",link)
61
 
        cycle(comp)
62
 
 
63
 
        path = link.name
64
 
        list = file_list(path)
65
 
        FileUtils.cd(path) {
66
 
            list.each { |file|
67
 
                unless FileTest.directory?(file)
68
 
                    assert(FileTest.symlink?(file))
69
 
                    target = File.readlink(file)
70
 
                    assert_equal(target,File.join(source,file.sub(/^\.\//,'')))
71
 
                end
72
 
            }
73
 
        }
74
 
    end
75
 
 
76
 
    # Test the different age iterations.
77
 
    def test_age_conversions
78
 
        tidy = Puppet::Type.type(:tidy).create :path => tempfile(), :age => "1m"
79
 
 
80
 
        convertors = {
81
 
            :second => 1,
82
 
            :minute => 60
83
 
        }
84
 
 
85
 
        convertors[:hour] = convertors[:minute] * 60
86
 
        convertors[:day] = convertors[:hour] * 24
87
 
        convertors[:week] = convertors[:day] * 7
88
 
 
89
 
        # First make sure we default to days
90
 
        assert_nothing_raised do
91
 
            tidy[:age] = "2"
92
 
        end
93
 
 
94
 
        assert_equal(2 * convertors[:day], tidy.should(:age),
95
 
            "Converted 2 wrong")
96
 
 
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
103
 
                        tidy[:age] = age
104
 
                    end
105
 
 
106
 
                    assert_equal(multi * convertors[name], tidy.should(:age),
107
 
                        "Converted %s wrong" % age)
108
 
                end
109
 
            end
110
 
        end
111
 
    end
112
 
 
113
 
    def test_size_conversions
114
 
        convertors = {
115
 
            :b => 0,
116
 
            :kb => 1,
117
 
            :mb => 2,
118
 
            :gb => 3
119
 
        }
120
 
 
121
 
        tidy = Puppet::Type.type(:tidy).create :path => tempfile(), :age => "1m"
122
 
 
123
 
        # First make sure we default to kb
124
 
        assert_nothing_raised do
125
 
            tidy[:size] = "2"
126
 
        end
127
 
 
128
 
        assert_equal(2048, tidy.should(:size),
129
 
            "Converted 2 wrong")
130
 
 
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
137
 
                        tidy[:size] = size
138
 
                    end
139
 
 
140
 
                    total = multi
141
 
                    number.times do total *= 1024 end
142
 
 
143
 
                    assert_equal(total, tidy.should(:size),
144
 
                        "Converted %s wrong" % size)
145
 
                end
146
 
            end
147
 
        end
148
 
    end
149
 
 
150
 
    def test_agetest
151
 
        tidy = Puppet::Type.type(:tidy).create :path => tempfile(), :age => "1m"
152
 
 
153
 
        age = tidy.property(:age)
154
 
 
155
 
        # Set it to something that should be fine
156
 
        assert(age.insync?(Time.now.to_i - 5), "Tried to tidy a low age")
157
 
 
158
 
        # Now to something that should fail
159
 
        assert(! age.insync?(Time.now.to_i - 120), "Incorrectly skipped tidy")
160
 
    end
161
 
 
162
 
    def test_sizetest
163
 
        tidy = Puppet::Type.type(:tidy).create :path => tempfile(), :size => "1k"
164
 
 
165
 
        size = tidy.property(:size)
166
 
 
167
 
        # Set it to something that should be fine
168
 
        assert(size.insync?(50), "Tried to tidy a low size")
169
 
 
170
 
        # Now to something that should fail
171
 
        assert(! size.insync?(2048), "Incorrectly skipped tidy")
172
 
    end
173
 
 
174
 
    # Make sure we can remove different types of files
175
 
    def test_tidytypes
176
 
        path = tempfile()
177
 
        tidy = Puppet::Type.type(:tidy).create :path => path, :size => "1b", :age => "1s"
178
 
 
179
 
        # Start with a file
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")
183
 
 
184
 
        # Then a link
185
 
        dest = tempfile
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")
191
 
 
192
 
        # And a directory
193
 
        Dir.mkdir(path)
194
 
        tidy[:rmdirs] = true
195
 
        assert_events([:file_tidied], tidy)
196
 
        assert(! FileTest.exists?(path), "File was not removed")
197
 
    end
198
 
    
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
203
 
        path = tempfile()
204
 
        File.open(path, "w") { |f| 10.times { f.puts "yayness " } }
205
 
        tidy = Puppet::Type.type(:tidy).create :path => path, :size => "1b"
206
 
        
207
 
        assert_apply(tidy)
208
 
        assert(! FileTest.exists?(path), "file did not get tidied")
209
 
        
210
 
        tidy.class.clear
211
 
 
212
 
        # Now try one with just an age attribute.
213
 
        time = Time.now - 10
214
 
        stat = stub 'stat', :mtime => time, :atime => time, :ftype => "file"
215
 
        File.stubs(:lstat)
216
 
        File.stubs(:lstat).with(path).returns(stat)
217
 
 
218
 
        File.open(path, "w") { |f| 10.times { f.puts "yayness " } }
219
 
        tidy = Puppet::Type.type(:tidy).create :path => path, :age => "5s"
220
 
        
221
 
 
222
 
        assert_apply(tidy)
223
 
        assert(! FileTest.exists?(path), "file did not get tidied")
224
 
    end
225
 
    
226
 
    # Testing #355.
227
 
    def test_remove_dead_links
228
 
        dir = tempfile()
229
 
        link = File.join(dir, "link")
230
 
        target = tempfile()
231
 
        Dir.mkdir(dir)
232
 
        File.symlink(target, link)
233
 
        
234
 
        tidy = Puppet::Type.type(:tidy).create :path => dir, :size => "1b", :recurse => true
235
 
        assert_apply(tidy)
236
 
        assert(! FileTest.symlink?(link), "link was not tidied")
237
 
    end
238
 
 
239
 
    def test_glob_matches_single
240
 
        dir = mktmpdir
241
 
        files = {
242
 
          :remove => File.join(dir, "01-foo"),
243
 
          :keep   => File.join(dir, "default")
244
 
        }
245
 
        files.each do |tag, file|
246
 
          File.open(file, "w") { |f|
247
 
              f.puts "some stuff"
248
 
          }
249
 
        end
250
 
 
251
 
        tidy = Puppet.type(:tidy).create(
252
 
            :name => dir,
253
 
            :size => "1b",
254
 
            :rmdirs => true,
255
 
            :recurse => true,
256
 
            :matches => "01-*"
257
 
        )
258
 
        assert_apply(tidy)
259
 
 
260
 
        assert(FileTest.exists?(files[:keep]), "%s was tidied" % files[:keep])
261
 
        assert(!FileTest.exists?(files[:remove]), "Tidied %s still exists" % files[:remove])
262
 
    end
263
 
 
264
 
    def test_glob_matches_multiple
265
 
        dir = mktmpdir
266
 
        files = {
267
 
          :remove1 => File.join(dir, "01-foo"),
268
 
          :remove2 => File.join(dir, "02-bar"),
269
 
          :keep1   => File.join(dir, "default")
270
 
        }
271
 
        files.each do |tag, file|
272
 
          File.open(file, "w") { |f|
273
 
              f.puts "some stuff"
274
 
          }
275
 
        end
276
 
 
277
 
        tidy = Puppet.type(:tidy).create(
278
 
            :name => dir,
279
 
            :size => "1b",
280
 
            :rmdirs => true,
281
 
            :recurse => true,
282
 
            :matches => ["01-*", "02-*"]
283
 
        )
284
 
        assert_apply(tidy)
285
 
 
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])
289
 
    end
290
 
end
291