summaryrefslogtreecommitdiffstats
path: root/spec/unit/indirector/node/ldap.rb
blob: 2a4f240edcf6b179c44fff4fdf94dce762da050b (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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../../spec_helper'

require 'puppet/indirector/node/ldap'

describe Puppet::Node::Ldap do
    describe "when searching for nodes" do
        before :each do
            @searcher = Puppet::Node::Ldap.new
            @entries = {}
            entries = @entries
            
            @connection = mock 'connection'
            @entry = mock 'entry'
            @connection.stubs(:search).yields(@entry)
            @searcher.stubs(:connection).returns(@connection)
            @searcher.stubs(:class_attributes).returns([])
            @searcher.stubs(:parent_attribute).returns(nil)
            @searcher.stubs(:search_base).returns(:yay)
            @searcher.stubs(:search_filter).returns(:filter)

            @node = mock 'node'
            @node.stubs(:fact_merge)
            @name = "mynode"
            Puppet::Node.stubs(:new).with(@name).returns(@node)
        end

        it "should return nil if no results are found in ldap" do
            @connection.stubs(:search)
            @searcher.find("mynode").should be_nil
        end

        it "should return a node object if results are found in ldap" do
            @entry.stubs(:to_hash).returns({})
            @searcher.find("mynode").should equal(@node)
        end

        it "should deduplicate class values" do
            @entry.stubs(:to_hash).returns({})
            @searcher.stubs(:class_attributes).returns(%w{one two})
            @entry.stubs(:vals).with("one").returns(%w{a b})
            @entry.stubs(:vals).with("two").returns(%w{b c})
            @node.expects(:classes=).with(%w{a b c})
            @searcher.find("mynode")
        end

        it "should add any values stored in the class_attributes attributes to the node classes" do
            @entry.stubs(:to_hash).returns({})
            @searcher.stubs(:class_attributes).returns(%w{one two})
            @entry.stubs(:vals).with("one").returns(%w{a b})
            @entry.stubs(:vals).with("two").returns(%w{c d})
            @node.expects(:classes=).with(%w{a b c d})
            @searcher.find("mynode")
        end

        it "should add all entry attributes as node parameters" do
            @entry.stubs(:to_hash).returns("one" => ["two"], "three" => ["four"])
            @node.expects(:parameters=).with("one" => "two", "three" => "four")
            @searcher.find("mynode")
        end

        it "should set the node's environment to the environment of the results" do
            @entry.stubs(:to_hash).returns("environment" => ["test"])
            @node.stubs(:parameters=)
            @node.expects(:environment=).with("test")
            @searcher.find("mynode")
        end

        it "should retain false parameter values" do
            @entry.stubs(:to_hash).returns("one" => [false])
            @node.expects(:parameters=).with("one" => false)
            @searcher.find("mynode")
        end

        it "should turn single-value parameter value arrays into single non-arrays" do
            @entry.stubs(:to_hash).returns("one" => ["a"])
            @node.expects(:parameters=).with("one" => "a")
            @searcher.find("mynode")
        end

        it "should keep multi-valued parametes as arrays" do
            @entry.stubs(:to_hash).returns("one" => ["a", "b"])
            @node.expects(:parameters=).with("one" => ["a", "b"])
            @searcher.find("mynode")
        end

        describe "and a parent node is specified" do
            before do
                @parent = mock 'parent'
                @parent_parent = mock 'parent_parent'

                @searcher.meta_def(:search_filter) do |name|
                    return name
                end
                @connection.stubs(:search).with { |*args| args[2] == @name              }.yields(@entry)
                @connection.stubs(:search).with { |*args| args[2] == 'parent'           }.yields(@parent)
                @connection.stubs(:search).with { |*args| args[2] == 'parent_parent'    }.yields(@parent_parent)

                @searcher.stubs(:parent_attribute).returns(:parent)
            end

            it "should fail if the parent cannot be found" do
                @connection.stubs(:search).with { |*args| args[2] == 'parent' }.returns("whatever")

                @entry.stubs(:to_hash).returns({})
                @entry.stubs(:vals).with(:parent).returns(%w{parent})

                proc { @searcher.find("mynode") }.should raise_error(Puppet::Error)
            end

            it "should add any parent classes to the node's classes" do
                @entry.stubs(:to_hash).returns({})
                @entry.stubs(:vals).with(:parent).returns(%w{parent})
                @entry.stubs(:vals).with("classes").returns(%w{a b})

                @parent.stubs(:to_hash).returns({})
                @parent.stubs(:vals).with("classes").returns(%w{c d})
                @parent.stubs(:vals).with(:parent).returns(nil)

                @searcher.stubs(:class_attributes).returns(%w{classes})
                @node.expects(:classes=).with(%w{a b c d})
                @searcher.find("mynode")
            end

            it "should add any parent parameters to the node's parameters" do
                @entry.stubs(:to_hash).returns("one" => "two")
                @entry.stubs(:vals).with(:parent).returns(%w{parent})

                @parent.stubs(:to_hash).returns("three" => "four")
                @parent.stubs(:vals).with(:parent).returns(nil)

                @node.expects(:parameters=).with("one" => "two", "three" => "four")
                @searcher.find("mynode")
            end

            it "should prefer node parameters over parent parameters" do
                @entry.stubs(:to_hash).returns("one" => "two")
                @entry.stubs(:vals).with(:parent).returns(%w{parent})

                @parent.stubs(:to_hash).returns("one" => "three")
                @parent.stubs(:vals).with(:parent).returns(nil)

                @node.expects(:parameters=).with("one" => "two")
                @searcher.find("mynode")
            end

            it "should use the parent's environment if the node has none" do
                @entry.stubs(:to_hash).returns({})
                @entry.stubs(:vals).with(:parent).returns(%w{parent})

                @parent.stubs(:to_hash).returns("environment" => ["parent"])
                @parent.stubs(:vals).with(:parent).returns(nil)

                @node.stubs(:parameters=)
                @node.expects(:environment=).with("parent")
                @searcher.find("mynode")
            end

            it "should prefer the node's environment to the parent's" do
                @entry.stubs(:to_hash).returns("environment" => %w{child})
                @entry.stubs(:vals).with(:parent).returns(%w{parent})

                @parent.stubs(:to_hash).returns("environment" => ["parent"])
                @parent.stubs(:vals).with(:parent).returns(nil)

                @node.stubs(:parameters=)
                @node.expects(:environment=).with("child")
                @searcher.find("mynode")
            end

            it "should recursively look up parent information" do
                @entry.stubs(:to_hash).returns("one" => "two")
                @entry.stubs(:vals).with(:parent).returns(%w{parent})

                @parent.stubs(:to_hash).returns("three" => "four")
                @parent.stubs(:vals).with(:parent).returns(['parent_parent'])

                @parent_parent.stubs(:to_hash).returns("five" => "six")
                @parent_parent.stubs(:vals).with(:parent).returns(nil)
                @parent_parent.stubs(:vals).with(:parent).returns(nil)

                @node.expects(:parameters=).with("one" => "two", "three" => "four", "five" => "six")
                @searcher.find("mynode")
            end

            it "should not allow loops in parent declarations" do
                @entry.stubs(:to_hash).returns("one" => "two")
                @entry.stubs(:vals).with(:parent).returns(%w{parent})

                @parent.stubs(:to_hash).returns("three" => "four")
                @parent.stubs(:vals).with(:parent).returns([@name])
                proc { @searcher.find("mynode") }.should raise_error(ArgumentError)
            end
        end
    end
end

describe Puppet::Node::Ldap, " when developing the search query" do
    before do
        @searcher = Puppet::Node::Ldap.new
    end

    it "should return the value of the :ldapclassattrs split on commas as the class attributes" do
        Puppet.stubs(:[]).with(:ldapclassattrs).returns("one,two")
        @searcher.class_attributes.should == %w{one two}
    end

    it "should return nil as the parent attribute if the :ldapparentattr is set to an empty string" do
        Puppet.stubs(:[]).with(:ldapparentattr).returns("")
        @searcher.parent_attribute.should be_nil
    end

    it "should return the value of the :ldapparentattr as the parent attribute" do
        Puppet.stubs(:[]).with(:ldapparentattr).returns("pere")
        @searcher.parent_attribute.should == "pere"
    end

    it "should use the value of the :ldapstring as the search filter" do
        Puppet.stubs(:[]).with(:ldapstring).returns("mystring")
        @searcher.search_filter("testing").should == "mystring"
    end

    it "should replace '%s' with the node name in the search filter if it is present" do
        Puppet.stubs(:[]).with(:ldapstring).returns("my%sstring")
        @searcher.search_filter("testing").should == "mytestingstring"
    end

    it "should not modify the global :ldapstring when replacing '%s' in the search filter" do
        filter = mock 'filter'
        filter.expects(:include?).with("%s").returns(true)
        filter.expects(:gsub).with("%s", "testing").returns("mynewstring")
        Puppet.stubs(:[]).with(:ldapstring).returns(filter)
        @searcher.search_filter("testing").should == "mynewstring"
    end
end

describe Puppet::Node::Ldap, " when deciding attributes to search for" do
    before do
        @searcher = Puppet::Node::Ldap.new
    end

    it "should use 'nil' if the :ldapattrs setting is 'all'" do
        Puppet.stubs(:[]).with(:ldapattrs).returns("all")
        @searcher.search_attributes.should be_nil
    end

    it "should split the value of :ldapattrs on commas and use the result as the attribute list" do
        Puppet.stubs(:[]).with(:ldapattrs).returns("one,two")
        @searcher.stubs(:class_attributes).returns([])
        @searcher.stubs(:parent_attribute).returns(nil)
        @searcher.search_attributes.should == %w{one two}
    end

    it "should add the class attributes to the search attributes if not returning all attributes" do
        Puppet.stubs(:[]).with(:ldapattrs).returns("one,two")
        @searcher.stubs(:class_attributes).returns(%w{three four})
        @searcher.stubs(:parent_attribute).returns(nil)
        # Sort them so i don't have to care about return order
        @searcher.search_attributes.sort.should == %w{one two three four}.sort
    end

    it "should add the parent attribute to the search attributes if not returning all attributes" do
        Puppet.stubs(:[]).with(:ldapattrs).returns("one,two")
        @searcher.stubs(:class_attributes).returns([])
        @searcher.stubs(:parent_attribute).returns("parent")
        @searcher.search_attributes.sort.should == %w{one two parent}.sort
    end

    it "should not add nil parent attributes to the search attributes" do
        Puppet.stubs(:[]).with(:ldapattrs).returns("one,two")
        @searcher.stubs(:class_attributes).returns([])
        @searcher.stubs(:parent_attribute).returns(nil)
        @searcher.search_attributes.should == %w{one two}
    end
end