summaryrefslogtreecommitdiffstats
path: root/test/language/ast/definition.rb
blob: 5a2e6ffeae043ef455e2513b86eaad27361cadef (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
#!/usr/bin/env ruby
#
#  Created by Luke A. Kanies on 2006-02-20.
#  Copyright (c) 2006. All rights reserved.

$:.unshift("../../lib") if __FILE__ =~ /\.rb$/

require 'puppettest'
require 'mocha'
require 'puppettest/parsertesting'
require 'puppettest/resourcetesting'

class TestASTDefinition < Test::Unit::TestCase
	include PuppetTest
	include PuppetTest::ParserTesting
    include PuppetTest::ResourceTesting
	AST = Puppet::Parser::AST

    def test_initialize
        parser = mkparser

        # Create a new definition
        klass = parser.newdefine "yayness",
            :arguments => [["owner", stringobj("nobody")], %w{mode}],
            :code => AST::ASTArray.new(
                :children => [resourcedef("file", "/tmp/$name",
                        "owner" => varref("owner"), "mode" => varref("mode"))]
            )

        # Test validattr? a couple different ways
        [:owner, "owner", :schedule, "schedule"].each do |var|
            assert(klass.validattr?(var), "%s was not considered valid" % var.inspect)
        end

        [:random, "random"].each do |var|
            assert(! klass.validattr?(var), "%s was considered valid" % var.inspect)
        end

    end

    def test_evaluate
        parser = mkparser
        config = mkconfig
        scope = config.topscope
        klass = parser.newdefine "yayness",
            :arguments => [["owner", stringobj("nobody")], %w{mode}],
            :code => AST::ASTArray.new(
                :children => [resourcedef("file", "/tmp/$name",
                        "owner" => varref("owner"), "mode" => varref("mode"))]
            )

        resource = stub 'resource',
            :title => "first",
            :name => "first",
            :type => "yayness",
            :to_hash => {"mode" => "755"},
            :exported => false,
            :virtual => false 

        resource.stubs(:title)
        assert_nothing_raised do
            klass.evaluate(:scope => scope, :resource => resource)
        end

        firstobj = config.findresource("File[/tmp/first]")
        assert(firstobj, "Did not create /tmp/first obj")

        assert_equal("file", firstobj.type)
        assert_equal("/tmp/first", firstobj.title)
        assert_equal("nobody", firstobj[:owner])
        assert_equal("755", firstobj[:mode])

        # Make sure we can't evaluate it with the same args
        assert_raise(Puppet::ParseError) do
            klass.evaluate(:scope => scope, :resource => resource)
        end

        # Now create another with different args
        resource2 = stub 'resource',
            :title => "second",
            :name => "second",
            :type => "yayness",
            :to_hash => {"mode" => "755", "owner" => "daemon"},
            :exported => false,
            :virtual => false 

        assert_nothing_raised do
            klass.evaluate(:scope => scope, :resource => resource2)
        end

        secondobj = config.findresource("File[/tmp/second]")
        assert(secondobj, "Did not create /tmp/second obj")

        assert_equal("file", secondobj.type)
        assert_equal("/tmp/second", secondobj.title)
        assert_equal("daemon", secondobj[:owner])
        assert_equal("755", secondobj[:mode])
    end

    # #539 - definitions should support both names and titles
    def test_names_and_titles
        parser = mkparser
        scope = mkscope :parser => parser

        [
            {:name => "one", :title => "two"},
            {:title => "mytitle"}
        ].each_with_index do |hash, i|
            # Create a definition that uses both name and title.  Put this
            # inside the loop so the subscope expectations work.
            klass = parser.newdefine "yayness%s" % i

            subscope = klass.subscope(scope, "yayness%s" % i)

            klass.expects(:subscope).returns(subscope)

            resource = stub 'resource',
                :title => hash[:title],
                :name => hash[:name] || hash[:title],
                :type => "yayness%s" % i,
                :to_hash => {},
                :exported => false,
                :virtual => false 

            if hash[:name]
                resource.stubs(:to_hash).returns({:name => hash[:name]})
            end

            assert_nothing_raised("Could not evaluate definition with %s" % hash.inspect) do
                klass.evaluate(:scope => scope, :resource => resource)
            end

            name = hash[:name] || hash[:title]
            title = hash[:title]

            assert_equal(name, subscope.lookupvar("name"),
                "Name did not get set correctly")
            assert_equal(title, subscope.lookupvar("title"),
                "title did not get set correctly")

            [:name, :title].each do |param|
                val = resource.send(param)
                assert(subscope.tags.include?(val),
                    "Scope was not tagged with %s '%s'" % [param, val])
            end
        end
    end

    # Testing the root cause of #615.  We should be using the fqname for the type, instead
    # of just the short name.
    def test_fully_qualified_types
        parser = mkparser
        klass = parser.newclass("one::two")

        assert_equal("one::two", klass.classname, "Class did not get fully qualified class name")
    end
end