summaryrefslogtreecommitdiffstats
path: root/test/server/pelement.rb
blob: 1a645876804adf69fe8db9883d27e7c70ef2bc20 (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
if __FILE__ == $0
    $:.unshift '../../lib'
    $:.unshift '..'
    $puppetbase = "../.."
end

require 'puppet'
require 'puppet/server/pelement'
require 'test/unit'
require 'puppettest.rb'
require 'base64'
require 'cgi'

class TestPElementServer < Test::Unit::TestCase
	include ServerTest

    def verify_described(type, described)
        described.each do |name, trans|
            type.clear
            obj = nil
            assert_nothing_raised do
                obj = trans.to_type
            end

            assert(obj, "Could not create object")
            assert_nothing_raised do
                obj.retrieve
            end

            assert(obj.insync?, "Described %s[%s] is not in sync" %
                [trans.type, name])

            if trans.type == :package
                assert_equal(Puppet::Type.type(:package).default, obj[:type])
            end
        end
        type.clear
    end

    def test_describe_file
        # Make a file to describe
        file = tempfile()
        str = "yayness\n"

        server = nil

        assert_nothing_raised do
            server = Puppet::Server::PElement.new()
        end

        # The first run we create the file on the copy, the second run
        # the file is already there so the object should be in sync
        2.times do |i|
            [   [nil],
                [[:content, :mode], []],
                [[], [:content]],
                [[:content], [:mode]]
            ].each do |ary|
                retrieve = ary[0] || []
                ignore = ary[1] || []

                File.open(file, "w") { |f| f.print str }

                result = nil
                assert_nothing_raised do
                    result = server.describe("file", file, *ary)
                end

                assert(result, "Could not retrieve file information")

                assert_instance_of(Puppet::TransObject, result)

                # Now we have to clear, so that the server's object gets removed
                Puppet::Type.type(:file).clear

                # And remove the file, so we can verify it gets recreated
                if i == 0
                    File.unlink(file)
                end

                object = nil
                assert_nothing_raised do
                    object = result.to_type
                end

                assert(object, "Could not create type")

                retrieve.each do |state|
                    assert(object.should(state), "Did not retrieve %s" % state)
                end

                ignore.each do |state|
                    assert(! object.should(state), "Incorrectly retrieved %s" % state)
                end

                if i == 0
                    assert_events([:file_created], object)
                else
                    assert_nothing_raised {
                        object.retrieve
                    }
                    assert(object.insync?, "Object was not in sync")
                end

                assert(FileTest.exists?(file), "File did not get recreated")

                if i == 0
                if object.should(:content)
                    assert_equal(str, File.read(file), "File contents are not the same")
                else
                    assert_equal("", File.read(file), "File content was incorrectly made")
                end
                end
                if FileTest.exists? file
                    File.unlink(file)
                end
            end
        end
    end

    def test_describe_directory
        # Make a file to describe
        file = tempfile()

        server = nil

        assert_nothing_raised do
            server = Puppet::Server::PElement.new()
        end

        [   [nil],
            [[:ensure, :checksum, :mode], []],
            [[], [:checksum]],
            [[:ensure, :checksum], [:mode]]
        ].each do |ary|
            retrieve = ary[0] || []
            ignore = ary[1] || []

            Dir.mkdir(file)

            result = nil
            assert_nothing_raised do
                result = server.describe("file", file, *ary)
            end

            assert(result, "Could not retrieve file information")

            assert_instance_of(Puppet::TransObject, result)

            # Now we have to clear, so that the server's object gets removed
            Puppet::Type.type(:file).clear

            # And remove the file, so we can verify it gets recreated
            Dir.rmdir(file)

            object = nil
            assert_nothing_raised do
                object = result.to_type
            end

            assert(object, "Could not create type")

            retrieve.each do |state|
                assert(object.should(state), "Did not retrieve %s" % state)
            end

            ignore.each do |state|
                assert(! object.should(state), "Incorrectly retrieved %s" % state)
            end

            assert_events([:directory_created], object)

            assert(FileTest.directory?(file), "Directory did not get recreated")
            Dir.rmdir(file)
        end
    end

    def test_describe_alltypes
        # Systems get pretty retarded, so I'm going to set the path to some fake
        # data for ports
        #Puppet::Type::ParsedType::Port.path = File.join($puppetbase,
        #    "test/data/types/ports/1")
        #Puppet.err Puppet::Type::ParsedType::Port.path
        server = nil
        assert_nothing_raised do
            server = Puppet::Server::PElement.new()
        end

        require 'etc'

        # Make the example schedules, for testing
        Puppet::Type.type(:schedule).mkdefaultschedules

        Puppet::Type.eachtype do |type|
            unless type.respond_to? :list
                Puppet.warning "%s does not respond to :list" % type.name
                next
            end
            #next unless type.name == :port
            Puppet.info "Describing each %s" % type.name

            # First do a listing from the server
            bucket = nil
            assert_nothing_raised {
                bucket = server.list(type.name)
            }

            #type.clear

            count = 0
            described = {}
            bucket.each do |obj|
                assert_instance_of(Puppet::TransObject, obj)
                break if count > 5
                described[obj.name] = server.describe(obj.type, obj.name)
                count += 1
            end

            verify_described(type, described)

            count = 0
            described = {}
            Puppet.info "listing again"
            type.list.each do |obj|
                assert_instance_of(type, obj)

                break if count > 5
                trans = nil
                assert_nothing_raised do
                    described[obj.name] = server.describe(type.name, obj.name)
                end

                count += 1
            end

            if described.empty?
                Puppet.notice "Got no example objects for %s" % type.name
            end

            # We separate these, in case the list operation creates objects
            verify_described(type, described)
        end
    end

    def test_apply
        server = nil
        assert_nothing_raised do
            server = Puppet::Server::PElement.new()
        end

        file = tempfile()
        str = "yayness\n"

        File.open(file, "w") { |f| f.print str }

        filetrans = nil
        assert_nothing_raised {
            filetrans = server.describe("file", file)
        }

        Puppet::Type.type(:file).clear

        Puppet.err filetrans[:parent].inspect

        #p filetrans

        bucket = Puppet::TransBucket.new
        bucket.type = "file"
        bucket.push filetrans

        #p bucket

        oldbucket = bucket.dup
        File.unlink(file)
        assert_nothing_raised {
            server.apply(bucket)
        }


        assert(FileTest.exists?(file), "File did not get recreated")

        # Now try it as a "nonlocal" server
        server.local = false
        yaml = nil
        assert_nothing_raised {
            yaml = Base64.encode64(YAML::dump(bucket))
        }

        Puppet::Type.type(:file).clear
        File.unlink(file)

        if Base64.decode64(yaml) =~ /(.{20}Loglevel.{20})/
            Puppet.warning "YAML is broken on this machine"
            return
        end
        #puts Base64.decode64(yaml)
        assert_nothing_raised("Could not reload yaml") {
            YAML::load(Base64.decode64(yaml))
        }

        assert_nothing_raised {
            server.apply(yaml)
        }
        assert(FileTest.exists?(file), "File did not get recreated from YAML")
    end
end

# $Id$