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

« back to all changes in this revision

Viewing changes to test/other/config.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
 
require 'puppet'
6
 
require 'puppet/config'
7
 
require 'puppettest'
8
 
require 'puppettest/parsertesting'
9
 
 
10
 
class TestConfig < Test::Unit::TestCase
11
 
        include PuppetTest
12
 
        include PuppetTest::ParserTesting
13
 
 
14
 
    def check_for_users
15
 
        count = Puppet::Type.type(:user).inject(0) { |c,o|
16
 
            c + 1
17
 
        }
18
 
        assert(count > 0, "Found no users")
19
 
    end
20
 
 
21
 
    def check_to_transportable(config)
22
 
        trans = nil
23
 
        assert_nothing_raised("Could not convert to a transportable") {
24
 
            trans = config.to_transportable
25
 
        }
26
 
 
27
 
        comp = nil
28
 
        assert_nothing_raised("Could not convert transportable to component") {
29
 
            comp = trans.to_type
30
 
        }
31
 
 
32
 
        check_for_users()
33
 
 
34
 
        assert_nothing_raised("Could not retrieve transported config") {
35
 
            comp.retrieve
36
 
        }
37
 
    end
38
 
 
39
 
    def check_to_manifest(config)
40
 
        manifest = nil
41
 
        assert_nothing_raised("Could not convert to a manifest") {
42
 
            manifest = config.to_manifest
43
 
        }
44
 
 
45
 
        Puppet[:parseonly] = true
46
 
 
47
 
        interp = nil
48
 
        assert_nothing_raised do
49
 
            interp = mkinterp :Code => manifest, :UseNodes => false
50
 
        end
51
 
 
52
 
        trans = nil
53
 
        assert_nothing_raised do
54
 
            trans = interp.evaluate(nil, {})
55
 
        end
56
 
        assert_nothing_raised("Could not instantiate objects") {
57
 
            trans.to_type
58
 
        }
59
 
        check_for_users()
60
 
    end
61
 
 
62
 
    def check_to_comp(config)
63
 
        comp = nil
64
 
        assert_nothing_raised("Could not convert to a component") {
65
 
            comp = config.to_component
66
 
        }
67
 
 
68
 
        assert_nothing_raised("Could not retrieve component") {
69
 
            comp.retrieve
70
 
        }
71
 
 
72
 
        check_for_users()
73
 
    end
74
 
 
75
 
    def check_to_config(config)
76
 
        newc = config.dup
77
 
 
78
 
        newfile = tempfile()
79
 
        File.open(newfile, "w") { |f| f.print config.to_config }
80
 
        assert_nothing_raised("Could not parse generated configuration") {
81
 
            newc.parse(newfile)
82
 
        }
83
 
 
84
 
        assert_equal(config, newc, "Configurations are not equal")
85
 
    end
86
 
 
87
 
    def mkconfig
88
 
        c = nil
89
 
        assert_nothing_raised {
90
 
            c = Puppet::Config.new
91
 
        }
92
 
        return c
93
 
    end
94
 
 
95
 
    def test_addbools
96
 
        c = mkconfig
97
 
 
98
 
        assert_nothing_raised {
99
 
            c.setdefaults(:testing, :booltest => [true, "testing"])
100
 
        }
101
 
 
102
 
        assert(c[:booltest])
103
 
        c = mkconfig
104
 
 
105
 
        assert_nothing_raised {
106
 
            c.setdefaults(:testing, :booltest => ["true", "testing"])
107
 
        }
108
 
 
109
 
        assert(c[:booltest])
110
 
 
111
 
        assert_nothing_raised {
112
 
            c[:booltest] = false
113
 
        }
114
 
 
115
 
        assert(! c[:booltest], "Booltest is not false")
116
 
 
117
 
        assert_nothing_raised {
118
 
            c[:booltest] = "false"
119
 
        }
120
 
 
121
 
        assert(! c[:booltest], "Booltest is not false")
122
 
 
123
 
        assert_raise(Puppet::Error) {
124
 
            c[:booltest] = "yayness"
125
 
        }
126
 
 
127
 
        assert_raise(Puppet::Error) {
128
 
            c[:booltest] = "/some/file"
129
 
        }
130
 
    end
131
 
 
132
 
    def test_strings
133
 
        c = mkconfig
134
 
        val = "this is a string"
135
 
        assert_nothing_raised {
136
 
            c.setdefaults(:testing, :strtest => [val, "testing"])
137
 
        }
138
 
 
139
 
        assert_equal(val, c[:strtest])
140
 
 
141
 
        # Verify that variables are interpolated
142
 
        assert_nothing_raised {
143
 
            c.setdefaults(:testing, :another => ["another $strtest", "testing"])
144
 
        }
145
 
 
146
 
        assert_equal("another #{val}", c[:another])
147
 
    end
148
 
 
149
 
    def test_files
150
 
        c = mkconfig
151
 
 
152
 
        parent = "/puppet"
153
 
        assert_nothing_raised {
154
 
            c.setdefaults(:testing, :parentdir => [parent, "booh"])
155
 
        }
156
 
 
157
 
        assert_nothing_raised {
158
 
            c.setdefaults(:testing, :child => ["$parent/child", "rah"])
159
 
        }
160
 
 
161
 
        assert_equal(parent, c[:parentdir])
162
 
        assert_equal("/puppet/child", File.join(c[:parentdir], "child"))
163
 
    end
164
 
 
165
 
    def test_getset
166
 
        c = mkconfig
167
 
        initial = "an initial value"
168
 
        assert_raise(Puppet::Error) {
169
 
            c[:yayness] = initial
170
 
        }
171
 
 
172
 
        default = "this is a default"
173
 
        assert_nothing_raised {
174
 
            c.setdefaults(:testing, :yayness => [default, "rah"])
175
 
        }
176
 
 
177
 
        assert_equal(default, c[:yayness])
178
 
 
179
 
        assert_nothing_raised {
180
 
            c[:yayness] = initial
181
 
        }
182
 
 
183
 
        assert_equal(initial, c[:yayness])
184
 
 
185
 
        assert_nothing_raised {
186
 
            c.clear
187
 
        }
188
 
 
189
 
        assert_equal(default, c[:yayness])
190
 
 
191
 
        assert_nothing_raised {
192
 
            c[:yayness] = "not default"
193
 
        }
194
 
        assert_equal("not default", c[:yayness])
195
 
    end
196
 
 
197
 
    def test_parse
198
 
        text = %{
199
 
one = this is a test
200
 
two = another test
201
 
owner = root
202
 
group = root
203
 
yay = /a/path
204
 
 
205
 
[section1]
206
 
    attr = value
207
 
    owner = puppet
208
 
    group = puppet
209
 
    attrdir = /some/dir
210
 
    attr3 = $attrdir/other
211
 
        }
212
 
 
213
 
        file = tempfile()
214
 
        File.open(file, "w") { |f| f.puts text }
215
 
 
216
 
        c = mkconfig
217
 
        assert_nothing_raised {
218
 
            c.setdefaults("puppet",
219
 
                :one => ["a", "one"],
220
 
                :two => ["a", "two"],
221
 
                :yay => ["/default/path", "boo"],
222
 
                :mkusers => [true, "uh, yeah"]
223
 
            )
224
 
        }
225
 
 
226
 
        assert_nothing_raised {
227
 
            c.setdefaults("section1",
228
 
                :attr => ["a", "one"],
229
 
                :attrdir => ["/another/dir", "two"],
230
 
                :attr3 => ["$attrdir/maybe", "boo"]
231
 
            )
232
 
        }
233
 
        
234
 
        assert_nothing_raised {
235
 
            c.parse(file)
236
 
        }
237
 
 
238
 
        assert_equal("value", c[:attr])
239
 
        assert_equal("/some/dir", c[:attrdir])
240
 
        assert_equal(:directory, c.element(:attrdir).type)
241
 
        assert_equal("/some/dir/other", c[:attr3])
242
 
 
243
 
        elem = nil
244
 
        assert_nothing_raised {
245
 
            elem = c.element(:attr3)
246
 
        }
247
 
 
248
 
        assert(elem)
249
 
        assert_equal("puppet", elem.owner)
250
 
 
251
 
        config = nil
252
 
        assert_nothing_raised {
253
 
            config = c.to_config
254
 
        }
255
 
 
256
 
        assert_nothing_raised("Could not create transportable config") {
257
 
            c.to_transportable
258
 
        }
259
 
 
260
 
        check_to_comp(c)
261
 
        Puppet::Type.allclear
262
 
        check_to_manifest(c)
263
 
        Puppet::Type.allclear
264
 
        check_to_config(c)
265
 
        Puppet::Type.allclear
266
 
        check_to_transportable(c)
267
 
    end
268
 
 
269
 
    def test_arghandling
270
 
        c = mkconfig
271
 
 
272
 
        assert_nothing_raised {
273
 
            c.setdefaults("testing",
274
 
                :onboolean => [true, "An on bool"],
275
 
                :offboolean => [false, "An off bool"],
276
 
                :string => ["a string", "A string arg"],
277
 
                :file => ["/path/to/file", "A file arg"]
278
 
            )
279
 
        }
280
 
 
281
 
        data = {
282
 
            :onboolean => [true, false],
283
 
            :offboolean => [true, false],
284
 
            :string => ["one string", "another string"],
285
 
            :file => %w{/a/file /another/file}
286
 
        }
287
 
        data.each { |param, values|
288
 
            values.each { |val|
289
 
                opt = nil
290
 
                arg = nil
291
 
                if c.boolean?(param)
292
 
                    if val
293
 
                        opt = "--%s" % param
294
 
                    else
295
 
                        opt = "--no-%s" % param
296
 
                    end
297
 
                else
298
 
                    opt = "--%s" % param
299
 
                    arg = val
300
 
                end
301
 
 
302
 
                assert_nothing_raised("Could not handle arg %s with value %s" %
303
 
                    [opt, val]) {
304
 
 
305
 
                    c.handlearg(opt, arg)
306
 
                }
307
 
            }
308
 
        }
309
 
    end
310
 
 
311
 
    def test_argadding
312
 
        c = mkconfig
313
 
 
314
 
        assert_nothing_raised {
315
 
            c.setdefaults("testing",
316
 
                :onboolean => [true, "An on bool"],
317
 
                :offboolean => [false, "An off bool"],
318
 
                :string => ["a string", "A string arg"],
319
 
                :file => ["/path/to/file", "A file arg"]
320
 
            )
321
 
        }
322
 
        options = []
323
 
 
324
 
        c.addargs(options)
325
 
 
326
 
        c.each { |param, obj|
327
 
            opt = "--%s" % param
328
 
            assert(options.find { |ary|
329
 
                ary[0] == opt
330
 
            }, "Argument %s was not added" % opt)
331
 
 
332
 
            if c.boolean?(param)
333
 
                o = "--no-%s" % param
334
 
                assert(options.find { |ary|
335
 
                ary[0] == o
336
 
                }, "Boolean off %s was not added" % o)
337
 
            end
338
 
        }
339
 
    end
340
 
 
341
 
    def test_usesection
342
 
        c = mkconfig
343
 
 
344
 
        dir = tempfile()
345
 
        file = "$mydir/myfile"
346
 
        realfile = File.join(dir, "myfile")
347
 
        otherfile = File.join(dir, "otherfile")
348
 
        section = "testing"
349
 
        assert_nothing_raised {
350
 
            c.setdefaults(section,
351
 
                :mydir => [dir, "A dir arg"],
352
 
                :otherfile => {
353
 
                    :default => "$mydir/otherfile",
354
 
                    :create => true,
355
 
                    :desc => "A file arg"
356
 
                },
357
 
                :myfile => [file, "A file arg"]
358
 
            )
359
 
        }
360
 
 
361
 
        assert_nothing_raised("Could not use a section") {
362
 
            c.use(section)
363
 
        }
364
 
 
365
 
        assert_nothing_raised("Could not reuse a section") {
366
 
            c.use(section)
367
 
        }
368
 
 
369
 
        assert(FileTest.directory?(dir), "Did not create directory")
370
 
        assert(FileTest.exists?(otherfile), "Did not create file")
371
 
        assert(!FileTest.exists?(realfile), "Created file")
372
 
    end
373
 
 
374
 
    def test_setdefaultsarray
375
 
        c = mkconfig
376
 
 
377
 
        assert_nothing_raised {
378
 
            c.setdefaults("yay",
379
 
                :a => [false, "some value"],
380
 
                :b => ["/my/file", "a file"]
381
 
            )
382
 
        }
383
 
 
384
 
        assert_equal(false, c[:a], "Values are not equal")
385
 
        assert_equal("/my/file", c[:b], "Values are not equal")
386
 
    end
387
 
 
388
 
    def test_setdefaultshash
389
 
        c = mkconfig
390
 
 
391
 
        assert_nothing_raised {
392
 
            c.setdefaults("yay",
393
 
                :a => {:default => false, :desc => "some value"},
394
 
                :b => {:default => "/my/file", :desc => "a file"}
395
 
            )
396
 
        }
397
 
 
398
 
        assert_equal(false, c[:a], "Values are not equal")
399
 
        assert_equal("/my/file", c[:b], "Values are not equal")
400
 
    end
401
 
 
402
 
    def test_reuse
403
 
        c = mkconfig
404
 
 
405
 
        file = tempfile()
406
 
        section = "testing"
407
 
        assert_nothing_raised {
408
 
            c.setdefaults(section,
409
 
                :myfile => {:default => file, :create => true}
410
 
            )
411
 
        }
412
 
 
413
 
        assert_nothing_raised("Could not use a section") {
414
 
            c.use(section)
415
 
        }
416
 
 
417
 
        assert(FileTest.exists?(file), "Did not create file")
418
 
 
419
 
        assert(! Puppet::Type.type(:file)[file], "File obj still exists")
420
 
 
421
 
        File.unlink(file)
422
 
 
423
 
        c.reuse
424
 
        assert(FileTest.exists?(file), "Did not create file")
425
 
    end
426
 
 
427
 
    def test_mkusers
428
 
        c = mkconfig
429
 
 
430
 
        file = tempfile()
431
 
        section = "testing"
432
 
        assert_nothing_raised {
433
 
            c.setdefaults(section,
434
 
                :mkusers => [false, "yay"],
435
 
                :myfile => {
436
 
                    :default => file,
437
 
                    :owner => "pptest",
438
 
                    :group => "pptest",
439
 
                    :create => true
440
 
                }
441
 
            )
442
 
        }
443
 
 
444
 
        comp = nil
445
 
        assert_nothing_raised {
446
 
            comp = c.to_component
447
 
        }
448
 
 
449
 
        [:user, :group].each do |type|
450
 
            # The objects might get created internally by Puppet::Util; just
451
 
            # make sure they're not being managed
452
 
            if obj = Puppet.type(type)["pptest"]
453
 
                assert(! obj.managed?, "%s objectis managed" % type)
454
 
            end
455
 
        end
456
 
        comp.each { |o| o.remove }
457
 
 
458
 
        c[:mkusers] = true
459
 
 
460
 
        assert_nothing_raised {
461
 
            c.to_component
462
 
        }
463
 
 
464
 
        assert(Puppet.type(:user)["pptest"], "User object did not get created")
465
 
        assert(Puppet.type(:user)["pptest"].managed?,
466
 
            "User object is not managed."
467
 
        )
468
 
        assert(Puppet.type(:group)["pptest"], "Group object did not get created")
469
 
        assert(Puppet.type(:group)["pptest"].managed?,
470
 
            "Group object is not managed."
471
 
        )
472
 
    end
473
 
 
474
 
    def test_notmanagingdev
475
 
        c = mkconfig
476
 
        path = "/dev/testing"
477
 
        c.setdefaults(:test,
478
 
            :file => {
479
 
                :default => path,
480
 
                :mode => 0640,
481
 
                :desc => 'yay'
482
 
            }
483
 
        )
484
 
 
485
 
        assert_nothing_raised {
486
 
            c.to_component
487
 
        }
488
 
 
489
 
        assert(! Puppet.type(:file)["/dev/testing"], "Created dev file")
490
 
    end
491
 
 
492
 
    def test_groupsetting
493
 
        cfile = tempfile()
494
 
 
495
 
        group = "yayness"
496
 
 
497
 
        File.open(cfile, "w") do |f|
498
 
            f.puts "[#{Puppet.name}]
499
 
            group = #{group}
500
 
            "
501
 
        end
502
 
 
503
 
        config = mkconfig
504
 
        config.setdefaults(Puppet.name, :group => ["puppet", "a group"])
505
 
 
506
 
        assert_nothing_raised {
507
 
            config.parse(cfile)
508
 
        }
509
 
 
510
 
        assert_equal(group, config[:group], "Group did not take")
511
 
    end
512
 
 
513
 
    # provide a method to modify and create files w/out specifying the info
514
 
    # already stored in a config
515
 
    def test_writingfiles
516
 
        path = tempfile()
517
 
        mode = 0644
518
 
 
519
 
        config = mkconfig
520
 
 
521
 
        args = { :default => path, :mode => mode }
522
 
 
523
 
        user = nonrootuser()
524
 
        group = nonrootgroup()
525
 
 
526
 
        if Puppet::SUIDManager.uid == 0
527
 
            args[:owner] = user.name
528
 
            args[:group] = group.name
529
 
        end
530
 
 
531
 
        config.setdefaults(:testing, :myfile => args)
532
 
 
533
 
        assert_nothing_raised {
534
 
            config.write(:myfile) do |file|
535
 
                file.puts "yay"
536
 
            end
537
 
        }
538
 
 
539
 
        assert_equal(mode, filemode(path), "Modes are not equal")
540
 
 
541
 
        # OS X is broken in how it chgrps files 
542
 
        if Puppet::SUIDManager.uid == 0
543
 
            assert_equal(user.uid, File.stat(path).uid, "UIDS are not equal")
544
 
 
545
 
            case Facter["operatingsystem"].value
546
 
            when /BSD/, "Darwin": # nothing
547
 
            else
548
 
                assert_equal(group.gid, File.stat(path).gid, "GIDS are not equal")
549
 
            end
550
 
        end
551
 
    end
552
 
 
553
 
    def test_mkdir
554
 
        path = tempfile()
555
 
        mode = 0755
556
 
 
557
 
        config = mkconfig
558
 
 
559
 
        args = { :default => path, :mode => mode }
560
 
 
561
 
        user = nonrootuser()
562
 
        group = nonrootgroup()
563
 
 
564
 
        if Puppet::SUIDManager.uid == 0
565
 
            args[:owner] = user.name
566
 
            args[:group] = group.name
567
 
        end
568
 
 
569
 
        config.setdefaults(:testing, :mydir => args)
570
 
 
571
 
        assert_nothing_raised {
572
 
            config.mkdir(:mydir)
573
 
        }
574
 
 
575
 
        assert_equal(mode, filemode(path), "Modes are not equal")
576
 
 
577
 
 
578
 
        # OS X and *BSD is broken in how it chgrps files 
579
 
        if Puppet::SUIDManager.uid == 0
580
 
            assert_equal(user.uid, File.stat(path).uid, "UIDS are not equal")
581
 
 
582
 
            case Facter["operatingsystem"].value
583
 
            when /BSD/, "Darwin": # nothing
584
 
            else
585
 
                assert_equal(group.gid, File.stat(path).gid, "GIDS are not equal")
586
 
            end
587
 
        end
588
 
    end
589
 
 
590
 
    def test_booleans_and_integers
591
 
        config = mkconfig
592
 
        config.setdefaults(:mysection,
593
 
            :booltest => [false, "yay"],
594
 
            :inttest => [14, "yay"]
595
 
        )
596
 
 
597
 
        file = tempfile()
598
 
 
599
 
        File.open(file, "w") do |f|
600
 
            f.puts %{
601
 
[mysection]
602
 
booltest = true
603
 
inttest = 27
604
 
}
605
 
        end
606
 
 
607
 
        assert_nothing_raised {
608
 
            config.parse(file)
609
 
        }
610
 
 
611
 
        assert_equal(true, config[:booltest], "Boolean was not converted")
612
 
        assert_equal(27, config[:inttest], "Integer was not converted")
613
 
 
614
 
        # Now make sure that they get converted through handlearg
615
 
        config.handlearg("--inttest", "true")
616
 
        assert_equal(true, config[:inttest], "Boolean was not converted")
617
 
        config.handlearg("--no-booltest", "false")
618
 
        assert_equal(false, config[:booltest], "Boolean was not converted")
619
 
    end
620
 
 
621
 
    # Make sure that tags are ignored when configuring
622
 
    def test_configs_ignore_tags
623
 
        config = mkconfig
624
 
        file = tempfile()
625
 
 
626
 
        config.setdefaults(:mysection,
627
 
            :mydir => [file, "a file"]
628
 
        )
629
 
 
630
 
        Puppet[:tags] = "yayness"
631
 
 
632
 
        assert_nothing_raised {
633
 
            config.use(:mysection)
634
 
        }
635
 
 
636
 
        assert(FileTest.directory?(file), "Directory did not get created")
637
 
 
638
 
        assert_equal("yayness", Puppet[:tags],
639
 
            "Tags got changed during config")
640
 
    end
641
 
 
642
 
    def test_configs_replace_in_url
643
 
        config = mkconfig
644
 
        
645
 
        config.setdefaults(:mysection, :name => ["yayness", "yay"])
646
 
        config.setdefaults(:mysection, :url => ["http://$name/rahness", "yay"])
647
 
 
648
 
        val = nil
649
 
        assert_nothing_raised {
650
 
            val = config[:url]
651
 
        }
652
 
 
653
 
        assert_equal("http://yayness/rahness", val,
654
 
            "Config got messed up")
655
 
    end
656
 
 
657
 
    def test_correct_type_assumptions
658
 
        config = mkconfig
659
 
 
660
 
        file = Puppet::Config::CFile
661
 
        element = Puppet::Config::CElement
662
 
        bool = Puppet::Config::CBoolean
663
 
 
664
 
        # We have to keep these ordered, unfortunately.
665
 
        [
666
 
            ["/this/is/a/file", file],
667
 
            ["true", bool],
668
 
            [true, bool],
669
 
            ["false", bool],
670
 
            ["server", element],
671
 
            ["http://$server/yay", element],
672
 
            ["$server/yayness", file],
673
 
            ["$server/yayness.conf", file]
674
 
        ].each do |ary|
675
 
            value, type = ary
676
 
            elem = nil
677
 
            assert_nothing_raised {
678
 
                elem = config.newelement(
679
 
                    :name => value,
680
 
                    :default => value,
681
 
                    :section => :yayness
682
 
                )
683
 
            }
684
 
 
685
 
            assert_instance_of(type, elem,
686
 
                "%s got created as wrong type" % value.inspect)
687
 
        end
688
 
    end
689
 
 
690
 
    # Make sure we correctly reparse our config files but don't lose CLI values.
691
 
    def test_reparse
692
 
        Puppet[:filetimeout] = 0
693
 
 
694
 
        config = mkconfig()
695
 
        config.setdefaults(:mysection, :default => ["default", "yay"])
696
 
        config.setdefaults(:mysection, :clichange => ["clichange", "yay"])
697
 
        config.setdefaults(:mysection, :filechange => ["filechange", "yay"])
698
 
 
699
 
        file = tempfile()
700
 
        # Set one parameter in the file
701
 
        File.open(file, "w") { |f|
702
 
            f.puts %{[mysection]\nfilechange = filevalue}
703
 
        }
704
 
        assert_nothing_raised {
705
 
            config.parse(file)
706
 
        }
707
 
 
708
 
        # Set another "from the cli"
709
 
        assert_nothing_raised {
710
 
            config.handlearg("clichange", "clivalue")
711
 
        }
712
 
 
713
 
        # And leave the other unset
714
 
        assert_equal("default", config[:default])
715
 
        assert_equal("filevalue", config[:filechange])
716
 
        assert_equal("clivalue", config[:clichange])
717
 
 
718
 
        # Now rewrite the file
719
 
        File.open(file, "w") { |f|
720
 
            f.puts %{[mysection]\nfilechange = newvalue}
721
 
        }
722
 
 
723
 
        cfile = config.file
724
 
        cfile.send("tstamp=".intern, Time.now - 50)
725
 
 
726
 
        # And check all of the values
727
 
        assert_equal("default", config[:default])
728
 
        assert_equal("clivalue", config[:clichange])
729
 
        assert_equal("newvalue", config[:filechange])
730
 
    end
731
 
 
732
 
    def test_parse_removes_quotes
733
 
        config = mkconfig()
734
 
        config.setdefaults(:mysection, :singleq => ["single", "yay"])
735
 
        config.setdefaults(:mysection, :doubleq => ["double", "yay"])
736
 
        config.setdefaults(:mysection, :none => ["noquote", "yay"])
737
 
        config.setdefaults(:mysection, :middle => ["midquote", "yay"])
738
 
 
739
 
        file = tempfile()
740
 
        # Set one parameter in the file
741
 
        File.open(file, "w") { |f|
742
 
            f.puts %{[mysection]\n
743
 
    singleq = 'one'
744
 
    doubleq = "one"
745
 
    none = one
746
 
    middle = mid"quote
747
 
}
748
 
        }
749
 
 
750
 
        assert_nothing_raised {
751
 
            config.parse(file)
752
 
        }
753
 
 
754
 
        %w{singleq doubleq none}.each do |p|
755
 
            assert_equal("one", config[p], "%s did not match" % p)
756
 
        end
757
 
        assert_equal('mid"quote', config["middle"], "middle did not match")
758
 
    end
759
 
 
760
 
    def test_timer
761
 
        Puppet[:filetimeout] = 0.1
762
 
        origpath = tempfile()
763
 
        config = mkconfig()
764
 
        config.setdefaults(:mysection, :paramdir => [tempfile(), "yay"])
765
 
 
766
 
        file = tempfile()
767
 
        # Set one parameter in the file
768
 
        File.open(file, "w") { |f|
769
 
            f.puts %{[mysection]\n
770
 
    paramdir = #{origpath}
771
 
}
772
 
        }
773
 
 
774
 
        assert_nothing_raised {
775
 
            config.parse(file)
776
 
            config.use(:mysection)
777
 
        }
778
 
 
779
 
        assert(FileTest.directory?(origpath), "dir did not get created")
780
 
 
781
 
        # Now start the timer
782
 
        assert_nothing_raised {
783
 
            EventLoop.current.monitor_timer config.timer
784
 
        }
785
 
 
786
 
        newpath = tempfile()
787
 
 
788
 
        File.open(file, "w") { |f|
789
 
            f.puts %{[mysection]\n
790
 
    paramdir = #{newpath}
791
 
}
792
 
        }
793
 
        config.file.send("tstamp=".intern, Time.now - 50)
794
 
        sleep 1
795
 
 
796
 
        assert_equal(newpath, config["paramdir"],
797
 
                    "File did not get reparsed from timer")
798
 
        assert(FileTest.directory?(newpath), "new dir did not get created")
799
 
 
800
 
 
801
 
    end
802
 
 
803
 
    # Test that config parameters correctly call passed-in blocks when the value
804
 
    # is set.
805
 
    def test_paramblocks
806
 
        config = mkconfig()
807
 
 
808
 
        testing = nil
809
 
        elem = nil
810
 
        assert_nothing_raised do
811
 
            elem = config.newelement :default => "yay",
812
 
                :name => :blocktest,
813
 
                :section => :test,
814
 
                :hook => proc { |value| testing = value }
815
 
        end
816
 
 
817
 
        assert_nothing_raised do
818
 
            assert_equal("yay", elem.value)
819
 
        end
820
 
 
821
 
        assert_nothing_raised do
822
 
            elem.value = "yaytest"
823
 
        end
824
 
 
825
 
        assert_nothing_raised do
826
 
            assert_equal("yaytest", elem.value)
827
 
        end
828
 
        assert_equal("yaytest", testing)
829
 
 
830
 
        assert_nothing_raised do
831
 
            elem.value = "another"
832
 
        end
833
 
 
834
 
        assert_nothing_raised do
835
 
            assert_equal("another", elem.value)
836
 
        end
837
 
        assert_equal("another", testing)
838
 
 
839
 
        # Now verify it works from setdefault
840
 
        assert_nothing_raised do
841
 
            config.setdefaults :test,
842
 
                :blocktest2 => {
843
 
                    :default => "yay",
844
 
                    :hook => proc { |v| testing = v }
845
 
                }
846
 
        end
847
 
 
848
 
        assert_equal("yay", config[:blocktest2])
849
 
 
850
 
        assert_nothing_raised do
851
 
            config[:blocktest2] = "footest"
852
 
        end
853
 
        assert_equal("footest", config[:blocktest2])
854
 
        assert_equal("footest", testing)
855
 
    end
856
 
 
857
 
    def test_no_modify_root
858
 
        config = mkconfig
859
 
        config.setdefaults(:yay,
860
 
            :mydir => {:default => tempfile(),
861
 
                :mode => 0644,
862
 
                :owner => "root",
863
 
                :group => "root",
864
 
                :desc => "yay"
865
 
            },
866
 
            :mkusers => [false, "yay"]
867
 
        )
868
 
 
869
 
        assert_nothing_raised do
870
 
            config.use(:yay)
871
 
        end
872
 
 
873
 
        # Now enable it so they'll be added
874
 
        config[:mkusers] = true
875
 
 
876
 
        comp = config.to_component
877
 
 
878
 
        Puppet::Type.type(:user).each do |u|
879
 
            assert(u.name != "root", "Tried to manage root user")
880
 
        end
881
 
        Puppet::Type.type(:group).each do |u|
882
 
            assert(u.name != "root", "Tried to manage root group")
883
 
            assert(u.name != "wheel", "Tried to manage wheel group")
884
 
        end
885
 
 
886
 
#        assert(yay, "Did not find yay component")
887
 
#        yay.each do |c|
888
 
#            puts c.ref
889
 
#        end
890
 
#        assert(! yay.find { |o| o.class.name == :user and o.name == "root" },
891
 
#            "Found root user")
892
 
#        assert(! yay.find { |o| o.class.name == :group and o.name == "root" },
893
 
#            "Found root group")
894
 
    end
895
 
end
896
 
 
897
 
# $Id: config.rb 1852 2006-11-10 06:01:03Z luke $