summaryrefslogtreecommitdiffstats
path: root/spec/unit/file_serving/configuration.rb
blob: c2d2e5014cb541d9214978e1bb01b010471b1d3f (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
#!/usr/bin/env ruby

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

require 'puppet/file_serving/configuration'

describe Puppet::FileServing::Configuration do
    it "should make :new a private method" do
        proc { Puppet::FileServing::Configuration.new }.should raise_error
    end

    it "should return the same configuration each time :create is called" do
        Puppet::FileServing::Configuration.create.should equal(Puppet::FileServing::Configuration.create)
    end

    it "should have a method for removing the current configuration instance" do
        old = Puppet::FileServing::Configuration.create
        Puppet::Util::Cacher.expire
        Puppet::FileServing::Configuration.create.should_not equal(old)
    end

    after do
        Puppet::Util::Cacher.expire
    end
end

describe Puppet::FileServing::Configuration do

    before :each do
        @path = "/path/to/configuration/file.conf"
        Puppet.settings.stubs(:value).with(:trace).returns(false)
        Puppet.settings.stubs(:value).with(:fileserverconfig).returns(@path)
    end

    after :each do
        Puppet::Util::Cacher.expire
    end

    describe "when initializing" do

        it "should work without a configuration file" do
            FileTest.stubs(:exists?).with(@path).returns(false)
            proc { Puppet::FileServing::Configuration.create }.should_not raise_error
        end

        it "should parse the configuration file if present" do
            FileTest.stubs(:exists?).with(@path).returns(true)
            @parser = mock 'parser'
            @parser.expects(:parse).returns({})
            Puppet::FileServing::Configuration::Parser.stubs(:new).returns(@parser)
            Puppet::FileServing::Configuration.create
        end

        it "should determine the path to the configuration file from the Puppet settings" do
            Puppet::FileServing::Configuration.create
        end
    end

    describe "when parsing the configuration file" do

        before do
            FileTest.stubs(:exists?).with(@path).returns(true)
            @parser = mock 'parser'
            Puppet::FileServing::Configuration::Parser.stubs(:new).returns(@parser)
        end

        it "should set the mount list to the results of parsing" do
            @parser.expects(:parse).returns("one" => mock("mount"))
            config = Puppet::FileServing::Configuration.create
            config.mounted?("one").should be_true
        end

        it "should not raise exceptions" do
            @parser.expects(:parse).raises(ArgumentError)
            proc { Puppet::FileServing::Configuration.create }.should_not raise_error
        end

        it "should replace the existing mount list with the results of reparsing" do
            @parser.expects(:parse).returns("one" => mock("mount"))
            config = Puppet::FileServing::Configuration.create
            config.mounted?("one").should be_true
            # Now parse again
            @parser.expects(:parse).returns("two" => mock('other'))
            config.send(:readconfig, false)
            config.mounted?("one").should be_false
            config.mounted?("two").should be_true
        end

        it "should not replace the mount list until the file is entirely parsed successfully" do
            @parser.expects(:parse).returns("one" => mock("mount"))
            @parser.expects(:parse).raises(ArgumentError)
            config = Puppet::FileServing::Configuration.create
            # Now parse again, so the exception gets thrown
            config.send(:readconfig, false)
            config.mounted?("one").should be_true
        end
    end

    describe "when finding the specified mount" do
        it "should choose the named mount if one exists" do
            config = Puppet::FileServing::Configuration.create
            config.expects(:mounts).returns("one" => "foo")
            config.find_mount("one", "mynode").should == "foo"
        end

        it "should modules mount's environment to find a matching module if the named module cannot be found" do
            config = Puppet::FileServing::Configuration.create

            mod = mock 'module'
            env = mock 'environment'
            env.expects(:module).with("foo").returns mod
            mount = mock 'mount'
            mount.expects(:environment).with("mynode").returns env

            config.stubs(:mounts).returns("modules" => mount)
            config.find_mount("foo", "mynode").should equal(mount)
        end

        it "should return nil if there is no such named mount and no module with the same name exists" do
            config = Puppet::FileServing::Configuration.create

            env = mock 'environment'
            env.expects(:module).with("foo").returns nil
            mount = mock 'mount'
            mount.expects(:environment).with("mynode").returns env

            config.stubs(:mounts).returns("modules" => mount)
            config.find_mount("foo", "mynode").should be_nil
        end
    end

    describe "when finding the mount name and relative path in a request key" do
        before do
            @config = Puppet::FileServing::Configuration.create
            @config.stubs(:find_mount)

            @request = stub 'request', :key => "puppet:///foo/bar/baz", :options => {}
        end

        it "should reread the configuration" do
            @config.expects(:readconfig)

            @config.split_path(@request)
        end

        it "should treat the first field of the URI path as the mount name" do
            @config.expects(:find_mount).with { |name, node| name == "foo" }

            @config.split_path(@request)
        end

        it "should fail if the mount name is not alpha-numeric" do
            @request.expects(:key).returns "puppet:///foo&bar/asdf"

            lambda { @config.split_path(@request) }.should raise_error(ArgumentError)
        end

        it "should support dashes in the mount name" do
            @request.expects(:key).returns "puppet:///foo-bar/asdf"

            lambda { @config.split_path(@request) }.should_not raise_error(ArgumentError)
        end

        it "should use the mount name and node to find the mount" do
            @config.expects(:find_mount).with { |name, node| name == "foo" and node == "mynode" }
            @request.options[:node] = "mynode"

            @config.split_path(@request)
        end

        it "should return nil if the mount cannot be found" do
            @config.expects(:find_mount).returns nil

            @config.split_path(@request).should be_nil
        end

        it "should return the mount and the relative path if the mount is found" do
            mount = stub 'mount', :name => "foo"
            @config.expects(:find_mount).returns mount

            @config.split_path(@request).should == [mount, "bar/baz"]
        end

        it "should remove any double slashes" do
            @request.stubs(:key).returns "puppet:///foo/bar//baz"
            mount = stub 'mount', :name => "foo"
            @config.expects(:find_mount).returns mount

            @config.split_path(@request).should == [mount, "bar/baz"]
        end

        it "should return the relative path as nil if it is an empty string" do
            @request.expects(:key).returns "puppet:///foo"
            mount = stub 'mount', :name => "foo"
            @config.expects(:find_mount).returns mount

            @config.split_path(@request).should == [mount, nil]
        end

        it "should add 'modules/' to the relative path if the modules mount is used but not specified, for backward compatibility" do
            @request.expects(:key).returns "puppet:///foo/bar"
            mount = stub 'mount', :name => "modules"
            @config.expects(:find_mount).returns mount

            @config.split_path(@request).should == [mount, "foo/bar"]
        end
    end
end