summaryrefslogtreecommitdiff
path: root/spec/functional/resource/chocolatey_package_spec.rb
blob: 75677183633ec60cb96aacac8974d551426a301c (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
#
# Author:: Matt Wrock (<matt@mattwrock.com>)
# Copyright:: Copyright (c) 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/powershell_out"

describe Chef::Resource::ChocolateyPackage, :windows_only, :choco_installed do
  include Chef::Mixin::PowershellOut

  let(:package_name) { "test-A" }
  let(:package_list) { proc { powershell_out!("choco list -lo -r #{Array(package_name).join(" ")}").stdout.chomp } }
  let(:package_source) { File.join(CHEF_SPEC_ASSETS, "chocolatey_feed") }

  subject do
    new_resource = Chef::Resource::ChocolateyPackage.new("test choco package", run_context)
    new_resource.package_name package_name
    new_resource.source package_source
    new_resource
  end

  context "installing a package" do
    after { remove_package }

    it "installs the latest version" do
      subject.run_action(:install)
      expect(package_list.call).to eq("#{package_name}|2.0")
    end

    it "does not install if already installed" do
      subject.run_action(:install)
      subject.run_action(:install)
      expect(subject).not_to be_updated_by_last_action
    end

    it "installs version given" do
      subject.version "1.0"
      subject.run_action(:install)
      expect(package_list.call).to eq("#{package_name}|1.0")
    end

    it "installs new version if one is already installed" do
      subject.version "1.0"
      subject.run_action(:install)
      expect(package_list.call).to eq("#{package_name}|1.0")

      subject.version "2.0"
      subject.run_action(:install)
      expect(package_list.call).to eq("#{package_name}|2.0")
    end

    context "installing multiple packages" do
      let(:package_name) { %w{test-A test-B} }

      it "installs both packages" do
        subject.run_action(:install)
        expect(package_list.call).to eq("test-A|2.0\r\ntest-B|1.0")
      end
    end

    it "raises if package is not found" do
      subject.package_name "blah"
      expect { subject.run_action(:install) }.to raise_error Chef::Exceptions::Package
    end

    it "installs with an option as a string" do
      subject.options "--force --confirm"
      subject.run_action(:install)
      expect(package_list.call).to eq("#{package_name}|2.0")
    end

    it "installs with multiple options as a string" do
      subject.options "--force --confirm"
      subject.run_action(:install)
      expect(package_list.call).to eq("#{package_name}|2.0")
    end

    it "installs with multiple options as an array" do
      subject.options [ "--force", "--confirm" ]
      subject.run_action(:install)
      expect(package_list.call).to eq("#{package_name}|2.0")
    end
  end

  context "upgrading a package" do
    after { remove_package }

    it "upgrades to a specific version" do
      subject.version "1.0"
      subject.run_action(:install)
      expect(package_list.call).to eq("#{package_name}|1.0")

      subject.version "1.5"
      subject.run_action(:upgrade)
      expect(package_list.call).to eq("#{package_name}|1.5")
    end

    it "upgrades to the latest version if no version given" do
      subject.version "1.0"
      subject.run_action(:install)
      expect(package_list.call).to eq("#{package_name}|1.0")

      subject2 = Chef::Resource::ChocolateyPackage.new("test-A", run_context)
      subject2.source package_source
      subject2.run_action(:upgrade)
      expect(package_list.call).to eq("#{package_name}|2.0")
    end
  end

  context "removing a package" do
    it "removes an installed package" do
      subject.run_action(:install)
      remove_package
      expect(package_list.call).to eq("")
    end
  end

  def remove_package
    pkg_to_remove = Chef::Resource::ChocolateyPackage.new(package_name, run_context)
    pkg_to_remove.source = package_source
    pkg_to_remove.run_action(:remove)
  end
end