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
|
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../spec_helper'
describe Puppet::Parser::Compile, " when compiling" do
before do
@node = stub 'node', :name => 'mynode'
@parser = stub 'parser', :version => "1.0"
@compile = Puppet::Parser::Compile.new(@node, @parser)
end
def compile_methods
[:set_node_parameters, :evaluate_main, :evaluate_ast_node, :evaluate_node_classes, :evaluate_generators, :fail_on_unevaluated,
:finish, :store, :extract]
end
# Stub all of the main compile methods except the ones we're specifically interested in.
def compile_stub(*except)
(compile_methods - except).each { |m| @compile.stubs(m) }
end
it "should set node parameters as variables in the top scope" do
params = {"a" => "b", "c" => "d"}
@node.stubs(:parameters).returns(params)
compile_stub(:set_node_parameters)
@compile.compile
@compile.topscope.lookupvar("a").should == "b"
@compile.topscope.lookupvar("c").should == "d"
end
it "should evaluate any existing classes named in the node" do
classes = %w{one two three four}
main = stub 'main'
one = stub 'one', :classname => "one"
three = stub 'three', :classname => "three"
@node.stubs(:name).returns("whatever")
@node.stubs(:classes).returns(classes)
@compile.expects(:evaluate_classes).with(classes, @compile.topscope)
@compile.send :evaluate_node_classes
end
it "should enable ast_nodes if the parser has any nodes" do
@parser.expects(:nodes).returns(:one => :yay)
@compile.ast_nodes?.should be_true
end
it "should disable ast_nodes if the parser has no nodes" do
@parser.expects(:nodes).returns({})
@compile.ast_nodes?.should be_false
end
end
describe Puppet::Parser::Compile, " when evaluating classes" do
before do
@node = stub 'node', :name => 'mynode'
@parser = stub 'parser', :version => "1.0"
@scope = stub 'scope', :source => mock("source")
@compile = Puppet::Parser::Compile.new(@node, @parser)
end
it "should fail if there's no source listed for the scope" do
scope = stub 'scope', :source => nil
proc { @compile.evaluate_classes(%w{one two}, scope) }.should raise_error(Puppet::DevError)
end
it "should tag the configuration with the name of each not-found class" do
@compile.configuration.expects(:tag).with("notfound")
@scope.expects(:findclass).with("notfound").returns(nil)
@compile.evaluate_classes(%w{notfound}, @scope)
end
end
describe Puppet::Parser::Compile, " when evaluating found classes" do
before do
@node = stub 'node', :name => 'mynode'
@parser = stub 'parser', :version => "1.0"
@scope = stub 'scope', :source => mock("source")
@compile = Puppet::Parser::Compile.new(@node, @parser)
@class = stub 'class', :classname => "my::class"
@scope.stubs(:findclass).with("myclass").returns(@class)
@resource = mock 'resource'
end
it "should create a resource for each found class" do
@compile.configuration.stubs(:tag)
@compile.stubs :store_resource
Puppet::Parser::Resource.expects(:new).with(:scope => @scope, :source => @scope.source, :title => "my::class", :type => "class").returns(@resource)
@compile.evaluate_classes(%w{myclass}, @scope)
end
it "should store each created resource in the compile" do
@compile.configuration.stubs(:tag)
@compile.expects(:store_resource).with(@scope, @resource)
Puppet::Parser::Resource.stubs(:new).returns(@resource)
@compile.evaluate_classes(%w{myclass}, @scope)
end
it "should tag the configuration with the fully-qualified name of each found class" do
@compile.configuration.expects(:tag).with("my::class")
@compile.stubs(:store_resource)
Puppet::Parser::Resource.stubs(:new).returns(@resource)
@compile.evaluate_classes(%w{myclass}, @scope)
end
it "should not evaluate the resources created for found classes unless asked" do
@compile.configuration.stubs(:tag)
@compile.stubs(:store_resource)
@resource.expects(:evaluate).never
Puppet::Parser::Resource.stubs(:new).returns(@resource)
@compile.evaluate_classes(%w{myclass}, @scope)
end
it "should immediately evaluate the resources created for found classes when asked" do
@compile.configuration.stubs(:tag)
@compile.stubs(:store_resource)
@resource.expects(:evaluate)
Puppet::Parser::Resource.stubs(:new).returns(@resource)
@compile.evaluate_classes(%w{myclass}, @scope, false)
end
it "should return the list of found classes" do
@compile.configuration.stubs(:tag)
@compile.stubs(:store_resource)
@scope.stubs(:findclass).with("notfound").returns(nil)
Puppet::Parser::Resource.stubs(:new).returns(@resource)
@compile.evaluate_classes(%w{myclass notfound}, @scope).should == %w{myclass}
end
end
describe Puppet::Parser::Compile, " when evaluating AST nodes with no AST nodes present" do
before do
@node = stub 'node', :name => "foo"
@parser = stub 'parser', :version => "1.0", :nodes => {}
@compile = Puppet::Parser::Compile.new(@node, @parser)
end
it "should do nothing" do
@compile.expects(:ast_nodes?).returns(false)
@compile.parser.expects(:nodes).never
Puppet::Parser::Resource.expects(:new).never
@compile.send(:evaluate_ast_node)
end
end
describe Puppet::Parser::Compile, " when evaluating AST nodes with AST nodes present" do
before do
@node = stub 'node', :name => "foo"
@parser = stub 'parser', :version => "1.0", :nodes => {}
@compile = Puppet::Parser::Compile.new(@node, @parser)
@nodes = mock 'node_hash'
@compile.stubs(:ast_nodes?).returns(true)
@compile.parser.stubs(:nodes).returns(@nodes)
# Set some names for our test
@node.stubs(:names).returns(%w{a b c})
@nodes.stubs(:[]).with("a").returns(nil)
@nodes.stubs(:[]).with("b").returns(nil)
@nodes.stubs(:[]).with("c").returns(nil)
# It should check this last, of course.
@nodes.stubs(:[]).with("default").returns(nil)
end
it "should fail if the named node cannot be found" do
proc { @compile.send(:evaluate_ast_node) }.should raise_error(Puppet::ParseError)
end
it "should create a resource for the first node class matching the node name" do
node_class = stub 'node', :classname => "c"
@nodes.stubs(:[]).with("c").returns(node_class)
node_resource = stub 'node resource', :ref => "Node[c]", :evaluate => nil
Puppet::Parser::Resource.expects(:new).with { |args| args[:title] == "c" and args[:type] == "node" }.returns(node_resource)
@compile.send(:evaluate_ast_node)
end
it "should match the default node if no matching node can be found" do
node_class = stub 'node', :classname => "default"
@nodes.stubs(:[]).with("default").returns(node_class)
node_resource = stub 'node resource', :ref => "Node[default]", :evaluate => nil
Puppet::Parser::Resource.expects(:new).with { |args| args[:title] == "default" and args[:type] == "node" }.returns(node_resource)
@compile.send(:evaluate_ast_node)
end
it "should tag the configuration with the found node name" do
node_class = stub 'node', :classname => "c"
@nodes.stubs(:[]).with("c").returns(node_class)
node_resource = stub 'node resource', :ref => "Node[c]", :evaluate => nil
Puppet::Parser::Resource.stubs(:new).returns(node_resource)
@compile.configuration.expects(:tag).with("c")
@compile.send(:evaluate_ast_node)
end
it "should evaluate the node resource immediately rather than using lazy evaluation" do
node_class = stub 'node', :classname => "c"
@nodes.stubs(:[]).with("c").returns(node_class)
node_resource = stub 'node resource', :ref => "Node[c]"
Puppet::Parser::Resource.stubs(:new).returns(node_resource)
node_resource.expects(:evaluate)
@compile.send(:evaluate_ast_node)
end
it "should set the node's scope as the top scope" do
node_class = stub 'node', :classname => "c"
@nodes.stubs(:[]).with("c").returns(node_class)
node_resource = stub 'node resource', :ref => "Node[c]"
Puppet::Parser::Resource.stubs(:new).returns(node_resource)
# The #evaluate method normally does this.
@compile.class_set(node_class.classname, :my_node_scope)
node_resource.stubs(:evaluate)
@compile.send(:evaluate_ast_node)
@compile.topscope.should == :my_node_scope
end
end
|