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
|
#!/usr/bin/env ruby
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
require 'puppet'
require 'puppettest'
# $Id: component.rb 1793 2006-10-16 22:01:40Z luke $
class TestComponent < Test::Unit::TestCase
include PuppetTest
def setup
super
@@used = {}
end
def randnum(limit)
num = nil
looped = 0
loop do
looped += 1
if looped > 2000
raise "Reached limit of looping"
break
end
num = rand(limit)
unless @@used.include?(num)
@@used[num] = true
break
end
end
num
end
def mkfile(num = nil)
unless num
num = randnum(1000)
end
name = tempfile() + num.to_s
file = Puppet.type(:file).create(
:path => name,
:checksum => "md5"
)
@@tmpfiles << name
file
end
def mkcomp
Puppet.type(:component).create(:name => "component_" + randnum(1000).to_s)
end
def mkrandcomp(numfiles, numdivs)
comp = mkcomp
hash = {}
found = 0
divs = {}
numdivs.times { |i|
num = i + 2
divs[num] = nil
}
while found < numfiles
num = randnum(numfiles)
found += 1
f = mkfile(num)
hash[f.name] = f
reqd = []
divs.each { |n,obj|
if rand(50) % n == 0
if obj
unless reqd.include?(obj.object_id)
f[:require] = [[obj.class.name, obj.name]]
reqd << obj.object_id
end
end
end
divs[n] = f
}
end
hash.each { |name, obj|
comp.push obj
}
comp.finalize
comp
end
def test_ordering
list = nil
comp = mkrandcomp(30,5)
assert_nothing_raised {
list = comp.flatten
}
list.each_with_index { |obj, index|
obj.eachdependency { |dep|
assert(list.index(dep) < index)
}
}
end
def test_correctsorting
tmpfile = tempfile()
@@tmpfiles.push tmpfile
trans = nil
cmd = nil
File.open(tmpfile, File::WRONLY|File::CREAT|File::TRUNC) { |of|
of.puts rand(100)
}
file = Puppet.type(:file).create(
:path => tmpfile,
:checksum => "md5"
)
assert_nothing_raised {
cmd = Puppet.type(:exec).create(
:command => "pwd",
:path => "/usr/bin:/bin:/usr/sbin:/sbin",
:subscribe => [[file.class.name,file.name]],
:refreshonly => true
)
}
order = nil
assert_nothing_raised {
order = Puppet.type(:component).sort([file, cmd])
}
[cmd, file].each { |obj|
assert_equal(1, order.find_all { |o| o.name == obj.name }.length)
}
end
def test_correctflattening
tmpfile = tempfile()
@@tmpfiles.push tmpfile
trans = nil
cmd = nil
File.open(tmpfile, File::WRONLY|File::CREAT|File::TRUNC) { |of|
of.puts rand(100)
}
file = Puppet.type(:file).create(
:path => tmpfile,
:checksum => "md5"
)
assert_nothing_raised {
cmd = Puppet.type(:exec).create(
:command => "pwd",
:path => "/usr/bin:/bin:/usr/sbin:/sbin",
:subscribe => [[file.class.name,file.name]],
:refreshonly => true
)
}
comp = newcomp(cmd, file)
comp.finalize
objects = nil
assert_nothing_raised {
objects = comp.flatten
}
[cmd, file].each { |obj|
assert_equal(1, objects.find_all { |o| o.name == obj.name }.length)
}
assert(objects[0] == file, "File was not first object")
assert(objects[1] == cmd, "Exec was not second object")
end
def test_deepflatten
tmpfile = tempfile()
@@tmpfiles.push tmpfile
trans = nil
cmd = nil
File.open(tmpfile, File::WRONLY|File::CREAT|File::TRUNC) { |of|
of.puts rand(100)
}
file = Puppet.type(:file).create(
:path => tmpfile,
:checksum => "md5"
)
assert_nothing_raised {
cmd = Puppet.type(:exec).create(
:command => "pwd",
:path => "/usr/bin:/bin:/usr/sbin:/sbin",
:refreshonly => true
)
}
fcomp = newcomp("fflatten", file)
ecomp = newcomp("eflatten", cmd)
# this subscription can screw up the sorting
ecomp[:subscribe] = [[fcomp.class.name,fcomp.name]]
comp = newcomp("bflatten", ecomp, fcomp)
comp.finalize
objects = nil
assert_nothing_raised {
objects = comp.flatten
}
assert_equal(objects.length, 2, "Did not get two sorted objects")
objects.each { |o|
assert(o.is_a?(Puppet::Type), "Object %s is not a Type" % o.class)
}
assert(objects[0] == file, "File was not first object")
assert(objects[1] == cmd, "Exec was not second object")
end
def test_deepflatten2
tmpfile = tempfile()
@@tmpfiles.push tmpfile
trans = nil
cmd = nil
File.open(tmpfile, File::WRONLY|File::CREAT|File::TRUNC) { |of|
of.puts rand(100)
}
file = Puppet.type(:file).create(
:path => tmpfile,
:checksum => "md5"
)
assert_nothing_raised {
cmd = Puppet.type(:exec).create(
:command => "pwd",
:path => "/usr/bin:/bin:/usr/sbin:/sbin",
:refreshonly => true
)
}
ocmd = nil
assert_nothing_raised {
ocmd = Puppet.type(:exec).create(
:command => "echo true",
:path => "/usr/bin:/bin:/usr/sbin:/sbin",
:refreshonly => true
)
}
fcomp = newcomp("fflatten", file)
ecomp = newcomp("eflatten", cmd)
ocomp = newcomp("oflatten", ocmd)
# this subscription can screw up the sorting
cmd[:subscribe] = [[fcomp.class.name,fcomp.name]]
ocmd[:subscribe] = [[cmd.class.name,cmd.name]]
comp = newcomp("bflatten", ocomp, ecomp, fcomp)
comp.finalize
objects = nil
assert_nothing_raised {
objects = comp.flatten
}
assert_equal(objects.length, 3, "Did not get three sorted objects")
objects.each { |o|
assert(o.is_a?(Puppet::Type), "Object %s is not a Type" % o.class)
}
assert(objects[0] == file, "File was not first object")
assert(objects[1] == cmd, "Exec was not second object")
assert(objects[2] == ocmd, "Other exec was not second object")
end
def test_moreordering
dir = tempfile()
comp = Puppet.type(:component).create(
:name => "ordertesting"
)
10.times { |i|
fileobj = Puppet.type(:file).create(
:path => File.join(dir, "file%s" % i),
:ensure => "file"
)
comp.push(fileobj)
}
dirobj = Puppet.type(:file).create(
:path => dir,
:ensure => "directory"
)
comp.push(dirobj)
assert_apply(comp)
end
end
|