3
require File.dirname(__FILE__) + '/../../spec_helper'
5
require 'puppet/parser/loaded_code'
7
describe Puppet::Parser::LoadedCode do
8
%w{hostclass node definition}.each do |data|
9
it "should have a method for adding a #{data}" do
10
Puppet::Parser::LoadedCode.new.should respond_to("add_" + data)
13
it "should be able to retrieve #{data} by name" do
14
loader = Puppet::Parser::LoadedCode.new
15
loader.send("add_" + data, "foo", "bar")
16
loader.send(data, "foo").should == "bar"
19
it "should retrieve #{data} insensitive to case" do
20
loader = Puppet::Parser::LoadedCode.new
21
loader.send("add_" + data, "Foo", "bar")
22
loader.send(data, "fOo").should == "bar"
25
it "should return nil when asked for a #{data} that has not been added" do
26
Puppet::Parser::LoadedCode.new.send(data, "foo").should be_nil
29
it "should be able to retrieve all #{data}s" do
30
plurals = { "hostclass" => "hostclasses", "node" => "nodes", "definition" => "definitions" }
31
loader = Puppet::Parser::LoadedCode.new
32
loader.send("add_" + data , "foo", "bar")
33
loader.send(plurals[data]).should == { "foo" => "bar" }
37
describe "when finding a qualified instance" do
38
it "should return any found instance if the instance name is fully qualified" do
39
loader = Puppet::Parser::LoadedCode.new
40
loader.add_hostclass "foo::bar", "yay"
41
loader.find("namespace", "::foo::bar", :hostclass).should == "yay"
44
it "should return nil if the instance name is fully qualified and no such instance exists" do
45
loader = Puppet::Parser::LoadedCode.new
46
loader.find("namespace", "::foo::bar", :hostclass).should be_nil
49
it "should return the partially qualified object if it exists in the provided namespace" do
50
loader = Puppet::Parser::LoadedCode.new
51
loader.add_hostclass "foo::bar::baz", "yay"
52
loader.find("foo", "bar::baz", :hostclass).should == "yay"
55
it "should return the unqualified object if it exists in the provided namespace" do
56
loader = Puppet::Parser::LoadedCode.new
57
loader.add_hostclass "foo::bar", "yay"
58
loader.find("foo", "bar", :hostclass).should == "yay"
61
it "should return the unqualified object if it exists in the parent namespace" do
62
loader = Puppet::Parser::LoadedCode.new
63
loader.add_hostclass "foo::bar", "yay"
64
loader.find("foo::bar::baz", "bar", :hostclass).should == "yay"
67
it "should should return the partially qualified object if it exists in the parent namespace" do
68
loader = Puppet::Parser::LoadedCode.new
69
loader.add_hostclass "foo::bar::baz", "yay"
70
loader.find("foo::bar", "bar::baz", :hostclass).should == "yay"
73
it "should return the qualified object if it exists in the root namespace" do
74
loader = Puppet::Parser::LoadedCode.new
75
loader.add_hostclass "foo::bar::baz", "yay"
76
loader.find("foo::bar", "foo::bar::baz", :hostclass).should == "yay"
79
it "should return nil if the object cannot be found" do
80
loader = Puppet::Parser::LoadedCode.new
81
loader.add_hostclass "foo::bar::baz", "yay"
82
loader.find("foo::bar", "eh", :hostclass).should be_nil
86
it "should use the generic 'find' method with an empty namespace to find nodes" do
87
loader = Puppet::Parser::LoadedCode.new
88
loader.expects(:find).with("", "bar", :node)
89
loader.find_node("bar")
92
it "should use the generic 'find' method to find hostclasses" do
93
loader = Puppet::Parser::LoadedCode.new
94
loader.expects(:find).with("foo", "bar", :hostclass)
95
loader.find_hostclass("foo", "bar")
98
it "should use the generic 'find' method to find definitions" do
99
loader = Puppet::Parser::LoadedCode.new
100
loader.expects(:find).with("foo", "bar", :definition)
101
loader.find_definition("foo", "bar")
104
it "should indicate whether any nodes are defined" do
105
loader = Puppet::Parser::LoadedCode.new
106
loader.add_node("foo", "bar")
107
loader.should be_nodes
110
it "should indicate whether no nodes are defined" do
111
Puppet::Parser::LoadedCode.new.should_not be_nodes
114
describe "when adding nodes" do
115
it "should create an HostName if nodename is a string" do
116
Puppet::Parser::AST::HostName.expects(:new).with(:value => "foo")
117
loader = Puppet::Parser::LoadedCode.new
118
loader.add_node("foo", "bar")
121
it "should not create an HostName if nodename is an HostName" do
122
name = Puppet::Parser::AST::HostName.new(:value => "foo")
124
Puppet::Parser::AST::HostName.expects(:new).with(:value => "foo").never
126
loader = Puppet::Parser::LoadedCode.new
127
loader.add_node(name, "bar")
131
describe "when finding nodes" do
133
@loader = Puppet::Parser::LoadedCode.new
135
@nodename1 = stub 'nodename1', :is_a? => true
136
@node1 = stub 'node1'
137
@nodename2 = stub 'nodename2', :is_a? => true
138
@node2 = stub 'node2'
140
it "should create an HostName if nodename is a string" do
141
Puppet::Parser::AST::HostName.expects(:new).with(:value => "foo")
146
it "should not create an HostName if nodename is an HostName" do
147
name = Puppet::Parser::AST::HostName.new(:value => "foo")
149
Puppet::Parser::AST::HostName.expects(:new).with(:value => "foo").never
154
it "should be able to find node by HostName" do
155
namein = Puppet::Parser::AST::HostName.new(:value => "foo")
156
nameout = Puppet::Parser::AST::HostName.new(:value => "foo")
158
@loader.add_node(namein, "bar")
159
@loader.node(nameout).should == "bar"
162
it "should be able to find node by HostName strict equality" do
163
namein = Puppet::Parser::AST::HostName.new(:value => "foo")
164
nameout = Puppet::Parser::AST::HostName.new(:value => "foo")
166
@loader.add_node(namein, "bar")
167
@loader.node_exists?(nameout).should == "bar"
170
it "should not use node name matching when finding with strict node HostName" do
171
name1 = Puppet::Parser::AST::HostName.new(:value => "foo")
172
name2 = Puppet::Parser::AST::HostName.new(:value => Puppet::Parser::AST::Regex.new(:value => /foo/))
174
@loader.add_node(name1, "bar")
175
@loader.add_node(name2, "baz")
176
@loader.node_exists?(name1).should == "bar"
179
it "should return the first matching regex nodename" do
180
@nodename1.stubs(:regex?).returns(true)
181
@nodename1.stubs(:match).returns(true)
182
@nodename2.stubs(:regex?).returns(true)
183
@nodename2.stubs(:match).returns(true)
185
@loader.add_node(@nodename1, @node1)
186
@loader.add_node(@nodename2, @node2)
188
@loader.node("test").should == @node1
191
it "should not scan non-regex node" do
192
@nodename1.stubs(:regex?).returns(true)
193
@nodename1.stubs(:match).returns(false)
194
@nodename2.stubs(:regex?).returns(false)
195
@nodename2.expects(:match).never
197
@loader.add_node(@nodename1,@node1)
198
@loader.add_node(@nodename2,@node2)
203
it "should prefer non-regex nodes to regex nodes" do
204
@nodename1.stubs(:regex?).returns(false)
205
@nodename1.expects(:match).never
206
@nodename2.stubs(:regex?).returns(true)
207
@nodename2.expects(:match).never
209
@loader.add_node(@nodename1,@node1)
210
@loader.add_node(@nodename2,@node2)
212
@loader.node(@nodename1)