3
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
5
# Test host job creation, modification, and destruction
9
require 'puppet/type/zone'
12
class TestZone < Test::Unit::TestCase
18
# Zones can only be tested on solaris.
19
if Facter["operatingsystem"].value == "Solaris"
27
current = %x{zoneadm list -cp}.split("\n").inject({}) { |h, line|
33
Puppet::Type.type(:zone).clear
35
# Get rid of any lingering zones
36
@@zones.each do |zone|
37
next unless current.include? zone
39
obj = Puppet::Type.type(:zone).create(:name => zone)
40
obj[:ensure] = :absent
44
# We can't delete the temp files until the zones are stopped and removed.
53
File.chmod(0700, base)
54
root = File.join(base, "zonebase")
55
assert_nothing_raised {
56
zone = Puppet::Type.type(:zone).create(
59
:ensure => "configured" # don't want to install zones automatically
70
assert_nothing_raised {
71
list = Puppet::Type.type(:zone).list
74
assert(! list.empty?, "Got no zones back")
76
assert(list.find { |z| z[:name] == "global" }, "Could not find global zone")
80
zone = mkzone("slicetest")
82
state = zone.state(:ensure)
85
assert_nothing_raised {
86
slice = state.class.valueslice(:absent, :installed).collect do |o|
92
assert_equal([:configured, :installed], slice)
94
assert_nothing_raised {
95
slice = state.class.valueslice(:running, :installed).collect do |o|
101
assert_equal(slice, [:installed])
105
# Make sure the ensure stuff behaves as we expect
107
zone = mkzone("ensurezone")
109
state = zone.state(:ensure)
111
assert(state, "Did not get ensure state")
113
assert_nothing_raised {
117
assert(! state.insync?, "State is somehow in sync")
119
assert(state.up?, "State incorrectly thinks it is not moving up")
121
zone.is = [:ensure, :configured]
122
zone[:ensure] = :installed
123
assert(state.up?, "State incorrectly thinks it is not moving up")
124
zone[:ensure] = :absent
125
assert(! state.up?, "State incorrectly thinks it is moving up")
128
# Make sure all mentioned methods actually exist.
129
def test_zonemethods_exist
131
zone = mkzone("methodtest")
133
state = zone.state(:ensure)
134
assert_nothing_raised {
135
state.class.valueslice(:absent, :running).each do |st|
136
[:up, :down].each do |m|
145
assert(Puppet::Type.type(:zone).method_defined?(m),
146
"Zones do not define method %s" % m)
151
# Make sure our state generates the correct text.
152
def test_inherit_state
153
zone = mkzone("configtesting")
154
zone[:ensure] = :configured
156
assert_nothing_raised {
157
zone[:inherit] = "/usr"
159
state = zone.state(:inherit)
160
assert(zone, "Did not get 'inherit' state")
162
assert_equal("add inherit-pkg-dir\nset dir=/usr\nend", state.configtext,
163
"Got incorrect config text")
167
assert_equal("", state.configtext,
168
"Got incorrect config text")
170
# Now we want multiple directories
171
state.should = %w{/usr /sbin /lib}
173
# The statements are sorted
174
text = "add inherit-pkg-dir
181
assert_equal(text, state.configtext,
182
"Got incorrect config text")
184
state.is = %w{/usr /sbin /lib}
185
state.should = %w{/usr /sbin}
187
text = "remove inherit-pkg-dir dir=/lib"
189
assert_equal(text, state.configtext,
190
"Got incorrect config text")
193
if Puppet::SUIDManager.uid == 0
194
# Make sure our ensure process actually works.
196
zone = mkzone("ensuretesting")
198
zone[:ensure] = :configured
205
assert(zone.insync?, "Zone is not insync")
209
zone = mkzone("configtesting")
216
zone[:ip] = "#{interface}:#{ip}"
218
IO.popen("zonecfg -z configtesting -f -", "w") do |f|
220
set zonepath=#{tempfile()}
244
assert_equal(0, $?, "Did not successfully create zone")
246
#@@zones << "configtesting"
248
assert_nothing_raised {
249
zone.send(:getconfig)
252
# Now, make sure everything is right.
253
assert_equal(%w{/sbin /usr /opt/csw /lib /platform}.sort,
254
zone.is(:inherit).sort, "Inherited dirs did not get collected correctly."
257
assert_equal(["#{interface}:#{ip}"], zone.is(:ip),
258
"IP addresses did not get collected correctly.")
260
assert_equal(:true, zone.is(:autoboot),
261
"Autoboot did not get collected correctly.")
264
# Make sure we can do all the various and sundry configuring things.
265
def test_configuring_zones
266
zone = mkzone("configtesting")
268
assert_nothing_raised {
269
zone[:inherit] = "/usr"
272
zone[:ensure] = :configured
279
assert(zone.insync?, "Zone is not insync")
281
# Now add a new directory to inherit
282
assert_nothing_raised {
283
zone[:inherit] = ["/sbin", "/usr"]
289
assert(zone.insync?, "Zone is not insync")
291
assert(%x{/usr/sbin/zonecfg -z #{zone[:name]} info} =~ /dir: \/sbin/,
292
"sbin was not added")
294
# And then remove it.
295
assert_nothing_raised {
296
zone[:inherit] = "/usr"
302
assert(zone.insync?, "Zone is not insync")
304
assert(%x{/usr/sbin/zonecfg -z #{zone[:name]} info} !~ /dir: \/sbin/,
305
"sbin was not removed")
307
# Now add an ip adddress. Fortunately (or not), zonecfg doesn't verify
308
# that the interface exists.
309
zone[:ip] = "hme0:192.168.0.1"
312
assert(! zone.insync?, "Zone is marked as in sync")
316
assert(zone.insync?, "Zone is not in sync")
317
assert(%x{/usr/sbin/zonecfg -z #{zone[:name]} info} =~ /192.168.0.1/,
319
zone[:ip] = ["hme1:192.168.0.2", "hme0:192.168.0.1"]
322
assert(zone.insync?, "Zone is not in sync")
323
assert(%x{/usr/sbin/zonecfg -z #{zone[:name]} info} =~ /192.168.0.2/,
325
zone[:ip] = ["hme1:192.168.0.2"]
328
assert(%x{/usr/sbin/zonecfg -z #{zone[:name]} info} !~ /192.168.0.1/,
329
"ip was not removed")
332
# Test creating and removing a zone, but only up to the configured state,
335
zone = mkzone("smallcreate")
336
# Include a bunch of stuff so the zone isn't as large
337
dirs = %w{/usr /sbin /lib /platform}
339
%w{/opt/csw}.each do |dir|
340
dirs << dir if FileTest.exists? dir
342
zone[:inherit] = dirs
344
assert(zone, "Did not make zone")
346
zone[:ensure] = :configured
348
assert(! zone.insync?, "Zone is incorrectly in sync")
352
assert_nothing_raised {
355
assert(zone.insync?, "Zone is incorrectly out of sync")
357
zone[:ensure] = :absent
363
assert_equal(:absent, zone.is(:ensure), "Zone is not absent")
366
# Just go through each method linearly and make sure it works.
368
zone = mkzone("methodtesting")
369
dirs = %w{/usr /sbin /lib /platform}
371
%w{/opt/csw}.each do |dir|
372
dirs << dir if FileTest.exists? dir
374
zone[:inherit] = dirs
376
[[:configure, :configured],
377
[:install, :installed],
380
[:uninstall, :configured],
381
[:unconfigure, :absent]
382
].each do |method, state|
383
Puppet.info "Testing %s" % method
384
assert_nothing_raised {
387
assert_nothing_raised {
390
assert_nothing_raised {
393
assert_equal(state, zone.is(:ensure),
394
"Method %s did not correctly set state %s" %
400
zone = mkzone("testmaking")
401
# Include a bunch of stuff so the zone isn't as large
402
dirs = %w{/usr /sbin /lib /platform}
404
%w{/opt/csw}.each do |dir|
405
dirs << dir if FileTest.exists? dir
407
zone[:inherit] = dirs
409
assert(zone, "Did not make zone")
412
[:configured, :installed, :running, :installed, :absent].each do |value|
413
assert_nothing_raised {
414
zone[:ensure] = value
416
assert(! zone.insync?, "Zone is incorrectly in sync")
420
assert_nothing_raised {
423
assert(zone.insync?, "Zone is incorrectly out of sync")
428
assert_equal(:absent, zone.is(:ensure), "Zone is not absent")
434
# $Id: zone.rb 1793 2006-10-16 22:01:40Z luke $