~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to test/types/zone.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080726154345-1fmgo76b4l72ulvc
ImportĀ upstreamĀ versionĀ 0.24.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env ruby
2
 
 
3
 
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
4
 
 
5
 
# Test host job creation, modification, and destruction
6
 
 
7
 
require 'puppettest'
8
 
require 'puppet'
9
 
require 'puppet/type/zone'
10
 
require 'facter'
11
 
 
12
 
class TestZone < Test::Unit::TestCase
13
 
        include PuppetTest
14
 
 
15
 
    def test_nothing
16
 
    end
17
 
 
18
 
    # Zones can only be tested on solaris.
19
 
    if Facter["operatingsystem"].value == "Solaris"
20
 
 
21
 
    def setup
22
 
        super
23
 
        @@zones = []
24
 
    end
25
 
 
26
 
    def teardown
27
 
        current = %x{zoneadm list -cp}.split("\n").inject({}) { |h, line|
28
 
            ary = line.split(":")
29
 
            h[ary[1]] = ary[2]
30
 
            h
31
 
        }
32
 
 
33
 
        Puppet::Type.type(:zone).clear
34
 
 
35
 
        # Get rid of any lingering zones
36
 
        @@zones.each do |zone|
37
 
            next unless current.include? zone
38
 
 
39
 
            obj = Puppet::Type.type(:zone).create(:name => zone)
40
 
            obj[:ensure] = :absent
41
 
            assert_apply(obj)
42
 
        end
43
 
 
44
 
        # We can't delete the temp files until the zones are stopped and removed.
45
 
        super
46
 
    end
47
 
 
48
 
    def mkzone(name)
49
 
        zone = nil
50
 
 
51
 
        base = tempfile()
52
 
        Dir.mkdir(base)
53
 
        File.chmod(0700, base)
54
 
        root = File.join(base, "zonebase")
55
 
        assert_nothing_raised {
56
 
            zone = Puppet::Type.type(:zone).create(
57
 
                :name => name,
58
 
                :path => root,
59
 
                :ensure => "configured" # don't want to install zones automatically
60
 
            )
61
 
        }
62
 
 
63
 
        @@zones << name
64
 
 
65
 
        return zone
66
 
    end
67
 
 
68
 
    def test_list
69
 
        list = nil
70
 
        assert_nothing_raised {
71
 
            list = Puppet::Type.type(:zone).list
72
 
        }
73
 
 
74
 
        assert(! list.empty?, "Got no zones back")
75
 
 
76
 
        assert(list.find { |z| z[:name] == "global" }, "Could not find global zone")
77
 
    end
78
 
 
79
 
    def test_valueslice
80
 
        zone = mkzone("slicetest")
81
 
 
82
 
        state = zone.state(:ensure)
83
 
 
84
 
        slice = nil
85
 
        assert_nothing_raised {
86
 
            slice = state.class.valueslice(:absent, :installed).collect do |o|
87
 
                o[:name]
88
 
            end
89
 
        }
90
 
 
91
 
 
92
 
        assert_equal([:configured, :installed], slice)
93
 
 
94
 
        assert_nothing_raised {
95
 
            slice = state.class.valueslice(:running, :installed).collect do |o|
96
 
                o[:name]
97
 
            end
98
 
        }
99
 
 
100
 
 
101
 
        assert_equal(slice, [:installed])
102
 
 
103
 
    end
104
 
 
105
 
    # Make sure the ensure stuff behaves as we expect
106
 
    def test_zoneensure
107
 
        zone = mkzone("ensurezone")
108
 
 
109
 
        state = zone.state(:ensure)
110
 
 
111
 
        assert(state, "Did not get ensure state")
112
 
 
113
 
        assert_nothing_raised {
114
 
            zone.retrieve
115
 
        }
116
 
 
117
 
        assert(! state.insync?, "State is somehow in sync")
118
 
 
119
 
        assert(state.up?, "State incorrectly thinks it is not moving up")
120
 
 
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")
126
 
    end
127
 
 
128
 
    # Make sure all mentioned methods actually exist.
129
 
    def test_zonemethods_exist
130
 
        methods = []
131
 
        zone = mkzone("methodtest")
132
 
 
133
 
        state = zone.state(:ensure)
134
 
        assert_nothing_raised {
135
 
            state.class.valueslice(:absent, :running).each do |st|
136
 
                [:up, :down].each do |m|
137
 
                    if st[m]
138
 
                        methods << st[m]
139
 
                    end
140
 
                end
141
 
            end
142
 
        }
143
 
 
144
 
        methods.each do |m|
145
 
            assert(Puppet::Type.type(:zone).method_defined?(m),
146
 
                "Zones do not define method %s" % m)
147
 
        end
148
 
 
149
 
    end
150
 
 
151
 
    # Make sure our state generates the correct text.
152
 
    def test_inherit_state
153
 
        zone = mkzone("configtesting")
154
 
        zone[:ensure] = :configured
155
 
 
156
 
        assert_nothing_raised {
157
 
            zone[:inherit] = "/usr"
158
 
        }
159
 
        state = zone.state(:inherit)
160
 
        assert(zone, "Did not get 'inherit' state")
161
 
 
162
 
        assert_equal("add inherit-pkg-dir\nset dir=/usr\nend", state.configtext,
163
 
            "Got incorrect config text")
164
 
 
165
 
        state.is = "/usr"
166
 
 
167
 
        assert_equal("", state.configtext,
168
 
            "Got incorrect config text")
169
 
 
170
 
        # Now we want multiple directories
171
 
        state.should = %w{/usr /sbin /lib}
172
 
 
173
 
        # The statements are sorted
174
 
        text = "add inherit-pkg-dir
175
 
set dir=/lib
176
 
end
177
 
add inherit-pkg-dir
178
 
set dir=/sbin
179
 
end"
180
 
 
181
 
        assert_equal(text, state.configtext,
182
 
            "Got incorrect config text")
183
 
 
184
 
        state.is = %w{/usr /sbin /lib}
185
 
        state.should = %w{/usr /sbin}
186
 
 
187
 
        text = "remove inherit-pkg-dir dir=/lib"
188
 
 
189
 
        assert_equal(text, state.configtext,
190
 
            "Got incorrect config text")
191
 
    end
192
 
 
193
 
    if Puppet::SUIDManager.uid == 0
194
 
    # Make sure our ensure process actually works.
195
 
    def test_ensure_sync
196
 
        zone = mkzone("ensuretesting")
197
 
 
198
 
        zone[:ensure] = :configured
199
 
 
200
 
        zone.retrieve
201
 
        assert_apply(zone)
202
 
 
203
 
        zone.retrieve
204
 
 
205
 
        assert(zone.insync?, "Zone is not insync")
206
 
    end
207
 
 
208
 
    def test_getconfig
209
 
        zone = mkzone("configtesting")
210
 
 
211
 
        base = tempfile()
212
 
        zone[:path] = base
213
 
 
214
 
        ip = "192.168.0.1"
215
 
        interface = "bge0"
216
 
        zone[:ip] = "#{interface}:#{ip}"
217
 
 
218
 
        IO.popen("zonecfg -z configtesting -f -", "w") do |f|
219
 
            f.puts %{create -b
220
 
set zonepath=#{tempfile()}
221
 
set autoboot=true
222
 
add inherit-pkg-dir
223
 
set dir=/lib
224
 
end
225
 
add inherit-pkg-dir
226
 
set dir=/platform
227
 
end
228
 
add inherit-pkg-dir
229
 
set dir=/sbin
230
 
end
231
 
add inherit-pkg-dir
232
 
set dir=/opt/csw
233
 
end
234
 
add inherit-pkg-dir
235
 
set dir=/usr
236
 
end
237
 
add net
238
 
set address=#{ip}
239
 
set physical=bge0
240
 
end
241
 
}
242
 
        end
243
 
 
244
 
        assert_equal(0, $?, "Did not successfully create zone")
245
 
 
246
 
        #@@zones << "configtesting"
247
 
 
248
 
        assert_nothing_raised {
249
 
            zone.send(:getconfig)
250
 
        }
251
 
 
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."
255
 
        )
256
 
 
257
 
        assert_equal(["#{interface}:#{ip}"], zone.is(:ip),
258
 
            "IP addresses did not get collected correctly.")
259
 
 
260
 
        assert_equal(:true, zone.is(:autoboot),
261
 
            "Autoboot did not get collected correctly.")
262
 
    end
263
 
 
264
 
    # Make sure we can do all the various and sundry configuring things.
265
 
    def test_configuring_zones
266
 
        zone = mkzone("configtesting")
267
 
 
268
 
        assert_nothing_raised {
269
 
            zone[:inherit] = "/usr"
270
 
        }
271
 
 
272
 
        zone[:ensure] = :configured
273
 
 
274
 
        zone.retrieve
275
 
        assert_apply(zone)
276
 
 
277
 
        zone.retrieve
278
 
 
279
 
        assert(zone.insync?, "Zone is not insync")
280
 
 
281
 
        # Now add a new directory to inherit
282
 
        assert_nothing_raised {
283
 
            zone[:inherit] = ["/sbin", "/usr"]
284
 
        }
285
 
        assert_apply(zone)
286
 
 
287
 
        zone.retrieve
288
 
 
289
 
        assert(zone.insync?, "Zone is not insync")
290
 
 
291
 
        assert(%x{/usr/sbin/zonecfg -z #{zone[:name]} info} =~ /dir: \/sbin/,
292
 
            "sbin was not added")
293
 
 
294
 
        # And then remove it.
295
 
        assert_nothing_raised {
296
 
            zone[:inherit] = "/usr"
297
 
        }
298
 
        assert_apply(zone)
299
 
 
300
 
        zone.retrieve
301
 
 
302
 
        assert(zone.insync?, "Zone is not insync")
303
 
 
304
 
        assert(%x{/usr/sbin/zonecfg -z #{zone[:name]} info} !~ /dir: \/sbin/,
305
 
            "sbin was not removed")
306
 
 
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"
310
 
 
311
 
        zone.retrieve
312
 
        assert(! zone.insync?, "Zone is marked as in sync")
313
 
 
314
 
        assert_apply(zone)
315
 
        zone.retrieve
316
 
        assert(zone.insync?, "Zone is not in sync")
317
 
        assert(%x{/usr/sbin/zonecfg -z #{zone[:name]} info} =~ /192.168.0.1/,
318
 
            "ip was not added")
319
 
        zone[:ip] = ["hme1:192.168.0.2", "hme0:192.168.0.1"]
320
 
        assert_apply(zone)
321
 
        zone.retrieve
322
 
        assert(zone.insync?, "Zone is not in sync")
323
 
        assert(%x{/usr/sbin/zonecfg -z #{zone[:name]} info} =~ /192.168.0.2/,
324
 
            "ip was not added")
325
 
        zone[:ip] = ["hme1:192.168.0.2"]
326
 
        assert_apply(zone)
327
 
        zone.retrieve
328
 
        assert(%x{/usr/sbin/zonecfg -z #{zone[:name]} info} !~ /192.168.0.1/,
329
 
            "ip was not removed")
330
 
    end
331
 
 
332
 
    # Test creating and removing a zone, but only up to the configured state,
333
 
    # so it's faster.
334
 
    def test_smallcreate
335
 
        zone = mkzone("smallcreate")
336
 
        # Include a bunch of stuff so the zone isn't as large
337
 
        dirs = %w{/usr /sbin /lib /platform}
338
 
 
339
 
        %w{/opt/csw}.each do |dir|
340
 
            dirs << dir if FileTest.exists? dir
341
 
        end
342
 
        zone[:inherit] = dirs
343
 
 
344
 
        assert(zone, "Did not make zone")
345
 
 
346
 
        zone[:ensure] = :configured
347
 
 
348
 
        assert(! zone.insync?, "Zone is incorrectly in sync")
349
 
 
350
 
        assert_apply(zone)
351
 
 
352
 
        assert_nothing_raised {
353
 
            zone.retrieve
354
 
        }
355
 
        assert(zone.insync?, "Zone is incorrectly out of sync")
356
 
 
357
 
        zone[:ensure] = :absent
358
 
 
359
 
        assert_apply(zone)
360
 
 
361
 
        zone.retrieve
362
 
 
363
 
        assert_equal(:absent, zone.is(:ensure), "Zone is not absent")
364
 
    end
365
 
 
366
 
    # Just go through each method linearly and make sure it works.
367
 
    def test_each_method
368
 
        zone = mkzone("methodtesting")
369
 
        dirs = %w{/usr /sbin /lib /platform}
370
 
 
371
 
        %w{/opt/csw}.each do |dir|
372
 
            dirs << dir if FileTest.exists? dir
373
 
        end
374
 
        zone[:inherit] = dirs
375
 
 
376
 
        [[:configure, :configured],
377
 
            [:install, :installed],
378
 
            [:start, :running],
379
 
            [:stop, :installed],
380
 
            [:uninstall, :configured],
381
 
            [:unconfigure, :absent]
382
 
        ].each do |method, state|
383
 
            Puppet.info "Testing %s" % method
384
 
            assert_nothing_raised {
385
 
                zone.retrieve
386
 
            }
387
 
            assert_nothing_raised {
388
 
                zone.send(method)
389
 
            }
390
 
            assert_nothing_raised {
391
 
                zone.retrieve
392
 
            }
393
 
            assert_equal(state, zone.is(:ensure),
394
 
                "Method %s did not correctly set state %s" %
395
 
                    [method, state])
396
 
        end
397
 
    end
398
 
 
399
 
    def test_mkzone
400
 
        zone = mkzone("testmaking")
401
 
        # Include a bunch of stuff so the zone isn't as large
402
 
        dirs = %w{/usr /sbin /lib /platform}
403
 
 
404
 
        %w{/opt/csw}.each do |dir|
405
 
            dirs << dir if FileTest.exists? dir
406
 
        end
407
 
        zone[:inherit] = dirs
408
 
 
409
 
        assert(zone, "Did not make zone")
410
 
 
411
 
 
412
 
        [:configured, :installed, :running, :installed, :absent].each do |value|
413
 
            assert_nothing_raised {
414
 
                zone[:ensure] = value
415
 
            }
416
 
            assert(! zone.insync?, "Zone is incorrectly in sync")
417
 
 
418
 
            assert_apply(zone)
419
 
 
420
 
            assert_nothing_raised {
421
 
                zone.retrieve
422
 
            }
423
 
            assert(zone.insync?, "Zone is incorrectly out of sync")
424
 
        end
425
 
 
426
 
        zone.retrieve
427
 
 
428
 
        assert_equal(:absent, zone.is(:ensure), "Zone is not absent")
429
 
    end
430
 
    end
431
 
    end
432
 
end
433
 
 
434
 
# $Id: zone.rb 1793 2006-10-16 22:01:40Z luke $