summaryrefslogtreecommitdiffstats
path: root/test/util/utiltest.rb
blob: 3f2532fc258fcedba30c499aa578f64698eea682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env ruby

$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/

require 'puppet'
require 'puppettest'

class TestPuppetUtil < Test::Unit::TestCase
    include PuppetTest

    # we're getting corrupt files, probably because multiple processes
    # are reading or writing the file at once
    # so we need to test that
    def test_multiwrite
        file = tempfile()
        File.open(file, "w") { |f| f.puts "starting" }

        value = {:a => :b}
        threads = []
        sync = Sync.new
        9.times { |a|
            threads << Thread.new {
                9.times { |b|
                    assert_nothing_raised {
                        sync.synchronize(Sync::SH) {
                            Puppet::Util.readlock(file) { |f|
                                f.read
                            }
                        }
                        sleep 0.01
                        sync.synchronize(Sync::EX) {
                            Puppet::Util.writelock(file) { |f|
                                f.puts "%s %s" % [a, b]
                            }
                        }
                    }
                }
            }
        }
        threads.each { |th| th.join }
    end


    def test_withumask
        oldmask = File.umask

        path = tempfile()

        # FIXME this fails on FreeBSD with a mode of 01777
        Puppet::Util.withumask(000) do
            Dir.mkdir(path, 0777)
        end

        assert(File.stat(path).mode & 007777 == 0777, "File has the incorrect mode")
        assert_equal(oldmask, File.umask, "Umask was not reset")
    end

    def test_benchmark
        path = tempfile()
        str = "yayness"
        File.open(path, "w") do |f| f.print "yayness" end

        # First test it with the normal args
        assert_nothing_raised do
            val = nil
            result = Puppet::Util.benchmark(:notice, "Read file") do
                val = File.read(path)
            end

            assert_equal(str, val)

            assert_instance_of(Float, result)

        end

        # Now test it with a passed object
        assert_nothing_raised do
            val = nil
            Puppet::Util.benchmark(Puppet, :notice, "Read file") do
                val = File.read(path)
            end

            assert_equal(str, val)
        end
    end

    unless Puppet::SUIDManager.uid == 0
        $stderr.puts "Run as root to perform Utility tests"
        def test_nothing
        end
    else

    def mknverify(file, user, group = nil, id = false)
        if File.exists?(file)
            File.unlink(file)
        end
        args = []
        unless user or group
            args << nil
        end
        if user
            if id
                args << user.uid
            else
                args << user.name
            end
        end

        if group
            if id
                args << group.gid
            else
                args << group.name
            end
        end

        gid = nil
        if group
            gid = group.gid
        else
            gid = Puppet::SUIDManager.gid
        end

        uid = nil
        if user
            uid = user.uid
        else
            uid = Puppet::SUIDManager.uid
        end

        assert_nothing_raised {
            Puppet::SUIDManager.asuser(*args) {
                assert_equal(Puppet::SUIDManager.euid, uid, "UID is %s instead of %s" %
                    [Puppet::SUIDManager.euid, uid]
                )
                assert_equal(Puppet::SUIDManager.egid, gid, "GID is %s instead of %s" %
                    [Puppet::SUIDManager.egid, gid]
                )
                system("touch %s" % file)
            }
        }
        if uid == 0
            #Puppet.warning "Not testing user"
        else
            #Puppet.warning "Testing user %s" % uid
            assert(File.exists?(file), "File does not exist")
            assert_equal(File.stat(file).uid, uid,
                "File is owned by %s instead of %s" %
                [File.stat(file).uid, uid]
            )
            #system("ls -l %s" % file)
        end
        # I'm skipping these, because it seems so system dependent.
        #if gid == 0
        #    #Puppet.warning "Not testing group"
        #else
        #    Puppet.warning "Testing group %s" % gid.inspect
        #    system("ls -l %s" % file)
        #    assert_equal(gid, File.stat(file).gid,
        #        "File group is %s instead of %s" %
        #        [File.stat(file).gid, gid]
        #    )
        #end
        assert_nothing_raised {
            File.unlink(file)
        }
    end

    def test_asuser
        file = File.join(tmpdir, "asusertest")
        @@tmpfiles << file
        [
            [nil], # Nothing
            [nonrootuser()], # just user, by name
            [nonrootuser(), nil, true], # user, by uid
            [nonrootuser(), nonrootgroup()], # user and group, by name
            [nonrootuser(), nonrootgroup(), true], # user and group, by id
        ].each { |ary|
            mknverify(file, *ary)
        }
    end

    # Verify that we get reset back to the right user
    def test_asuser_recovery
        begin
            Puppet::Util.asuser(nonrootuser()) {
                raise "an error"
            }
        rescue
        end

        assert(Puppet::SUIDManager.euid == 0, "UID did not get reset")
    end
    end

    def test_proxy
        klass = Class.new do
            attr_accessor :hash
            class << self
                attr_accessor :ohash
            end
        end
        klass.send(:include, Puppet::Util)

        klass.ohash = {}

        inst = klass.new
        inst.hash = {}
        assert_nothing_raised do
            Puppet::Util.proxy klass, :hash, "[]", "[]=", :clear, :delete
        end

        assert_nothing_raised do
            Puppet::Util.classproxy klass, :ohash, "[]", "[]=", :clear, :delete
        end

        assert_nothing_raised do
            inst[:yay] = "boo"
            inst["cool"] = :yayness
        end

        [:yay, "cool"].each do |var|
            assert_equal(inst.hash[var], inst[var],
                        "Var %s did not take" % var)
        end

        assert_nothing_raised do
            klass[:Yay] = "boo"
            klass["Cool"] = :yayness
        end

        [:Yay, "Cool"].each do |var|
            assert_equal(inst.hash[var], inst[var],
                        "Var %s did not take" % var)
        end
    end

    def test_symbolize
        ret = nil
        assert_nothing_raised {
            ret = Puppet::Util.symbolize("yayness")
        }

        assert_equal(:yayness, ret)

        assert_nothing_raised {
            ret = Puppet::Util.symbolize(:yayness)
        }

        assert_equal(:yayness, ret)

        assert_nothing_raised {
            ret = Puppet::Util.symbolize(43)
        }

        assert_equal(43, ret)

        assert_nothing_raised {
            ret = Puppet::Util.symbolize(nil)
        }

        assert_equal(nil, ret)
    end
    
    def test_execute
        command = tempfile()
        File.open(command, "w") { |f|
            f.puts %{#!/bin/sh\n/bin/echo "$1">&1; echo "$2">&2}
        }
        File.chmod(0755, command)
        output = nil
        assert_nothing_raised do
            output = Puppet::Util.execute([command, "yaytest", "funtest"])
        end
        assert_equal("yaytest\nfuntest\n", output)
        
        # Now try it with a single quote
        assert_nothing_raised do
            output = Puppet::Util.execute([command, "yay'test", "funtest"])
            # output = Puppet::Util.execute(command)
            
        end
        assert_equal("yay'test\nfuntest\n", output)
        
        # Now test that we correctly fail if the command returns non-zero
        assert_raise(Puppet::ExecutionFailure) do
            out = Puppet::Util.execute(["touch", "/no/such/file/could/exist"])
        end
        
        # And that we can tell it not to fail
        assert_nothing_raised() do
            out = Puppet::Util.execute(["touch", "/no/such/file/could/exist"], false)
        end
        
        if Process.uid == 0
            # Make sure we correctly set our uid and gid
            user = nonrootuser
            group = nonrootgroup
            file = tempfile()
            assert_nothing_raised do
                Puppet::Util.execute(["touch", file], true, user.name, group.name)
            end
            assert(FileTest.exists?(file), "file was not created")
            assert_equal(user.uid, File.stat(file).uid, "uid was not set correctly")
            
            # We can't really check the gid, because it just behaves too inconsistently everywhere.
            # assert_equal(group.gid, File.stat(file).gid, "gid was not set correctly")
        end
    end
    
    # This is mostly to test #380.
    def test_get_provider_value
        group = Puppet::Type.type(:group).create :name => "yayness", :ensure => :present
        
        root = Puppet::Type.type(:user).create :name => "root", :ensure => :present
        
        val = nil
        assert_nothing_raised do
            val = Puppet::Util.get_provider_value(:group, :gid, "yayness")
        end
        assert_nil(val, "returned a value on a missing group")
        
        # Now make sure we get a value for one we know exists
        assert_nothing_raised do
            val = Puppet::Util.get_provider_value(:user, :uid, "root")
        end
        assert_equal(0, val, "got invalid uid for root")
    end
end

# $Id$