2
# Author:: Adam Jacob (<adam@opscode.com>)
3
# Copyright:: Copyright (c) 2008 Opscode, Inc.
4
# License:: Apache License, Version 2.0
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
19
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
21
describe Chef::Provider::Package, "initialize" do
23
@node = mock("Chef::Node", :null_object => true)
24
@new_resource = mock("Chef::Resource", :null_object => true)
27
it "should return a Chef::Provider::Package object" do
28
provider = Chef::Provider::Package.new(@node, @new_resource)
29
provider.should be_a_kind_of(Chef::Provider::Package)
33
describe Chef::Provider::Package, "action_install" do
35
@node = mock("Chef::Node", :null_object => true)
36
@new_resource = mock("Chef::Resource::Package",
40
:package_name => "emacs",
44
@current_resource = mock("Chef::Resource::Package",
48
:package_name => "emacs"
50
@provider = Chef::Provider::Package.new(@node, @new_resource)
51
@provider.candidate_version = "1.0"
52
@provider.current_resource = @current_resource
53
@provider.stub!(:install_package).and_return(true)
56
it "should raise a Chef::Exceptions::Package if no version is specified, and no candidate is available" do
57
@provider.candidate_version = nil
58
lambda { @provider.action_install }.should raise_error(Chef::Exceptions::Package)
61
it "should call preseed_package if a response_file is given" do
62
@new_resource.stub!(:response_file).and_return("foo")
63
@provider.should_receive(:preseed_package).with(
65
@provider.candidate_version
67
@provider.action_install
70
it "should not call preseed_package if a response_file is not given" do
71
@provider.should_not_receive(:preseed_package)
72
@provider.action_install
75
it "should install the package at the candidate_version if it is not already installed" do
76
@provider.should_receive(:install_package).with(
78
@provider.candidate_version
80
@provider.action_install
83
it "should install the package at the version specified if it is not already installed" do
84
@new_resource.stub!(:version).and_return("1.0")
85
@provider.should_receive(:install_package).with(
89
@provider.action_install
92
it "should install the package at the version specified if a different version is installed" do
93
@new_resource.stub!(:version).and_return("1.0")
94
@current_resource.stub!(:version).and_return("0.99")
95
@provider.should_receive(:install_package).with(
99
@provider.action_install
102
it "should not install the package if it is already installed and no version is specified" do
103
@current_resource.stub!(:version).and_return("1.0")
104
@provider.should_not_receive(:install_package)
105
@provider.action_install
108
it "should not install the package if it is already installed at the version specified" do
109
@current_resource.stub!(:version).and_return("1.0")
110
@new_resource.stub!(:version).and_return("1.0")
111
@provider.should_not_receive(:install_package)
112
@provider.action_install
115
it "should call the candidate_version accessor if the package is not currently installed" do
116
@provider.should_receive(:candidate_version).and_return(true)
117
@provider.action_install
120
it "should not call the candidate_version accessor if the package is already installed and no version is specified" do
121
@current_resource.stub!(:version).and_return("1.0")
122
@provider.should_not_receive(:candidate_version)
123
@provider.action_install
126
it "should not call the candidate_version accessor if the package is already installed at the version specified" do
127
@current_resource.stub!(:version).and_return("1.0")
128
@new_resource.stub!(:version).and_return("1.0")
129
@provider.should_not_receive(:candidate_version)
130
@provider.action_install
133
it "should not call the candidate_version accessor if the package is not installed new package's version is specified" do
134
@new_resource.stub!(:version).and_return("1.0")
135
@provider.should_not_receive(:candidate_version)
136
@provider.action_install
139
it "should not call the candidate_version accessor if the package at the version specified is a different version than installed" do
140
@new_resource.stub!(:version).and_return("1.0")
141
@current_resource.stub!(:version).and_return("0.99")
142
@provider.should_not_receive(:candidate_version)
143
@provider.action_install
146
it "should set the resource to updated if it installs the package" do
147
@new_resource.should_receive(:updated=).with(true)
148
@provider.action_install
153
describe Chef::Provider::Package, "action_upgrade" do
155
@node = mock("Chef::Node", :null_object => true)
156
@new_resource = mock("Chef::Resource::Package",
157
:null_object => true,
160
:package_name => "emacs",
161
:to_s => 'package[emacs]'
163
@current_resource = mock("Chef::Resource::Package",
164
:null_object => true,
167
:package_name => "emacs"
169
@provider = Chef::Provider::Package.new(@node, @new_resource)
170
@provider.candidate_version = "1.0"
171
@provider.current_resource = @current_resource
172
@provider.stub!(:upgrade_package).and_return(true)
175
it "should upgrade the package if the current version is not the candidate version" do
176
@provider.should_receive(:upgrade_package).with(
178
@provider.candidate_version
180
@provider.action_upgrade
183
it "should set the resource to updated if it installs the package" do
184
@new_resource.should_receive(:updated=).with(true)
185
@provider.action_upgrade
188
it "should not install the package if the current version is the candidate version" do
189
@current_resource.stub!(:version).and_return("1.0")
190
@provider.should_not_receive(:upgrade_package)
191
@provider.action_upgrade
194
it "should print the word 'uninstalled' if there was no original version" do
195
@current_resource.stub!(:version).and_return(nil)
196
Chef::Log.should_receive(:info).with("Upgrading #{@new_resource} version from uninstalled to 1.0")
197
@provider.action_upgrade
201
# Oh ruby, you are so nice.
202
# Remove is the same as purge, just with a different method.
203
%w{remove purge}.each do |act|
204
act_string = "action_#{act}"
205
act_symbol = "action_#{act}".to_sym
206
act_method = "#{act}_package".to_sym
208
describe Chef::Provider::Package, act_string do
210
@node = mock("Chef::Node", :null_object => true)
211
@new_resource = mock("Chef::Resource::Package",
212
:null_object => true,
215
:package_name => "emacs"
217
@current_resource = mock("Chef::Resource::Package",
218
:null_object => true,
221
:package_name => "emacs"
223
@provider = Chef::Provider::Package.new(@node, @new_resource)
224
@provider.candidate_version = "1.0"
225
@provider.current_resource = @current_resource
226
@provider.stub!(act_method).and_return(true)
229
it "should #{act} the package if it is installed" do
230
@provider.should_receive(act_method).with(
232
@new_resource.version
234
@provider.send(act_symbol)
237
it "should #{act} the package at a specific version if it is installed at that version" do
238
@new_resource.stub!(:version).and_return("1.0")
239
@current_resource.stub!(:version).and_return("1.0")
240
@provider.should_receive(act_method).with(
242
@new_resource.version
244
@provider.send(act_symbol)
247
it "should not #{act} the package at a specific version if it is not installed at that version" do
248
@new_resource.stub!(:version).and_return("1.0")
249
@current_resource.stub!(:version).and_return("1.2")
250
@provider.should_not_receive(act_method)
251
@provider.send(act_symbol)
254
it "should not #{act} the package if it is not installed" do
255
@provider.should_not_receive(act_method)
256
@current_resource.stub!(:version).and_return(nil)
257
@provider.send(act_symbol)
260
it "should set the resource to updated if it #{act}s the package" do
261
@new_resource.should_receive(:updated=).with(true)
262
@provider.send(act_symbol)
268
%w{install upgrade remove purge}.each do |act|
269
act_string = "#{act}_package"
271
describe Chef::Provider::Package, act_string do
273
@node = mock("Chef::Node", :null_object => true)
274
@new_resource = mock("Chef::Resource::Package",
275
:null_object => true,
278
:package_name => "emacs"
280
@current_resource = mock("Chef::Resource::Package",
281
:null_object => true,
284
:package_name => "emacs"
286
@provider = Chef::Provider::Package.new(@node, @new_resource)
287
@provider.candidate_version = "1.0"
288
@provider.current_resource = @current_resource
291
it "should raise Chef::Exceptions::UnsupportedAction" do
292
lambda { @provider.send(act_string, @new_resource.name, @new_resource.version) }.should raise_error(Chef::Exceptions::UnsupportedAction)
297
describe Chef::Provider::Package, "preseed_package" do
299
@node = mock("Chef::Node", :null_object => true)
300
@new_resource = mock("Chef::Resource::Package",
301
:null_object => true,
304
:package_name => "emacs"
306
@current_resource = mock("Chef::Resource::Package",
307
:null_object => true,
310
:package_name => "emacs"
312
@provider = Chef::Provider::Package.new(@node, @new_resource)
313
@provider.candidate_version = "1.0"
314
@provider.current_resource = @current_resource
317
it "should raise Chef::Exceptions::UnsupportedAction" do
318
lambda { @provider.preseed_package(@new_resource.name, @new_resource.version, "response_file") }.should raise_error(Chef::Exceptions::UnsupportedAction)
322
describe Chef::Provider::Package, "get_preseed_file" do
324
@node = mock("Chef::Node", :null_object => true)
325
@new_resource = mock("Chef::Resource::Package",
326
:null_object => true,
329
:package_name => "java",
330
:cookbook_name => "java"
332
@provider = Chef::Provider::Package.new(@node, @new_resource)
333
@provider.candidate_version = "1.0"
334
@provider.current_resource = @current_resource
336
@remote_file = mock("Chef::Resource::RemoteFile",
337
:null_object => true,
338
:cookbook_name => "java",
339
:source => "java-6.seed",
343
@rf_provider = mock("Chef::Provider::RemoteFile",
344
:null_object => true,
345
:load_current_resource => true,
346
:action_create => true
348
Chef::Resource::RemoteFile.stub!(:new).and_return(@remote_file)
349
Chef::Platform.stub!(:find_provider_for_node).and_return(Chef::Provider::RemoteFile)
350
Chef::Provider::RemoteFile.stub!(:new).and_return(@rf_provider)
351
Chef::FileCache.stub!(:create_cache_path).and_return("/tmp")
352
Chef::FileCache.stub!(:load).and_return("/tmp/java-6.seed")
355
it "should find the full cache path" do
356
Chef::FileCache.should_receive(:create_cache_path).with("preseed/java")
357
@provider.get_preseed_file("java", "6")
360
it "should create a new RemoteFile for the response file" do
361
Chef::Resource::RemoteFile.should_receive(:new).with(
365
).and_return(@remote_file)
366
@provider.get_preseed_file("java", "6")
369
it "should set the cookbook name of the remote file to the new resources cookbook name" do
370
@remote_file.should_receive(:cookbook_name=).with(@new_resource.cookbook_name).and_return(true)
371
@provider.get_preseed_file("java", "6")
374
it "should set remote files source to the new resources response file" do
375
@remote_file.should_receive(:source).with(@new_resource.response_file).and_return(true)
376
@provider.get_preseed_file("java", "6")
379
it "should never back up the cached response file" do
380
@remote_file.should_receive(:backup).with(false).and_return(true)
381
@provider.get_preseed_file("java", "6")
384
it "should find the provider for the remote file" do
385
Chef::Platform.should_receive(:find_provider_for_node).and_return(Chef::Provider::RemoteFile)
386
@provider.get_preseed_file("java", "6")
389
it "should create a new provider for the remote file" do
390
Chef::Provider::RemoteFile.should_receive(:new).with(@node, @remote_file).and_return(@rf_provider)
391
@provider.get_preseed_file("java", "6")
394
it "should load the current resource state for the remote file" do
395
@rf_provider.should_receive(:load_current_resource)
396
@provider.get_preseed_file("java", "6")
399
it "should run the create action for the remote file" do
400
@rf_provider.should_receive(:action_create)
401
@provider.get_preseed_file("java", "6")
404
it "should check to see if the response file has been updated" do
405
@remote_file.should_receive(:updated).and_return(false)
406
@provider.get_preseed_file("java", "6")
409
it "should return false if the response file has not been updated" do
410
@provider.get_preseed_file("java", "6").should be(false)
413
it "should return the full path to the cached response file if the response file has been updated" do
414
@remote_file.should_receive(:updated).and_return(true)
415
@provider.get_preseed_file("java", "6").should == "/tmp/java-6.seed"