File: fileignoresource.rb

package info (click to toggle)
puppet 0.20.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,072 kB
  • ctags: 3,365
  • sloc: ruby: 47,009; sh: 496; lisp: 143; xml: 122; makefile: 67
file content (279 lines) | stat: -rwxr-xr-x 8,147 bytes parent folder | download
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
#!/usr/bin/env ruby

$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/

require 'puppet'
require 'cgi'
require 'fileutils'
require 'puppettest'


class TestFileIgnoreSources < Test::Unit::TestCase
    include PuppetTest::FileTesting
   
    def setup
        super
        begin
            initstorage
        rescue
            system("rm -rf %s" % Puppet[:statefile])
        end
    end

#This is not needed unless using md5 (correct me if I'm wrong)
    def initstorage
        Puppet::Storage.init
        Puppet::Storage.load
    end

    def clearstorage
        Puppet::Storage.store
        Puppet::Storage.clear
    end

    def test_ignore_simple_source

        #Temp directory to run tests in
        path = tempfile()
        @@tmpfiles.push path

        #source directory
        sourcedir = "sourcedir"
        sourcefile1 = "sourcefile1"
        sourcefile2 = "sourcefile2"

        frompath = File.join(path,sourcedir)
        FileUtils.mkdir_p frompath

        topath = File.join(path,"destdir")
        FileUtils.mkdir topath

        #initialize variables before block
        tofile = nil
        trans = nil

        #create source files

        File.open(File.join(frompath,sourcefile1), 
          File::WRONLY|File::CREAT|File::APPEND) { |of|
            of.puts "yayness"
        }
      
        File.open(File.join(frompath,sourcefile2), 
          File::WRONLY|File::CREAT|File::APPEND) { |of|
            of.puts "even yayer"
        }
      

        #makes Puppet file Object
        assert_nothing_raised {
            tofile = Puppet.type(:file).create(
                :name => topath,
                :source => frompath,
                :recurse => true,                             
                :ignore => "sourcefile2"                            
            )
        }

        #make a component and adds the file
        comp = Puppet.type(:component).create(
            :name => "component"
        )
        comp.push tofile

        #make, evaluate transaction and sync the component
        assert_nothing_raised {
            trans = comp.evaluate
        }
        assert_nothing_raised {
            trans.evaluate
        }
  
      
        #topath should exist as a directory with sourcedir as a directory
       
        #This file should exist
        assert(FileTest.exists?(File.join(topath,sourcefile1)))

        #This file should not
        assert(!(FileTest.exists?(File.join(topath,sourcefile2))))
     
        Puppet::Type.allclear
    end

    def test_ignore_with_wildcard
        #Temp directory to run tests in
        path = tempfile()
        @@tmpfiles.push path

        #source directory
        sourcedir = "sourcedir"
        subdir = "subdir"
        subdir2 = "subdir2"
        sourcefile1 = "sourcefile1"
        sourcefile2 = "sourcefile2"

        frompath = File.join(path,sourcedir)
        FileUtils.mkdir_p frompath
        
        FileUtils.mkdir_p(File.join(frompath, subdir))
        FileUtils.mkdir_p(File.join(frompath, subdir2))
        dir =  Dir.glob(File.join(path,"**/*"))

        topath = File.join(path,"destdir")
        FileUtils.mkdir topath

        #initialize variables before block
        tofile = nil
        trans = nil

        #create source files
               
        dir.each { |dir|       
            File.open(File.join(dir,sourcefile1), 
             File::WRONLY|File::CREAT|File::APPEND) { |of|
                of.puts "yayness"
            }
      
            File.open(File.join(dir,sourcefile2), 
             File::WRONLY|File::CREAT|File::APPEND) { |of|
              of.puts "even yayer"
            }
      
        }

        #makes Puppet file Object
        assert_nothing_raised {
            tofile = Puppet.type(:file).create(
                :name => topath,
                :source => frompath,
                :recurse => true,                             
                :ignore => "*2"                            
            )
        }

        #make a component and adds the file
        comp = Puppet.type(:component).create(
            :name => "component"
        )
        comp.push tofile

        #make, evaluate transaction and sync the component
        assert_nothing_raised {
            trans = comp.evaluate
        }
        assert_nothing_raised {
            trans.evaluate
        }
              
        #topath should exist as a directory with sourcedir as a directory
       
        #This file should exist
        assert(FileTest.exists?(File.join(topath,sourcefile1)))
        assert(FileTest.exists?(File.join(topath,subdir)))
        assert(FileTest.exists?(File.join(File.join(topath,subdir),sourcefile1)))
        #This file should not
        assert(!(FileTest.exists?(File.join(topath,sourcefile2))))
        assert(!(FileTest.exists?(File.join(topath,subdir2))))
        assert(!(FileTest.exists?(File.join(File.join(topath,subdir),sourcefile2))))
        Puppet::Type.allclear

    end

    def test_ignore_array
        #Temp directory to run tests in
        path = tempfile()
        @@tmpfiles.push path

        #source directory
        sourcedir = "sourcedir"
        subdir = "subdir"
        subdir2 = "subdir2"
        subdir3 = "anotherdir"
        sourcefile1 = "sourcefile1"
        sourcefile2 = "sourcefile2"

        frompath = File.join(path,sourcedir)
        FileUtils.mkdir_p frompath
        
        FileUtils.mkdir_p(File.join(frompath, subdir))
        FileUtils.mkdir_p(File.join(frompath, subdir2))
        FileUtils.mkdir_p(File.join(frompath, subdir3))
        sourcedir =  Dir.glob(File.join(path,"**/*"))

        topath = File.join(path,"destdir")
        FileUtils.mkdir topath

        #initialize variables before block
        tofile = nil
        trans = nil

        #create source files
       

    
        sourcedir.each { |dir|       
            File.open(File.join(dir,sourcefile1), 
             File::WRONLY|File::CREAT|File::APPEND) { |of|
                of.puts "yayness"
            }
      
            File.open(File.join(dir,sourcefile2), 
             File::WRONLY|File::CREAT|File::APPEND) { |of|
              of.puts "even yayer"
            }
      
        }


        #makes Puppet file Object
        assert_nothing_raised {
            tofile = Puppet.type(:file).create(
                :name => topath,
                :source => frompath,
                :recurse => true,
                :ignore => ["*2", "an*"]                            
               # :ignore => ["*2", "an*", "nomatch"]                            
            )
        }

        #make a component and adds the file
        comp = Puppet.type(:component).create(
            :name => "component"
        )
        comp.push tofile

        #make, evaluate transaction and sync the component
        assert_nothing_raised {
            trans = comp.evaluate
        }
        assert_nothing_raised {
            trans.evaluate
        }

        #topath should exist as a directory with sourcedir as a directory

        # This file should exist
        # proper files in destination
        assert(FileTest.exists?(File.join(topath,sourcefile1)), "file1 not in destdir")
        assert(FileTest.exists?(File.join(topath,subdir)), "subdir1 not in destdir")
        assert(FileTest.exists?(File.join(File.join(topath,subdir),sourcefile1)), "file1 not in subdir")
        # proper files in source 
        assert(FileTest.exists?(File.join(frompath,subdir)), "subdir not in source")
        assert(FileTest.exists?(File.join(frompath,subdir2)), "subdir2 not in source")
        assert(FileTest.exists?(File.join(frompath,subdir3)), "subdir3 not in source")

        # This file should not
        assert(!(FileTest.exists?(File.join(topath,sourcefile2))), "file2 in dest")
        assert(!(FileTest.exists?(File.join(topath,subdir2))), "subdir2 in dest")
        assert(!(FileTest.exists?(File.join(topath,subdir3))), "anotherdir in dest")
        assert(!(FileTest.exists?(File.join(File.join(topath,subdir),sourcefile2))), "file2 in dest/sub")
        

        Puppet::Type.allclear

    end

end

# $Id$