3 require File.dirname(__FILE__) + '/../lib/puppettest'
8 class TestPuppetUtil < Test::Unit::TestCase
11 # we're getting corrupt files, probably because multiple processes
12 # are reading or writing the file at once
13 # so we need to test that
16 File.open(file, "w") { |f| f.puts "starting" }
22 threads << Thread.new {
24 assert_nothing_raised {
25 sync.synchronize(Sync::SH) {
26 Puppet::Util.readlock(file) { |f|
31 sync.synchronize(Sync::EX) {
32 Puppet::Util.writelock(file) { |f|
33 f.puts "%s %s" % [a, b]
40 threads.each { |th| th.join }
49 # FIXME this fails on FreeBSD with a mode of 01777
50 Puppet::Util.withumask(000) do
54 assert(File.stat(path).mode & 007777 == 0777, "File has the incorrect mode")
55 assert_equal(oldmask, File.umask, "Umask was not reset")
61 File.open(path, "w") do |f| f.print "yayness" end
63 # First test it with the normal args
64 assert_nothing_raised do
66 result = Puppet::Util.benchmark(:notice, "Read file") do
70 assert_equal(str, val)
72 assert_instance_of(Float, result)
76 # Now test it with a passed object
77 assert_nothing_raised do
79 Puppet::Util.benchmark(Puppet, :notice, "Read file") do
83 assert_equal(str, val)
94 klass.send(:include, Puppet::Util)
100 assert_nothing_raised do
101 Puppet::Util.proxy klass, :hash, "[]", "[]=", :clear, :delete
104 assert_nothing_raised do
105 Puppet::Util.classproxy klass, :ohash, "[]", "[]=", :clear, :delete
108 assert_nothing_raised do
110 inst["cool"] = :yayness
113 [:yay, "cool"].each do |var|
114 assert_equal(inst.hash[var], inst[var],
115 "Var %s did not take" % var)
118 assert_nothing_raised do
120 klass["Cool"] = :yayness
123 [:Yay, "Cool"].each do |var|
124 assert_equal(inst.hash[var], inst[var],
125 "Var %s did not take" % var)
131 assert_nothing_raised {
132 ret = Puppet::Util.symbolize("yayness")
135 assert_equal(:yayness, ret)
137 assert_nothing_raised {
138 ret = Puppet::Util.symbolize(:yayness)
141 assert_equal(:yayness, ret)
143 assert_nothing_raised {
144 ret = Puppet::Util.symbolize(43)
147 assert_equal(43, ret)
149 assert_nothing_raised {
150 ret = Puppet::Util.symbolize(nil)
153 assert_equal(nil, ret)
158 File.open(command, "w") { |f|
159 f.puts %{#!/bin/sh\n/bin/echo "$1">&1; echo "$2">&2}
161 File.chmod(0755, command)
163 assert_nothing_raised do
164 output = Puppet::Util.execute([command, "yaytest", "funtest"])
166 assert_equal("yaytest\nfuntest\n", output)
168 # Now try it with a single quote
169 assert_nothing_raised do
170 output = Puppet::Util.execute([command, "yay'test", "funtest"])
172 assert_equal("yay'test\nfuntest\n", output)
174 # Now make sure we can squelch output (#565)
175 assert_nothing_raised do
176 output = Puppet::Util.execute([command, "yay'test", "funtest"], :squelch => true)
178 assert_equal(nil, output)
180 # Now test that we correctly fail if the command returns non-zero
181 assert_raise(Puppet::ExecutionFailure) do
182 out = Puppet::Util.execute(["touch", "/no/such/file/could/exist"])
185 # And that we can tell it not to fail
186 assert_nothing_raised() do
187 out = Puppet::Util.execute(["touch", "/no/such/file/could/exist"], :failonfail => false)
191 # Make sure we correctly set our uid and gid
195 assert_nothing_raised do
196 Puppet::Util.execute(["touch", file], :uid => user.name, :gid => group.name)
198 assert(FileTest.exists?(file), "file was not created")
199 assert_equal(user.uid, File.stat(file).uid, "uid was not set correctly")
201 # We can't really check the gid, because it just behaves too
202 # inconsistently everywhere.
203 # assert_equal(group.gid, File.stat(file).gid,
204 # "gid was not set correctly")
207 # (#565) Test the case of patricide.
208 patricidecommand = tempfile()
209 File.open(patricidecommand, "w") { |f|
210 f.puts %{#!/bin/bash\n/bin/bash -c 'kill -TERM \$PPID' &;\n while [ 1 ]; do echo -n ''; done;\n}
212 File.chmod(0755, patricidecommand)
213 assert_nothing_raised do
214 output = Puppet::Util.execute([patricidecommand], :squelch => true)
216 assert_equal(nil, output)
217 # See what happens if we try and read the pipe to the command...
218 assert_raise(Puppet::ExecutionFailure) do
219 output = Puppet::Util.execute([patricidecommand])
221 assert_nothing_raised do
222 output = Puppet::Util.execute([patricidecommand], :failonfail => false)
226 def test_lang_environ_in_execute
227 orig_lang = ENV["LANG"]
228 orig_lc_all = ENV["LC_ALL"]
229 orig_lc_messages = ENV["LC_MESSAGES"]
230 orig_language = ENV["LANGUAGE"]
233 ENV["LANG"] = orig_lang
234 ENV["LC_ALL"] = orig_lc_all
235 ENV["LC_MESSAGES"] = orig_lc_messages
236 ENV["LANGUAGE"] = orig_lc_messages
239 # Mmm, we love gettext(3)
240 ENV["LANG"] = "en_US"
241 ENV["LC_ALL"] = "en_US"
242 ENV["LC_MESSAGES"] = "en_US"
243 ENV["LANGUAGE"] = "en_US"
245 %w{LANG LC_ALL LC_MESSAGES LANGUAGE}.each do |env|
247 Puppet::Util.execute(['ruby', '-e', "print ENV['#{env}']"]),
248 "Environment var #{env} wasn't set to 'C'")
250 assert_equal 'en_US', ENV[env], "Environment var #{env} not set back correctly"
255 # Check whether execute() accepts strings in addition to arrays.
257 cmd = "/bin/echo howdy"
259 assert_raise(ArgumentError) {
260 output = Puppet::Util.execute(cmd)
262 #assert_equal("howdy\n", output)
263 #assert_raise(RuntimeError) {
264 # Puppet::Util.execute(cmd, 0, 0)
268 # This is mostly to test #380.
269 def test_get_provider_value
270 group = Puppet::Type.type(:group).create :name => "yayness", :ensure => :present
272 root = Puppet::Type.type(:user).create :name => "root", :ensure => :present
275 assert_nothing_raised do
276 val = Puppet::Util.get_provider_value(:group, :gid, "yayness")
278 assert_nil(val, "returned a value on a missing group")
280 # Now make sure we get a value for one we know exists
281 assert_nothing_raised do
282 val = Puppet::Util.get_provider_value(:user, :uid, "root")
284 assert_equal(0, val, "got invalid uid for root")