3
require File.dirname(__FILE__) + '/../../spec_helper'
5
require 'puppet/parser/files'
7
describe Puppet::Parser::Files do
8
it "should have a method for finding a template" do
9
Puppet::Parser::Files.should respond_to(:find_template)
12
it "should have a method for finding manifests" do
13
Puppet::Parser::Files.should respond_to(:find_manifests)
16
describe "when searching for templates" do
17
it "should return fully-qualified templates directly" do
18
Puppet::Parser::Files.expects(:modulepath).never
19
Puppet::Parser::Files.find_template("/my/template").should == "/my/template"
22
it "should return the template from the first found module" do
24
Puppet::Node::Environment.new.expects(:module).with("mymod").returns mod
26
mod.expects(:template).returns("/one/mymod/templates/mytemplate")
27
Puppet::Parser::Files.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate"
30
it "should return the file in the templatedir if it exists" do
31
Puppet.settings.expects(:value).with(:templatedir, nil).returns("/my/templates")
32
Puppet[:modulepath] = "/one:/two"
33
File.stubs(:directory?).returns(true)
34
FileTest.stubs(:exist?).returns(true)
35
Puppet::Parser::Files.find_template("mymod/mytemplate").should == "/my/templates/mymod/mytemplate"
38
it "should not raise an error if no valid templatedir exists and the template exists in a module" do
40
Puppet::Node::Environment.new.expects(:module).with("mymod").returns mod
42
mod.expects(:template).returns("/one/mymod/templates/mytemplate")
43
Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(nil)
45
Puppet::Parser::Files.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate"
48
it "should return unqualified templates if they exist in the template dir" do
49
FileTest.stubs(:exist?).returns true
50
Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(["/my/templates"])
51
Puppet::Parser::Files.find_template("mytemplate").should == "/my/templates/mytemplate"
54
it "should only return templates if they actually exist" do
55
FileTest.expects(:exist?).with("/my/templates/mytemplate").returns true
56
Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(["/my/templates"])
57
Puppet::Parser::Files.find_template("mytemplate").should == "/my/templates/mytemplate"
60
it "should return nil when asked for a template that doesn't exist" do
61
FileTest.expects(:exist?).with("/my/templates/mytemplate").returns false
62
Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(["/my/templates"])
63
Puppet::Parser::Files.find_template("mytemplate").should be_nil
66
it "should search in the template directories before modules" do
67
FileTest.stubs(:exist?).returns true
68
Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(["/my/templates"])
69
Puppet::Module.expects(:find).never
70
Puppet::Parser::Files.find_template("mytemplate")
73
it "should accept relative templatedirs" do
74
FileTest.stubs(:exist?).returns true
75
Puppet[:templatedir] = "my/templates"
76
File.expects(:directory?).with(File.join(Dir.getwd,"my/templates")).returns(true)
77
Puppet::Parser::Files.find_template("mytemplate").should == File.join(Dir.getwd,"my/templates/mytemplate")
80
it "should use the environment templatedir if no module is found and an environment is specified" do
81
FileTest.stubs(:exist?).returns true
82
Puppet::Parser::Files.stubs(:templatepath).with("myenv").returns(["/myenv/templates"])
83
Puppet::Parser::Files.find_template("mymod/mytemplate", "myenv").should == "/myenv/templates/mymod/mytemplate"
86
it "should use first dir from environment templatedir if no module is found and an environment is specified" do
87
FileTest.stubs(:exist?).returns true
88
Puppet::Parser::Files.stubs(:templatepath).with("myenv").returns(["/myenv/templates", "/two/templates"])
89
Puppet::Parser::Files.find_template("mymod/mytemplate", "myenv").should == "/myenv/templates/mymod/mytemplate"
92
it "should use a valid dir when templatedir is a path for unqualified templates and the first dir contains template" do
93
Puppet::Parser::Files.stubs(:templatepath).returns(["/one/templates", "/two/templates"])
94
FileTest.expects(:exist?).with("/one/templates/mytemplate").returns(true)
95
Puppet::Parser::Files.find_template("mytemplate").should == "/one/templates/mytemplate"
98
it "should use a valid dir when templatedir is a path for unqualified templates and only second dir contains template" do
99
Puppet::Parser::Files.stubs(:templatepath).returns(["/one/templates", "/two/templates"])
100
FileTest.expects(:exist?).with("/one/templates/mytemplate").returns(false)
101
FileTest.expects(:exist?).with("/two/templates/mytemplate").returns(true)
102
Puppet::Parser::Files.find_template("mytemplate").should == "/two/templates/mytemplate"
105
it "should use the node environment if specified" do
107
Puppet::Node::Environment.new("myenv").expects(:module).with("mymod").returns mod
109
mod.expects(:template).returns("/my/modules/mymod/templates/envtemplate")
111
Puppet::Parser::Files.find_template("mymod/envtemplate", "myenv").should == "/my/modules/mymod/templates/envtemplate"
114
it "should return nil if no template can be found" do
115
Puppet::Parser::Files.find_template("foomod/envtemplate", "myenv").should be_nil
118
after { Puppet.settings.clear }
121
describe "when searching for manifests" do
122
it "should ignore invalid modules" do
124
Puppet::Node::Environment.new.expects(:module).with("mymod").raises Puppet::Module::InvalidName
125
Puppet.expects(:value).with(:modulepath).never
126
Dir.stubs(:glob).returns %w{foo}
128
Puppet::Parser::Files.find_manifests("mymod/init.pp").should == %w{foo}
132
describe "when searching for manifests when no module is found" do
134
File.stubs(:find).returns(nil)
137
it "should not look for modules when paths are fully qualified" do
138
Puppet.expects(:value).with(:modulepath).never
139
file = "/fully/qualified/file.pp"
140
Dir.stubs(:glob).with(file).returns([file])
141
Puppet::Parser::Files.find_manifests(file)
144
it "should directly return fully qualified files" do
145
file = "/fully/qualified/file.pp"
146
Dir.stubs(:glob).with(file).returns([file])
147
Puppet::Parser::Files.find_manifests(file).should == [file]
150
it "should match against provided fully qualified patterns" do
151
pattern = "/fully/qualified/pattern/*"
152
Dir.expects(:glob).with(pattern).returns(%w{my file list})
153
Puppet::Parser::Files.find_manifests(pattern).should == %w{my file list}
156
it "should look for files relative to the current directory" do
158
Dir.expects(:glob).with("#{cwd}/foobar/init.pp").returns(["#{cwd}/foobar/init.pp"])
159
Puppet::Parser::Files.find_manifests("foobar/init.pp").should == ["#{cwd}/foobar/init.pp"]
162
it "should only return files, not directories" do
163
pattern = "/fully/qualified/pattern/*"
165
dir = "/my/directory"
166
Dir.expects(:glob).with(pattern).returns([file, dir])
167
FileTest.expects(:directory?).with(file).returns(false)
168
FileTest.expects(:directory?).with(dir).returns(true)
169
Puppet::Parser::Files.find_manifests(pattern).should == [file]
173
describe "when searching for manifests in a found module" do
174
it "should return the manifests from the first found module" do
176
Puppet::Node::Environment.new.expects(:module).with("mymod").returns mod
177
mod.expects(:match_manifests).with("init.pp").returns(%w{/one/mymod/manifests/init.pp})
178
Puppet::Parser::Files.find_manifests("mymod/init.pp").should == ["/one/mymod/manifests/init.pp"]
181
it "should use the node environment if specified" do
183
Puppet::Node::Environment.new("myenv").expects(:module).with("mymod").returns mod
184
mod.expects(:match_manifests).with("init.pp").returns(%w{/one/mymod/manifests/init.pp})
185
Puppet::Parser::Files.find_manifests("mymod/init.pp", :environment => "myenv").should == ["/one/mymod/manifests/init.pp"]
188
after { Puppet.settings.clear }