summaryrefslogtreecommitdiffstats
path: root/test/lib/support/parser.rb
blob: fcad0250199d4d578b49ea1ebc578993e57cd77d (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
332
333
334
335
336
337
338
339
340
341
342
343
344
module PuppetTestSupport
    module Parser
        AST = Puppet::Parser::AST

        def astarray(*args)
            AST::ASTArray.new(
                              :children => args
                             )
        end

        def classobj(name, args = {})
            args[:type] ||= nameobj(name)
            args[:code] ||= AST::ASTArray.new(
                                              :file => __FILE__,
                                              :line => __LINE__,
                                              :children => [
                                                  varobj("%svar" % name, "%svalue" % name),
                                                  fileobj("/%s" % name)
            ]
                                             )
                                             assert_nothing_raised("Could not create class %s" % name) {
                                                 return AST::ClassDef.new(args)
                                             }
        end

        def tagobj(*names)
            args = {}
            newnames = names.collect do |name|
                if name.is_a? AST
                    name
                else
                    nameobj(name)
                end
            end
            args[:type] = astarray(*newnames)
            assert_nothing_raised("Could not create tag %s" % names.inspect) {
                return AST::Tag.new(args)
            }
        end

        def compobj(name, args = {})
            args[:file] ||= tempfile()
            args[:line] ||= rand(100)
            args[:type] ||= nameobj(name)
            args[:args] ||= AST::ASTArray.new(
                                              :file => tempfile(),
                                              :line => rand(100),
                                              :children => []
                                             )
                                             args[:code] ||= AST::ASTArray.new(
                                                                               :file => tempfile(),
                                                                               :line => rand(100),
                                                                               :children => [
                                                                                   varobj("%svar" % name, "%svalue" % name),
                                                                                   fileobj("/%s" % name)
                                             ]
                                                                              )
                                                                              assert_nothing_raised("Could not create compdef %s" % name) {
                                                                                  return AST::CompDef.new(args)
                                                                              }
        end

        def objectdef(type, name, params)
            assert_nothing_raised("Could not create %s %s" % [type, name]) {
                return AST::ObjectDef.new(
                                          :file => __FILE__,
                                          :line => __LINE__,
                                          :name => stringobj(name),
                                          :type => nameobj(type),
                                          :params => objectinst(params)
                                         )
            }
        end

        def fileobj(path, hash = {"owner" => "root"})
            assert_nothing_raised("Could not create file %s" % path) {
                return objectdef("file", path, hash)
                #            return AST::ObjectDef.new(
                #                :file => tempfile(),
                #                :line => rand(100),
                #                :name => stringobj(path),
                #                :type => nameobj("file"),
                #                :params => objectinst(hash)
                #            )
            }
        end

        def nameobj(name)
            assert_nothing_raised("Could not create name %s" % name) {
                return AST::Name.new(
                                     :file => tempfile(),
                                     :line => rand(100),
                                     :value => name
                                    )
            }
        end

        def typeobj(name)
            assert_nothing_raised("Could not create type %s" % name) {
                return AST::Type.new(
                                     :file => tempfile(),
                                     :line => rand(100),
                                     :value => name
                                    )
            }
        end

        def nodedef(name)
            assert_nothing_raised("Could not create node %s" % name) {
                return AST::NodeDef.new(
                                        :file => tempfile(),
                                        :line => rand(100),
                                        :names => nameobj(name),
                                        :code => AST::ASTArray.new(
                                                                   :children => [
                                                                       varobj("%svar" % name, "%svalue" % name),
                                                                       fileobj("/%s" % name)
            ]
                                                                  )
                                       )
            }
        end

        def objectinst(hash)
            assert_nothing_raised("Could not create object instance") {
                params = hash.collect { |param, value|
                objectparam(param, value)
            }
            return AST::ObjectInst.new(
                                       :file => tempfile(),
                                       :line => rand(100),
                                       :children => params
                                      )
            }
        end

        def objectparam(param, value)
            # Allow them to pass non-strings in
            if value.is_a?(String)
                value = stringobj(value)
            end
            assert_nothing_raised("Could not create param %s" % param) {
                return AST::ObjectParam.new(
                                            :file => tempfile(),
                                            :line => rand(100),
                                            :param => nameobj(param),
                                            :value => value
                                           )
            }
        end

        def stringobj(value)
            AST::String.new(
                            :file => tempfile(),
                            :line => rand(100),
                            :value => value
                           )
        end

        def varobj(name, value)
            unless value.is_a? AST
                value = stringobj(value)
            end
            assert_nothing_raised("Could not create %s code" % name) {
                return AST::VarDef.new(
                                       :file => tempfile(),
                                       :line => rand(100),
                                       :name => nameobj(name),
                                       :value => value
                                      )
            }
        end

        def varref(name)
            assert_nothing_raised("Could not create %s variable" % name) {
                return AST::Variable.new(
                                         :file => __FILE__,
                                         :line => __LINE__,
                                         :value => name
                                        )
            }
        end

        def argobj(name, value)
            assert_nothing_raised("Could not create %s compargument" % name) {
                return AST::CompArgument.new(
                                             :children => [nameobj(name), stringobj(value)]
                                            )
            }
        end

        def defaultobj(type, params)
            pary = []
            params.each { |p,v|
                pary << AST::ObjectParam.new(
                                             :file => __FILE__,
                                             :line => __LINE__,
                                             :param => nameobj(p),
                                             :value => stringobj(v)
                                            )
            }
            past = AST::ASTArray.new(
                                     :file => __FILE__,
                                     :line => __LINE__,
                                     :children => pary
                                    )

                                    assert_nothing_raised("Could not create defaults for %s" % type) {
                                        return AST::TypeDefaults.new(
                                                                     :file => __FILE__,
                                                                     :line => __LINE__,
                                                                     :type => typeobj(type),
                                                                     :params => past
                                                                    )
                                    }
        end

        def taggedobj(name, ftype = :statement)
            functionobj("tagged", name, ftype)
        end

        def functionobj(function, name, ftype = :statement)
            func = nil
            assert_nothing_raised do
                func = Puppet::Parser::AST::Function.new(
                                                         :name => function,
                                                         :ftype => ftype,
                                                         :arguments => AST::ASTArray.new(
                                                                                         :children => [nameobj(name)]
                                                                                        )
                                                        )
            end

            return func
        end

        # This assumes no nodes
        def assert_creates(manifest, *files)
            interp = nil
            assert_nothing_raised {
                interp = Puppet::Parser::Interpreter.new(
                                                         :Manifest => manifest,
                                                         :UseNodes => false
                                                        )
            }

            config = nil
            assert_nothing_raised {
                config = interp.run(Facter["hostname"].value, {})
            }

            comp = nil
            assert_nothing_raised {
                comp = config.to_type
            }

            assert_apply(comp)

            files.each do |file|
                assert(FileTest.exists?(file), "Did not create %s" % file)
            end
        end

        def mk_transobject(file = "/etc/passwd")
            obj = nil
            assert_nothing_raised {
                obj = Puppet::TransObject.new("file", file)
                obj["owner"] = "root"
                obj["mode"] = "644"
            }

            return obj
        end

        def mk_transbucket(*objects)
            bucket = nil
            assert_nothing_raised {
                bucket = Puppet::TransBucket.new
                bucket.name = "yayname"
                bucket.type = "yaytype"
            }

            objects.each { |o| bucket << o }

            return bucket
        end

        # Make a tree of objects, yielding if desired
        def mk_transtree(depth = 4, width = 2)
            top = nil
            assert_nothing_raised {
                top = Puppet::TransBucket.new
                top.name = "top"
                top.type = "bucket"
            }

            bucket = top

            file = tempfile()
            depth.times do |i|
                objects = []
                width.times do |j|
                    path = tempfile + i.to_s
                    obj = Puppet::TransObject.new("file", path)
                    obj["owner"] = "root"
                    obj["mode"] = "644"

                    # Yield, if they want
                    if block_given?
                        yield(obj, i, j)
                    end

                    objects << obj
                end

                newbucket = mk_transbucket(*objects)

                bucket.push newbucket
                bucket = newbucket
            end

            return top
        end

        # Take a list of AST objects, evaluate them, and return the results
        def assert_evaluate(children)
            top = nil
            assert_nothing_raised("Could not create top object") {
                top = AST::ASTArray.new(
                                        :children => children
                                       )
            }

            trans = nil
            scope = nil
            assert_nothing_raised {
                scope = Puppet::Parser::Scope.new()
                trans = scope.evaluate(:ast => top)
            }

            return trans
        end
    end
end