pairing with luke, nagios_command provider skeleton
[vinpup.git] / test / util / utiltest.rb
blob35e1446a1ba7649553ba5cf1daf050ee8a55a5e1
1 #!/usr/bin/env ruby
3 require File.dirname(__FILE__) + '/../lib/puppettest'
5 require 'puppettest'
6 require 'mocha'
8 class TestPuppetUtil < Test::Unit::TestCase
9     include PuppetTest
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
14     def test_multiwrite
15         file = tempfile()
16         File.open(file, "w") { |f| f.puts "starting" }
18         value = {:a => :b}
19         threads = []
20         sync = Sync.new
21         9.times { |a|
22             threads << Thread.new {
23                 9.times { |b|
24                     assert_nothing_raised {
25                         sync.synchronize(Sync::SH) {
26                             Puppet::Util.readlock(file) { |f|
27                                 f.read
28                             }
29                         }
30                         sleep 0.01
31                         sync.synchronize(Sync::EX) {
32                             Puppet::Util.writelock(file) { |f|
33                                 f.puts "%s %s" % [a, b]
34                             }
35                         }
36                     }
37                 }
38             }
39         }
40         threads.each { |th| th.join }
41     end
44     def test_withumask
45         oldmask = File.umask
47         path = tempfile()
49         # FIXME this fails on FreeBSD with a mode of 01777
50         Puppet::Util.withumask(000) do
51             Dir.mkdir(path, 0777)
52         end
54         assert(File.stat(path).mode & 007777 == 0777, "File has the incorrect mode")
55         assert_equal(oldmask, File.umask, "Umask was not reset")
56     end
58     def test_benchmark
59         path = tempfile()
60         str = "yayness"
61         File.open(path, "w") do |f| f.print "yayness" end
63         # First test it with the normal args
64         assert_nothing_raised do
65             val = nil
66             result = Puppet::Util.benchmark(:notice, "Read file") do
67                 val = File.read(path)
68             end
70             assert_equal(str, val)
72             assert_instance_of(Float, result)
74         end
76         # Now test it with a passed object
77         assert_nothing_raised do
78             val = nil
79             Puppet::Util.benchmark(Puppet, :notice, "Read file") do
80                 val = File.read(path)
81             end
83             assert_equal(str, val)
84         end
85     end
87     def test_proxy
88         klass = Class.new do
89             attr_accessor :hash
90             class << self
91                 attr_accessor :ohash
92             end
93         end
94         klass.send(:include, Puppet::Util)
96         klass.ohash = {}
98         inst = klass.new
99         inst.hash = {}
100         assert_nothing_raised do
101             Puppet::Util.proxy klass, :hash, "[]", "[]=", :clear, :delete
102         end
104         assert_nothing_raised do
105             Puppet::Util.classproxy klass, :ohash, "[]", "[]=", :clear, :delete
106         end
108         assert_nothing_raised do
109             inst[:yay] = "boo"
110             inst["cool"] = :yayness
111         end
113         [:yay, "cool"].each do |var|
114             assert_equal(inst.hash[var], inst[var],
115                         "Var %s did not take" % var)
116         end
118         assert_nothing_raised do
119             klass[:Yay] = "boo"
120             klass["Cool"] = :yayness
121         end
123         [:Yay, "Cool"].each do |var|
124             assert_equal(inst.hash[var], inst[var],
125                         "Var %s did not take" % var)
126         end
127     end
129     def test_symbolize
130         ret = nil
131         assert_nothing_raised {
132             ret = Puppet::Util.symbolize("yayness")
133         }
135         assert_equal(:yayness, ret)
137         assert_nothing_raised {
138             ret = Puppet::Util.symbolize(:yayness)
139         }
141         assert_equal(:yayness, ret)
143         assert_nothing_raised {
144             ret = Puppet::Util.symbolize(43)
145         }
147         assert_equal(43, ret)
149         assert_nothing_raised {
150             ret = Puppet::Util.symbolize(nil)
151         }
153         assert_equal(nil, ret)
154     end
155     
156     def test_execute
157         command = tempfile()
158         File.open(command, "w") { |f|
159             f.puts %{#!/bin/sh\n/bin/echo "$1">&1; echo "$2">&2}
160         }
161         File.chmod(0755, command)
162         output = nil
163         assert_nothing_raised do
164             output = Puppet::Util.execute([command, "yaytest", "funtest"])
165         end
166         assert_equal("yaytest\nfuntest\n", output)
167         
168         # Now try it with a single quote
169         assert_nothing_raised do
170             output = Puppet::Util.execute([command, "yay'test", "funtest"])
171         end
172         assert_equal("yay'test\nfuntest\n", output)
173         
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)
177         end
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"])
183         end
184         
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)
188         end
189         
190         if Process.uid == 0
191             # Make sure we correctly set our uid and gid
192             user = nonrootuser
193             group = nonrootgroup
194             file = tempfile()
195             assert_nothing_raised do
196                 Puppet::Util.execute(["touch", file], :uid => user.name, :gid => group.name)
197             end
198             assert(FileTest.exists?(file), "file was not created")
199             assert_equal(user.uid, File.stat(file).uid, "uid was not set correctly")
200             
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")
205         end
206         
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}
211         }
212         File.chmod(0755, patricidecommand)
213         assert_nothing_raised do
214             output = Puppet::Util.execute([patricidecommand], :squelch => true)
215         end
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])
220         end
221         assert_nothing_raised do
222             output = Puppet::Util.execute([patricidecommand], :failonfail => false)
223         end
224     end
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"]
232       cleanup do
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
237       end
238       
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|
246         assert_equal('C',
247                      Puppet::Util.execute(['ruby', '-e', "print ENV['#{env}']"]),
248                      "Environment var #{env} wasn't set to 'C'")
249         
250         assert_equal 'en_US', ENV[env], "Environment var #{env} not set back correctly"
251       end
253     end
254     
255     # Check whether execute() accepts strings in addition to arrays.
256     def test_string_exec
257         cmd = "/bin/echo howdy"
258         output = nil
259         assert_raise(ArgumentError) {
260             output = Puppet::Util.execute(cmd)
261         }
262         #assert_equal("howdy\n", output)
263         #assert_raise(RuntimeError) {
264         #    Puppet::Util.execute(cmd, 0, 0)
265         #}
266     end
268     # This is mostly to test #380.
269     def test_get_provider_value
270         group = Puppet::Type.type(:group).create :name => "yayness", :ensure => :present
271         
272         root = Puppet::Type.type(:user).create :name => "root", :ensure => :present
273         
274         val = nil
275         assert_nothing_raised do
276             val = Puppet::Util.get_provider_value(:group, :gid, "yayness")
277         end
278         assert_nil(val, "returned a value on a missing group")
279         
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")
283         end
284         assert_equal(0, val, "got invalid uid for root")
285     end