summaryrefslogtreecommitdiff
path: root/spec/unit/provider/package_spec.rb
blob: accd913ebe9139bf81b4037c516dedd37150b1c8 (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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
#
# Author:: Adam Jacob (<adam@chef.io>)
# Copyright:: Copyright 2008-2018, 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 do
  let(:node) do
    node = Chef::Node.new
    node.automatic_attrs[:platform] = :just_testing
    node.automatic_attrs[:platform_version] = :just_testing
    node
  end
  let(:events) { Chef::EventDispatch::Dispatcher.new }
  let(:logger) { double("Mixlib::Log::Child").as_null_object }
  let(:run_context) { Chef::RunContext.new(node, {}, events) }
  let(:new_resource) { Chef::Resource::Package.new("install emacs") }
  let(:current_resource) { Chef::Resource::Package.new("install emacs") }
  let(:candidate_version) { "1.0" }
  let(:provider) do
    new_resource.package_name = "emacs"
    current_resource.package_name = "emacs"
    provider = Chef::Provider::Package.new(new_resource, run_context)
    provider.current_resource = current_resource
    provider.candidate_version = candidate_version
    provider
  end

  before do
    allow(run_context).to receive(:logger).and_return(logger)
  end

  describe "when installing a package" do
    before(:each) do
      provider.current_resource = current_resource
      allow(provider).to receive(:install_package).and_return(true)
    end

    it "raises a Chef::Exceptions::InvalidResourceSpecification if both multipackage and source are provided" do
      new_resource.package_name(%w{a b})
      new_resource.source("foo")
      expect { provider.run_action(:install) }.to raise_error(Chef::Exceptions::InvalidResourceSpecification)
    end

    it "should raise a Chef::Exceptions::Package if no version is specified, and no candidate is available" do
      provider.candidate_version = nil
      expect { provider.run_action(:install) }.to raise_error(Chef::Exceptions::Package)
    end

    it "should install the package at the candidate_version if it is not already installed" do
      expect(provider).to receive(:install_package).with(
        new_resource.package_name,
        provider.candidate_version
      ).and_return(true)
      provider.run_action(:install)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should install the package at the version specified if it is not already installed" do
      new_resource.version("1.0")
      expect(provider).to receive(:install_package).with(
        new_resource.package_name,
        new_resource.version
      ).and_return(true)
      provider.run_action(:install)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should install the package at the version specified if a different version is installed" do
      new_resource.version("1.0")
      allow(current_resource).to receive(:version).and_return("0.99")
      expect(provider).to receive(:install_package).with(
        new_resource.package_name,
        new_resource.version
      ).and_return(true)
      provider.run_action(:install)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should not install the package if it is already installed and no version is specified" do
      current_resource.version("1.0")
      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 install the package if it is already installed at the version specified" do
      current_resource.version("1.0")
      new_resource.version("1.0")
      expect(provider).not_to receive(:install_package)
      provider.run_action(:install)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "should call the candidate_version accessor only once if the package is already installed and no version is specified" do
      current_resource.version("1.0")
      allow(provider).to receive(:candidate_version).and_return("1.0")
      provider.run_action(:install)
    end

    it "should call the candidate_version accessor only once if the package is already installed at the version specified" do
      current_resource.version("1.0")
      new_resource.version("1.0")
      provider.run_action(:install)
    end

    it "should set the resource to updated if it installs the package" do
      provider.run_action(:install)
      expect(new_resource).to be_updated
    end

  end

  describe "when upgrading the package" do
    before(:each) do
      allow(provider).to receive(:upgrade_package).and_return(true)
    end

    it "should upgrade the package if the current version is not the candidate version" do
      expect(provider).to receive(:upgrade_package).with(
        new_resource.package_name,
        provider.candidate_version
      ).and_return(true)
      provider.run_action(:upgrade)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should set the resource to updated if it installs the package" do
      provider.run_action(:upgrade)
      expect(new_resource).to be_updated
    end

    it "should not install the package if the current version is the candidate version" do
      current_resource.version "1.0"
      expect(provider).not_to receive(:upgrade_package)
      provider.run_action(:upgrade)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "should print the word 'uninstalled' if there was no original version" do
      allow(current_resource).to receive(:version).and_return(nil)
      expect(logger).to receive(:info).with("package[install emacs] upgraded(allow_downgrade) emacs to 1.0")
      provider.run_action(:upgrade)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should raise a Chef::Exceptions::Package if current version and candidate are nil" do
      allow(current_resource).to receive(:version).and_return(nil)
      provider.candidate_version = nil
      expect { provider.run_action(:upgrade) }.to raise_error(Chef::Exceptions::Package)
    end

    it "should not install the package if candidate version is nil" do
      current_resource.version "1.0"
      provider.candidate_version = nil
      expect(provider).not_to receive(:upgrade_package)
      provider.run_action(:upgrade)
      expect(new_resource).not_to be_updated_by_last_action
    end
  end

  describe "When removing the package" do
    before(:each) do
      allow(provider).to receive(:remove_package).and_return(true)
      current_resource.version "1.4.2"
    end

    it "should remove the package if it is installed" do
      expect(provider).to be_removing_package
      expect(provider).to receive(:remove_package).with("emacs", nil)
      provider.run_action(:remove)
      expect(new_resource).to be_updated
      expect(new_resource).to be_updated_by_last_action
    end

    it "should remove the package at a specific version if it is installed at that version" do
      new_resource.version "1.4.2"
      expect(provider).to be_removing_package
      expect(provider).to receive(:remove_package).with("emacs", "1.4.2")
      provider.run_action(:remove)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should not remove the package at a specific version if it is not installed at that version" do
      new_resource.version "1.0"
      expect(provider).not_to be_removing_package
      expect(provider).not_to receive(:remove_package)
      provider.run_action(:remove)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "should not remove the package if it is not installed" do
      expect(provider).not_to receive(:remove_package)
      allow(current_resource).to receive(:version).and_return(nil)
      provider.run_action(:remove)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "should set the resource to updated if it removes the package" do
      provider.run_action(:remove)
      expect(new_resource).to be_updated
    end

  end

  describe "When purging the package" do
    before(:each) do
      allow(provider).to receive(:purge_package).and_return(true)
      current_resource.version "1.4.2"
    end

    it "should purge the package if it is installed" do
      expect(provider).to be_removing_package
      expect(provider).to receive(:purge_package).with("emacs", nil)
      provider.run_action(:purge)
      expect(new_resource).to be_updated
      expect(new_resource).to be_updated_by_last_action
    end

    it "should purge the package at a specific version if it is installed at that version" do
      new_resource.version "1.4.2"
      expect(provider).to be_removing_package
      expect(provider).to receive(:purge_package).with("emacs", "1.4.2")
      provider.run_action(:purge)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should not purge the package at a specific version if it is not installed at that version" do
      new_resource.version "1.0"
      expect(provider).not_to be_removing_package
      expect(provider).not_to receive(:purge_package)
      provider.run_action(:purge)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "should not purge the package if it is not installed" do
      current_resource.instance_variable_set(:@version, nil)
      expect(provider).not_to be_removing_package

      expect(provider).not_to receive(:purge_package)
      provider.run_action(:purge)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "should set the resource to updated if it purges the package" do
      provider.run_action(:purge)
      expect(new_resource).to be_updated
    end

  end

  describe "When locking the package" do
    before(:each) do
      allow(provider).to receive(:lock_package).with(
        new_resource.package_name,
        nil
      ).and_return(true)
    end

    it "should lock the package if it is unlocked" do
      allow(provider).to receive(:package_locked).with(
        new_resource.package_name,
        nil
      ).and_return(false)
      provider.run_action(:lock)
    end

    it "should not lock the package if it is already locked" do
      allow(provider).to receive(:package_locked).with(
        new_resource.package_name,
        nil
      ).and_return(true)
      expect(provider).not_to receive(:lock_package)
      provider.run_action(:lock)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "should set the resource to updated if it locks the package" do
      allow(provider).to receive(:package_locked).with(
        new_resource.package_name,
        nil
      ).and_return(false)
      provider.run_action(:lock)
      expect(new_resource).to be_updated
    end
  end

  describe "When unlocking the package" do
    before(:each) do
      allow(provider).to receive(:unlock_package).and_return(true)
    end

    it "should unlock the package if it is locked" do
      allow(provider).to receive(:package_locked).with(
        new_resource.package_name,
        nil
      ).and_return(true)
      expect(provider).to receive(:unlock_package)
      provider.run_action(:unlock)
    end

    it "should not unlock the package if it is already unlocked" do
      allow(provider).to receive(:package_locked).with(
        new_resource.package_name,
        nil
      ).and_return(false)
      expect(provider).not_to receive(:unlock_package)
      provider.run_action(:unlock)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "should set the resource to updated if it unlocks the package" do
      allow(provider).to receive(:package_locked).with(
        new_resource.package_name,
        nil
      ).and_return(true)
      provider.run_action(:unlock)
      expect(new_resource).to be_updated
    end
  end

  describe "when running commands to be implemented by subclasses" do
    it "should raises UnsupportedAction for install" do
      expect { provider.install_package("emacs", "1.4.2") }.to raise_error(Chef::Exceptions::UnsupportedAction)
    end

    it "should raises UnsupportedAction for upgrade" do
      expect { provider.upgrade_package("emacs", "1.4.2") }.to raise_error(Chef::Exceptions::UnsupportedAction)
    end

    it "should raises UnsupportedAction for remove" do
      expect { provider.remove_package("emacs", "1.4.2") }.to raise_error(Chef::Exceptions::UnsupportedAction)
    end

    it "should raises UnsupportedAction for purge" do
      expect { provider.purge_package("emacs", "1.4.2") }.to raise_error(Chef::Exceptions::UnsupportedAction)
    end

    it "should raise UnsupportedAction for preseed_package" do
      preseed_file = "/tmp/sun-jdk-package-preseed-file.seed"
      expect { provider.preseed_package(preseed_file) }.to raise_error(Chef::Exceptions::UnsupportedAction)
    end

    it "should raise UnsupportedAction for reconfig" do
      expect { provider.reconfig_package("emacs") }.to raise_error(Chef::Exceptions::UnsupportedAction)
    end

    it "should raise UnsupportedAction for lock" do
      expect { provider.lock_package("emacs", nil) }.to raise_error(Chef::Exceptions::UnsupportedAction)
    end

    it "should raise UnsupportedAction for unlock" do
      expect { provider.unlock_package("emacs", nil) }.to raise_error(Chef::Exceptions::UnsupportedAction)
    end
  end
end

describe "Subclass with use_multipackage_api" do
  class MyPackageResource < Chef::Resource::Package
  end

  class MyPackageProvider < Chef::Provider::Package
    use_multipackage_api
  end

  let(:node) { Chef::Node.new }
  let(:events) { Chef::EventDispatch::Dispatcher.new }
  let(:run_context) { Chef::RunContext.new(node, {}, events) }
  let(:new_resource) { MyPackageResource.new("installs the packages") }
  let(:current_resource) { MyPackageResource.new("installs the packages") }
  let(:provider) do
    provider = MyPackageProvider.new(new_resource, run_context)
    provider.current_resource = current_resource
    provider
  end

  it "has use_multipackage_api? methods on the class and instance" do
    expect(MyPackageProvider.use_multipackage_api?).to be true
    expect(provider.use_multipackage_api?).to be true
  end

  it "when user passes string to package_name, passes arrays to install_package" do
    new_resource.package_name "vim"
    new_resource.version nil
    provider.candidate_version = [ "1.0" ]
    expect(provider).to receive(:install_package).with(
      [ "vim" ],
      [ "1.0" ]
    ).and_return(true)
    provider.run_action(:install)
    expect(new_resource).to be_updated_by_last_action
    expect(new_resource.version).to eql(nil)
  end

  it "when user pases string to package_name and version, passes arrays to install_package" do
    new_resource.package_name "vim"
    new_resource.version "1.0"
    provider.candidate_version = [ "1.0" ]
    expect(provider).to receive(:install_package).with(
      [ "vim" ],
      [ "1.0" ]
    ).and_return(true)
    provider.run_action(:install)
    expect(new_resource).to be_updated_by_last_action
    expect(new_resource.version).to eql("1.0")
  end

  it "when user passes string to package_name, passes arrays to upgrade_package" do
    new_resource.package_name "vim"
    new_resource.version nil
    provider.candidate_version = [ "1.0" ]
    expect(provider).to receive(:upgrade_package).with(
      [ "vim" ],
      [ "1.0" ]
    ).and_return(true)
    provider.run_action(:upgrade)
    expect(new_resource).to be_updated_by_last_action
    expect(new_resource.version).to eql(nil)
  end

  it "when user pases string to package_name and version, passes arrays to upgrade_package" do
    new_resource.package_name "vim"
    new_resource.version "1.0"
    provider.candidate_version = [ "1.0" ]
    expect(provider).to receive(:upgrade_package).with(
      [ "vim" ],
      [ "1.0" ]
    ).and_return(true)
    provider.run_action(:upgrade)
    expect(new_resource).to be_updated_by_last_action
    expect(new_resource.version).to eql("1.0")
  end

  it "when user passes string to package_name, passes arrays to remove_package" do
    new_resource.package_name "vim"
    current_resource.package_name "vim"
    current_resource.version [ "1.0" ]
    provider.candidate_version = [ "1.0" ]
    expect(provider).to receive(:remove_package).with(
      [ "vim" ],
      [ nil ]
    ).and_return(true)
    provider.run_action(:remove)
    expect(new_resource).to be_updated_by_last_action
    expect(new_resource.version).to eql(nil)
  end

  it "when user passes string to package_name, passes arrays to purge_package" do
    new_resource.package_name "vim"
    current_resource.package_name "vim"
    current_resource.version [ "1.0" ]
    provider.candidate_version = [ "1.0" ]
    expect(provider).to receive(:purge_package).with(
      [ "vim" ],
      [ nil ]
    ).and_return(true)
    provider.run_action(:purge)
    expect(new_resource).to be_updated_by_last_action
    expect(new_resource.version).to eql(nil)
  end
end

describe "Chef::Provider::Package - Multi" do
  let(:node) { Chef::Node.new }
  let(:events) { Chef::EventDispatch::Dispatcher.new }
  let(:run_context) { Chef::RunContext.new(node, {}, events) }
  let(:new_resource) { Chef::Resource::Package.new(%w{emacs vi}) }
  let(:current_resource) { Chef::Resource::Package.new(%w{emacs vi}) }
  let(:candidate_version) { [ "1.0", "6.2" ] }
  class MyPackageProvider < Chef::Provider::Package
    use_multipackage_api
  end
  let(:provider) do
    provider = MyPackageProvider.new(new_resource, run_context)
    provider.current_resource = current_resource
    provider.candidate_version = candidate_version
    provider
  end

  describe "when installing multiple packages" do
    before(:each) do
      provider.current_resource = current_resource
      allow(provider).to receive(:install_package).and_return(true)
    end

    it "installs the candidate versions when none are installed" do
      expect(provider).to receive(:install_package).with(
        %w{emacs vi},
        ["1.0", "6.2"]
      ).and_return(true)
      provider.run_action(:install)
      expect(new_resource).to be_updated
    end

    it "installs the candidate versions when some are installed" do
      expect(provider).to receive(:install_package).with(
        [ "vi" ],
        [ "6.2" ]
      ).and_return(true)
      current_resource.version(["1.0", nil])
      provider.run_action(:install)
      expect(new_resource).to be_updated
    end

    it "installs the specified version when some are out of date" do
      current_resource.version(["1.0", "6.2"])
      new_resource.version(["1.0", "6.3"])
      provider.run_action(:install)
      expect(new_resource).to be_updated
    end

    it "does not install any version if all are installed at the right version" do
      current_resource.version(["1.0", "6.2"])
      new_resource.version(["1.0", "6.2"])
      provider.run_action(:install)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "does install an older version by default" do
      current_resource.version(["1.1", "6.2"])
      new_resource.version(["1.0", "6.1"])
      provider.run_action(:install)
      expect(new_resource).to be_updated_by_last_action
    end

    it "does not install an older version if the resource subclass has allow_downgrade set to false" do
      allow(new_resource).to receive(:allow_downgrade).and_return(false)
      current_resource.version(["1.1", "6.2"])
      new_resource.version(["1.0", "6.1"])
      provider.run_action(:install)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "does not install any version if all are installed, and no version was specified" do
      current_resource.version(["1.0", "6.2"])
      provider.run_action(:install)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "raises an exception if both are not installed and no caondidates are available" do
      current_resource.version([nil, nil])
      provider.candidate_version = [nil, nil]
      expect { provider.run_action(:install) }.to raise_error(Chef::Exceptions::Package)
    end

    it "raises an exception if one is not installed and no candidates are available" do
      current_resource.version(["1.0", nil])
      provider.candidate_version = ["1.0", nil]
      expect { provider.run_action(:install) }.to raise_error(Chef::Exceptions::Package)
    end

    it "does not raise an exception if the packages are installed or have a candidate" do
      current_resource.version(["1.0", nil])
      provider.candidate_version = [nil, "6.2"]
      expect { provider.run_action(:install) }.not_to raise_error
    end

    it "raises an exception if an explicit version is asked for, an old version is installed, but no candidate" do
      new_resource.version ["1.0", "6.2"]
      current_resource.version(["1.0", "6.1"])
      provider.candidate_version = ["1.0", nil]
      expect { provider.run_action(:install) }.to raise_error(Chef::Exceptions::Package)
    end

    it "does not raise an exception if an explicit version is asked for, and is installed, but no candidate" do
      new_resource.version ["1.0", "6.2"]
      current_resource.version(["1.0", "6.2"])
      provider.candidate_version = ["1.0", nil]
      expect { provider.run_action(:install) }.not_to raise_error
    end

    it "raise an exception if an explicit version is asked for, and is not installed, and no candidate" do
      new_resource.version ["1.0", "6.2"]
      current_resource.version(["1.0", nil])
      provider.candidate_version = ["1.0", nil]
      expect { provider.run_action(:install) }.to raise_error(Chef::Exceptions::Package)
    end

    it "does not raise an exception if an explicit version is asked for, and is not installed, and there is a candidate" do
      new_resource.version ["1.0", "6.2"]
      current_resource.version(["1.0", nil])
      provider.candidate_version = ["1.0", "6.2"]
      expect { provider.run_action(:install) }.not_to raise_error
    end
  end

  describe "when upgrading multiple packages" do
    before(:each) do
      provider.current_resource = current_resource
      allow(provider).to receive(:upgrade_package).and_return(true)
    end

    it "should upgrade the package if the current versions are not the candidate version" do
      current_resource.version ["0.9", "6.1"]
      expect(provider).to receive(:upgrade_package).with(
        new_resource.package_name,
        provider.candidate_version
      ).and_return(true)
      provider.run_action(:upgrade)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should upgrade the package if some of current versions are not the candidate versions" do
      current_resource.version ["1.0", "6.1"]
      expect(provider).to receive(:upgrade_package).with(
        ["vi"],
        ["6.2"]
      ).and_return(true)
      provider.run_action(:upgrade)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should not install the package if the current versions are the candidate version" do
      current_resource.version ["1.0", "6.2"]
      expect(provider).not_to receive(:upgrade_package)
      provider.run_action(:upgrade)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "should raise an exception if both are not installed and no caondidates are available" do
      current_resource.version([nil, nil])
      provider.candidate_version = [nil, nil]
      expect { provider.run_action(:upgrade) }.to raise_error(Chef::Exceptions::Package)
    end

    it "should raise an exception if one is not installed and no candidates are available" do
      current_resource.version(["1.0", nil])
      provider.candidate_version = ["1.0", nil]
      expect { provider.run_action(:upgrade) }.to raise_error(Chef::Exceptions::Package)
    end

    it "should not raise an exception if the packages are installed or have a candidate" do
      current_resource.version(["1.0", nil])
      provider.candidate_version = [nil, "6.2"]
      expect { provider.run_action(:upgrade) }.not_to raise_error
    end

    it "should not raise an exception if the packages are installed or have a candidate" do
      current_resource.version(["1.0", nil])
      provider.candidate_version = [nil, "6.2"]
      expect { provider.run_action(:upgrade) }.not_to raise_error
    end
  end

  describe "When removing multiple packages " do
    before(:each) do
      allow(provider).to receive(:remove_package).and_return(true)
      current_resource.version ["1.0", "6.2"]
    end

    it "should remove the packages if all are installed" do
      expect(provider).to be_removing_package
      expect(provider).to receive(:remove_package).with(%w{emacs vi}, [nil])
      provider.run_action(:remove)
      expect(new_resource).to be_updated
      expect(new_resource).to be_updated_by_last_action
    end

    it "should remove the packages if some are installed" do
      current_resource.version ["1.0", nil]
      expect(provider).to be_removing_package
      expect(provider).to receive(:remove_package).with(%w{emacs vi}, [nil])
      provider.run_action(:remove)
      expect(new_resource).to be_updated
      expect(new_resource).to be_updated_by_last_action
    end

    it "should remove the packages at a specific version if they are installed at that version" do
      new_resource.version ["1.0", "6.2"]
      expect(provider).to be_removing_package
      expect(provider).to receive(:remove_package).with(%w{emacs vi}, ["1.0", "6.2"])
      provider.run_action(:remove)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should remove the packages at a specific version any are is installed at that version" do
      new_resource.version ["0.5", "6.2"]
      expect(provider).to be_removing_package
      expect(provider).to receive(:remove_package).with(%w{emacs vi}, ["0.5", "6.2"])
      provider.run_action(:remove)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should not remove the packages at a specific version if they are not installed at that version" do
      new_resource.version ["0.5", "6.0"]
      expect(provider).not_to be_removing_package
      expect(provider).not_to receive(:remove_package)
      provider.run_action(:remove)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "should not remove the packages if they are not installed" do
      expect(provider).not_to receive(:remove_package)
      allow(current_resource).to receive(:version).and_return(nil)
      provider.run_action(:remove)
      expect(new_resource).not_to be_updated_by_last_action
    end

  end

  describe "When purging multiple packages " do
    before(:each) do
      allow(provider).to receive(:purge_package).and_return(true)
      current_resource.version ["1.0", "6.2"]
    end

    it "should purge the packages if all are installed" do
      expect(provider).to be_removing_package
      expect(provider).to receive(:purge_package).with(%w{emacs vi}, [nil])
      provider.run_action(:purge)
      expect(new_resource).to be_updated
      expect(new_resource).to be_updated_by_last_action
    end

    it "should purge the packages if some are installed" do
      current_resource.version ["1.0", nil]
      expect(provider).to be_removing_package
      expect(provider).to receive(:purge_package).with(%w{emacs vi}, [nil])
      provider.run_action(:purge)
      expect(new_resource).to be_updated
      expect(new_resource).to be_updated_by_last_action
    end

    it "should purge the packages at a specific version if they are installed at that version" do
      new_resource.version ["1.0", "6.2"]
      expect(provider).to be_removing_package
      expect(provider).to receive(:purge_package).with(%w{emacs vi}, ["1.0", "6.2"])
      provider.run_action(:purge)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should purge the packages at a specific version any are is installed at that version" do
      new_resource.version ["0.5", "6.2"]
      expect(provider).to be_removing_package
      expect(provider).to receive(:purge_package).with(%w{emacs vi}, ["0.5", "6.2"])
      provider.run_action(:purge)
      expect(new_resource).to be_updated_by_last_action
    end

    it "should not purge the packages at a specific version if they are not installed at that version" do
      new_resource.version ["0.5", "6.0"]
      expect(provider).not_to be_removing_package
      expect(provider).not_to receive(:purge_package)
      provider.run_action(:purge)
      expect(new_resource).not_to be_updated_by_last_action
    end

    it "should not purge the packages if they are not installed" do
      expect(provider).not_to receive(:purge_package)
      allow(current_resource).to receive(:version).and_return(nil)
      provider.run_action(:purge)
      expect(new_resource).not_to be_updated_by_last_action
    end
  end

  describe "version_compare" do
    it "tests equality" do
      expect(provider.version_compare("1.3", "1.3")).to eql(0)
    end

    it "tests less than" do
      expect(provider.version_compare("1.2", "1.3")).to eql(-1)
    end

    it "tests greater than" do
      expect(provider.version_compare("1.5", "1.3")).to eql(1)
    end

    it "x.10 is greater than x.2 (so does not do floating point comparisons)" do
      expect(provider.version_compare("1.10", "1.2")).to eql(1)
    end

    it "sanitizes inputs" do
      expect(provider.version_compare("1.3_3", "1.3")).to eql(0)
    end
  end
end