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
|
# $Id$
require 'test/unit'
class TestPuppet < Test::Unit::TestCase
def setup
@@tmpfiles = []
Puppet[:loglevel] = :debug if __FILE__ == $0
end
def teardown
@@tmpfiles.each { |file|
if FileTest.exists?(file)
system("chmod -R 755 %s" % file)
system("rm -rf %s" % file)
end
}
@@tmpfiles.clear
Puppet::Type.allclear
end
def test_nothing
end
end
unless defined? PuppetTestSuite
$:.unshift File.join(Dir.getwd, '../lib')
class PuppetTestSuite
attr_accessor :subdir
def self.list
Dir.entries(".").find_all { |file|
FileTest.directory?(file) and file !~ /^\./
}
end
def initialize(name)
unless FileTest.directory?(name)
puts "TestSuites are directories containing test cases"
puts "no such directory: %s" % name
exit(65)
end
# load each of the files
Dir.entries(name).collect { |file|
File.join(name,file)
}.find_all { |file|
FileTest.file?(file) and file =~ /tc_.+\.rb$/
}.sort { |a,b|
# in the order they were modified, so the last modified files
# are loaded and thus displayed last
File.stat(b) <=> File.stat(a)
}.each { |file|
require file
}
end
end
def textfiles
textdir = File.join($puppetbase,"examples","code")
# only parse this one file now
yield File.join(textdir,"head")
end
def failers
textdir = File.join($puppetbase,"examples","code", "failers")
# only parse this one file now
files = Dir.entries(textdir).reject { |file|
file =~ %r{\.swp}
}.reject { |file|
file =~ %r{\.disabled}
}.collect { |file|
File.join(textdir,file)
}.find_all { |file|
FileTest.file?(file)
}.sort.each { |file|
Puppet.debug "Processing %s" % file
yield file
}
end
module FileTesting
def newcomp(name,*ary)
comp = Puppet::Type::Component.new(
:name => name
)
ary.each { |item| comp.push item }
return comp
end
def cycle(comp)
trans = nil
assert_nothing_raised {
trans = comp.evaluate
}
assert_nothing_raised {
trans.evaluate
}
end
def randlist(list)
num = rand(4)
if num == 0
num = 1
end
set = []
ret = []
num.times { |index|
item = list[rand(list.length)]
if set.include?(item)
redo
end
ret.push item
}
return ret
end
def mkranddirsandfiles(dirs = nil,files = nil,depth = 3)
if depth < 0
return
end
unless dirs
dirs = %w{This Is A Set Of Directories}
end
unless files
files = %w{and this is a set of files}
end
tfiles = randlist(files)
tdirs = randlist(dirs)
tfiles.each { |file|
File.open(file, "w") { |of|
4.times {
of.puts rand(100)
}
}
}
tdirs.each { |dir|
# it shouldn't already exist, but...
unless FileTest.exists?(dir)
Dir.mkdir(dir)
FileUtils.cd(dir) {
mkranddirsandfiles(dirs,files,depth - 1)
}
end
}
end
def file_list(dir)
list = nil
FileUtils.cd(dir) {
list = %x{find . 2>/dev/null}.chomp.split(/\n/)
}
return list
end
def assert_trees_equal(fromdir,todir)
assert(FileTest.directory?(fromdir))
assert(FileTest.directory?(todir))
# verify the file list is the same
fromlist = nil
FileUtils.cd(fromdir) {
fromlist = %x{find . 2>/dev/null}.chomp.split(/\n/).reject { |file|
! FileTest.readable?(file)
}.sort
}
tolist = file_list(todir).sort
assert_equal(fromlist,tolist)
# and then do some verification that the files are actually set up
# the same
checked = 0
fromlist.each_with_index { |file,i|
fromfile = File.join(fromdir,file)
tofile = File.join(todir,file)
fromstat = File.stat(fromfile)
tostat = File.stat(tofile)
[:ftype,:gid,:mode,:uid].each { |method|
assert_equal(
fromstat.send(method),
tostat.send(method)
)
next if fromstat.ftype == "directory"
if checked < 10 and i % 3 == 0
from = File.open(fromfile) { |f| f.read }
to = File.open(tofile) { |f| f.read }
assert_equal(from,to)
checked += 1
end
}
}
end
def random_files(dir)
checked = 0
list = file_list(dir)
list.reverse.each_with_index { |file,i|
path = File.join(dir,file)
stat = File.stat(dir)
if checked < 10 and (i % 3) == 2
unless yield path
next
end
checked += 1
end
}
end
def delete_random_files(dir)
random_files(dir) { |file|
stat = File.stat(file)
begin
if stat.ftype == "directory"
false
else
File.unlink(file)
true
end
rescue => detail
# we probably won't be able to open our own secured files
puts detail
false
end
}
end
def add_random_files(dir)
added = []
random_files(dir) { |file|
stat = File.stat(file)
begin
if stat.ftype == "directory"
name = File.join(file,"file" + rand(100).to_s)
File.open(name, "w") { |f|
f.puts rand(10)
}
added << name
else
false
end
rescue => detail
# we probably won't be able to open our own secured files
puts detail
false
end
}
return added
end
def modify_random_files(dir)
modded = []
random_files(dir) { |file|
stat = File.stat(file)
begin
if stat.ftype == "directory"
false
else
File.open(file, "w") { |f|
f.puts rand(10)
}
modded << name
true
end
rescue => detail
# we probably won't be able to open our own secured files
puts detail
false
end
}
return modded
end
def readonly_random_files(dir)
modded = []
random_files(dir) { |file|
stat = File.stat(file)
begin
if stat.ftype == "directory"
File.new(file).chmod(0111)
else
File.new(file).chmod(0000)
end
modded << file
rescue => detail
# we probably won't be able to open our own secured files
puts detail
false
end
}
return modded
end
def conffile
File.join($puppetbase,"examples/root/etc/configfile")
end
def tempfile
File.join(self.tmpdir(), "puppetestfile")
end
def tmpdir
"/tmp"
end
end
end
|