3
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
6
require 'puppet/config'
8
require 'puppettest/parsertesting'
10
class TestConfig < Test::Unit::TestCase
12
include PuppetTest::ParserTesting
15
count = Puppet::Type.type(:user).inject(0) { |c,o|
18
assert(count > 0, "Found no users")
21
def check_to_transportable(config)
23
assert_nothing_raised("Could not convert to a transportable") {
24
trans = config.to_transportable
28
assert_nothing_raised("Could not convert transportable to component") {
34
assert_nothing_raised("Could not retrieve transported config") {
39
def check_to_manifest(config)
41
assert_nothing_raised("Could not convert to a manifest") {
42
manifest = config.to_manifest
45
Puppet[:parseonly] = true
48
assert_nothing_raised do
49
interp = mkinterp :Code => manifest, :UseNodes => false
53
assert_nothing_raised do
54
trans = interp.evaluate(nil, {})
56
assert_nothing_raised("Could not instantiate objects") {
62
def check_to_comp(config)
64
assert_nothing_raised("Could not convert to a component") {
65
comp = config.to_component
68
assert_nothing_raised("Could not retrieve component") {
75
def check_to_config(config)
79
File.open(newfile, "w") { |f| f.print config.to_config }
80
assert_nothing_raised("Could not parse generated configuration") {
84
assert_equal(config, newc, "Configurations are not equal")
89
assert_nothing_raised {
90
c = Puppet::Config.new
98
assert_nothing_raised {
99
c.setdefaults(:testing, :booltest => [true, "testing"])
105
assert_nothing_raised {
106
c.setdefaults(:testing, :booltest => ["true", "testing"])
111
assert_nothing_raised {
115
assert(! c[:booltest], "Booltest is not false")
117
assert_nothing_raised {
118
c[:booltest] = "false"
121
assert(! c[:booltest], "Booltest is not false")
123
assert_raise(Puppet::Error) {
124
c[:booltest] = "yayness"
127
assert_raise(Puppet::Error) {
128
c[:booltest] = "/some/file"
134
val = "this is a string"
135
assert_nothing_raised {
136
c.setdefaults(:testing, :strtest => [val, "testing"])
139
assert_equal(val, c[:strtest])
141
# Verify that variables are interpolated
142
assert_nothing_raised {
143
c.setdefaults(:testing, :another => ["another $strtest", "testing"])
146
assert_equal("another #{val}", c[:another])
153
assert_nothing_raised {
154
c.setdefaults(:testing, :parentdir => [parent, "booh"])
157
assert_nothing_raised {
158
c.setdefaults(:testing, :child => ["$parent/child", "rah"])
161
assert_equal(parent, c[:parentdir])
162
assert_equal("/puppet/child", File.join(c[:parentdir], "child"))
167
initial = "an initial value"
168
assert_raise(Puppet::Error) {
169
c[:yayness] = initial
172
default = "this is a default"
173
assert_nothing_raised {
174
c.setdefaults(:testing, :yayness => [default, "rah"])
177
assert_equal(default, c[:yayness])
179
assert_nothing_raised {
180
c[:yayness] = initial
183
assert_equal(initial, c[:yayness])
185
assert_nothing_raised {
189
assert_equal(default, c[:yayness])
191
assert_nothing_raised {
192
c[:yayness] = "not default"
194
assert_equal("not default", c[:yayness])
210
attr3 = $attrdir/other
214
File.open(file, "w") { |f| f.puts text }
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"]
226
assert_nothing_raised {
227
c.setdefaults("section1",
228
:attr => ["a", "one"],
229
:attrdir => ["/another/dir", "two"],
230
:attr3 => ["$attrdir/maybe", "boo"]
234
assert_nothing_raised {
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])
244
assert_nothing_raised {
245
elem = c.element(:attr3)
249
assert_equal("puppet", elem.owner)
252
assert_nothing_raised {
256
assert_nothing_raised("Could not create transportable config") {
261
Puppet::Type.allclear
263
Puppet::Type.allclear
265
Puppet::Type.allclear
266
check_to_transportable(c)
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"]
282
:onboolean => [true, false],
283
:offboolean => [true, false],
284
:string => ["one string", "another string"],
285
:file => %w{/a/file /another/file}
287
data.each { |param, values|
295
opt = "--no-%s" % param
302
assert_nothing_raised("Could not handle arg %s with value %s" %
305
c.handlearg(opt, arg)
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"]
326
c.each { |param, obj|
328
assert(options.find { |ary|
330
}, "Argument %s was not added" % opt)
333
o = "--no-%s" % param
334
assert(options.find { |ary|
336
}, "Boolean off %s was not added" % o)
345
file = "$mydir/myfile"
346
realfile = File.join(dir, "myfile")
347
otherfile = File.join(dir, "otherfile")
349
assert_nothing_raised {
350
c.setdefaults(section,
351
:mydir => [dir, "A dir arg"],
353
:default => "$mydir/otherfile",
355
:desc => "A file arg"
357
:myfile => [file, "A file arg"]
361
assert_nothing_raised("Could not use a section") {
365
assert_nothing_raised("Could not reuse a section") {
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")
374
def test_setdefaultsarray
377
assert_nothing_raised {
379
:a => [false, "some value"],
380
:b => ["/my/file", "a file"]
384
assert_equal(false, c[:a], "Values are not equal")
385
assert_equal("/my/file", c[:b], "Values are not equal")
388
def test_setdefaultshash
391
assert_nothing_raised {
393
:a => {:default => false, :desc => "some value"},
394
:b => {:default => "/my/file", :desc => "a file"}
398
assert_equal(false, c[:a], "Values are not equal")
399
assert_equal("/my/file", c[:b], "Values are not equal")
407
assert_nothing_raised {
408
c.setdefaults(section,
409
:myfile => {:default => file, :create => true}
413
assert_nothing_raised("Could not use a section") {
417
assert(FileTest.exists?(file), "Did not create file")
419
assert(! Puppet::Type.type(:file)[file], "File obj still exists")
424
assert(FileTest.exists?(file), "Did not create file")
432
assert_nothing_raised {
433
c.setdefaults(section,
434
:mkusers => [false, "yay"],
445
assert_nothing_raised {
446
comp = c.to_component
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)
456
comp.each { |o| o.remove }
460
assert_nothing_raised {
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."
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."
474
def test_notmanagingdev
476
path = "/dev/testing"
485
assert_nothing_raised {
489
assert(! Puppet.type(:file)["/dev/testing"], "Created dev file")
492
def test_groupsetting
497
File.open(cfile, "w") do |f|
498
f.puts "[#{Puppet.name}]
504
config.setdefaults(Puppet.name, :group => ["puppet", "a group"])
506
assert_nothing_raised {
510
assert_equal(group, config[:group], "Group did not take")
513
# provide a method to modify and create files w/out specifying the info
514
# already stored in a config
515
def test_writingfiles
521
args = { :default => path, :mode => mode }
524
group = nonrootgroup()
526
if Puppet::SUIDManager.uid == 0
527
args[:owner] = user.name
528
args[:group] = group.name
531
config.setdefaults(:testing, :myfile => args)
533
assert_nothing_raised {
534
config.write(:myfile) do |file|
539
assert_equal(mode, filemode(path), "Modes are not equal")
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")
545
case Facter["operatingsystem"].value
546
when /BSD/, "Darwin": # nothing
548
assert_equal(group.gid, File.stat(path).gid, "GIDS are not equal")
559
args = { :default => path, :mode => mode }
562
group = nonrootgroup()
564
if Puppet::SUIDManager.uid == 0
565
args[:owner] = user.name
566
args[:group] = group.name
569
config.setdefaults(:testing, :mydir => args)
571
assert_nothing_raised {
575
assert_equal(mode, filemode(path), "Modes are not equal")
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")
582
case Facter["operatingsystem"].value
583
when /BSD/, "Darwin": # nothing
585
assert_equal(group.gid, File.stat(path).gid, "GIDS are not equal")
590
def test_booleans_and_integers
592
config.setdefaults(:mysection,
593
:booltest => [false, "yay"],
594
:inttest => [14, "yay"]
599
File.open(file, "w") do |f|
607
assert_nothing_raised {
611
assert_equal(true, config[:booltest], "Boolean was not converted")
612
assert_equal(27, config[:inttest], "Integer was not converted")
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")
621
# Make sure that tags are ignored when configuring
622
def test_configs_ignore_tags
626
config.setdefaults(:mysection,
627
:mydir => [file, "a file"]
630
Puppet[:tags] = "yayness"
632
assert_nothing_raised {
633
config.use(:mysection)
636
assert(FileTest.directory?(file), "Directory did not get created")
638
assert_equal("yayness", Puppet[:tags],
639
"Tags got changed during config")
642
def test_configs_replace_in_url
645
config.setdefaults(:mysection, :name => ["yayness", "yay"])
646
config.setdefaults(:mysection, :url => ["http://$name/rahness", "yay"])
649
assert_nothing_raised {
653
assert_equal("http://yayness/rahness", val,
654
"Config got messed up")
657
def test_correct_type_assumptions
660
file = Puppet::Config::CFile
661
element = Puppet::Config::CElement
662
bool = Puppet::Config::CBoolean
664
# We have to keep these ordered, unfortunately.
666
["/this/is/a/file", file],
671
["http://$server/yay", element],
672
["$server/yayness", file],
673
["$server/yayness.conf", file]
677
assert_nothing_raised {
678
elem = config.newelement(
685
assert_instance_of(type, elem,
686
"%s got created as wrong type" % value.inspect)
690
# Make sure we correctly reparse our config files but don't lose CLI values.
692
Puppet[:filetimeout] = 0
695
config.setdefaults(:mysection, :default => ["default", "yay"])
696
config.setdefaults(:mysection, :clichange => ["clichange", "yay"])
697
config.setdefaults(:mysection, :filechange => ["filechange", "yay"])
700
# Set one parameter in the file
701
File.open(file, "w") { |f|
702
f.puts %{[mysection]\nfilechange = filevalue}
704
assert_nothing_raised {
708
# Set another "from the cli"
709
assert_nothing_raised {
710
config.handlearg("clichange", "clivalue")
713
# And leave the other unset
714
assert_equal("default", config[:default])
715
assert_equal("filevalue", config[:filechange])
716
assert_equal("clivalue", config[:clichange])
718
# Now rewrite the file
719
File.open(file, "w") { |f|
720
f.puts %{[mysection]\nfilechange = newvalue}
724
cfile.send("tstamp=".intern, Time.now - 50)
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])
732
def test_parse_removes_quotes
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"])
740
# Set one parameter in the file
741
File.open(file, "w") { |f|
742
f.puts %{[mysection]\n
750
assert_nothing_raised {
754
%w{singleq doubleq none}.each do |p|
755
assert_equal("one", config[p], "%s did not match" % p)
757
assert_equal('mid"quote', config["middle"], "middle did not match")
761
Puppet[:filetimeout] = 0.1
762
origpath = tempfile()
764
config.setdefaults(:mysection, :paramdir => [tempfile(), "yay"])
767
# Set one parameter in the file
768
File.open(file, "w") { |f|
769
f.puts %{[mysection]\n
770
paramdir = #{origpath}
774
assert_nothing_raised {
776
config.use(:mysection)
779
assert(FileTest.directory?(origpath), "dir did not get created")
781
# Now start the timer
782
assert_nothing_raised {
783
EventLoop.current.monitor_timer config.timer
788
File.open(file, "w") { |f|
789
f.puts %{[mysection]\n
790
paramdir = #{newpath}
793
config.file.send("tstamp=".intern, Time.now - 50)
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")
803
# Test that config parameters correctly call passed-in blocks when the value
810
assert_nothing_raised do
811
elem = config.newelement :default => "yay",
814
:hook => proc { |value| testing = value }
817
assert_nothing_raised do
818
assert_equal("yay", elem.value)
821
assert_nothing_raised do
822
elem.value = "yaytest"
825
assert_nothing_raised do
826
assert_equal("yaytest", elem.value)
828
assert_equal("yaytest", testing)
830
assert_nothing_raised do
831
elem.value = "another"
834
assert_nothing_raised do
835
assert_equal("another", elem.value)
837
assert_equal("another", testing)
839
# Now verify it works from setdefault
840
assert_nothing_raised do
841
config.setdefaults :test,
844
:hook => proc { |v| testing = v }
848
assert_equal("yay", config[:blocktest2])
850
assert_nothing_raised do
851
config[:blocktest2] = "footest"
853
assert_equal("footest", config[:blocktest2])
854
assert_equal("footest", testing)
857
def test_no_modify_root
859
config.setdefaults(:yay,
860
:mydir => {:default => tempfile(),
866
:mkusers => [false, "yay"]
869
assert_nothing_raised do
873
# Now enable it so they'll be added
874
config[:mkusers] = true
876
comp = config.to_component
878
Puppet::Type.type(:user).each do |u|
879
assert(u.name != "root", "Tried to manage root user")
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")
886
# assert(yay, "Did not find yay component")
890
# assert(! yay.find { |o| o.class.name == :user and o.name == "root" },
892
# assert(! yay.find { |o| o.class.name == :group and o.name == "root" },
893
# "Found root group")
897
# $Id: config.rb 1852 2006-11-10 06:01:03Z luke $