File: zone.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 (434 lines) | stat: -rwxr-xr-x 11,098 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/env ruby

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

# Test host job creation, modification, and destruction

require 'puppettest'
require 'puppet'
require 'puppet/type/zone'
require 'facter'

class TestZone < Test::Unit::TestCase
	include PuppetTest

    def test_nothing
    end

    # Zones can only be tested on solaris.
    if Facter["operatingsystem"].value == "Solaris"

    def setup
        super
        @@zones = []
    end

    def teardown
        current = %x{zoneadm list -cp}.split("\n").inject({}) { |h, line|
            ary = line.split(":")
            h[ary[1]] = ary[2]
            h
        }

        Puppet::Type.type(:zone).clear

        # Get rid of any lingering zones
        @@zones.each do |zone|
            next unless current.include? zone

            obj = Puppet::Type.type(:zone).create(:name => zone)
            obj[:ensure] = :absent
            assert_apply(obj)
        end

        # We can't delete the temp files until the zones are stopped and removed.
        super
    end

    def mkzone(name)
        zone = nil

        base = tempfile()
        Dir.mkdir(base)
        File.chmod(0700, base)
        root = File.join(base, "zonebase")
        assert_nothing_raised {
            zone = Puppet::Type.type(:zone).create(
                :name => name,
                :path => root,
                :ensure => "configured" # don't want to install zones automatically
            )
        }

        @@zones << name

        return zone
    end

    def test_list
        list = nil
        assert_nothing_raised {
            list = Puppet::Type.type(:zone).list
        }

        assert(! list.empty?, "Got no zones back")

        assert(list.find { |z| z[:name] == "global" }, "Could not find global zone")
    end

    def test_valueslice
        zone = mkzone("slicetest")

        state = zone.state(:ensure)

        slice = nil
        assert_nothing_raised {
            slice = state.class.valueslice(:absent, :installed).collect do |o|
                o[:name]
            end
        }


        assert_equal([:configured, :installed], slice)

        assert_nothing_raised {
            slice = state.class.valueslice(:running, :installed).collect do |o|
                o[:name]
            end
        }


        assert_equal(slice, [:installed])

    end

    # Make sure the ensure stuff behaves as we expect
    def test_zoneensure
        zone = mkzone("ensurezone")

        state = zone.state(:ensure)

        assert(state, "Did not get ensure state")

        assert_nothing_raised {
            zone.retrieve
        }

        assert(! state.insync?, "State is somehow in sync")

        assert(state.up?, "State incorrectly thinks it is not moving up")

        zone.is = [:ensure, :configured]
        zone[:ensure] = :installed
        assert(state.up?, "State incorrectly thinks it is not moving up")
        zone[:ensure] = :absent
        assert(! state.up?, "State incorrectly thinks it is moving up")
    end

    # Make sure all mentioned methods actually exist.
    def test_zonemethods_exist
        methods = []
        zone = mkzone("methodtest")

        state = zone.state(:ensure)
        assert_nothing_raised {
            state.class.valueslice(:absent, :running).each do |st|
                [:up, :down].each do |m|
                    if st[m]
                        methods << st[m]
                    end
                end
            end
        }

        methods.each do |m|
            assert(Puppet::Type.type(:zone).method_defined?(m),
                "Zones do not define method %s" % m)
        end

    end

    # Make sure our state generates the correct text.
    def test_inherit_state
        zone = mkzone("configtesting")
        zone[:ensure] = :configured

        assert_nothing_raised {
            zone[:inherit] = "/usr"
        }
        state = zone.state(:inherit)
        assert(zone, "Did not get 'inherit' state")

        assert_equal("add inherit-pkg-dir\nset dir=/usr\nend", state.configtext,
            "Got incorrect config text")

        state.is = "/usr"

        assert_equal("", state.configtext,
            "Got incorrect config text")

        # Now we want multiple directories
        state.should = %w{/usr /sbin /lib}

        # The statements are sorted
        text = "add inherit-pkg-dir
set dir=/lib
end
add inherit-pkg-dir
set dir=/sbin
end"

        assert_equal(text, state.configtext,
            "Got incorrect config text")

        state.is = %w{/usr /sbin /lib}
        state.should = %w{/usr /sbin}

        text = "remove inherit-pkg-dir dir=/lib"

        assert_equal(text, state.configtext,
            "Got incorrect config text")
    end

    if Puppet::SUIDManager.uid == 0
    # Make sure our ensure process actually works.
    def test_ensure_sync
        zone = mkzone("ensuretesting")

        zone[:ensure] = :configured

        zone.retrieve
        assert_apply(zone)

        zone.retrieve

        assert(zone.insync?, "Zone is not insync")
    end

    def test_getconfig
        zone = mkzone("configtesting")

        base = tempfile()
        zone[:path] = base

        ip = "192.168.0.1"
        interface = "bge0"
        zone[:ip] = "#{interface}:#{ip}"

        IO.popen("zonecfg -z configtesting -f -", "w") do |f|
            f.puts %{create -b
set zonepath=#{tempfile()}
set autoboot=true
add inherit-pkg-dir
set dir=/lib
end
add inherit-pkg-dir
set dir=/platform
end
add inherit-pkg-dir
set dir=/sbin
end
add inherit-pkg-dir
set dir=/opt/csw
end
add inherit-pkg-dir
set dir=/usr
end
add net
set address=#{ip}
set physical=bge0
end
}
        end

        assert_equal(0, $?, "Did not successfully create zone")

        #@@zones << "configtesting"

        assert_nothing_raised {
            zone.send(:getconfig)
        }

        # Now, make sure everything is right.
        assert_equal(%w{/sbin /usr /opt/csw /lib /platform}.sort,
            zone.is(:inherit).sort, "Inherited dirs did not get collected correctly."
        )

        assert_equal(["#{interface}:#{ip}"], zone.is(:ip),
            "IP addresses did not get collected correctly.")

        assert_equal(:true, zone.is(:autoboot),
            "Autoboot did not get collected correctly.")
    end

    # Make sure we can do all the various and sundry configuring things.
    def test_configuring_zones
        zone = mkzone("configtesting")

        assert_nothing_raised {
            zone[:inherit] = "/usr"
        }

        zone[:ensure] = :configured

        zone.retrieve
        assert_apply(zone)

        zone.retrieve

        assert(zone.insync?, "Zone is not insync")

        # Now add a new directory to inherit
        assert_nothing_raised {
            zone[:inherit] = ["/sbin", "/usr"]
        }
        assert_apply(zone)

        zone.retrieve

        assert(zone.insync?, "Zone is not insync")

        assert(%x{/usr/sbin/zonecfg -z #{zone[:name]} info} =~ /dir: \/sbin/,
            "sbin was not added")

        # And then remove it.
        assert_nothing_raised {
            zone[:inherit] = "/usr"
        }
        assert_apply(zone)

        zone.retrieve

        assert(zone.insync?, "Zone is not insync")

        assert(%x{/usr/sbin/zonecfg -z #{zone[:name]} info} !~ /dir: \/sbin/,
            "sbin was not removed")

        # Now add an ip adddress.  Fortunately (or not), zonecfg doesn't verify
        # that the interface exists.
        zone[:ip] = "hme0:192.168.0.1"

        zone.retrieve
        assert(! zone.insync?, "Zone is marked as in sync")

        assert_apply(zone)
        zone.retrieve
        assert(zone.insync?, "Zone is not in sync")
        assert(%x{/usr/sbin/zonecfg -z #{zone[:name]} info} =~ /192.168.0.1/,
            "ip was not added")
        zone[:ip] = ["hme1:192.168.0.2", "hme0:192.168.0.1"]
        assert_apply(zone)
        zone.retrieve
        assert(zone.insync?, "Zone is not in sync")
        assert(%x{/usr/sbin/zonecfg -z #{zone[:name]} info} =~ /192.168.0.2/,
            "ip was not added")
        zone[:ip] = ["hme1:192.168.0.2"]
        assert_apply(zone)
        zone.retrieve
        assert(%x{/usr/sbin/zonecfg -z #{zone[:name]} info} !~ /192.168.0.1/,
            "ip was not removed")
    end

    # Test creating and removing a zone, but only up to the configured state,
    # so it's faster.
    def test_smallcreate
        zone = mkzone("smallcreate")
        # Include a bunch of stuff so the zone isn't as large
        dirs = %w{/usr /sbin /lib /platform}

        %w{/opt/csw}.each do |dir|
            dirs << dir if FileTest.exists? dir
        end
        zone[:inherit] = dirs

        assert(zone, "Did not make zone")

        zone[:ensure] = :configured

        assert(! zone.insync?, "Zone is incorrectly in sync")

        assert_apply(zone)

        assert_nothing_raised {
            zone.retrieve
        }
        assert(zone.insync?, "Zone is incorrectly out of sync")

        zone[:ensure] = :absent

        assert_apply(zone)

        zone.retrieve

        assert_equal(:absent, zone.is(:ensure), "Zone is not absent")
    end

    # Just go through each method linearly and make sure it works.
    def test_each_method
        zone = mkzone("methodtesting")
        dirs = %w{/usr /sbin /lib /platform}

        %w{/opt/csw}.each do |dir|
            dirs << dir if FileTest.exists? dir
        end
        zone[:inherit] = dirs

        [[:configure, :configured],
            [:install, :installed],
            [:start, :running],
            [:stop, :installed],
            [:uninstall, :configured],
            [:unconfigure, :absent]
        ].each do |method, state|
            Puppet.info "Testing %s" % method
            assert_nothing_raised {
                zone.retrieve
            }
            assert_nothing_raised {
                zone.send(method)
            }
            assert_nothing_raised {
                zone.retrieve
            }
            assert_equal(state, zone.is(:ensure),
                "Method %s did not correctly set state %s" %
                    [method, state])
        end
    end

    def test_mkzone
        zone = mkzone("testmaking")
        # Include a bunch of stuff so the zone isn't as large
        dirs = %w{/usr /sbin /lib /platform}

        %w{/opt/csw}.each do |dir|
            dirs << dir if FileTest.exists? dir
        end
        zone[:inherit] = dirs

        assert(zone, "Did not make zone")


        [:configured, :installed, :running, :installed, :absent].each do |value|
            assert_nothing_raised {
                zone[:ensure] = value
            }
            assert(! zone.insync?, "Zone is incorrectly in sync")

            assert_apply(zone)

            assert_nothing_raised {
                zone.retrieve
            }
            assert(zone.insync?, "Zone is incorrectly out of sync")
        end

        zone.retrieve

        assert_equal(:absent, zone.is(:ensure), "Zone is not absent")
    end
    end
    end
end

# $Id: zone.rb 1793 2006-10-16 22:01:40Z luke $