summaryrefslogtreecommitdiff
path: root/spec/unit/provider/package/chocolatey_spec.rb
blob: 97d5f0e1384511003f61fcab783fdc3bd348c9d1 (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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#
# Author:: Adam Jacob (<adam@chef.io>)
# Copyright:: Copyright 2008-2019, 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::Chocolatey do
  let(:timeout) { 900 }

  let(:new_resource) { Chef::Resource::ChocolateyPackage.new("git") }

  let(:provider) do
    node = Chef::Node.new
    events = Chef::EventDispatch::Dispatcher.new
    run_context = Chef::RunContext.new(node, {}, events)
    Chef::Provider::Package::Chocolatey.new(new_resource, run_context)
  end

  let(:choco_install_path) { "C:\\ProgramData\\chocolatey" }
  let(:choco_exe) { "#{choco_install_path}\\bin\\choco.exe" }

  # installed packages (ConEmu is upgradable)
  let(:local_list_stdout) do
    <<~EOF
      Chocolatey v0.9.9.11
      chocolatey|0.9.9.11
      ConEmu|15.10.25.0
    EOF
  end

  before do
    allow(provider).to receive(:choco_install_path).and_return(choco_install_path)
    allow(provider).to receive(:choco_exe).and_return(choco_exe)
    local_list_obj = double(stdout: local_list_stdout)
    allow(provider).to receive(:shell_out_compacted!).with(choco_exe, "list", "-l", "-r", { returns: [0, 2], timeout: timeout }).and_return(local_list_obj)
  end

  def allow_remote_list(package_names, args = nil)
    remote_list_stdout = <<~EOF
      Chocolatey v0.9.9.11
      chocolatey|0.9.9.11
      ConEmu|15.10.25.1
      Git|2.6.1
      Git|2.6.2
      munin-node|1.6.1.20130823
    EOF
    remote_list_obj = double(stdout: remote_list_stdout)
    package_names.each do |pkg|
      if args
        allow(provider).to receive(:shell_out_compacted!).with(choco_exe, "list", "-r", pkg, *args, { returns: [0, 2], timeout: timeout }).and_return(remote_list_obj)
      else
        allow(provider).to receive(:shell_out_compacted!).with(choco_exe, "list", "-r", pkg, { returns: [0, 2], timeout: timeout }).and_return(remote_list_obj)
      end
    end
  end

  describe "#initialize" do
    it "should return the correct class" do
      expect(provider).to be_kind_of(Chef::Provider::Package::Chocolatey)
    end

    it "should support arrays" do
      expect(provider.use_multipackage_api?).to be true
    end
  end

  describe "#candidate_version" do
    it "should set the candidate_version to the latest version when not pinning" do
      allow_remote_list(["git"])
      expect(provider.candidate_version).to eql(["2.6.2"])
    end

    it "should set the candidate_version to pinned version if available" do
      allow_remote_list(["git"])
      new_resource.version("2.6.1")
      expect(provider.candidate_version).to eql(["2.6.1"])
    end

    it "should set the candidate_version to nil if there is no candidate" do
      allow_remote_list(["vim"])
      new_resource.package_name("vim")
      expect(provider.candidate_version).to eql([nil])
    end

    it "should set the candidate_version correctly when there are two packages to install" do
      allow_remote_list(%w{ConEmu chocolatey})
      new_resource.package_name(%w{ConEmu chocolatey})
      expect(provider.candidate_version).to eql(["15.10.25.1", "0.9.9.11"])
    end

    it "should set the candidate_version correctly when only the first is installable" do
      allow_remote_list(%w{ConEmu vim})
      new_resource.package_name(%w{ConEmu vim})
      expect(provider.candidate_version).to eql(["15.10.25.1", nil])
    end

    it "should set the candidate_version correctly when only the last is installable" do
      allow_remote_list(%w{vim chocolatey})
      new_resource.package_name(%w{vim chocolatey})
      expect(provider.candidate_version).to eql([nil, "0.9.9.11"])
    end

    it "should set the candidate_version correctly when neither are is installable" do
      allow_remote_list(%w{vim ruby})
      new_resource.package_name(%w{vim ruby})
      expect(provider.candidate_version).to eql([nil, nil])
    end
  end

  describe "#load_current_resource" do
    it "should return a current_resource" do
      expect(provider.load_current_resource).to be_kind_of(Chef::Resource::ChocolateyPackage)
    end

    it "should set the current_resource#package_name" do
      provider.load_current_resource
      expect(provider.current_resource.package_name).to eql(["git"])
    end

    it "should load and downcase names in the installed_packages hash" do
      provider.load_current_resource
      expect(provider.send(:installed_packages)).to eql(
        { "chocolatey" => "0.9.9.11", "conemu" => "15.10.25.0" }
      )
    end

    it "should load and downcase names in the available_packages hash" do
      allow_remote_list(["git"])
      provider.load_current_resource
      expect(provider.send(:available_packages)).to eql(
        { "chocolatey" => "0.9.9.11", "conemu" => "15.10.25.1", "git" => "2.6.2", "munin-node" => "1.6.1.20130823" }
      )
    end

    it "installing a package that does not exist throws an error" do
      new_resource.package_name("package-does-not-exist")
      new_resource.returns([0])
      allow(provider).to receive(:shell_out_compacted!)
        .with(choco_exe, "list", "-r", "#{new_resource.package_name.first}", { returns: new_resource.returns, timeout: timeout })
        .and_raise(Mixlib::ShellOut::ShellCommandFailed, "Expected process to exit with [0], but received '2'")
      expect { provider.send(:available_packages) }.to raise_error(Mixlib::ShellOut::ShellCommandFailed, "Expected process to exit with [0], but received '2'")
    end

    it "should set the current_resource.version to nil when the package is not installed" do
      provider.load_current_resource
      expect(provider.current_resource.version).to eql([nil])
    end

    it "should set the current_resource.version to the installed version when the package is installed" do
      new_resource.package_name("ConEmu")
      provider.load_current_resource
      expect(provider.current_resource.version).to eql(["15.10.25.0"])
    end

    it "should set the current_resource.version when there are two packages that are installed" do
      new_resource.package_name(%w{ConEmu chocolatey})
      provider.load_current_resource
      expect(provider.current_resource.version).to eql(["15.10.25.0", "0.9.9.11"])
    end

    it "should set the current_resource.version correctly when only the first is installed" do
      new_resource.package_name(%w{ConEmu git})
      provider.load_current_resource
      expect(provider.current_resource.version).to eql(["15.10.25.0", nil])
    end

    it "should set the current_resource.version correctly when only the last is installed" do
      new_resource.package_name(%w{git chocolatey})
      provider.load_current_resource
      expect(provider.current_resource.version).to eql([nil, "0.9.9.11"])
    end

    it "should set the current_resource.version correctly when none are installed" do
      new_resource.package_name(%w{git vim})
      provider.load_current_resource
      expect(provider.current_resource.version).to eql([nil, nil])
    end
  end

  describe "#action_install" do
    it "should install a single package" do
      allow_remote_list(["git"])
      provider.load_current_resource
      expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "install", "-y", "git", { returns: [0, 2], timeout: timeout }).and_return(double)
      provider.run_action(:install)
      expect(new_resource).to be_updated_by_last_action
    end

    context "when changing the timeout to 3600" do
      let(:timeout) { 3600 }
      it "sets the timeout on shell_out commands" do
        allow_remote_list(["git"])
        new_resource.timeout(timeout)
        provider.load_current_resource
        expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "install", "-y", "git", { returns: [0, 2], timeout: timeout }).and_return(double)
        provider.run_action(:install)
        expect(new_resource).to be_updated_by_last_action
      end
    end

    it "should not install packages that are up-to-date" do
      allow_remote_list(["chocolatey"])
      new_resource.package_name("chocolatey")
      provider.load_current_resource
      expect(provider).not_to receive(:install_package)
      provider.run_action(:install)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "should not upgrade packages" do
      allow_remote_list(["ConEmu"])
      new_resource.package_name("ConEmu")
      provider.load_current_resource
      expect(provider).not_to receive(:install_package)
      provider.run_action(:install)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "should upgrade packages when given a version pin" do
      allow_remote_list(["ConEmu"])
      new_resource.package_name("ConEmu")
      new_resource.version("15.10.25.1")
      provider.load_current_resource
      expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "install", "-y", "--version", "15.10.25.1", "conemu", { returns: [0, 2], timeout: timeout }).and_return(double)
      provider.run_action(:install)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should handle complicated cases when the name/version array is pruned" do
      # chocolatey will be pruned by the superclass out of the args to install_package and we
      # implicitly test that we correctly pick up new_resource.version[1] instead of
      # new_version.resource[0]
      allow_remote_list(%w{chocolatey ConEmu})
      new_resource.package_name(%w{chocolatey ConEmu})
      new_resource.version([nil, "15.10.25.1"])
      provider.load_current_resource
      expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "install", "-y", "--version", "15.10.25.1", "conemu", { returns: [0, 2], timeout: timeout }).and_return(double)
      provider.run_action(:install)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should be case-insensitive" do
      allow_remote_list(["conemu"])
      new_resource.package_name("conemu")
      new_resource.version("15.10.25.1")
      provider.load_current_resource
      expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "install", "-y", "--version", "15.10.25.1", "conemu", { returns: [0, 2], timeout: timeout }).and_return(double)
      provider.run_action(:install)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should split up commands when given two packages, one with a version pin" do
      allow_remote_list(%w{ConEmu git})
      new_resource.package_name(%w{ConEmu git})
      new_resource.version(["15.10.25.1", nil])
      provider.load_current_resource
      expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "install", "-y", "--version", "15.10.25.1", "conemu", { returns: [0, 2], timeout: timeout }).and_return(double)
      expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "install", "-y", "git", { returns: [0, 2], timeout: timeout }).and_return(double)
      provider.run_action(:install)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should do multipackage installs when given two packages without constraints" do
      allow_remote_list(%w{git munin-node})
      new_resource.package_name(%w{git munin-node})
      provider.load_current_resource
      expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "install", "-y", "git", "munin-node", { returns: [0, 2], timeout: timeout }).and_return(double)
      provider.run_action(:install)
      expect(new_resource).to be_updated_by_last_action
    end

    context "when passing a source argument" do
      it "should pass options into the install command" do
        allow_remote_list(["git"], ["-source", "localpackages"])
        new_resource.source("localpackages")
        provider.load_current_resource
        expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "install", "-y", "-source", "localpackages", "git", { returns: [0, 2], timeout: timeout }).and_return(double)
        provider.run_action(:install)
        expect(new_resource).to be_updated_by_last_action
      end
    end

    it "should pass options into the install command" do
      allow_remote_list(["git"])
      new_resource.options("-force")
      provider.load_current_resource
      expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "install", "-y", "-force", "git", { returns: [0, 2], timeout: timeout }).and_return(double)
      provider.run_action(:install)
      expect(new_resource).to be_updated_by_last_action
    end

    it "installing a package that does not exist throws an error" do
      allow_remote_list(["package-does-not-exist"])
      new_resource.package_name("package-does-not-exist")
      provider.load_current_resource
      expect { provider.run_action(:install) }.to raise_error(Chef::Exceptions::Package)
    end

    it "installing multiple packages with a package that does not exist throws an error" do
      allow_remote_list(%w{git package-does-not-exist})
      new_resource.package_name(%w{git package-does-not-exist})
      provider.load_current_resource
      expect { provider.run_action(:install) }.to raise_error(Chef::Exceptions::Package)
    end

    context "alternate source" do
      it "installing a package that does not exist throws an error" do
        allow_remote_list(["package-does-not-exist"], ["-source", "alternate_source"])
        new_resource.package_name("package-does-not-exist")
        new_resource.source("alternate_source")
        provider.load_current_resource
        expect { provider.run_action(:install) }.to raise_error(Chef::Exceptions::Package)
      end
    end

    context "private source" do
      it "installing a package with valid credentials" do
        allow_remote_list(["git"], ["-source", "auth_source", "--user", "ubuntu", "--password", "ubuntu@123"])
        new_resource.source("auth_source")
        new_resource.user("ubuntu")
        new_resource.password("ubuntu@123")
        provider.load_current_resource
        expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "install", "-y", "-source", "auth_source", "--user", "ubuntu", "--password", "ubuntu@123", "git", { returns: [0, 2], timeout: timeout }).and_return(double)
        provider.run_action(:install)
        expect(new_resource).to be_updated_by_last_action
      end

      it "installing a package with invalid credentials throws an error" do
        allow_remote_list(["package-invalid-auth"], ["-source", "auth_source", "--user", "ubuntu", "--password", "ubuntu@123"])
        new_resource.package_name("package-invalid-auth")
        new_resource.source("auth_source")
        new_resource.user("ubuntu")
        new_resource.password("ubuntu@123")
        provider.load_current_resource
        expect { provider.run_action(:install) }.to raise_error(Chef::Exceptions::Package)
      end

      it "only credentials and list options pass into the list command" do
        allow_remote_list(["git"], ["-source", "auth_source", "--user", "ubuntu", "--password", "ubuntu@123", "--local-only"])
        new_resource.source("auth_source")
        new_resource.list_options("--local-only")
        new_resource.user("ubuntu")
        new_resource.password("ubuntu@123")
        provider.load_current_resource
        expect(provider.send(:available_packages)).to eql(
          { "chocolatey" => "0.9.9.11", "conemu" => "15.10.25.1", "git" => "2.6.2", "munin-node" => "1.6.1.20130823" }
        )
      end
    end
  end

  describe "#action_upgrade" do
    it "should install a package that is not installed" do
      allow_remote_list(["git"])
      provider.load_current_resource
      expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "upgrade", "-y", "git", { returns: [0, 2], timeout: timeout }).and_return(double)
      provider.run_action(:upgrade)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should upgrade a package that is installed but upgradable" do
      allow_remote_list(["ConEmu"])
      new_resource.package_name("ConEmu")
      provider.load_current_resource
      expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "upgrade", "-y", "conemu", { returns: [0, 2], timeout: timeout }).and_return(double)
      provider.run_action(:upgrade)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should be case insensitive" do
      allow_remote_list(["conemu"])
      new_resource.package_name("conemu")
      provider.load_current_resource
      expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "upgrade", "-y", "conemu", { returns: [0, 2], timeout: timeout }).and_return(double)
      provider.run_action(:upgrade)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should not install a package that is up-to-date" do
      allow_remote_list(["chocolatey"])
      new_resource.package_name("chocolatey")
      provider.load_current_resource
      expect(provider).not_to receive(:shell_out_compacted!).with(choco_exe, "upgrade", "-y", "chocolatey", { returns: [0, 2], timeout: timeout })
      provider.run_action(:upgrade)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "version pins work as well" do
      allow_remote_list(["git"])
      new_resource.version("2.6.2")
      provider.load_current_resource
      expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "upgrade", "-y", "--version", "2.6.2", "git", { returns: [0, 2], timeout: timeout })
      provider.run_action(:upgrade)
      expect(new_resource).to be_updated_by_last_action
    end

    it "upgrading multiple packages uses a single command" do
      allow_remote_list(%w{conemu git})
      new_resource.package_name(%w{conemu git})
      expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "upgrade", "-y", "conemu", "git", { returns: [0, 2], timeout: timeout }).and_return(double)
      provider.run_action(:upgrade)
      expect(new_resource).to be_updated_by_last_action
    end

    it "upgrading a package that does not exist throws an error" do
      allow_remote_list(["package-does-not-exist"])
      new_resource.package_name("package-does-not-exist")
      provider.load_current_resource
      expect { provider.run_action(:upgrade) }.to raise_error(Chef::Exceptions::Package)
    end

    it "upgrading multiple packages with a package that does not exist throws an error" do
      allow_remote_list(%w{git package-does-not-exist})
      new_resource.package_name(%w{git package-does-not-exist})
      provider.load_current_resource
      expect { provider.run_action(:upgrade) }.to raise_error(Chef::Exceptions::Package)
    end

    context "alternate source" do
      it "installing a package that does not exist throws an error" do
        allow_remote_list(["package-does-not-exist"], ["-source", "alternate_source"])
        new_resource.package_name("package-does-not-exist")
        new_resource.source("alternate_source")
        provider.load_current_resource
        expect { provider.run_action(:upgrade) }.to raise_error(Chef::Exceptions::Package)
      end
    end
  end

  describe "#action_remove" do
    it "does nothing when the package is already removed" do
      allow_remote_list(["git"])
      provider.load_current_resource
      expect(provider).not_to receive(:remove_package)
      provider.run_action(:remove)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "does nothing when all the packages are already removed" do
      allow_remote_list(%w{git package-does-not-exist})
      new_resource.package_name(%w{git package-does-not-exist})
      provider.load_current_resource
      expect(provider).not_to receive(:remove_package)
      provider.run_action(:remove)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "removes a package" do
      allow_remote_list(["ConEmu"])
      new_resource.package_name("ConEmu")
      provider.load_current_resource
      expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "uninstall", "-y", "ConEmu", { returns: [0, 2], timeout: timeout }).and_return(double)
      provider.run_action(:remove)
      expect(new_resource).to be_updated_by_last_action
    end

    it "is case-insensitive" do
      allow_remote_list(["conemu"])
      new_resource.package_name("conemu")
      provider.load_current_resource
      expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "uninstall", "-y", "conemu", { returns: [0, 2], timeout: timeout }).and_return(double)
      provider.run_action(:remove)
      expect(new_resource).to be_updated_by_last_action
    end

    it "removes a single package when its the only one installed" do
      pending "this is a bug in the superclass"
      allow_remote_list(%w{git conemu})
      new_resource.package_name(%w{git conemu})
      provider.load_current_resource
      expect(provider).to receive(:shell_out_compacted!).with(choco_exe, "uninstall", "-y", "conemu", { returns: [0, 2], timeout: timeout }).and_return(double)
      provider.run_action(:remove)
      expect(new_resource).to be_updated_by_last_action
    end
  end
end

describe "behavior when Chocolatey is not installed" do
  let(:new_resource) { Chef::Resource::ChocolateyPackage.new("git") }

  let(:provider) do
    node = Chef::Node.new
    events = Chef::EventDispatch::Dispatcher.new
    run_context = Chef::RunContext.new(node, {}, events)
    Chef::Provider::Package::Chocolatey.new(new_resource, run_context)
  end

  before do
    # the shellout sometimes returns "", but test nil to be safe.
    allow(provider).to receive(:choco_install_path).and_return(nil)
    provider.instance_variable_set("@choco_install_path", nil)

    # we don't care what this returns, but we have to let it be called.
    allow(provider).to receive(:shell_out_compacted!).and_return(double(stdout: ""))
  end

  let(:error_regex) do
    /Could not locate.*install.*cookbook.*PowerShell.*GetEnvironmentVariable/m
  end

  context "#choco_exe" do
    it "triggers a MissingLibrary exception when Chocolatey is not installed" do
      expect { provider.send(:choco_exe) }.to raise_error(Chef::Exceptions::MissingLibrary, error_regex)
    end
  end

  context "#load_current_resource" do
    it "triggers a MissingLibrary exception when Chocolatey is not installed" do
      expect { provider.load_current_resource }.to raise_error(Chef::Exceptions::MissingLibrary, error_regex)
    end
  end
end