summaryrefslogtreecommitdiffstats
path: root/test/language/ast.rb
blob: 441c7d892fea62cf14b221c7201a6d3b91fd553d (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#!/usr/bin/ruby

if __FILE__ == $0
    $:.unshift '../../lib'
    $:.unshift '..'
    $puppetbase = "../.."
end

require 'puppet'
require 'puppet/parser/interpreter'
require 'puppet/parser/parser'
require 'puppet/client'
require 'test/unit'
require 'puppettest'

class TestAST < Test::Unit::TestCase
	include ParserTesting

    # Test that classes behave like singletons
    def test_classsingleton
        parent = child1 = child2 = nil
        children = []

        # create the parent class
        children << classobj("parent")

        # Create child class one
        children << classobj("child1", :parentclass => nameobj("parent"))

        # Create child class two
        children << classobj("child2", :parentclass => nameobj("parent"))

        # Now call the two classes
        assert_nothing_raised("Could not add AST nodes for calling") {
            children << AST::ObjectDef.new(
                :type => nameobj("child1"),
                :name => nameobj("yayness"),
                :params => astarray()
            )
            children << AST::ObjectDef.new(
                :type => nameobj("child2"),
                :name => nameobj("booness"),
                :params => astarray()
            )
        }

        top = nil
        assert_nothing_raised("Could not create top object") {
            top = AST::ASTArray.new(
                :children => children
            )
        }

        scope = nil
        assert_nothing_raised("Could not evaluate") {
            scope = Puppet::Parser::Scope.new()
            objects = top.evaluate(:scope => scope)
        }

        assert_equal(1, scope.find_all { |child|
            child.lookupobject(:name => "/parent", :type => "file")
        }.length, "Found incorrect number of '/parent' objects")
    end

    # Test that 'setobject' collects all of an object's parameters and stores
    # them in one TransObject, rather than many.  This is probably a bad idea.
    def test_setobject
        top = nil
        children = [
            fileobj("/etc", "owner" => "root"),
            fileobj("/etc", "group" => "root")
        ]
        assert_nothing_raised("Could not create top object") {
            top = AST::ASTArray.new(
                :children => children
            )
        }

        scope = Puppet::Parser::Scope.new()
        assert_nothing_raised("Could not evaluate") {
            top.evaluate(:scope => scope)
        }

        obj = nil
        assert_nothing_raised("Could not retrieve file object") {
            obj = scope.lookupobject(:name => "/etc", :type => "file")
        }

        assert(obj, "could not retrieve file object")

        %w{owner group}.each { |param|
            assert(obj.include?(param), "Object did not include %s" % param)
        }

    end

    # Verify that objects can only have parents of the same type.
    def test_validparent
        parent = child1 = nil
        children = []

        # create the parent class
        children << compobj("parent", :args => AST::ASTArray.new(:children => []))

        # Create child class one
        children << classobj("child1", :parentclass => nameobj("parent"))

        # Now call the two classes
        assert_nothing_raised("Could not add AST nodes for calling") {
            children << AST::ObjectDef.new(
                :type => nameobj("child1"),
                :name => nameobj("yayness"),
                :params => astarray()
            )
        }

        top = nil
        assert_nothing_raised("Could not create top object") {
            top = AST::ASTArray.new(
                :children => children
            )
        }

        scope = nil
        assert_raise(Puppet::ParseError, "Invalid parent type was allowed") {
            scope = Puppet::Parser::Scope.new()
            objects = top.evaluate(:scope => scope)
        }
    end

    # Verify that classes are correctly defined in node scopes.
    def test_nodeclasslookup
        parent = child1 = nil
        children = []

        # create the parent class
        children << classobj("parent")

        # Create child class one
        children << classobj("child1", :parentclass => nameobj("parent"))

        # Now call the two classes
        assert_nothing_raised("Could not add AST nodes for calling") {
            children << AST::ObjectDef.new(
                :type => nameobj("child1"),
                :name => nameobj("yayness"),
                :params => astarray()
            )
        }

        # create the node
        nodename = "mynodename"
        node = nil
        assert_nothing_raised("Could not create parent object") {
            node = AST::NodeDef.new(
                :names => nameobj(nodename),
                :code => AST::ASTArray.new(
                    :children => children
                )
            )
        }

        # Create the wrapper object
        top = nil
        assert_nothing_raised("Could not create top object") {
            top = AST::ASTArray.new(
                :children => [node]
            )
        }

        # Evaluate the parse tree
        scope = nil
        assert_nothing_raised("Could not evaluate node") {
            scope = Puppet::Parser::Scope.new()
            top.evaluate(:scope => scope)
        }

        # Verify that, well, nothing really happened, and especially verify
        # that the top scope is not a node scope
        assert(scope.topscope?, "Scope is not top scope")
        assert(! scope.nodescope?, "Scope is mistakenly node scope")
        assert(! scope.lookupclass("parent"), "Found parent class in top scope")

        # verify we can find our node
        assert(scope.node(nodename), "Could not find node")

        # And verify that we can evaluate it okay
        objects = nil
        assert_nothing_raised("Could not retrieve node definition") {
            objects = scope.evalnode(:name => [nodename], :facts => {})
        }

        assert(objects, "Could not retrieve node definition")

        # Because node scopes are temporary (i.e., they get destroyed after the node's
        # config is returned) we should not be able to find the node scope.
        nodescope = nil
        assert_nothing_raised {
            nodescope = scope.find { |child|
                child.nodescope?
            }
        }

        assert_nil(nodescope, "Found nodescope")

        # And now verify again that the top scope cannot find the node's definition
        # of the parent class
        assert(! scope.lookupclass("parent"), "Found parent class in top scope")

        trans = nil
        # Verify that we can evaluate the node twice
        assert_nothing_raised("Could not retrieve node definition") {
            trans = scope.evalnode(:name => [nodename], :facts => {})
        }

        objects = nil
        assert_nothing_raised("Could not convert to objects") {
            objects = trans.to_type
        }

        Puppet.type(:file).each { |obj|
            assert(obj.path !~ /#{nodename}\[#{nodename}\]/,
                "Node name appears twice")
        }

        assert(Puppet::Type.type(:file)["/child1"], "Could not find child")
        assert(Puppet::Type.type(:file)["/parent"], "Could not find parent")
    end

    # Test that you can look a host up using multiple names, e.g., an FQDN and
    # a short name
    def test_multiplenodenames
        children = []

        # create a short-name node
        shortname = "mynodename"
        children << nodeobj(shortname)

        # And a long-name node
        longname = "node.domain.com"
        children << nodeobj(longname)

        # Create the wrapper object
        top = nil
        assert_nothing_raised("Could not create top object") {
            top = AST::ASTArray.new(
                :children => children
            )
        }

        # Evaluate the parse tree
        scope = nil
        assert_nothing_raised("Could not evaluate node") {
            scope = Puppet::Parser::Scope.new()
            top.evaluate(:scope => scope)
        }

        # Verify we can find the node via a search list
        objects = nil
        assert_nothing_raised("Could not retrieve short node definition") {
            objects = scope.evalnode(
                :name => ["%s.domain.com" % shortname, shortname], :facts => {}
            )
        }
        assert(objects, "Could not retrieve short node definition")

        # and then look for the long name
        assert_nothing_raised("Could not retrieve long node definition") {
            objects = scope.evalnode(
                :name => [longname.sub(/\..+/, ''), longname], :facts => {}
            )
        }
        assert(objects, "Could not retrieve long node definition")
    end

    # Test that a node gets the entire configuration except for work meant for
    # another node
    def test_fullconfigwithnodes
        children = []

        children << fileobj("/testing")

        # create a short-name node
        name = "mynodename"
        children << nodeobj(name)

        # Create the wrapper object
        top = nil
        assert_nothing_raised("Could not create top object") {
            top = AST::ASTArray.new(
                :children => children
            )
        }

        # Evaluate the parse tree
        scope = nil
        assert_nothing_raised("Could not evaluate node") {
            scope = Puppet::Parser::Scope.new()
            top.evaluate(:scope => scope)
        }

        # Verify we can find the node via a search list
        objects = nil
        assert_nothing_raised("Could not retrieve short node definition") {
            objects = scope.evalnode(:name => [name], :facts => {})
        }
        assert(objects, "Could not retrieve short node definition")

        # And now verify that we got both the top and node objects
        assert_nothing_raised("Could not find top-declared object") {
            assert_equal("/testing", objects[0].name)
        }

        assert_nothing_raised("Could not find node-declared object") {
            assert_equal("/%s" % name, objects[1][0].name)
        }
    end

    # Test that we can 'include' variables, not just normal strings.
    def test_includevars
        children = []

        # Create our class for testin
        klassname = "include"
        children << classobj(klassname)

        # Then add our variable assignment
        children << varobj("klassvar", klassname)

        # And finally add our calling of the variable
        children << AST::ObjectDef.new(
            :type => AST::Variable.new(:value => "klassvar"),
            :params => astarray
        )

        # And then create our top object
        top = AST::ASTArray.new(
            :children => children
        )

        # Evaluate the parse tree
        scope = nil
        assert_nothing_raised("Could not evaluate node") {
            scope = Puppet::Parser::Scope.new()
            top.evaluate(:scope => scope)
        }

        # Verify we can find the node via a search list
        objects = nil
        assert_nothing_raised("Could not retrieve objects") {
            objects = scope.to_trans
        }
        assert(objects, "Could not retrieve objects")

        assert_nothing_raised("Could not find top-declared object") {
            assert_equal("/%s" % klassname, objects[0][0].name)
        }
    end

    # Test that node inheritance works correctly
    def test_nodeinheritance
        children = []

        # create the base node
        name = "basenode"
        children << nodeobj(name)

        # and the sub node
        name = "subnode"
        children << AST::NodeDef.new(
            :names => nameobj(name),
            :parentclass => nameobj("basenode"),
            :code => AST::ASTArray.new(
                :children => [
                    varobj("%svar" % name, "%svalue" % name),
                    fileobj("/%s" % name)
                ]
            )
        )
        #subnode = nodeobj(name)
        #subnode.parentclass = "basenode"

        #children << subnode

        # and the top object
        top = nil
        assert_nothing_raised("Could not create top object") {
            top = AST::ASTArray.new(
                :children => children
            )
        }

        # Evaluate the parse tree
        scope = nil
        assert_nothing_raised("Could not evaluate node") {
            scope = Puppet::Parser::Scope.new()
            top.evaluate(:scope => scope)
        }

        # Verify we can find the node via a search list
        objects = nil
        assert_nothing_raised("Could not retrieve node definition") {
            objects = scope.evalnode(:name => [name], :facts => {})
        }
        assert(objects, "Could not retrieve node definition")

        assert_nothing_raised {
            inner = objects[0]

            # And now verify that we got the subnode file
            assert_nothing_raised("Could not find basenode file") {
                base = inner[0]
                assert_equal("/basenode", base.name)
            }

            # and the parent node file
            assert_nothing_raised("Could not find subnode file") {
                sub = inner[1]
                assert_equal("/subnode", sub.name)
            }

            inner.each { |obj|
                %w{basenode subnode}.each { |tag|
                    assert(obj.tags.include?(tag),
                        "%s did not include %s tag" % [obj.name, tag]
                    )
                }
            }
        }
    end

    def test_typechecking
        object = nil
        children = []
        type = "deftype"
        assert_nothing_raised("Could not add AST nodes for calling") {
            object = AST::ObjectDef.new(
                :type => nameobj(type),
                :name => nameobj("yayness"),
                :params => astarray()
            )
        }

        assert_nothing_raised("Typecheck failed") {
            object.typecheck(type)
        }

        # Add a scope, which makes it think it's evaluating
        assert_nothing_raised {
            scope = Puppet::Parser::Scope.new()
            object.scope = scope
        }

        # Verify an error is thrown when it can't find the type
        assert_raise(Puppet::ParseError) {
            object.typecheck(type)
        }

        # Create child class one
        children << classobj(type)
        children << object

        top = nil
        assert_nothing_raised("Could not create top object") {
            top = AST::ASTArray.new(
                :children => children
            )
        }

        scope = nil
        assert_nothing_raised("Could not evaluate") {
            scope = Puppet::Parser::Scope.new()
            objects = top.evaluate(:scope => scope)
        }
    end

    def disabled_test_paramcheck
        object = nil
        children = []
        type = "deftype"
        params = %w{param1 param2}

        comp = compobj(type, {
            :args => astarray(
                argobj("param1", "yay"),
                argobj("param2", "rah")
            ),
            :code => AST::ASTArray.new(
                :children => [
                    varobj("%svar" % name, "%svalue" % name),
                    fileobj("/%s" % name)
                ]
            )
        })
        assert_nothing_raised("Could not add AST nodes for calling") {
            object = AST::ObjectDef.new(
                :type => nameobj(type),
                :name => nameobj("yayness"),
                :params => astarray(
                    astarray(stringobj("param1"), stringobj("value1")),
                    astarray(stringobj("param2"), stringobj("value2"))
                )
            )
        }

        # Add a scope, which makes it think it's evaluating
        assert_nothing_raised {
            scope = Puppet::Parser::Scope.new()
            object.scope = scope
        }

        # Verify an error is thrown when it can't find the type
        assert_raise(Puppet::ParseError) {
            object.paramcheck(false, comp)
        }

        # Create child class one
        children << classobj(type)
        children << object

        top = nil
        assert_nothing_raised("Could not create top object") {
            top = AST::ASTArray.new(
                :children => children
            )
        }

        scope = nil
        assert_nothing_raised("Could not evaluate") {
            scope = Puppet::Parser::Scope.new()
            objects = top.evaluate(:scope => scope)
        }
    end
end