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

require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/file_serving/mount'

module FileServingMountTesting
    def stub_facter(hostname)
        Facter.stubs(:value).with("hostname").returns(hostname.sub(/\..+/, ''))
        Facter.stubs(:value).with("domain").returns(hostname.sub(/^[^.]+\./, ''))
    end
end

describe Puppet::FileServing::Mount do
    it "should provide a method for clearing its cached host information" do
        old = Puppet::FileServing::Mount.localmap
        Puppet::Util::Cacher.invalidate
        Puppet::FileServing::Mount.localmap.should_not equal(old)
    end
end

describe Puppet::FileServing::Mount, " when initializing" do
    it "should fail on non-alphanumeric name" do
        proc { Puppet::FileServing::Mount.new("non alpha") }.should raise_error(ArgumentError)
    end

    it "should allow dashes in its name" do
        Puppet::FileServing::Mount.new("non-alpha").name.should == "non-alpha"
    end

    it "should allow an optional path" do
        Puppet::FileServing::Mount.new("name", "/").path.should == "/"
    end
end

describe Puppet::FileServing::Mount, " when setting the path" do
    before do
        @mount = Puppet::FileServing::Mount.new("test")
        @dir = "/this/path/does/not/exist"
    end

    it "should fail if the path is not a directory" do
        FileTest.expects(:directory?).returns(false)
        proc { @mount.path = @dir }.should raise_error(ArgumentError)
    end

    it "should fail if the path is not readable" do
        FileTest.expects(:directory?).returns(true)
        FileTest.expects(:readable?).returns(false)
        proc { @mount.path = @dir }.should raise_error(ArgumentError)
    end
end

describe Puppet::FileServing::Mount, " when finding files" do
    include FileServingMountTesting

    before do
        FileTest.stubs(:directory?).returns(true)
        FileTest.stubs(:readable?).returns(true)
        @mount = Puppet::FileServing::Mount.new("test")
        @host = "host.domain.com"
    end

    it "should fail if the mount path has not been set" do
        proc { @mount.file_path("/blah") }.should raise_error(ArgumentError)
    end

    it "should replace incidences of %h in the path with the client's short name" do
        @mount.path = "/dir/%h/yay"
        @mount.path(@host).should == "/dir/host/yay"
    end

    it "should replace incidences of %H in the path with the client's fully qualified name" do
        @mount.path = "/dir/%H/yay"
        @mount.path(@host).should == "/dir/host.domain.com/yay"
    end

    it "should replace incidences of %d in the path with the client's domain name" do
        @mount.path = "/dir/%d/yay"
        @mount.path(@host).should == "/dir/domain.com/yay"
    end

    it "should perform all necessary replacements" do
        @mount.path = "/%h/%d/%H"
        @mount.path(@host).should == "/host/domain.com/host.domain.com"
    end

    it "should perform replacements on the base path" do
        @mount.path = "/blah/%h"
        @mount.file_path("/my/stuff", @host).should == "/blah/host/my/stuff"
    end

    it "should not perform replacements on the per-file path" do
        @mount.path = "/blah"
        @mount.file_path("/%h/stuff", @host).should == "/blah/%h/stuff"
    end

    it "should look for files relative to its base directory" do
        @mount.path = "/blah"
        @mount.file_path("/my/stuff", @host).should == "/blah/my/stuff"
    end

    it "should use local host information if no client data is provided" do
        stub_facter("myhost.mydomain.com")
        @mount.path = "/%h/%d/%H"
        @mount.path().should == "/myhost/mydomain.com/myhost.mydomain.com"
    end

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

describe Puppet::FileServing::Mount, " when providing file paths" do
    include FileServingMountTesting

    before do
        FileTest.stubs(:exists?).returns(true)
        FileTest.stubs(:directory?).returns(true)
        FileTest.stubs(:readable?).returns(true)
        @mount = Puppet::FileServing::Mount.new("test", "/mount/%h")
        stub_facter("myhost.mydomain.com")
        @host = "host.domain.com"
    end

    it "should return nil if the file is absent" do
        FileTest.stubs(:exists?).returns(false)
        @mount.file("/my/path").should be_nil
    end

    it "should return the file path if the file is absent" do
        FileTest.stubs(:exists?).with("/my/path").returns(true)
        @mount.file("/my/path").should == "/mount/myhost/my/path"
    end

    it "should treat a nil file name as the path to the mount itself" do
        FileTest.stubs(:exists?).returns(true)
        @mount.file(nil).should == "/mount/myhost"
    end

    it "should use the client host name if provided in the options" do
        @mount.file("/my/path", :node => @host).should == "/mount/host/my/path"
    end
end