summaryrefslogtreecommitdiffstats
path: root/spec/unit/indirector/module_files.rb
blob: 0f6dbb6df85c3e6af130295f84a8b33f21bbbaa0 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env ruby
#
#  Created by Luke Kanies on 2007-10-19.
#  Copyright (c) 2007. All rights reserved.

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

require 'puppet/indirector/module_files'


describe Puppet::Indirector::ModuleFiles do

  before :each do
      Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
      Puppet::Indirector::Terminus.stubs(:register_terminus_class)
      @model = mock 'model'
      @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model
      Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)

      @module_files_class = Class.new(Puppet::Indirector::ModuleFiles) do
          def self.to_s
              "Testing::Mytype"
          end
      end

      @module_files = @module_files_class.new

      @uri = "puppetmounts://host/modules/my/local/file"
      @module = Puppet::Module.new("mymod", "/module/path")
  end

    describe Puppet::Indirector::ModuleFiles, " when finding files" do

        it "should strip off the leading '/modules' mount name" do
            Puppet::Module.expects(:find).with('my', "myenv").returns @module
            @module_files.find(@uri)
        end

        it "should not strip off leading terms that start with '/modules' but are longer words" do
            Puppet::Module.expects(:find).with('modulestart', "myenv").returns nil
            @module_files.find("puppetmounts://host/modulestart/my/local/file")
        end

        it "should search for a module whose name is the first term in the remaining file path" do
            Puppet::Module.expects(:find).with('my', "myenv").returns @module
            @module_files.find(@uri)
        end

        it "should search for a file relative to the module's files directory" do
            Puppet::Module.expects(:find).with('my', "myenv").returns @module
            FileTest.expects(:exists?).with("/module/path/files/local/file")
            @module_files.find(@uri)
        end

        it "should return nil if the module does not exist" do
            Puppet::Module.expects(:find).with('my', "myenv").returns nil
            @module_files.find(@uri).should be_nil
        end

        it "should return nil if the module exists but the file does not" do
            Puppet::Module.expects(:find).with('my', "myenv").returns @module
            FileTest.expects(:exists?).with("/module/path/files/local/file").returns(false)
            @module_files.find(@uri).should be_nil
        end

        it "should return an instance of the model if a module is found and the file exists" do
            Puppet::Module.expects(:find).with('my', "myenv").returns @module
            FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true)
            @model.expects(:new).returns(:myinstance)
            @module_files.find(@uri).should == :myinstance
        end

        it "should use the node's environment to look up the module if the node name is provided" do
            node = stub "node", :environment => "testing"
            Puppet::Node.expects(:find).with("mynode").returns(node)
            Puppet::Module.expects(:find).with('my', "testing")
            @module_files.find(@uri, :node => "mynode")
        end

        it "should use the default environment setting to look up the module if the node name is not provided" do
            env = stub "environment", :name => "testing"
            Puppet::Node::Environment.stubs(:new).returns(env)
            Puppet::Module.expects(:find).with('my', "testing")
            @module_files.find(@uri)
        end
    end

    describe Puppet::Indirector::ModuleFiles, " when returning instances" do

        before do
            Puppet::Module.expects(:find).with('my', "myenv").returns @module
            FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true)
            @instance = mock 'instance'
        end

        it "should create the instance with the key used to find the instance" do
            @model.expects(:new).with { |key, *options| key == @uri }
            @module_files.find(@uri)
        end

        it "should create the instance with the path at which the instance was found" do
            @model.expects(:new).with { |key, options| options[:path] == "/module/path/files/local/file" }
            @module_files.find(@uri)
        end

        it "should set the provided :links setting on to the instance if one is provided" do
            @model.expects(:new).returns(@instance)
            @instance.expects(:links=).with(:mytest)
            @module_files.find(@uri, :links => :mytest)
        end

        it "should not set a :links value if no :links parameter is provided" do
            @model.expects(:new).returns(@instance)
            @module_files.find(@uri)
        end
    end

    describe Puppet::Indirector::ModuleFiles, " when authorizing" do

        before do
            @configuration = mock 'configuration'
            Puppet::FileServing::Configuration.stubs(:create).returns(@configuration)
        end

        it "should have an authorization hook" do
            @module_files.should respond_to(:authorized?)
        end

        it "should deny the :destroy method" do
            @module_files.authorized?(:destroy, "whatever").should be_false
        end

        it "should deny the :save method" do
            @module_files.authorized?(:save, "whatever").should be_false
        end

        it "should use the file server configuration to determine authorization" do
            @configuration.expects(:authorized?)
            @module_files.authorized?(:find, "puppetmounts://host/my/file")
        end

        it "should use the path directly from the URI if it already includes /modules" do
            @configuration.expects(:authorized?).with { |uri, *args| uri == "/modules/my/file" }
            @module_files.authorized?(:find, "puppetmounts://host/modules/my/file")
        end

        it "should add /modules to the file path if it's not included in the URI" do
            @configuration.expects(:authorized?).with { |uri, *args| uri == "/modules/my/file" }
            @module_files.authorized?(:find, "puppetmounts://host/my/file")
        end

        it "should pass the node name to the file server configuration" do
            @configuration.expects(:authorized?).with { |key, options| options[:node] == "mynode" }
            @module_files.authorized?(:find, "puppetmounts://host/my/file", :node => "mynode")
        end

        it "should pass the IP address to the file server configuration" do
            @configuration.expects(:authorized?).with { |key, options| options[:ipaddress] == "myip" }
            @module_files.authorized?(:find, "puppetmounts://host/my/file", :ipaddress => "myip")
        end

        it "should return false if the file server configuration denies authorization" do
            @configuration.expects(:authorized?).returns(false)
            @module_files.authorized?(:find, "puppetmounts://host/my/file").should be_false
        end

        it "should return true if the file server configuration approves authorization" do
            @configuration.expects(:authorized?).returns(true)
            @module_files.authorized?(:find, "puppetmounts://host/my/file").should be_true
        end
    end

    describe Puppet::Indirector::ModuleFiles, " when searching for files" do

        it "should strip off the leading '/modules' mount name" do
            Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
            Puppet::Module.expects(:find).with('my', "myenv").returns @module
            @module_files.search(@uri)
        end

        it "should not strip off leading terms that start with '/modules' but are longer words" do
            Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
            Puppet::Module.expects(:find).with('modulestart', "myenv").returns nil
            @module_files.search("puppetmounts://host/modulestart/my/local/file")
        end

        it "should search for a module whose name is the first term in the remaining file path" do
            Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
            Puppet::Module.expects(:find).with('my', "myenv").returns @module
            @module_files.search(@uri)
        end

        it "should search for a file relative to the module's files directory" do
            Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
            Puppet::Module.expects(:find).with('my', "myenv").returns @module
            FileTest.expects(:exists?).with("/module/path/files/local/file")
            @module_files.search(@uri)
        end

        it "should return nil if the module does not exist" do
            Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
            Puppet::Module.expects(:find).with('my', "myenv").returns nil
            @module_files.search(@uri).should be_nil
        end

        it "should return nil if the module exists but the file does not" do
            Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
            Puppet::Module.expects(:find).with('my', "myenv").returns @module
            FileTest.expects(:exists?).with("/module/path/files/local/file").returns(false)
            @module_files.search(@uri).should be_nil
        end

        it "should use the node's environment to look up the module if the node name is provided" do
            node = stub "node", :environment => "testing"
            Puppet::Node.expects(:find).with("mynode").returns(node)
            Puppet::Module.expects(:find).with('my', "testing")
            @module_files.search(@uri, :node => "mynode")
        end

        it "should use the default environment setting to look up the module if the node name is not provided and the environment is not set to ''" do
            env = stub 'env', :name => "testing"
            Puppet::Node::Environment.stubs(:new).returns(env)
            Puppet::Module.expects(:find).with('my', "testing")
            @module_files.search(@uri)
        end

        it "should use :path2instances from the terminus_helper to return instances if a module is found and the file exists" do
            Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
            Puppet::Module.expects(:find).with('my', "myenv").returns @module
            FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true)
            @module_files.expects(:path2instances).with(@uri, "/module/path/files/local/file", {})
            @module_files.search(@uri)
        end

        it "should pass any options on to :path2instances" do
            Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
            Puppet::Module.expects(:find).with('my', "myenv").returns @module
            FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true)
            @module_files.expects(:path2instances).with(@uri, "/module/path/files/local/file", :testing => :one, :other => :two)
            @module_files.search(@uri, :testing => :one, :other => :two)
        end
    end
end