summaryrefslogtreecommitdiffstats
path: root/test/language/ast.rb
blob: b714fcdea3d9f6c37f35c8890dea46e364c32c3b (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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
#!/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"))
        classes = %w{parent child1 child2}

        # 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
        objects = nil
        assert_nothing_raised("Could not evaluate") {
            scope = Puppet::Parser::Scope.new()
            objects = scope.evaluate(:ast => top)
        }

        assert_instance_of(Puppet::TransBucket, objects)

        assert_equal(1, scope.find_all { |child|
            if child.is_a? Puppet::Parser::Scope
                child.lookupobject(:name => "/parent", :type => "file")
            else
                nil
            end
        }.length, "Found incorrect number of '/parent' objects")

        assert_equal(classes.sort, scope.classlist.sort)
    end

    # Test that 'tagobject' collects all of an object's parameters and stores
    # them in one TransObject, rather than many.  This is probably a bad idea.
    def test_tagobject
        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 = scope.evaluate(:ast => top)
        }
    end

    # Verify that nodes don't evaluate code in other node scopes but that their
    # facts work outside their scopes.
    def test_nodescopes
        parent = child1 = nil
        topchildren = []

        # create the parent class
        topchildren << classobj("everyone")

        topchildren << classobj("parent")


        classes = %w{everyone parent}

        # And a variable, so we verify the facts get set at the top
        assert_nothing_raised {
            children = []
            children << varobj("yaytest", "$hostname")
        }

        nodes = []

        3.times do |i|
            children = []

            # Create a child class
            topchildren << classobj("perchild#{i}", :parentclass => nameobj("parent"))
            classes << "perchild%s"

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

            classes << "child"

            ["child", "everyone", "perchild#{i}"].each do |name|
                # Now call our child class
                assert_nothing_raised {
                    children << AST::ObjectDef.new(
                        :type => nameobj(name),
                        :params => astarray()
                    )
                }
            end

            # and another variable
            assert_nothing_raised {
                children << varobj("rahtest", "$hostname")
            }

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

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

        nodes.each_with_index do |node, i|
            # Evaluate the parse tree
            scope = Puppet::Parser::Scope.new()
            args = {:names => [node], :facts => {"hostname" => node}, :ast => top}

            # verify that we can evaluate it okay
            trans = nil
            assert_nothing_raised("Could not retrieve node definition") {
                trans = scope.evaluate(args)
            }

            assert_equal(node, scope.lookupvar("hostname"))

            assert(trans, "Could not retrieve trans objects")

            # and that we can convert them to type objects
            objects = nil
            assert_nothing_raised("Could not retrieve node definition") {
                objects = trans.to_type
            }

            assert(objects, "Could not retrieve trans objects")

            count = 0
            # Make sure the node name gets into the path correctly.
            Puppet.type(:file).each { |obj|
                count += 1
                assert(obj.path !~ /#{node}\[#{node}\]/,
                    "Node name appears twice")
            }

            assert(count > 0, "Did not create any files")

            classes.each do |name|
                if name =~ /%s/
                    name = name % i
                end
                assert(Puppet::Type.type(:file)["/#{name}"], "Could not find '#{name}'")
            end

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

    # Verify that classes are correctly defined in node scopes.
    def disabled_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 << nodedef(shortname)

        # And a long-name node
        longname = "node.domain.com"
        children << nodedef(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 = Puppet::Parser::Scope.new()

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

        scope = Puppet::Parser::Scope.new()

        # and then look for the long name
        assert_nothing_raised("Could not retrieve long node definition") {
            objects = scope.evaluate(
                :names => [longname.sub(/\..+/, ''), longname], :facts => {},
                :ast => top
            )
        }
        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 << nodedef(name)

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

        scope = Puppet::Parser::Scope.new()

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

        # 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 %s" %
            "/%s" % name
        ) {
            assert_equal("/%s" % name, objects[1][0].name)
        }
    end

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

        # Create our class for testin
        klassname = "include"
        children << classobj(klassname)
        classes << 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
        objects = nil
        assert_nothing_raised("Could not evaluate node") {
            scope = Puppet::Parser::Scope.new()
            objects = scope.evaluate(:ast => top)
        }

        # Verify we get the right classlist back
        assert_equal(classes.sort, scope.classlist.sort)

        # Verify we can find the node via a search list
        #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 << nodedef(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 = nodedef(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 = Puppet::Parser::Scope.new()

        # Verify we can find the node via a search list
        objects = nil
        assert_nothing_raised("Could not evaluate node") {
            objects = scope.evaluate(:names => [name], :facts => {}, :ast => top)
        }
        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

    def test_setclass
        type = "yay"
        classes = [type]
        children = []
        # Create child class one
        children << varobj("variable", "aclass")
        children << tagobj(type, varref("variable"))
        children << tagobj(type)

        classes << "aclass"

        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)
        }


        classes.each do |tag|
            assert(scope.classlist.include?(tag), "Did not set class %s" % tag)
        end

    end

    # Test that we strip the domain off of host names before they are set as classes
    def test_nodenamestrip
        children = []

        longname = "node.domain.com"
        children << nodedef(longname)

        # Create the wrapper object
        top = nil
        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 node") {
            objects = scope.evaluate(:names => [longname], :facts => {}, :ast => top)
        }

        assert(!scope.classlist.include?("node.domain.com"),
            "Node's long name got set")
        assert(scope.classlist.include?("node"), "Node's name did not get set")
    end

    # Make sure that deep class parentage works
    def test_classparentage
        children = []
        files = []
        base = classobj("base")
        files << "/base"

        children << base

        parent = "base"
        5.times { |i|
            name = "child%s" % i
            files << "/%s" % name
            children << classobj(name, :parentclass => nameobj(parent))

            parent = name
        }

        children << functionobj("include", parent)

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

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

        objects = objects.flatten

        files.each do |file|
            assert(objects.find { |o| o.name == file },
                "Could not find file %s" % file)
        end
    end

    # To fix #140.  Currently non-functional.
    def disabled_test_classreuse
        children = []

        # Create the parent class, with a definition in it.
        children << classobj("parent", :code => AST::ASTArray.new(
            :file => __FILE__,
            :line => __LINE__,
            :children => [
                compobj("foo", :args => AST::ASTArray.new(
                        :children => [nameobj("arg")]
                    ),
                    :code => AST::ASTArray.new(
                        :file => __FILE__,
                        :line => __LINE__,
                        :children => [fileobj("/$arg")]
                    )
                ),
                objectdef("foo", "ptest", {"arg" => "parentfoo"})
            ]
        ))

        # Create child class, also trying to use that definition
        children << classobj("child1", :parentclass => nameobj("parent"),
            :code => AST::ASTArray.new(
                :file => __FILE__,
                :line => __LINE__,
                :children => [
                    objectdef("foo", "ctest", {"arg" => "childfoo"})
                ]
            )
        )

        # Call the parent first
        children << functionobj("include", "parent")

        # Then call the child, and make sure it can look up the definition
        children << functionobj("include", "child1")

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

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

# $Id$