pairing with luke, nagios_command provider skeleton
[vinpup.git] / spec / unit / indirector / module_files.rb
blob9011530dd4f6f62d53cfb19895e789e0c18ea384
1 #!/usr/bin/env ruby
3 #  Created by Luke Kanies on 2007-10-19.
4 #  Copyright (c) 2007. All rights reserved.
6 require File.dirname(__FILE__) + '/../../spec_helper'
8 require 'puppet/indirector/module_files'
10 module ModuleFilesTerminusTesting
11     def setup
12         Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
13         Puppet::Indirector::Terminus.stubs(:register_terminus_class)
14         @model = mock 'model'
15         @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model
16         Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)
18         @module_files_class = Class.new(Puppet::Indirector::ModuleFiles) do
19             def self.to_s
20                 "Testing::Mytype"
21             end
22         end
24         @module_files = @module_files_class.new
26         @uri = "puppetmounts://host/modules/my/local/file"
27         @module = Puppet::Module.new("mymod", "/module/path")
28     end
29 end
31 describe Puppet::Indirector::ModuleFiles, " when finding files" do
32     include ModuleFilesTerminusTesting
34     it "should strip off the leading '/modules' mount name" do
35         Puppet::Module.expects(:find).with('my', "myenv").returns @module
36         @module_files.find(@uri)
37     end
39     it "should not strip off leading terms that start with '/modules' but are longer words" do
40         Puppet::Module.expects(:find).with('modulestart', "myenv").returns nil
41         @module_files.find("puppetmounts://host/modulestart/my/local/file")
42     end
44     it "should search for a module whose name is the first term in the remaining file path" do
45         Puppet::Module.expects(:find).with('my', "myenv").returns @module
46         @module_files.find(@uri)
47     end
49     it "should search for a file relative to the module's files directory" do
50         Puppet::Module.expects(:find).with('my', "myenv").returns @module
51         FileTest.expects(:exists?).with("/module/path/files/local/file")
52         @module_files.find(@uri)
53     end
55     it "should return nil if the module does not exist" do
56         Puppet::Module.expects(:find).with('my', "myenv").returns nil
57         @module_files.find(@uri).should be_nil
58     end
60     it "should return nil if the module exists but the file does not" do
61         Puppet::Module.expects(:find).with('my', "myenv").returns @module
62         FileTest.expects(:exists?).with("/module/path/files/local/file").returns(false)
63         @module_files.find(@uri).should be_nil
64     end
66     it "should return an instance of the model if a module is found and the file exists" do
67         Puppet::Module.expects(:find).with('my', "myenv").returns @module
68         FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true)
69         @model.expects(:new).returns(:myinstance)
70         @module_files.find(@uri).should == :myinstance
71     end
73     it "should use the node's environment to look up the module if the node name is provided" do
74         node = stub "node", :environment => "testing"
75         Puppet::Node.expects(:find).with("mynode").returns(node)
76         Puppet::Module.expects(:find).with('my', "testing")
77         @module_files.find(@uri, :node => "mynode")
78     end
80     it "should use the default environment setting to look up the module if the node name is not provided" do
81         env = stub "environment", :name => "testing"
82         Puppet::Node::Environment.stubs(:new).returns(env)
83         Puppet::Module.expects(:find).with('my', "testing")
84         @module_files.find(@uri)
85     end
86 end
88 describe Puppet::Indirector::ModuleFiles, " when returning instances" do
89     include ModuleFilesTerminusTesting
91     before do
92         Puppet::Module.expects(:find).with('my', "myenv").returns @module
93         FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true)
94         @instance = mock 'instance'
95     end
97     it "should create the instance with the key used to find the instance" do
98         @model.expects(:new).with { |key, *options| key == @uri }
99         @module_files.find(@uri)
100     end
102     it "should create the instance with the path at which the instance was found" do
103         @model.expects(:new).with { |key, options| options[:path] == "/module/path/files/local/file" }
104         @module_files.find(@uri)
105     end
107     it "should set the provided :links setting on to the instance if one is provided" do
108         @model.expects(:new).returns(@instance)
109         @instance.expects(:links=).with(:mytest)
110         @module_files.find(@uri, :links => :mytest)
111     end
113     it "should not set a :links value if no :links parameter is provided" do
114         @model.expects(:new).returns(@instance)
115         @module_files.find(@uri)
116     end
119 describe Puppet::Indirector::ModuleFiles, " when authorizing" do
120     include ModuleFilesTerminusTesting
122     before do
123         @configuration = mock 'configuration'
124         Puppet::FileServing::Configuration.stubs(:create).returns(@configuration)
125     end
127     it "should have an authorization hook" do
128         @module_files.should respond_to(:authorized?)
129     end
131     it "should deny the :destroy method" do
132         @module_files.authorized?(:destroy, "whatever").should be_false
133     end
135     it "should deny the :save method" do
136         @module_files.authorized?(:save, "whatever").should be_false
137     end
139     it "should use the file server configuration to determine authorization" do
140         @configuration.expects(:authorized?)
141         @module_files.authorized?(:find, "puppetmounts://host/my/file")
142     end
144     it "should use the path directly from the URI if it already includes /modules" do
145         @configuration.expects(:authorized?).with { |uri, *args| uri == "/modules/my/file" }
146         @module_files.authorized?(:find, "puppetmounts://host/modules/my/file")
147     end
149     it "should add /modules to the file path if it's not included in the URI" do
150         @configuration.expects(:authorized?).with { |uri, *args| uri == "/modules/my/file" }
151         @module_files.authorized?(:find, "puppetmounts://host/my/file")
152     end
154     it "should pass the node name to the file server configuration" do
155         @configuration.expects(:authorized?).with { |key, options| options[:node] == "mynode" }
156         @module_files.authorized?(:find, "puppetmounts://host/my/file", :node => "mynode")
157     end
159     it "should pass the IP address to the file server configuration" do
160         @configuration.expects(:authorized?).with { |key, options| options[:ipaddress] == "myip" }
161         @module_files.authorized?(:find, "puppetmounts://host/my/file", :ipaddress => "myip")
162     end
164     it "should return false if the file server configuration denies authorization" do
165         @configuration.expects(:authorized?).returns(false)
166         @module_files.authorized?(:find, "puppetmounts://host/my/file").should be_false
167     end
169     it "should return true if the file server configuration approves authorization" do
170         @configuration.expects(:authorized?).returns(true)
171         @module_files.authorized?(:find, "puppetmounts://host/my/file").should be_true
172     end
175 describe Puppet::Indirector::ModuleFiles, " when searching for files" do
176     include ModuleFilesTerminusTesting
178     it "should strip off the leading '/modules' mount name" do
179         Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
180         Puppet::Module.expects(:find).with('my', "myenv").returns @module
181         @module_files.search(@uri)
182     end
184     it "should not strip off leading terms that start with '/modules' but are longer words" do
185         Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
186         Puppet::Module.expects(:find).with('modulestart', "myenv").returns nil
187         @module_files.search("puppetmounts://host/modulestart/my/local/file")
188     end
190     it "should search for a module whose name is the first term in the remaining file path" do
191         Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
192         Puppet::Module.expects(:find).with('my', "myenv").returns @module
193         @module_files.search(@uri)
194     end
196     it "should search for a file relative to the module's files directory" do
197         Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
198         Puppet::Module.expects(:find).with('my', "myenv").returns @module
199         FileTest.expects(:exists?).with("/module/path/files/local/file")
200         @module_files.search(@uri)
201     end
203     it "should return nil if the module does not exist" do
204         Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
205         Puppet::Module.expects(:find).with('my', "myenv").returns nil
206         @module_files.search(@uri).should be_nil
207     end
209     it "should return nil if the module exists but the file does not" do
210         Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
211         Puppet::Module.expects(:find).with('my', "myenv").returns @module
212         FileTest.expects(:exists?).with("/module/path/files/local/file").returns(false)
213         @module_files.search(@uri).should be_nil
214     end
216     it "should use the node's environment to look up the module if the node name is provided" do
217         node = stub "node", :environment => "testing"
218         Puppet::Node.expects(:find).with("mynode").returns(node)
219         Puppet::Module.expects(:find).with('my', "testing")
220         @module_files.search(@uri, :node => "mynode")
221     end
223     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
224         env = stub 'env', :name => "testing"
225         Puppet::Node::Environment.stubs(:new).returns(env)
226         Puppet::Module.expects(:find).with('my', "testing")
227         @module_files.search(@uri)
228     end
230     it "should use :path2instances from the terminus_helper to return instances if a module is found and the file exists" do
231         Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
232         Puppet::Module.expects(:find).with('my', "myenv").returns @module
233         FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true)
234         @module_files.expects(:path2instances).with(@uri, "/module/path/files/local/file", {})
235         @module_files.search(@uri)
236     end
238     it "should pass any options on to :path2instances" do
239         Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv"))
240         Puppet::Module.expects(:find).with('my', "myenv").returns @module
241         FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true)
242         @module_files.expects(:path2instances).with(@uri, "/module/path/files/local/file", :testing => :one, :other => :two)
243         @module_files.search(@uri, :testing => :one, :other => :two)
244     end