~ubuntu-branches/ubuntu/karmic/chef/karmic

« back to all changes in this revision

Viewing changes to chef/spec/unit/provider/package_spec.rb

  • Committer: Bazaar Package Importer
  • Author(s): Joshua Timberman
  • Date: 2009-08-12 12:18:48 UTC
  • Revision ID: james.westby@ubuntu.com-20090812121848-yl38hpd7nfsl51xz
Tags: upstream-0.7.8
ImportĀ upstreamĀ versionĀ 0.7.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Author:: Adam Jacob (<adam@opscode.com>)
 
3
# Copyright:: Copyright (c) 2008 Opscode, Inc.
 
4
# License:: Apache License, Version 2.0
 
5
#
 
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
 
9
 
10
#     http://www.apache.org/licenses/LICENSE-2.0
 
11
 
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.
 
17
#
 
18
 
 
19
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
 
20
 
 
21
describe Chef::Provider::Package, "initialize" do
 
22
  before(:each) do
 
23
    @node = mock("Chef::Node", :null_object => true)
 
24
    @new_resource = mock("Chef::Resource", :null_object => true)
 
25
  end
 
26
  
 
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)
 
30
  end  
 
31
end
 
32
 
 
33
describe Chef::Provider::Package, "action_install" do
 
34
  before(:each) do
 
35
    @node = mock("Chef::Node", :null_object => true)
 
36
    @new_resource = mock("Chef::Resource::Package", 
 
37
      :null_object => true,
 
38
      :name => "emacs",
 
39
      :version => nil,
 
40
      :package_name => "emacs",
 
41
      :updated => nil,
 
42
      :response_file => nil
 
43
    )
 
44
    @current_resource = mock("Chef::Resource::Package", 
 
45
      :null_object => true,
 
46
      :name => "emacs",
 
47
      :version => nil,
 
48
      :package_name => "emacs"
 
49
    )
 
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)
 
54
  end
 
55
  
 
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)
 
59
  end
 
60
  
 
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(
 
64
      @new_resource.name, 
 
65
      @provider.candidate_version
 
66
    ).and_return(true)
 
67
    @provider.action_install
 
68
  end
 
69
  
 
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
 
73
  end
 
74
  
 
75
  it "should install the package at the candidate_version if it is not already installed" do
 
76
    @provider.should_receive(:install_package).with(
 
77
      @new_resource.name, 
 
78
      @provider.candidate_version
 
79
    ).and_return(true)
 
80
    @provider.action_install
 
81
  end 
 
82
  
 
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(
 
86
      @new_resource.name, 
 
87
      @new_resource.version
 
88
    ).and_return(true)
 
89
    @provider.action_install
 
90
  end
 
91
  
 
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(
 
96
      @new_resource.name, 
 
97
      @new_resource.version
 
98
    ).and_return(true)
 
99
    @provider.action_install
 
100
  end
 
101
  
 
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
 
106
  end 
 
107
  
 
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
 
113
  end
 
114
 
 
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
 
118
  end 
 
119
 
 
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
 
124
  end
 
125
 
 
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
 
131
  end
 
132
 
 
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
 
137
  end
 
138
 
 
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
 
144
  end
 
145
  
 
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
 
149
  end
 
150
  
 
151
end
 
152
 
 
153
describe Chef::Provider::Package, "action_upgrade" do
 
154
  before(:each) do
 
155
    @node = mock("Chef::Node", :null_object => true)
 
156
    @new_resource = mock("Chef::Resource::Package", 
 
157
      :null_object => true,
 
158
      :name => "emacs",
 
159
      :version => nil,
 
160
      :package_name => "emacs", 
 
161
      :to_s => 'package[emacs]'
 
162
    )
 
163
    @current_resource = mock("Chef::Resource::Package", 
 
164
      :null_object => true,
 
165
      :name => "emacs",
 
166
      :version => "0.99",
 
167
      :package_name => "emacs"
 
168
    )
 
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)
 
173
  end
 
174
 
 
175
  it "should upgrade the package if the current version is not the candidate version" do
 
176
    @provider.should_receive(:upgrade_package).with(
 
177
      @new_resource.name, 
 
178
      @provider.candidate_version
 
179
    ).and_return(true)
 
180
    @provider.action_upgrade
 
181
  end
 
182
  
 
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
 
186
  end
 
187
  
 
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
 
192
  end
 
193
  
 
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
 
198
  end
 
199
end
 
200
 
 
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
 
207
  
 
208
  describe Chef::Provider::Package, act_string do
 
209
    before(:each) do
 
210
      @node = mock("Chef::Node", :null_object => true)
 
211
      @new_resource = mock("Chef::Resource::Package", 
 
212
        :null_object => true,
 
213
        :name => "emacs",
 
214
        :version => nil,
 
215
        :package_name => "emacs"
 
216
      )
 
217
      @current_resource = mock("Chef::Resource::Package", 
 
218
        :null_object => true,
 
219
        :name => "emacs",
 
220
        :version => "0.99",
 
221
        :package_name => "emacs"
 
222
      )
 
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)
 
227
    end
 
228
 
 
229
    it "should #{act} the package if it is installed" do
 
230
      @provider.should_receive(act_method).with(
 
231
        @new_resource.name, 
 
232
        @new_resource.version
 
233
      ).and_return(true)
 
234
      @provider.send(act_symbol)
 
235
    end
 
236
  
 
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(
 
241
        @new_resource.name, 
 
242
        @new_resource.version
 
243
      ).and_return(true)
 
244
      @provider.send(act_symbol)
 
245
    end
 
246
  
 
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)
 
252
    end
 
253
  
 
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)
 
258
    end
 
259
  
 
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)
 
263
    end
 
264
 
 
265
  end
 
266
end
 
267
 
 
268
%w{install upgrade remove purge}.each do |act|
 
269
  act_string = "#{act}_package"
 
270
    
 
271
  describe Chef::Provider::Package, act_string do
 
272
    before(:each) do
 
273
      @node = mock("Chef::Node", :null_object => true)
 
274
      @new_resource = mock("Chef::Resource::Package", 
 
275
        :null_object => true,
 
276
        :name => "emacs",
 
277
        :version => nil,
 
278
        :package_name => "emacs"
 
279
      )
 
280
      @current_resource = mock("Chef::Resource::Package", 
 
281
        :null_object => true,
 
282
        :name => "emacs",
 
283
        :version => "0.99",
 
284
        :package_name => "emacs"
 
285
      )
 
286
      @provider = Chef::Provider::Package.new(@node, @new_resource)
 
287
      @provider.candidate_version = "1.0"
 
288
      @provider.current_resource = @current_resource
 
289
    end
 
290
    
 
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)      
 
293
    end
 
294
  end
 
295
end
 
296
 
 
297
describe Chef::Provider::Package, "preseed_package" do
 
298
  before(:each) do
 
299
    @node = mock("Chef::Node", :null_object => true)
 
300
    @new_resource = mock("Chef::Resource::Package", 
 
301
      :null_object => true,
 
302
      :name => "emacs",
 
303
      :version => nil,
 
304
      :package_name => "emacs"
 
305
    )
 
306
    @current_resource = mock("Chef::Resource::Package", 
 
307
      :null_object => true,
 
308
      :name => "emacs",
 
309
      :version => "0.99",
 
310
      :package_name => "emacs"
 
311
    )
 
312
    @provider = Chef::Provider::Package.new(@node, @new_resource)
 
313
    @provider.candidate_version = "1.0"
 
314
    @provider.current_resource = @current_resource
 
315
  end
 
316
  
 
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)      
 
319
  end
 
320
end
 
321
 
 
322
describe Chef::Provider::Package, "get_preseed_file" do
 
323
  before(:each) do
 
324
    @node = mock("Chef::Node", :null_object => true)
 
325
    @new_resource = mock("Chef::Resource::Package", 
 
326
      :null_object => true,
 
327
      :name => "java",
 
328
      :version => nil,
 
329
      :package_name => "java",
 
330
      :cookbook_name => "java"
 
331
    )
 
332
    @provider = Chef::Provider::Package.new(@node, @new_resource)
 
333
    @provider.candidate_version = "1.0"
 
334
    @provider.current_resource = @current_resource
 
335
    
 
336
    @remote_file = mock("Chef::Resource::RemoteFile", 
 
337
      :null_object => true,
 
338
      :cookbook_name => "java",
 
339
      :source => "java-6.seed",
 
340
      :backup => false,
 
341
      :updated => false
 
342
    )
 
343
    @rf_provider = mock("Chef::Provider::RemoteFile",
 
344
      :null_object => true,
 
345
      :load_current_resource => true,
 
346
      :action_create => true
 
347
    )
 
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")
 
353
  end
 
354
  
 
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")
 
358
  end
 
359
  
 
360
  it "should create a new RemoteFile for the response file" do
 
361
    Chef::Resource::RemoteFile.should_receive(:new).with(
 
362
      "/tmp/java-6.seed",
 
363
      nil,
 
364
      @node
 
365
    ).and_return(@remote_file)
 
366
    @provider.get_preseed_file("java", "6")
 
367
  end
 
368
  
 
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")
 
372
  end
 
373
  
 
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")
 
377
  end
 
378
  
 
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")
 
382
  end
 
383
  
 
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")
 
387
  end
 
388
  
 
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")
 
392
  end
 
393
  
 
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")
 
397
  end
 
398
   
 
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")
 
402
  end
 
403
  
 
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")
 
407
  end
 
408
  
 
409
  it "should return false if the response file has not been updated" do
 
410
    @provider.get_preseed_file("java", "6").should be(false)
 
411
  end
 
412
  
 
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"
 
416
  end
 
417
end