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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
#
# Author:: Joshua Timberman (<joshua@chef.io>)
# Author:: Daniel DeLeo (<dan@chef.io>)
# Copyright:: Copyright 2008-2016, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require "spec_helper"
describe Chef::Provider::Package::Rpm do
let(:provider) { Chef::Provider::Package::Rpm.new(new_resource, run_context) }
let(:node) { Chef::Node.new }
let(:events) { Chef::EventDispatch::Dispatcher.new }
let(:run_context) { Chef::RunContext.new(node, {}, events) }
let(:package_source) { "/tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm" }
let(:package_name) { "ImageMagick-c++" }
let(:new_resource) do
Chef::Resource::Package.new(package_name).tap do |resource|
resource.source(package_source)
end
end
# `rpm -qp [stuff] $source`
let(:rpm_qp_status) { instance_double("Mixlib::ShellOut", exitstatus: rpm_qp_exitstatus, stdout: rpm_qp_stdout) }
# `rpm -q [stuff] $package_name`
let(:rpm_q_status) { instance_double("Mixlib::ShellOut", exitstatus: rpm_q_exitstatus, stdout: rpm_q_stdout) }
before(:each) do
allow(::File).to receive(:exists?).with("PLEASE STUB File.exists? EXACTLY").and_return(true)
# Ensure all shell out usage is stubbed with exact arguments
allow(provider).to receive(:shell_out!).with("PLEASE STUB YOUR SHELLOUT CALLS").and_return(nil)
allow(provider).to receive(:shell_out).with("PLEASE STUB YOUR SHELLOUT CALLS").and_return(nil)
end
describe "when the package source is not valid" do
context "when source is not defiend" do
let(:new_resource) { Chef::Resource::Package.new("ImageMagick-c++") }
it "should raise an exception when attempting any action" do
expect { provider.run_action(:any) }.to raise_error(Chef::Exceptions::Package)
end
end
context "when the source is a file that doesn't exist" do
it "should raise an exception when attempting any action" do
allow(::File).to receive(:exists?).with(package_source).and_return(false)
expect { provider.run_action(:any) }.to raise_error(Chef::Exceptions::Package)
end
end
context "when the source is an unsupported URI scheme" do
let(:package_source) { "foobar://example.com/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm" }
it "should raise an exception if an uri formed source is non-supported scheme" do
allow(::File).to receive(:exists?).with(package_source).and_return(false)
# verify let bindings are as we expect
expect(new_resource.source).to eq("foobar://example.com/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm")
expect(provider.load_current_resource).to be_nil
expect { provider.run_action(:any) }.to raise_error(Chef::Exceptions::Package)
end
end
end
describe "when the package source is valid" do
before do
expect(provider).to receive(:shell_out!).
with("rpm -qp --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' #{package_source}", timeout: 900).
and_return(rpm_qp_status)
expect(provider).to receive(:shell_out).
with("rpm -q --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' #{package_name}", timeout: 900).
and_return(rpm_q_status)
end
context "when rpm fails when querying package installed state" do
before do
allow(::File).to receive(:exists?).with(package_source).and_return(true)
end
let(:rpm_qp_stdout) { "ImageMagick-c++ 6.5.4.7-7.el6_5" }
let(:rpm_q_stdout) { "" }
let(:rpm_qp_exitstatus) { 0 }
let(:rpm_q_exitstatus) { -1 }
it "raises an exception when attempting any action" do
expected_message = "Unable to determine current version due to RPM failure."
expect { provider.run_action(:install) }.to raise_error do |error|
expect(error).to be_a_kind_of(Chef::Exceptions::Package)
expect(error.to_s).to include(expected_message)
end
end
end
context "when the package is installed" do
let(:rpm_qp_stdout) { "ImageMagick-c++ 6.5.4.7-7.el6_5" }
let(:rpm_q_stdout) { "ImageMagick-c++ 6.5.4.7-7.el6_5" }
let(:rpm_qp_exitstatus) { 0 }
let(:rpm_q_exitstatus) { 0 }
let(:action) { :install }
context "when the source is a file system path" do
before do
allow(::File).to receive(:exists?).with(package_source).and_return(true)
provider.action = action
provider.load_current_resource
provider.define_resource_requirements
provider.process_resource_requirements
end
it "should get the source package version from rpm if provided" do
expect(provider.current_resource.package_name).to eq("ImageMagick-c++")
expect(provider.new_resource.version).to eq("6.5.4.7-7.el6_5")
end
it "should return the current version installed if found by rpm" do
expect(provider.current_resource.version).to eq("6.5.4.7-7.el6_5")
end
describe "action install" do
context "when at the desired version already" do
it "does nothing when the correct version is installed" do
expect(provider).to_not receive(:shell_out!).with("rpm -i /tmp/imagemagick-c++-6.5.4.7-7.el6_5.x86_64.rpm", timeout: 900)
provider.action_install
end
end
context "when a newer version is desired" do
let(:rpm_q_stdout) { "imagemagick-c++ 0.5.4.7-7.el6_5" }
it "runs rpm -u with the package source to upgrade" do
expect(provider).to receive(:shell_out!).with("rpm -U /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm", timeout: 900)
provider.action_install
end
end
context "when an older version is desired" do
let(:new_resource) do
Chef::Resource::RpmPackage.new(package_name).tap do |r|
r.source(package_source)
r.allow_downgrade(true)
end
end
let(:rpm_q_stdout) { "imagemagick-c++ 21.4-19.el6_5" }
it "should run rpm -u --oldpackage with the package source to downgrade" do
expect(provider).to receive(:shell_out!).with("rpm -U --oldpackage /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm", timeout: 900)
provider.action_install
end
end
end
describe "action upgrade" do
let(:action) { :upgrade }
context "when at the desired version already" do
it "does nothing when the correct version is installed" do
expect(provider).to_not receive(:shell_out!).with("rpm -i /tmp/imagemagick-c++-6.5.4.7-7.el6_5.x86_64.rpm", timeout: 900)
provider.action_upgrade
end
end
context "when a newer version is desired" do
let(:rpm_q_stdout) { "imagemagick-c++ 0.5.4.7-7.el6_5" }
it "runs rpm -u with the package source to upgrade" do
expect(provider).to receive(:shell_out!).with("rpm -U /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm", timeout: 900)
provider.action_upgrade
end
end
context "when an older version is desired" do
let(:new_resource) do
Chef::Resource::RpmPackage.new(package_name).tap do |r|
r.source(package_source)
r.allow_downgrade(true)
end
end
let(:rpm_q_stdout) { "imagemagick-c++ 21.4-19.el6_5" }
it "should run rpm -u --oldpackage with the package source to downgrade" do
expect(provider).to receive(:shell_out!).with("rpm -U --oldpackage /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm", timeout: 900)
provider.action_upgrade
end
end
end
describe "action :remove" do
let(:action) { :remove }
it "should remove the package" do
expect(provider).to receive(:shell_out!).with("rpm -e ImageMagick-c++-6.5.4.7-7.el6_5", timeout: 900)
provider.action_remove
end
end
context "when the package name contains a tilde (chef#3503)" do
let(:package_name) { "supermarket" }
let(:package_source) { "/tmp/supermarket-1.10.1~alpha.0-1.el5.x86_64.rpm" }
let(:rpm_qp_stdout) { "supermarket 1.10.1~alpha.0-1.el5" }
let(:rpm_q_stdout) { "supermarket 1.10.1~alpha.0-1.el5" }
let(:rpm_qp_exitstatus) { 0 }
let(:rpm_q_exitstatus) { 0 }
it "should correctly determine the candidate version and installed version" do
expect(provider.current_resource.package_name).to eq("supermarket")
expect(provider.new_resource.version).to eq("1.10.1~alpha.0-1.el5")
end
end
context "when the package name contains a plus symbol (chef#3671)" do
let(:package_name) { "chef-server-core" }
let(:package_source) { "/tmp/chef-server-core-12.2.0+20150713220422-1.el6.x86_64.rpm" }
let(:rpm_qp_stdout) { "chef-server-core 12.2.0+20150713220422-1.el6" }
let(:rpm_q_stdout) { "chef-server-core 12.2.0+20150713220422-1.el6" }
let(:rpm_qp_exitstatus) { 0 }
let(:rpm_q_exitstatus) { 0 }
it "should correctly determine the candidate version and installed version" do
expect(provider.current_resource.package_name).to eq("chef-server-core")
expect(provider.new_resource.version).to eq("12.2.0+20150713220422-1.el6")
end
end
end
context "when the source is given as an URI" do
before(:each) do
allow(::File).to receive(:exists?).with(package_source).and_return(false)
provider.action = action
provider.load_current_resource
provider.define_resource_requirements
provider.process_resource_requirements
end
%w{http HTTP https HTTPS ftp FTP file FILE}.each do |scheme|
context "when the source URI uses protocol scheme '#{scheme}'" do
let(:package_source) { "#{scheme}://example.com/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm" }
it "should get the source package version from rpm if provided" do
expect(provider.current_resource.package_name).to eq("ImageMagick-c++")
expect(provider.new_resource.version).to eq("6.5.4.7-7.el6_5")
end
it "should return the current version installed if found by rpm" do
expect(provider.current_resource.version).to eq("6.5.4.7-7.el6_5")
end
end
end
end
end
context "when the package is not installed" do
let(:package_name) { "openssh-askpass" }
let(:package_source) { "/tmp/openssh-askpass-1.2.3-4.el6_5.x86_64.rpm" }
let(:rpm_qp_stdout) { "openssh-askpass 1.2.3-4.el6_5" }
let(:rpm_q_stdout) { "package openssh-askpass is not installed" }
let(:rpm_qp_exitstatus) { 0 }
let(:rpm_q_exitstatus) { 0 }
let(:action) { :install }
before do
allow(File).to receive(:exists?).with(package_source).and_return(true)
provider.action = action
provider.load_current_resource
provider.define_resource_requirements
provider.process_resource_requirements
end
it "should not detect the package name as version when not installed" do
expect(provider.current_resource.version).to be_nil
end
context "when the package name contains a tilde (chef#3503)" do
let(:package_name) { "supermarket" }
let(:package_source) { "/tmp/supermarket-1.10.1~alpha.0-1.el5.x86_64.rpm" }
let(:rpm_qp_stdout) { "supermarket 1.10.1~alpha.0-1.el5" }
let(:rpm_q_stdout) { "package supermarket is not installed" }
let(:rpm_qp_exitstatus) { 0 }
let(:rpm_q_exitstatus) { 0 }
it "should correctly determine the candidate version" do
expect(provider.new_resource.version).to eq("1.10.1~alpha.0-1.el5")
end
end
describe "managing the package" do
describe "action install" do
it "installs the package" do
expect(provider).to receive(:shell_out!).with("rpm -i #{package_source}", timeout: 900)
provider.action_install
end
context "when custom resource options are given" do
it "installs with custom options specified in the resource" do
new_resource.options("--dbpath /var/lib/rpm")
expect(provider).to receive(:shell_out!).with("rpm --dbpath /var/lib/rpm -i #{package_source}", timeout: 900)
provider.action_install
end
end
end
describe "action upgrade" do
let(:action) { :upgrade }
it "installs the package" do
expect(provider).to receive(:shell_out!).with("rpm -i #{package_source}", timeout: 900)
provider.action_upgrade
end
end
describe "when removing the package" do
let(:action) { :remove }
it "should do nothing" do
expect(provider).to_not receive(:shell_out!).with("rpm -e ImageMagick-c++-6.5.4.7-7.el6_5", timeout: 900)
provider.action_remove
end
end
end
end
end
context "when the resource name is the path to the package" do
let(:new_resource) do
# When we pass a source in as the name, then #initialize in the
# provider will call File.exists?. Because of the ordering in our
# let() bindings and such, we have to set the stub here and not in a
# before block.
allow(::File).to receive(:exists?).with(package_source).and_return(true)
Chef::Resource::Package.new("/tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm")
end
let(:current_resource) { Chef::Resource::Package.new("ImageMagick-c++") }
it "should install from a path when the package is a path and the source is nil" do
expect(new_resource.source).to eq("/tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm")
provider.current_resource = current_resource
expect(provider).to receive(:shell_out!).with("rpm -i /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm", timeout: 900)
provider.install_package("/tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm", "6.5.4.7-7.el6_5")
end
it "should uprgrade from a path when the package is a path and the source is nil" do
expect(new_resource.source).to eq("/tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm")
current_resource.version("21.4-19.el5")
provider.current_resource = current_resource
expect(provider).to receive(:shell_out!).with("rpm -U /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm", timeout: 900)
provider.upgrade_package("/tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm", "6.5.4.7-7.el6_5")
end
end
end
|