~ubuntu-branches/ubuntu/lucid/puppet/lucid-security

« back to all changes in this revision

Viewing changes to spec/unit/file_serving/mount/file.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
require File.dirname(__FILE__) + '/../../../spec_helper'
 
4
require 'puppet/file_serving/mount/file'
 
5
 
 
6
module FileServingMountTesting
 
7
    def stub_facter(hostname)
 
8
        Facter.stubs(:value).with("hostname").returns(hostname.sub(/\..+/, ''))
 
9
        Facter.stubs(:value).with("domain").returns(hostname.sub(/^[^.]+\./, ''))
 
10
    end
 
11
end
 
12
 
 
13
describe Puppet::FileServing::Mount::File do
 
14
    it "should provide a method for clearing its cached host information" do
 
15
        old = Puppet::FileServing::Mount::File.localmap
 
16
        Puppet::Util::Cacher.expire
 
17
        Puppet::FileServing::Mount::File.localmap.should_not equal(old)
 
18
    end
 
19
 
 
20
    it "should be invalid if it does not have a path" do
 
21
        lambda { Puppet::FileServing::Mount::File.new("foo").validate }.should raise_error(ArgumentError)
 
22
    end
 
23
 
 
24
    it "should be valid if it has a path" do
 
25
        FileTest.stubs(:directory?).returns true
 
26
        FileTest.stubs(:readable?).returns true
 
27
        mount = Puppet::FileServing::Mount::File.new("foo")
 
28
        mount.path = "/foo"
 
29
        lambda { mount.validate }.should_not raise_error(ArgumentError)
 
30
    end
 
31
end
 
32
 
 
33
describe Puppet::FileServing::Mount::File, " when setting the path" do
 
34
    before do
 
35
        @mount = Puppet::FileServing::Mount::File.new("test")
 
36
        @dir = "/this/path/does/not/exist"
 
37
    end
 
38
 
 
39
    it "should fail if the path is not a directory" do
 
40
        FileTest.expects(:directory?).returns(false)
 
41
        proc { @mount.path = @dir }.should raise_error(ArgumentError)
 
42
    end
 
43
 
 
44
    it "should fail if the path is not readable" do
 
45
        FileTest.expects(:directory?).returns(true)
 
46
        FileTest.expects(:readable?).returns(false)
 
47
        proc { @mount.path = @dir }.should raise_error(ArgumentError)
 
48
    end
 
49
end
 
50
 
 
51
describe Puppet::FileServing::Mount::File, " when substituting hostnames and ip addresses into file paths" do
 
52
    include FileServingMountTesting
 
53
 
 
54
    before do
 
55
        FileTest.stubs(:directory?).returns(true)
 
56
        FileTest.stubs(:readable?).returns(true)
 
57
        @mount = Puppet::FileServing::Mount::File.new("test")
 
58
        @host = "host.domain.com"
 
59
    end
 
60
 
 
61
    it "should replace incidences of %h in the path with the client's short name" do
 
62
        @mount.path = "/dir/%h/yay"
 
63
        @mount.path(@host).should == "/dir/host/yay"
 
64
    end
 
65
 
 
66
    it "should replace incidences of %H in the path with the client's fully qualified name" do
 
67
        @mount.path = "/dir/%H/yay"
 
68
        @mount.path(@host).should == "/dir/host.domain.com/yay"
 
69
    end
 
70
 
 
71
    it "should replace incidences of %d in the path with the client's domain name" do
 
72
        @mount.path = "/dir/%d/yay"
 
73
        @mount.path(@host).should == "/dir/domain.com/yay"
 
74
    end
 
75
 
 
76
    it "should perform all necessary replacements" do
 
77
        @mount.path = "/%h/%d/%H"
 
78
        @mount.path(@host).should == "/host/domain.com/host.domain.com"
 
79
    end
 
80
 
 
81
    it "should use local host information if no client data is provided" do
 
82
        stub_facter("myhost.mydomain.com")
 
83
        @mount.path = "/%h/%d/%H"
 
84
        @mount.path().should == "/myhost/mydomain.com/myhost.mydomain.com"
 
85
    end
 
86
 
 
87
    after do
 
88
        Puppet::Util::Cacher.expire
 
89
    end
 
90
end
 
91
 
 
92
describe Puppet::FileServing::Mount::File, "when determining the complete file path" do
 
93
    include FileServingMountTesting
 
94
 
 
95
    before do
 
96
        FileTest.stubs(:exist?).returns(true)
 
97
        FileTest.stubs(:directory?).returns(true)
 
98
        FileTest.stubs(:readable?).returns(true)
 
99
        @mount = Puppet::FileServing::Mount::File.new("test")
 
100
        @mount.path = "/mount"
 
101
        stub_facter("myhost.mydomain.com")
 
102
        @host = "host.domain.com"
 
103
    end
 
104
 
 
105
    it "should return nil if the file is absent" do
 
106
        FileTest.stubs(:exist?).returns(false)
 
107
        @mount.complete_path("/my/path", nil).should be_nil
 
108
    end
 
109
 
 
110
    it "should return the file path if the file is present" do
 
111
        FileTest.stubs(:exist?).with("/my/path").returns(true)
 
112
        @mount.complete_path("/my/path", nil).should == "/mount/my/path"
 
113
    end
 
114
 
 
115
    it "should treat a nil file name as the path to the mount itself" do
 
116
        FileTest.stubs(:exist?).returns(true)
 
117
        @mount.complete_path(nil, nil).should == "/mount"
 
118
    end
 
119
 
 
120
    it "should use the client host name if provided in the options" do
 
121
        @mount.path = "/mount/%h"
 
122
        @mount.complete_path("/my/path", @host).should == "/mount/host/my/path"
 
123
    end
 
124
 
 
125
    it "should perform replacements on the base path" do
 
126
        @mount.path = "/blah/%h"
 
127
        @mount.complete_path("/my/stuff", @host).should == "/blah/host/my/stuff"
 
128
    end
 
129
 
 
130
    it "should not perform replacements on the per-file path" do
 
131
        @mount.path = "/blah"
 
132
        @mount.complete_path("/%h/stuff", @host).should == "/blah/%h/stuff"
 
133
    end
 
134
 
 
135
    it "should look for files relative to its base directory" do
 
136
        @mount.complete_path("/my/stuff", @host).should == "/mount/my/stuff"
 
137
    end
 
138
end
 
139
 
 
140
describe Puppet::FileServing::Mount::File, "when finding files" do
 
141
    include FileServingMountTesting
 
142
 
 
143
    before do
 
144
        FileTest.stubs(:exist?).returns(true)
 
145
        FileTest.stubs(:directory?).returns(true)
 
146
        FileTest.stubs(:readable?).returns(true)
 
147
        @mount = Puppet::FileServing::Mount::File.new("test")
 
148
        @mount.path = "/mount"
 
149
        stub_facter("myhost.mydomain.com")
 
150
        @host = "host.domain.com"
 
151
 
 
152
        @request = stub 'request', :node => "foo"
 
153
    end
 
154
 
 
155
    it "should return the results of the complete file path" do
 
156
        FileTest.stubs(:exist?).returns(false)
 
157
        @mount.expects(:complete_path).with("/my/path", "foo").returns "eh"
 
158
        @mount.find("/my/path", @request).should == "eh"
 
159
    end
 
160
end
 
161
 
 
162
describe Puppet::FileServing::Mount::File, "when searching for files" do
 
163
    include FileServingMountTesting
 
164
 
 
165
    before do
 
166
        FileTest.stubs(:exist?).returns(true)
 
167
        FileTest.stubs(:directory?).returns(true)
 
168
        FileTest.stubs(:readable?).returns(true)
 
169
        @mount = Puppet::FileServing::Mount::File.new("test")
 
170
        @mount.path = "/mount"
 
171
        stub_facter("myhost.mydomain.com")
 
172
        @host = "host.domain.com"
 
173
 
 
174
        @request = stub 'request', :node => "foo"
 
175
    end
 
176
 
 
177
    it "should return the results of the complete file path as an array" do
 
178
        FileTest.stubs(:exist?).returns(false)
 
179
        @mount.expects(:complete_path).with("/my/path", "foo").returns "eh"
 
180
        @mount.search("/my/path", @request).should == ["eh"]
 
181
    end
 
182
 
 
183
    it "should return nil if the complete path is nil" do
 
184
        FileTest.stubs(:exist?).returns(false)
 
185
        @mount.expects(:complete_path).with("/my/path", "foo").returns nil
 
186
        @mount.search("/my/path", @request).should be_nil
 
187
    end
 
188
end