summaryrefslogtreecommitdiffstats
path: root/spec/unit/type/tidy.rb
blob: 72f6b0f10136868a4fb1b5bc08d8ab95603abd05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

describe Puppet::Type.type(:tidy) do
    it "should use :lstat when stating a file" do
        tidy = Puppet::Type.type(:tidy).new :path => "/foo/bar", :age => "1d"
        stat = mock 'stat'
        File.expects(:lstat).with("/foo/bar").returns stat
        tidy.stat("/foo/bar").should == stat
    end

    [:age, :size, :path, :matches, :type, :recurse, :rmdirs].each do |param|
        it "should have a %s parameter" % param do
            Puppet::Type.type(:tidy).attrclass(param).ancestors.should be_include(Puppet::Parameter)
        end

        it "should have documentation for its %s param" % param do
            Puppet::Type.type(:tidy).attrclass(param).doc.should be_instance_of(String)
        end
    end

    describe "when validating parameter values" do
        describe "for 'recurse'" do
            before do
                @tidy = Puppet::Type.type(:tidy).new :path => "/tmp", :age => "100d"
            end

            it "should allow 'true'" do
                lambda { @tidy[:recurse] = true }.should_not raise_error
            end

            it "should allow 'false'" do
                lambda { @tidy[:recurse] = false }.should_not raise_error
            end

            it "should allow integers" do
                lambda { @tidy[:recurse] = 10 }.should_not raise_error
            end

            it "should allow string representations of integers" do
                lambda { @tidy[:recurse] = "10" }.should_not raise_error
            end

            it "should allow 'inf'" do
                lambda { @tidy[:recurse] = "inf" }.should_not raise_error
            end

            it "should not allow arbitrary values" do
                lambda { @tidy[:recurse] = "whatever" }.should raise_error
            end
        end
    end

    describe "when matching files by age" do
        convertors = {
            :second => 1,
            :minute => 60
        }

        convertors[:hour] = convertors[:minute] * 60
        convertors[:day] = convertors[:hour] * 24
        convertors[:week] = convertors[:day] * 7

        convertors.each do |unit, multiple|
            it "should consider a %s to be %s seconds" % [unit, multiple] do
                tidy = Puppet::Type.type(:tidy).new :path => "/what/ever", :age => "5%s" % unit.to_s[0..0]

                tidy[:age].should == 5 * multiple
            end
        end
    end

    describe "when matching files by size" do
        convertors = {
            :b => 0,
            :kb => 1,
            :mb => 2,
            :gb => 3
        }

        convertors.each do |unit, multiple|
            it "should consider a %s to be 1024^%s bytes" % [unit, multiple] do
                tidy = Puppet::Type.type(:tidy).new :path => "/what/ever", :size => "5%s" % unit

                total = 5
                multiple.times { total *= 1024 }
                tidy[:size].should == total
            end
        end
    end

    describe "when tidying" do
        before do
            @tidy = Puppet::Type.type(:tidy).new :path => "/what/ever"
            @stat = stub 'stat', :ftype => "directory"
            File.stubs(:lstat).with("/what/ever").returns @stat
        end

        describe "and generating files" do
            it "should set the backup on the file if backup is set on the tidy instance" do
                @tidy[:backup] = "whatever"
                Puppet::Type.type(:file).expects(:new).with { |args| args[:backup] == "whatever" }

                @tidy.mkfile("/what/ever")
            end

            it "should set the file's path to the tidy's path" do
                Puppet::Type.type(:file).expects(:new).with { |args| args[:path] == "/what/ever" }

                @tidy.mkfile("/what/ever")
            end

            it "should configure the file for deletion" do
                Puppet::Type.type(:file).expects(:new).with { |args| args[:ensure] == :absent }

                @tidy.mkfile("/what/ever")
            end

            it "should force deletion on the file" do
                Puppet::Type.type(:file).expects(:new).with { |args| args[:force] == true }

                @tidy.mkfile("/what/ever")
            end

            it "should do nothing if the targeted file does not exist" do
                File.expects(:lstat).with("/what/ever").raises Errno::ENOENT

                @tidy.generate.should == []
            end
        end

        describe "and recursion is not used" do
            it "should generate a file resource if the file should be tidied" do
                @tidy.expects(:tidy?).with("/what/ever").returns true
                file = Puppet::Type.type(:file).new(:path => "/eh")
                @tidy.expects(:mkfile).with("/what/ever").returns file

                @tidy.generate.should == [file]
            end

            it "should do nothing if the file should not be tidied" do
                @tidy.expects(:tidy?).with("/what/ever").returns false
                @tidy.expects(:mkfile).never

                @tidy.generate.should == []
            end
        end

        describe "and recursion is used" do
            before do
                @tidy[:recurse] = true
                Puppet::FileServing::Fileset.any_instance.stubs(:stat).returns mock("stat")
                @fileset = Puppet::FileServing::Fileset.new("/what/ever")
                Puppet::FileServing::Fileset.stubs(:new).returns @fileset
            end

            it "should use a Fileset for recursion" do
                Puppet::FileServing::Fileset.expects(:new).with("/what/ever", :recurse => true).returns @fileset
                @fileset.expects(:files).returns %w{. one two}
                @tidy.stubs(:tidy?).returns false

                @tidy.generate
            end

            it "should generate a file resource for every file that should be tidied but not for files that should not be tidied" do
                @fileset.expects(:files).returns %w{. one two}

                @tidy.expects(:tidy?).with("/what/ever").returns true
                @tidy.expects(:tidy?).with("/what/ever/one").returns true
                @tidy.expects(:tidy?).with("/what/ever/two").returns false

                file = Puppet::Type.type(:file).new(:path => "/eh")
                @tidy.expects(:mkfile).with("/what/ever").returns file
                @tidy.expects(:mkfile).with("/what/ever/one").returns file

                @tidy.generate
            end
        end

        describe "and determining whether a file matches provided glob patterns" do
            before do
                @tidy = Puppet::Type.type(:tidy).new :path => "/what/ever"
                @tidy[:matches] = %w{*foo* *bar*}

                @stat = mock 'stat'

                @matcher = @tidy.parameter(:matches)
            end

            it "should always convert the globs to an array" do
                @matcher.value = "*foo*"
                @matcher.value.should == %w{*foo*}
            end

            it "should return true if any pattern matches the last part of the file" do
                @matcher.value = %w{*foo* *bar*}
                @matcher.must be_tidy("/file/yaybarness", @stat)
            end

            it "should return false if no pattern matches the last part of the file" do
                @matcher.value = %w{*foo* *bar*}
                @matcher.should_not be_tidy("/file/yayness", @stat)
            end
        end

        describe "and determining whether a file is too old" do
            before do
                @tidy = Puppet::Type.type(:tidy).new :path => "/what/ever"
                @stat = stub 'stat'

                @tidy[:age] = "1s"
                @tidy[:type] = "mtime"
                @ager = @tidy.parameter(:age)
            end

            it "should use the age type specified" do
                @tidy[:type] = :ctime
                @stat.expects(:ctime).returns(Time.now)

                @ager.tidy?("/what/ever", @stat)
            end

            it "should return false if the file is more recent than the specified age" do
                @stat.expects(:mtime).returns(Time.now)

                @ager.should_not be_tidy("/what/ever", @stat)
            end

            it "should return true if the file is older than the specified age" do
                @stat.expects(:mtime).returns(Time.now - 10)

                @ager.must be_tidy("/what/ever", @stat)
            end
        end

        describe "and determining whether a file is too large" do
            before do
                @tidy = Puppet::Type.type(:tidy).new :path => "/what/ever"
                @stat = stub 'stat', :ftype => "file"

                @tidy[:size] = "1kb"
                @sizer = @tidy.parameter(:size)
            end

            it "should return false if the file is smaller than the specified size" do
                @stat.expects(:size).returns(4) # smaller than a kilobyte

                @sizer.should_not be_tidy("/what/ever", @stat)
            end

            it "should return true if the file is larger than the specified size" do
                @stat.expects(:size).returns(1500) # larger than a kilobyte

                @sizer.must be_tidy("/what/ever", @stat)
            end
        end

        describe "and determining whether a file should be tidied" do
            before do
                @tidy = Puppet::Type.type(:tidy).new :path => "/what/ever"
                @stat = stub 'stat', :ftype => "file"
                File.stubs(:lstat).with("/what/ever").returns @stat
            end

            it "should not try to recurse if the file does not exist" do
                @tidy[:recurse] = true

                File.stubs(:lstat).with("/what/ever").returns nil

                @tidy.generate.should == []
            end

            it "should not be tidied if the file does not exist" do
                File.expects(:lstat).with("/what/ever").raises Errno::ENOENT

                @tidy.should_not be_tidy("/what/ever")
            end

            it "should not be tidied if the user has no access to the file" do
                File.expects(:lstat).with("/what/ever").raises Errno::EACCES

                @tidy.should_not be_tidy("/what/ever")
            end

            it "should not be tidied if it is a directory and rmdirs is set to false" do
                stat = mock 'stat', :ftype => "directory"
                File.expects(:lstat).with("/what/ever").returns stat

                @tidy.should_not be_tidy("/what/ever")
            end

            it "should return false if it does not match any provided globs" do
                @tidy[:matches] = "globs"

                matches = @tidy.parameter(:matches)
                matches.expects(:tidy?).with("/what/ever", @stat).returns false
                @tidy.should_not be_tidy("/what/ever")
            end

            it "should return false if it does not match aging requirements" do
                @tidy[:age] = "1d"

                ager = @tidy.parameter(:age)
                ager.expects(:tidy?).with("/what/ever", @stat).returns false
                @tidy.should_not be_tidy("/what/ever")
            end

            it "should return false if it does not match size requirements" do
                @tidy[:size] = "1b"

                sizer = @tidy.parameter(:size)
                sizer.expects(:tidy?).with("/what/ever", @stat).returns false
                @tidy.should_not be_tidy("/what/ever")
            end

            it "should tidy a file if age and size are set but only size matches" do
                @tidy[:size] = "1b"
                @tidy[:age] = "1d"

                @tidy.parameter(:size).stubs(:tidy?).returns true
                @tidy.parameter(:age).stubs(:tidy?).returns false
                @tidy.should be_tidy("/what/ever")
            end

            it "should tidy a file if age and size are set but only age matches" do
                @tidy[:size] = "1b"
                @tidy[:age] = "1d"

                @tidy.parameter(:size).stubs(:tidy?).returns false
                @tidy.parameter(:age).stubs(:tidy?).returns true
                @tidy.should be_tidy("/what/ever")
            end

            it "should tidy all files if neither age nor size is set" do
                @tidy.must be_tidy("/what/ever")
            end

            it "should sort the results inversely by path length, so files are added to the catalog before their directories" do
                @tidy[:recurse] = true
                @tidy[:rmdirs] = true
                fileset = Puppet::FileServing::Fileset.new("/what/ever")
                Puppet::FileServing::Fileset.expects(:new).returns fileset
                fileset.expects(:files).returns %w{. one one/two}

                @tidy.stubs(:tidy?).returns true

                @tidy.generate.collect { |r| r[:path] }.should == %w{/what/ever/one/two /what/ever/one /what/ever}
            end
        end

        it "should configure directories to require their contained files if rmdirs is enabled, so the files will be deleted first" do
            @tidy[:recurse] = true
            @tidy[:rmdirs] = true
            fileset = mock 'fileset'
            Puppet::FileServing::Fileset.expects(:new).with("/what/ever", :recurse => true).returns fileset
            fileset.expects(:files).returns %w{. one two one/subone two/subtwo one/subone/ssone}
            @tidy.stubs(:tidy?).returns true

            result = @tidy.generate.inject({}) { |hash, res| hash[res[:path]] = res; hash }
            {
                "/what/ever" => %w{/what/ever/one /what/ever/two},
                "/what/ever/one" => ["/what/ever/one/subone"],
                "/what/ever/two" => ["/what/ever/two/subtwo"],
                "/what/ever/one/subone" => ["/what/ever/one/subone/ssone"]
            }.each do |parent, children|
                children.each do |child|
                    ref = Puppet::Resource::Reference.new(:file, child)
                    result[parent][:require].find { |req| req.to_s == ref.to_s }.should_not be_nil
                end
            end
        end
    end
end