summaryrefslogtreecommitdiff
path: root/spec/functional/resource/dpkg_package_spec.rb
blob: 3a09faf0d6443ae117bcda5416f003a631f8cfc3 (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
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
#
# Copyright:: Copyright 2014-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"
require "chef/mixin/shell_out"

describe Chef::Resource::DpkgPackage, :requires_root, :debian_family_only, arch: "x86_64" do
  include Chef::Mixin::ShellOut

  let(:apt_data) { File.join(CHEF_SPEC_DATA, "apt") }

  let(:test1_0) { File.join(apt_data, "chef-integration-test_1.0-1_amd64.deb") }
  let(:test1_1) { File.join(apt_data, "chef-integration-test_1.1-1_amd64.deb") }
  let(:test2_0) { File.join(apt_data, "chef-integration-test2_1.0-1_amd64.deb") }

  let(:run_context) {
    node = TEST_NODE.dup
    events = Chef::EventDispatch::Dispatcher.new
    Chef::RunContext.new(node, {}, events)
  }

  let(:dpkg_package) { Chef::Resource::DpkgPackage.new(test1_0, run_context)}

  before(:each) do
    shell_out("dpkg -P chef-integration-test chef-integration-test2")
  end

  # handles setting the name property after the initializer runs
  def set_dpkg_package_name(name)
    dpkg_package.name name
    dpkg_package.package_name name
  end

  def should_be_purged_or_removed(package, action=nil)
    status = shell_out("dpkg -s #{package}")
    output = status.stdout + status.stderr
    if action.nil? || action == :purge
      expect(output).to match(/no info|not-installed|not installed/)
    elsif action == :remove
      expect(output).to match(/deinstall ok config-files/)
    else
      raise "Unknown action"
    end
  end

  shared_examples_for "common behavior for upgrade or install" do
    it "installs a package when given only the filename as a name argument (no source)" do
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
      shell_out!("dpkg -s chef-integration-test")
    end

    it "installs a package when given the name and a source argument" do
      set_dpkg_package_name "chef-integration-test"
      dpkg_package.source test1_0
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
      shell_out!("dpkg -s chef-integration-test")
    end

    it "installs a package when given a different name and a source argument" do
      set_dpkg_package_name "some other name"
      dpkg_package.source test1_0
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
      shell_out!("dpkg -s chef-integration-test")
    end

    it "installs a package when given a path as a package_name and no source" do
      set_dpkg_package_name "chef-integration-test"
      dpkg_package.package_name test1_0
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
      shell_out!("dpkg -s chef-integration-test")
    end

    it "raises an error when the name is not a path and the source is not given" do
      set_dpkg_package_name "chef-integration-test"
      dpkg_package.package_name "chef-integration-test"
      expect { dpkg_package.run_action(action) }.to raise_error(Chef::Exceptions::Package)
    end

    it "raises an error when passed a package_name that does not exist" do
      set_dpkg_package_name File.join(test1_0, "make.it.fail")
      expect { dpkg_package.run_action(action) }.to raise_error(Chef::Exceptions::Package)
    end

    it "raises an error when passed a source that does not exist" do
      set_dpkg_package_name "chef-integration-test"
      dpkg_package.source File.join(test1_0, "make.it.fail")
      expect { dpkg_package.run_action(action) }.to raise_error(Chef::Exceptions::Package)
    end

    it "should not install an already installed package" do
      shell_out!("dpkg -i #{test1_0}")
      dpkg_package.run_action(action)
      expect(dpkg_package).not_to be_updated_by_last_action
      shell_out!("dpkg -s chef-integration-test")
    end

    it "should handle a multipackage install" do
      set_dpkg_package_name [ test1_0, test2_0 ]
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
      shell_out!("dpkg -s chef-integration-test")
      shell_out!("dpkg -s chef-integration-test2")
    end

    it "should not update multipackages that are up-to-date" do
      shell_out!("dpkg -i #{test1_0} #{test2_0}")
      set_dpkg_package_name [ test1_0, test2_0 ]
      dpkg_package.run_action(action)
      expect(dpkg_package).not_to be_updated_by_last_action
      shell_out!("dpkg -s chef-integration-test")
      shell_out!("dpkg -s chef-integration-test2")
    end

    it "should install the second if the first is installed" do
      shell_out!("dpkg -i #{test1_0}")
      set_dpkg_package_name [ test1_0, test2_0 ]
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
      shell_out!("dpkg -s chef-integration-test")
      shell_out!("dpkg -s chef-integration-test2")
    end

    it "should install the first if the second is installed" do
      shell_out!("dpkg -i #{test2_0}")
      set_dpkg_package_name [ test1_0, test2_0 ]
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
      shell_out!("dpkg -s chef-integration-test")
      shell_out!("dpkg -s chef-integration-test2")
    end
  end

  context "action :install" do
    let(:action) { :install }
    it_behaves_like "common behavior for upgrade or install"

    it "should not upgrade a package" do
      shell_out!("dpkg -i #{test1_0}")
      set_dpkg_package_name test1_1
      dpkg_package.run_action(action)
      expect(dpkg_package).not_to be_updated_by_last_action
    end

    it "should not upgrade on a multipackage install" do
      shell_out!("dpkg -i #{test1_0} #{test2_0}")
      set_dpkg_package_name [ test1_1, test2_0 ]
      dpkg_package.run_action(action)
      expect(dpkg_package).not_to be_updated_by_last_action
    end
  end

  context "action :upgrade" do
    let(:action) { :upgrade }
    it_behaves_like "common behavior for upgrade or install"

    it "should upgrade a package" do
      shell_out!("dpkg -i #{test1_0}")
      set_dpkg_package_name test1_1
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
    end

    it "should upgrade on a multipackage install" do
      shell_out!("dpkg -i #{test1_0} #{test2_0}")
      set_dpkg_package_name [ test1_1, test2_0 ]
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
    end
  end

  shared_examples_for "common behavior for remove or purge" do
    it "should remove a package that is installed when the name is a source" do
      shell_out!("dpkg -i #{test1_0}")
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
      should_be_purged_or_removed("chef-integration-test")
    end

    it "should do nothing if the package is not installed when the name is a source" do
      dpkg_package.run_action(action)
      expect(dpkg_package).not_to be_updated_by_last_action
      should_be_purged_or_removed("chef-integration-test")
    end

    it "should remove a package that is installed when the name is the package name and source is nil" do
      shell_out!("dpkg -i #{test1_0}")
      set_dpkg_package_name "chef-integration-test"
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
      should_be_purged_or_removed("chef-integration-test")
    end

    it "should do nothing if the package is not installed when the name is the package name and the source is nil" do
      set_dpkg_package_name "chef-integration-test"
      dpkg_package.run_action(action)
      expect(dpkg_package).not_to be_updated_by_last_action
      should_be_purged_or_removed("chef-integration-test")
    end

    it "should remove a package that is installed when the name is changed but the source is a package" do
      shell_out!("dpkg -i #{test1_0}")
      set_dpkg_package_name "some other name"
      dpkg_package.source test1_0
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
      should_be_purged_or_removed("chef-integration-test")
    end

    it "should do nothing if the package is not installed when the name is changed but the source is a package" do
      set_dpkg_package_name "some other name"
      dpkg_package.source test1_0
      dpkg_package.run_action(action)
      expect(dpkg_package).not_to be_updated_by_last_action
      should_be_purged_or_removed("chef-integration-test")
    end

    it "should remove a package if the name is a file that does not exist, but the source exists" do
      shell_out!("dpkg -i #{test1_0}")
      dpkg_package.name "whatever"
      dpkg_package.package_name File.join(test1_0, "make.it.fail")
      dpkg_package.source test1_0
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
      should_be_purged_or_removed("chef-integration-test")
    end

    it "should do nothing if the package is not installed when the name is a file that does not exist, but the source exists" do
      set_dpkg_package_name "some other name"
      dpkg_package.name "whatever"
      dpkg_package.package_name File.join(test1_0, "make.it.fail")
      dpkg_package.source test1_0
      dpkg_package.run_action(action)
      expect(dpkg_package).not_to be_updated_by_last_action
      should_be_purged_or_removed("chef-integration-test")
    end

    it "should remove a package if the package_name is correct, but the source does not exist" do
      shell_out!("dpkg -i #{test1_0}")
      dpkg_package.name "whatever"
      dpkg_package.package_name "chef-integration-test"
      dpkg_package.source File.join(test1_0, "make.it.fail")
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
      should_be_purged_or_removed("chef-integration-test")
    end

    it "should do nothing if the package_name is correct, but the source does not exist, and the package is not installed" do
      dpkg_package.name "whatever"
      dpkg_package.package_name "chef-integration-test"
      dpkg_package.source File.join(test1_0, "make.it.fail")
      dpkg_package.run_action(action)
      expect(dpkg_package).not_to be_updated_by_last_action
      should_be_purged_or_removed("chef-integration-test")
    end

    it "should remove both packages when called with two" do
      shell_out!("dpkg -i #{test1_0} #{test2_0}")
      set_dpkg_package_name [ "chef-integration-test", "chef-integration-test2" ]
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
      should_be_purged_or_removed("chef-integration-test")
      should_be_purged_or_removed("chef-integration-test2", action)
    end

    it "should remove a package when only the first one is installed" do
      shell_out!("dpkg -i #{test1_0}")
      set_dpkg_package_name [ "chef-integration-test", "chef-integration-test2" ]
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
      should_be_purged_or_removed("chef-integration-test")
      should_be_purged_or_removed("chef-integration-test2")
    end

    it "should remove a package when only the second one is installed" do
      shell_out!("dpkg -i #{test2_0}")
      set_dpkg_package_name [ "chef-integration-test", "chef-integration-test2" ]
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
      should_be_purged_or_removed("chef-integration-test")
      should_be_purged_or_removed("chef-integration-test2", action)
    end

    it "should do nothing when both packages are not installed" do
      set_dpkg_package_name [ "chef-integration-test", "chef-integration-test2" ]
      dpkg_package.run_action(action)
      expect(dpkg_package).not_to be_updated_by_last_action
      should_be_purged_or_removed("chef-integration-test")
      should_be_purged_or_removed("chef-integration-test2")
    end
  end

  context "action :remove" do
    let(:action) { :remove }
    it_behaves_like "common behavior for remove or purge"

    it "should not remove a removed package when the name is a source" do
      # the "test2" file has a conffile declared in it
      shell_out!("dpkg -i #{test2_0}")
      shell_out!("dpkg -r chef-integration-test2")
      set_dpkg_package_name "chef-integration-test2"
      dpkg_package.run_action(action)
      expect(dpkg_package).not_to be_updated_by_last_action
      shell_out!("dpkg -s chef-integration-test2")  # its still 'installed'
    end
  end

  context "action :purge" do
    let(:action) { :purge }
    it_behaves_like "common behavior for remove or purge"

    it "should purge a removed package when the name is a source" do
      # the "test2" file has a conffile declared in it
      shell_out!("dpkg -i #{test2_0}")
      shell_out!("dpkg -r chef-integration-test2")
      set_dpkg_package_name "chef-integration-test2"
      dpkg_package.run_action(action)
      expect(dpkg_package).to be_updated_by_last_action
      should_be_purged_or_removed("chef-integration-test2", action)
    end
  end
end