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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../spec_helper'
describe Puppet::Module do
[:plugins, :templates, :files, :manifests].each do |filetype|
it "should be able to indicate whether it has #{filetype}" do
Puppet::Module.new("foo", "/foo/bar").should respond_to(filetype.to_s + "?")
end
it "should correctly detect when it has #{filetype}" do
FileTest.expects(:exist?).with("/foo/bar/#{filetype}").returns true
Puppet::Module.new("foo", "/foo/bar").send(filetype.to_s + "?").should be_true
end
it "should correctly detect when it does not have #{filetype}" do
FileTest.expects(:exist?).with("/foo/bar/#{filetype}").returns false
Puppet::Module.new("foo", "/foo/bar").send(filetype.to_s + "?").should be_false
end
it "should have a method for returning the full path to the #{filetype}" do
Puppet::Module.new("foo", "/foo/bar").send(filetype.to_s).should == File.join("/foo/bar", filetype.to_s)
end
it "should be able to return individual #{filetype}" do
path = File.join("/foo/bar", filetype.to_s, "my/file")
FileTest.expects(:exist?).with(path).returns true
Puppet::Module.new("foo", "/foo/bar").send(filetype.to_s.sub(/s$/, ''), "my/file").should == path
end
it "should return nil if asked to return individual #{filetype} that don't exist" do
FileTest.expects(:exist?).with(File.join("/foo/bar", filetype.to_s, "my/file")).returns false
Puppet::Module.new("foo", "/foo/bar").send(filetype.to_s.sub(/s$/, ''), "my/file").should be_nil
end
it "should return the base directory if asked for a nil path" do
path = File.join("/foo/bar", filetype.to_s)
FileTest.expects(:exist?).with(path).returns true
Puppet::Module.new("foo", "/foo/bar").send(filetype.to_s.sub(/s$/, ''), nil).should == path
end
end
end
describe Puppet::Module, "when yielding each module in a list of directories" do
before do
FileTest.stubs(:directory?).returns true
end
it "should search for modules in each directory in the list" do
Dir.expects(:entries).with("/one").returns []
Dir.expects(:entries).with("/two").returns []
Puppet::Module.each_module("/one", "/two")
end
it "should accept the list of directories as an array" do
Dir.expects(:entries).with("/one").returns []
Dir.expects(:entries).with("/two").returns []
Puppet::Module.each_module(%w{/one /two})
end
it "should accept the list of directories joined by #{File::PATH_SEPARATOR}" do
Dir.expects(:entries).with("/one").returns []
Dir.expects(:entries).with("/two").returns []
list = %w{/one /two}.join(File::PATH_SEPARATOR)
Puppet::Module.each_module(list)
end
it "should not create modules for '.' or '..' in the provided directory list" do
Dir.expects(:entries).with("/one").returns(%w{. ..})
result = []
Puppet::Module.each_module("/one") do |mod|
result << mod
end
result.should be_empty
end
it "should not create modules for non-directories in the provided directory list" do
Dir.expects(:entries).with("/one").returns(%w{notdir})
FileTest.expects(:directory?).with("/one/notdir").returns false
result = []
Puppet::Module.each_module("/one") do |mod|
result << mod
end
result.should be_empty
end
it "should yield each found module" do
Dir.expects(:entries).with("/one").returns(%w{f1 f2})
one = mock 'one'
two = mock 'two'
Puppet::Module.expects(:new).with("f1", "/one/f1").returns one
Puppet::Module.expects(:new).with("f2", "/one/f2").returns two
result = []
Puppet::Module.each_module("/one") do |mod|
result << mod
end
result.should == [one, two]
end
it "should not yield a module with the same name as a previously yielded module" do
Dir.expects(:entries).with("/one").returns(%w{f1})
Dir.expects(:entries).with("/two").returns(%w{f1})
one = mock 'one'
Puppet::Module.expects(:new).with("f1", "/one/f1").returns one
Puppet::Module.expects(:new).with("f1", "/two/f1").never
result = []
Puppet::Module.each_module("/one", "/two") do |mod|
result << mod
end
result.should == [one]
end
end
describe Puppet::Module, " when building its search path" do
it "should use the current environment's search path if no environment is specified" do
env = mock 'env'
env.expects(:modulepath).returns "eh"
Puppet::Node::Environment.expects(:new).with(nil).returns env
Puppet::Module.modulepath.should == "eh"
end
it "should use the specified environment's search path if an environment is specified" do
env = mock 'env'
env.expects(:modulepath).returns "eh"
Puppet::Node::Environment.expects(:new).with("foo").returns env
Puppet::Module.modulepath("foo").should == "eh"
end
end
describe Puppet::Module, " when searching for modules" do
it "should use the current environment to find the specified module if no environment is provided" do
env = mock 'env'
env.expects(:module).with("foo").returns "yay"
Puppet::Node::Environment.expects(:new).with(nil).returns env
Puppet::Module.find("foo").should == "yay"
end
it "should use the specified environment to find the specified module if an environment is provided" do
env = mock 'env'
env.expects(:module).with("foo").returns "yay"
Puppet::Node::Environment.expects(:new).with("myenv").returns env
Puppet::Module.find("foo", "myenv").should == "yay"
end
end
describe Puppet::Module, " when searching for templates" do
it "should return fully-qualified templates directly" do
Puppet::Module.expects(:modulepath).never
Puppet::Module.find_template("/my/template").should == "/my/template"
end
it "should return the template from the first found module" do
mod = mock 'module'
Puppet::Node::Environment.new.expects(:module).with("mymod").returns mod
mod.expects(:template).returns("/one/mymod/templates/mytemplate")
Puppet::Module.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate"
end
it "should return the file in the templatedir if it exists" do
Puppet.settings.expects(:value).with(:templatedir, nil).returns("/my/templates")
Puppet[:modulepath] = "/one:/two"
File.stubs(:directory?).returns(true)
FileTest.stubs(:exist?).returns(true)
Puppet::Module.find_template("mymod/mytemplate").should == "/my/templates/mymod/mytemplate"
end
it "should raise an error if no valid templatedir exists" do
Puppet::Module.stubs(:templatepath).with(nil).returns(nil)
lambda { Puppet::Module.find_template("mytemplate") }.should raise_error
end
it "should not raise an error if no valid templatedir exists and the template exists in a module" do
mod = mock 'module'
Puppet::Node::Environment.new.expects(:module).with("mymod").returns mod
mod.expects(:template).returns("/one/mymod/templates/mytemplate")
Puppet::Module.stubs(:templatepath).with(nil).returns(nil)
Puppet::Module.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate"
end
it "should use the main templatedir if no module is found" do
Puppet::Module.stubs(:templatepath).with(nil).returns(["/my/templates"])
Puppet::Module.expects(:find).with("mymod", nil).returns(nil)
Puppet::Module.find_template("mymod/mytemplate").should == "/my/templates/mymod/mytemplate"
end
it "should return unqualified templates directly in the template dir" do
Puppet::Module.stubs(:templatepath).with(nil).returns(["/my/templates"])
Puppet::Module.expects(:find).never
Puppet::Module.find_template("mytemplate").should == "/my/templates/mytemplate"
end
it "should accept relative templatedirs" do
Puppet[:templatedir] = "my/templates"
File.expects(:directory?).with(File.join(Dir.getwd,"my/templates")).returns(true)
Puppet::Module.find_template("mytemplate").should == File.join(Dir.getwd,"my/templates/mytemplate")
end
it "should use the environment templatedir if no module is found and an environment is specified" do
Puppet::Module.stubs(:templatepath).with("myenv").returns(["/myenv/templates"])
Puppet::Module.expects(:find).with("mymod", "myenv").returns(nil)
Puppet::Module.find_template("mymod/mytemplate", "myenv").should == "/myenv/templates/mymod/mytemplate"
end
it "should use first dir from environment templatedir if no module is found and an environment is specified" do
Puppet::Module.stubs(:templatepath).with("myenv").returns(["/myenv/templates", "/two/templates"])
Puppet::Module.expects(:find).with("mymod", "myenv").returns(nil)
Puppet::Module.find_template("mymod/mytemplate", "myenv").should == "/myenv/templates/mymod/mytemplate"
end
it "should use a valid dir when templatedir is a path for unqualified templates and the first dir contains template" do
Puppet::Module.stubs(:templatepath).returns(["/one/templates", "/two/templates"])
FileTest.expects(:exist?).with("/one/templates/mytemplate").returns(true)
Puppet::Module.expects(:find).never
Puppet::Module.find_template("mytemplate").should == "/one/templates/mytemplate"
end
it "should use a valid dir when templatedir is a path for unqualified templates and only second dir contains template" do
Puppet::Module.stubs(:templatepath).returns(["/one/templates", "/two/templates"])
FileTest.expects(:exist?).with("/one/templates/mytemplate").returns(false)
FileTest.expects(:exist?).with("/two/templates/mytemplate").returns(true)
Puppet::Module.expects(:find).never
Puppet::Module.find_template("mytemplate").should == "/two/templates/mytemplate"
end
it "should use the node environment if specified" do
mod = mock 'module'
Puppet::Node::Environment.new("myenv").expects(:module).with("mymod").returns mod
mod.expects(:template).returns("/my/modules/mymod/templates/envtemplate")
Puppet::Module.find_template("mymod/envtemplate", "myenv").should == "/my/modules/mymod/templates/envtemplate"
end
after { Puppet.settings.clear }
end
describe Puppet::Module, " when searching for manifests when no module is found" do
before do
File.stubs(:find).returns(nil)
end
it "should not look for modules when paths are fully qualified" do
Puppet.expects(:value).with(:modulepath).never
file = "/fully/qualified/file.pp"
Dir.stubs(:glob).with(file).returns([file])
Puppet::Module.find_manifests(file)
end
it "should directly return fully qualified files" do
file = "/fully/qualified/file.pp"
Dir.stubs(:glob).with(file).returns([file])
Puppet::Module.find_manifests(file).should == [file]
end
it "should match against provided fully qualified patterns" do
pattern = "/fully/qualified/pattern/*"
Dir.expects(:glob).with(pattern).returns(%w{my file list})
Puppet::Module.find_manifests(pattern).should == %w{my file list}
end
it "should look for files relative to the current directory" do
cwd = Dir.getwd
Dir.expects(:glob).with("#{cwd}/foobar/init.pp").returns(["#{cwd}/foobar/init.pp"])
Puppet::Module.find_manifests("foobar/init.pp").should == ["#{cwd}/foobar/init.pp"]
end
it "should only return files, not directories" do
pattern = "/fully/qualified/pattern/*"
file = "/my/file"
dir = "/my/directory"
Dir.expects(:glob).with(pattern).returns([file, dir])
FileTest.expects(:directory?).with(file).returns(false)
FileTest.expects(:directory?).with(dir).returns(true)
Puppet::Module.find_manifests(pattern).should == [file]
end
end
describe Puppet::Module, " when searching for manifests in a found module" do
before do
@module = Puppet::Module.new("mymod", "/one")
end
it "should return the manifests from the first found module" do
mod = mock 'module'
Puppet::Node::Environment.new.expects(:module).with("mymod").returns mod
mod.expects(:match_manifests).with("init.pp").returns(%w{/one/mymod/manifests/init.pp})
Puppet::Module.find_manifests("mymod/init.pp").should == ["/one/mymod/manifests/init.pp"]
end
it "should use the node environment if specified" do
mod = mock 'module'
Puppet::Node::Environment.new("myenv").expects(:module).with("mymod").returns mod
mod.expects(:match_manifests).with("init.pp").returns(%w{/one/mymod/manifests/init.pp})
Puppet::Module.find_manifests("mymod/init.pp", :environment => "myenv").should == ["/one/mymod/manifests/init.pp"]
end
it "should return all manifests matching the glob pattern" do
File.stubs(:directory?).returns(true)
Dir.expects(:glob).with("/one/manifests/yay/*.pp").returns(%w{/one /two})
@module.match_manifests("yay/*.pp").should == %w{/one /two}
end
it "should not return directories" do
Dir.expects(:glob).with("/one/manifests/yay/*.pp").returns(%w{/one /two})
FileTest.expects(:directory?).with("/one").returns false
FileTest.expects(:directory?).with("/two").returns true
@module.match_manifests("yay/*.pp").should == %w{/one}
end
it "should default to the 'init.pp' file in the manifests directory" do
Dir.expects(:glob).with("/one/manifests/init.pp").returns(%w{/init.pp})
@module.match_manifests(nil).should == %w{/init.pp}
end
after { Puppet.settings.clear }
end
describe Puppet::Module, " when returning files" do
it "should return the path to the module's 'files' directory" do
mod = Puppet::Module.send(:new, "mymod", "/my/mod")
mod.files.should == "/my/mod/files"
end
end
|