~ubuntu-branches/ubuntu/lucid/puppet/lucid-security

« back to all changes in this revision

Viewing changes to spec/unit/parser/loaded_code.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

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
require 'puppet/parser/loaded_code'
 
6
 
 
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)
 
11
        end
 
12
 
 
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"
 
17
        end
 
18
 
 
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"
 
23
        end
 
24
 
 
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
 
27
        end
 
28
 
 
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" }
 
34
        end
 
35
    end
 
36
 
 
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"
 
42
        end
 
43
 
 
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
 
47
        end
 
48
 
 
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"
 
53
        end
 
54
 
 
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"
 
59
        end
 
60
 
 
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"
 
65
        end
 
66
 
 
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"
 
71
        end
 
72
 
 
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"
 
77
        end
 
78
 
 
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
 
83
        end
 
84
    end
 
85
 
 
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")
 
90
    end
 
91
 
 
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")
 
96
    end
 
97
 
 
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")
 
102
    end
 
103
 
 
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
 
108
    end
 
109
 
 
110
    it "should indicate whether no nodes are defined" do
 
111
        Puppet::Parser::LoadedCode.new.should_not be_nodes
 
112
    end
 
113
 
 
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")
 
119
        end
 
120
 
 
121
        it "should not create an HostName if nodename is an HostName" do
 
122
            name = Puppet::Parser::AST::HostName.new(:value => "foo")
 
123
 
 
124
            Puppet::Parser::AST::HostName.expects(:new).with(:value => "foo").never
 
125
 
 
126
            loader = Puppet::Parser::LoadedCode.new
 
127
            loader.add_node(name, "bar")
 
128
        end
 
129
    end
 
130
 
 
131
    describe "when finding nodes" do
 
132
        before :each do
 
133
            @loader = Puppet::Parser::LoadedCode.new
 
134
 
 
135
            @nodename1 = stub 'nodename1', :is_a? => true
 
136
            @node1 = stub 'node1'
 
137
            @nodename2 = stub 'nodename2', :is_a? => true
 
138
            @node2 = stub 'node2'
 
139
        end
 
140
        it "should create an HostName if nodename is a string" do
 
141
            Puppet::Parser::AST::HostName.expects(:new).with(:value => "foo")
 
142
 
 
143
            @loader.node("foo")
 
144
        end
 
145
 
 
146
        it "should not create an HostName if nodename is an HostName" do
 
147
            name = Puppet::Parser::AST::HostName.new(:value => "foo")
 
148
 
 
149
            Puppet::Parser::AST::HostName.expects(:new).with(:value => "foo").never
 
150
 
 
151
            @loader.node(name)
 
152
        end
 
153
 
 
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")
 
157
 
 
158
            @loader.add_node(namein, "bar")
 
159
            @loader.node(nameout).should == "bar"
 
160
        end
 
161
 
 
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")
 
165
 
 
166
            @loader.add_node(namein, "bar")
 
167
            @loader.node_exists?(nameout).should == "bar"
 
168
        end
 
169
 
 
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/))
 
173
 
 
174
            @loader.add_node(name1, "bar")
 
175
            @loader.add_node(name2, "baz")
 
176
            @loader.node_exists?(name1).should == "bar"
 
177
        end
 
178
 
 
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)
 
184
 
 
185
            @loader.add_node(@nodename1, @node1)
 
186
            @loader.add_node(@nodename2, @node2)
 
187
 
 
188
            @loader.node("test").should == @node1
 
189
        end
 
190
 
 
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
 
196
 
 
197
            @loader.add_node(@nodename1,@node1)
 
198
            @loader.add_node(@nodename2,@node2)
 
199
 
 
200
            @loader.node("test")
 
201
        end
 
202
 
 
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
 
208
 
 
209
            @loader.add_node(@nodename1,@node1)
 
210
            @loader.add_node(@nodename2,@node2)
 
211
 
 
212
            @loader.node(@nodename1)
 
213
        end
 
214
    end
 
215
end