~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to spec/unit/parser/ast/definition.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080726154345-1fmgo76b4l72ulvc
ImportĀ upstreamĀ versionĀ 0.24.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
require File.dirname(__FILE__) + '/../../../spec_helper'
 
4
 
 
5
describe Puppet::Parser::AST::Definition, "when initializing" do
 
6
end
 
7
 
 
8
describe Puppet::Parser::AST::Definition, "when evaluating" do
 
9
    before do
 
10
        @type = Puppet::Parser::Resource
 
11
        @parser = Puppet::Parser::Parser.new :Code => ""
 
12
        @source = @parser.newclass ""
 
13
        @definition = @parser.newdefine "mydefine"
 
14
        @node = Puppet::Node.new("yaynode")
 
15
        @compiler = Puppet::Parser::Compiler.new(@node, @parser)
 
16
        @scope = @compiler.topscope
 
17
 
 
18
        @resource = Puppet::Parser::Resource.new(:type => "mydefine", :title => "myresource", :scope => @scope, :source => @source)
 
19
    end
 
20
 
 
21
    it "should create a new scope" do
 
22
        scope = nil
 
23
        code = mock 'code'
 
24
        code.expects(:safeevaluate).with do |scope|
 
25
            scope.object_id.should_not == @scope.object_id
 
26
            true
 
27
        end
 
28
        @definition.stubs(:code).returns(code)
 
29
        @definition.evaluate_code(@resource)
 
30
    end
 
31
 
 
32
#    it "should copy its namespace to the scope"
 
33
#
 
34
#    it "should mark the scope virtual if the resource is virtual"
 
35
#
 
36
#    it "should mark the scope exported if the resource is exported"
 
37
#
 
38
#    it "should set the resource's parameters as variables in the scope"
 
39
#
 
40
#    it "should set the resource's title as a variable in the scope"
 
41
#
 
42
#    it "should copy the resource's title in a 'name' variable in the scope"
 
43
#
 
44
#    it "should not copy the resource's title as the name if 'name' is one of the resource parameters"
 
45
#
 
46
#    it "should evaluate the associated code with the new scope"
 
47
 
 
48
    def old_test_initialize
 
49
        parser = mkparser
 
50
 
 
51
        # Create a new definition
 
52
        klass = parser.newdefine "yayness",
 
53
            :arguments => [["owner", stringobj("nobody")], %w{mode}],
 
54
            :code => AST::ASTArray.new(
 
55
                :children => [resourcedef("file", "/tmp/$name",
 
56
                        "owner" => varref("owner"), "mode" => varref("mode"))]
 
57
            )
 
58
 
 
59
        # Test validattr? a couple different ways
 
60
        [:owner, "owner", :schedule, "schedule"].each do |var|
 
61
            assert(klass.validattr?(var), "%s was not considered valid" % var.inspect)
 
62
        end
 
63
 
 
64
        [:random, "random"].each do |var|
 
65
            assert(! klass.validattr?(var), "%s was considered valid" % var.inspect)
 
66
        end
 
67
 
 
68
    end
 
69
 
 
70
    def oldtest_evaluate
 
71
        parser = mkparser
 
72
        config = mkcompiler
 
73
        config.send(:evaluate_main)
 
74
        scope = config.topscope
 
75
        klass = parser.newdefine "yayness",
 
76
            :arguments => [["owner", stringobj("nobody")], %w{mode}],
 
77
            :code => AST::ASTArray.new(
 
78
                :children => [resourcedef("file", "/tmp/$name",
 
79
                        "owner" => varref("owner"), "mode" => varref("mode"))]
 
80
            )
 
81
 
 
82
        resource = Puppet::Parser::Resource.new(
 
83
            :title => "first",
 
84
            :type => "yayness",
 
85
            :exported => false,
 
86
            :virtual => false,
 
87
            :scope => scope,
 
88
            :source => scope.source
 
89
        )
 
90
        resource.send(:set_parameter, "name", "first")
 
91
        resource.send(:set_parameter, "mode", "755")
 
92
 
 
93
        resource.stubs(:title)
 
94
        assert_nothing_raised do
 
95
            klass.evaluate_code(resource)
 
96
        end
 
97
 
 
98
        firstobj = config.findresource("File[/tmp/first]")
 
99
        assert(firstobj, "Did not create /tmp/first obj")
 
100
 
 
101
        assert_equal("File", firstobj.type)
 
102
        assert_equal("/tmp/first", firstobj.title)
 
103
        assert_equal("nobody", firstobj[:owner])
 
104
        assert_equal("755", firstobj[:mode])
 
105
 
 
106
        # Make sure we can't evaluate it with the same args
 
107
        assert_raise(Puppet::ParseError) do
 
108
            klass.evaluate_code(resource)
 
109
        end
 
110
 
 
111
        # Now create another with different args
 
112
        resource2 = Puppet::Parser::Resource.new(
 
113
            :title => "second",
 
114
            :type => "yayness",
 
115
            :exported => false,
 
116
            :virtual => false,
 
117
            :scope => scope,
 
118
            :source => scope.source
 
119
        )
 
120
        resource2.send(:set_parameter, "name", "second")
 
121
        resource2.send(:set_parameter, "mode", "755")
 
122
        resource2.send(:set_parameter, "owner", "daemon")
 
123
 
 
124
        assert_nothing_raised do
 
125
            klass.evaluate_code(resource2)
 
126
        end
 
127
 
 
128
        secondobj = config.findresource("File[/tmp/second]")
 
129
        assert(secondobj, "Did not create /tmp/second obj")
 
130
 
 
131
        assert_equal("File", secondobj.type)
 
132
        assert_equal("/tmp/second", secondobj.title)
 
133
        assert_equal("daemon", secondobj[:owner])
 
134
        assert_equal("755", secondobj[:mode])
 
135
    end
 
136
 
 
137
    # #539 - definitions should support both names and titles
 
138
    def oldtest_names_and_titles
 
139
        parser = mkparser
 
140
        scope = mkscope :parser => parser
 
141
 
 
142
        [
 
143
            {:name => "one", :title => "two"},
 
144
            {:title => "mytitle"}
 
145
        ].each_with_index do |hash, i|
 
146
            # Create a definition that uses both name and title.  Put this
 
147
            # inside the loop so the subscope expectations work.
 
148
            klass = parser.newdefine "yayness%s" % i
 
149
 
 
150
            resource = Puppet::Parser::Resource.new(
 
151
                :title => hash[:title],
 
152
                :type => "yayness%s" % i,
 
153
                :exported => false,
 
154
                :virtual => false,
 
155
                :scope => scope,
 
156
                :source => scope.source
 
157
            )
 
158
 
 
159
            subscope = klass.subscope(scope, resource)
 
160
 
 
161
            klass.expects(:subscope).returns(subscope)
 
162
 
 
163
            if hash[:name]
 
164
                resource.stubs(:to_hash).returns({:name => hash[:name]})
 
165
            end
 
166
 
 
167
            assert_nothing_raised("Could not evaluate definition with %s" % hash.inspect) do
 
168
                klass.evaluate_code(resource)
 
169
            end
 
170
 
 
171
            name = hash[:name] || hash[:title]
 
172
            title = hash[:title]
 
173
 
 
174
            assert_equal(name, subscope.lookupvar("name"),
 
175
                "Name did not get set correctly")
 
176
            assert_equal(title, subscope.lookupvar("title"),
 
177
                "title did not get set correctly")
 
178
 
 
179
            [:name, :title].each do |param|
 
180
                val = resource.send(param)
 
181
                assert(subscope.tags.include?(val),
 
182
                    "Scope was not tagged with %s '%s'" % [param, val])
 
183
            end
 
184
        end
 
185
    end
 
186
 
 
187
    # Testing the root cause of #615.  We should be using the fqname for the type, instead
 
188
    # of just the short name.
 
189
    def oldtest_fully_qualified_types
 
190
        parser = mkparser
 
191
        klass = parser.newclass("one::two")
 
192
 
 
193
        assert_equal("one::two", klass.classname, "Class did not get fully qualified class name")
 
194
    end
 
195
end