summaryrefslogtreecommitdiffstats
path: root/test/util/utiltest.rb
blob: a66b175e575d991bbee325fb7a4ebea410d57d82 (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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../lib/puppettest'

require 'puppettest'
require 'mocha'

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

    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

    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 #{var} did not take")
        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 #{var} did not take")
        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"])
        end
        assert_equal("yay'test\nfuntest\n", output)

        # Now make sure we can squelch output (#565)
        assert_nothing_raised do
            output = Puppet::Util.execute([command, "yay'test", "funtest"], :squelch => true)
        end
        assert_equal(nil, 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"], :failonfail => 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], :uid => user.name, :gid => 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

        # (#565) Test the case of patricide.
        patricidecommand = tempfile()
        File.open(patricidecommand, "w") { |f|
            f.puts %{#!/bin/bash\n/bin/bash -c 'kill -TERM \$PPID' &;\n while [ 1 ]; do echo -n ''; done;\n}
        }
        File.chmod(0755, patricidecommand)
        assert_nothing_raised do
            output = Puppet::Util.execute([patricidecommand], :squelch => true)
        end
        assert_equal(nil, output)
        # See what happens if we try and read the pipe to the command...
        assert_raise(Puppet::ExecutionFailure) do
            output = Puppet::Util.execute([patricidecommand])
        end
        assert_nothing_raised do
            output = Puppet::Util.execute([patricidecommand], :failonfail => false)
        end
    end

    def test_lang_environ_in_execute
        orig_lang = ENV["LANG"]
        orig_lc_all = ENV["LC_ALL"]
        orig_lc_messages = ENV["LC_MESSAGES"]
        orig_language = ENV["LANGUAGE"]

        cleanup do
            ENV["LANG"] = orig_lang
            ENV["LC_ALL"] = orig_lc_all
            ENV["LC_MESSAGES"] = orig_lc_messages
            ENV["LANGUAGE"] = orig_lc_messages
        end

        # Mmm, we love gettext(3)
        ENV["LANG"] = "en_US"
        ENV["LC_ALL"] = "en_US"
        ENV["LC_MESSAGES"] = "en_US"
        ENV["LANGUAGE"] = "en_US"

        %w{LANG LC_ALL LC_MESSAGES LANGUAGE}.each do |env|

            assert_equal(
                'C',
                    Puppet::Util.execute(['ruby', '-e', "print ENV['#{env}']"]),

                    "Environment var #{env} wasn't set to 'C'")

            assert_equal 'en_US', ENV[env], "Environment var #{env} not set back correctly"
        end

    end

    # Check whether execute() accepts strings in addition to arrays.
    def test_string_exec
        cmd = "/bin/echo howdy"
        output = nil
        assert_raise(ArgumentError) {
            output = Puppet::Util.execute(cmd)
        }
        #assert_equal("howdy\n", output)
        #assert_raise(RuntimeError) {
        #    Puppet::Util.execute(cmd, 0, 0)
        #}
    end
end