4
require 'puppet/file_serving/configuration'
6
describe Puppet::FileServing::Configuration do
7
include PuppetSpec::Files
10
@path = make_absolute("/path/to/configuration/file.conf")
11
Puppet.settings.stubs(:value).with(:trace).returns(false)
12
Puppet.settings.stubs(:value).with(:fileserverconfig).returns(@path)
16
Puppet::FileServing::Configuration.instance_variable_set(:@configuration, nil)
19
it "should make :new a private method" do
20
proc { Puppet::FileServing::Configuration.new }.should raise_error
23
it "should return the same configuration each time 'configuration' is called" do
24
Puppet::FileServing::Configuration.configuration.should equal(Puppet::FileServing::Configuration.configuration)
27
describe "when initializing" do
29
it "should work without a configuration file" do
30
FileTest.stubs(:exists?).with(@path).returns(false)
31
proc { Puppet::FileServing::Configuration.configuration }.should_not raise_error
34
it "should parse the configuration file if present" do
35
FileTest.stubs(:exists?).with(@path).returns(true)
36
@parser = mock 'parser'
37
@parser.expects(:parse).returns({})
38
Puppet::FileServing::Configuration::Parser.stubs(:new).returns(@parser)
39
Puppet::FileServing::Configuration.configuration
42
it "should determine the path to the configuration file from the Puppet settings" do
43
Puppet::FileServing::Configuration.configuration
47
describe "when parsing the configuration file" do
50
FileTest.stubs(:exists?).with(@path).returns(true)
51
@parser = mock 'parser'
52
Puppet::FileServing::Configuration::Parser.stubs(:new).returns(@parser)
55
it "should set the mount list to the results of parsing" do
56
@parser.expects(:parse).returns("one" => mock("mount"))
57
config = Puppet::FileServing::Configuration.configuration
58
config.mounted?("one").should be_true
61
it "should not raise exceptions" do
62
@parser.expects(:parse).raises(ArgumentError)
63
proc { Puppet::FileServing::Configuration.configuration }.should_not raise_error
66
it "should replace the existing mount list with the results of reparsing" do
67
@parser.expects(:parse).returns("one" => mock("mount"))
68
config = Puppet::FileServing::Configuration.configuration
69
config.mounted?("one").should be_true
71
@parser.expects(:parse).returns("two" => mock('other'))
72
config.send(:readconfig, false)
73
config.mounted?("one").should be_false
74
config.mounted?("two").should be_true
77
it "should not replace the mount list until the file is entirely parsed successfully" do
78
@parser.expects(:parse).returns("one" => mock("mount"))
79
@parser.expects(:parse).raises(ArgumentError)
80
config = Puppet::FileServing::Configuration.configuration
81
# Now parse again, so the exception gets thrown
82
config.send(:readconfig, false)
83
config.mounted?("one").should be_true
86
it "should add modules and plugins mounts even if the file does not exist" do
87
FileTest.expects(:exists?).returns false # the file doesn't exist
88
config = Puppet::FileServing::Configuration.configuration
89
config.mounted?("modules").should be_true
90
config.mounted?("plugins").should be_true
93
it "should allow all access to modules and plugins if no fileserver.conf exists" do
94
FileTest.expects(:exists?).returns false # the file doesn't exist
95
modules = stub 'modules', :empty? => true
96
Puppet::FileServing::Mount::Modules.stubs(:new).returns(modules)
97
modules.expects(:allow).with('*')
99
plugins = stub 'plugins', :empty? => true
100
Puppet::FileServing::Mount::Plugins.stubs(:new).returns(plugins)
101
plugins.expects(:allow).with('*')
103
Puppet::FileServing::Configuration.configuration
106
it "should not allow access from all to modules and plugins if the fileserver.conf provided some rules" do
107
FileTest.expects(:exists?).returns false # the file doesn't exist
109
modules = stub 'modules', :empty? => false
110
Puppet::FileServing::Mount::Modules.stubs(:new).returns(modules)
111
modules.expects(:allow).with('*').never
113
plugins = stub 'plugins', :empty? => false
114
Puppet::FileServing::Mount::Plugins.stubs(:new).returns(plugins)
115
plugins.expects(:allow).with('*').never
117
Puppet::FileServing::Configuration.configuration
120
it "should add modules and plugins mounts even if they are not returned by the parser" do
121
@parser.expects(:parse).returns("one" => mock("mount"))
122
FileTest.expects(:exists?).returns true # the file doesn't exist
123
config = Puppet::FileServing::Configuration.configuration
124
config.mounted?("modules").should be_true
125
config.mounted?("plugins").should be_true
129
describe "when finding the specified mount" do
130
it "should choose the named mount if one exists" do
131
config = Puppet::FileServing::Configuration.configuration
132
config.expects(:mounts).returns("one" => "foo")
133
config.find_mount("one", mock('env')).should == "foo"
136
it "should use the provided environment to find a matching module if the named module cannot be found" do
137
config = Puppet::FileServing::Configuration.configuration
140
env = mock 'environment'
141
env.expects(:module).with("foo").returns mod
144
config.stubs(:mounts).returns("modules" => mount)
145
Puppet::Util::Warnings.expects(:notice_once)
146
config.find_mount("foo", env).should equal(mount)
149
it "should return nil if there is no such named mount and no module with the same name exists" do
150
config = Puppet::FileServing::Configuration.configuration
152
env = mock 'environment'
153
env.expects(:module).with("foo").returns nil
156
config.stubs(:mounts).returns("modules" => mount)
157
config.find_mount("foo", env).should be_nil
161
describe "when finding the mount name and relative path in a request key" do
163
@config = Puppet::FileServing::Configuration.configuration
164
@config.stubs(:find_mount)
166
@request = stub 'request', :key => "foo/bar/baz", :options => {}, :node => nil, :environment => mock("env")
169
it "should reread the configuration" do
170
@config.expects(:readconfig)
172
@config.split_path(@request)
175
it "should treat the first field of the URI path as the mount name" do
176
@config.expects(:find_mount).with { |name, node| name == "foo" }
178
@config.split_path(@request)
181
it "should fail if the mount name is not alpha-numeric" do
182
@request.expects(:key).returns "foo&bar/asdf"
184
lambda { @config.split_path(@request) }.should raise_error(ArgumentError)
187
it "should support dashes in the mount name" do
188
@request.expects(:key).returns "foo-bar/asdf"
190
lambda { @config.split_path(@request) }.should_not raise_error(ArgumentError)
193
it "should use the mount name and environment to find the mount" do
194
@config.expects(:find_mount).with { |name, env| name == "foo" and env == @request.environment }
195
@request.stubs(:node).returns("mynode")
197
@config.split_path(@request)
200
it "should return nil if the mount cannot be found" do
201
@config.expects(:find_mount).returns nil
203
@config.split_path(@request).should be_nil
206
it "should return the mount and the relative path if the mount is found" do
207
mount = stub 'mount', :name => "foo"
208
@config.expects(:find_mount).returns mount
210
@config.split_path(@request).should == [mount, "bar/baz"]
213
it "should remove any double slashes" do
214
@request.stubs(:key).returns "foo/bar//baz"
215
mount = stub 'mount', :name => "foo"
216
@config.expects(:find_mount).returns mount
218
@config.split_path(@request).should == [mount, "bar/baz"]
221
it "should return the relative path as nil if it is an empty string" do
222
@request.expects(:key).returns "foo"
223
mount = stub 'mount', :name => "foo"
224
@config.expects(:find_mount).returns mount
226
@config.split_path(@request).should == [mount, nil]
229
it "should add 'modules/' to the relative path if the modules mount is used but not specified, for backward compatibility" do
230
@request.expects(:key).returns "foo/bar"
231
mount = stub 'mount', :name => "modules"
232
@config.expects(:find_mount).returns mount
234
@config.split_path(@request).should == [mount, "foo/bar"]