summaryrefslogtreecommitdiff
path: root/spec/functional/resource/zypper_package_spec.rb
blob: 31168d9fe834c4f8debb412ef16564740a9f9885 (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
#
# Author:: Dheeraj Dubey (<dheeraj.dubey@msystechnologies.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 "functional/resource/base"
require "chef/mixin/shell_out"

describe Chef::Resource::ZypperPackage, :requires_root, :suse_only do
  include Chef::Mixin::ShellOut

  # NOTE: every single test here needs to explicitly call preinstall.

  def preinstall(*rpms)
    rpms.each do |rpm|
      shell_out!("rpm -ivh #{CHEF_SPEC_ASSETS}/zypprepo/#{rpm}")
    end
  end

  before(:each) do
    File.open("/etc/zypp/repos.d/chef-zypp-localtesting.repo", "w+") do |f|
      f.write <<~EOF
        [chef-zypp-localtesting]
        name=Chef zypper spec testing repo
        baseurl=file://#{CHEF_SPEC_ASSETS}/zypprepo
        enable=1
        gpgcheck=0
      EOF
    end
    shell_out!("rpm -qa --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm | xargs -r rpm -e")
    # next line is useful cleanup if you happen to have been testing zypper func tests on the same box and
    # have some zypper garbage left around
    # FileUtils.rm_f "/etc/zypp/repos.d/chef-zypp-localtesting.repo"
  end

  after(:all) do
    shell_out!("rpm -qa --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm | xargs -r rpm -e")
    FileUtils.rm_f "/etc/zypp/repos.d/chef-zypp-localtesting.repo"
  end

  let(:package_name) { "chef_rpm" }
  let(:zypper_package) do
    r = Chef::Resource::ZypperPackage.new(package_name, run_context)
    r.global_options("--no-gpg-checks")
    r
  end

  def pkg_arch
    ohai[:kernel][:machine]
  end

  context "installing a package" do
    after { remove_package }
    it "installs the latest version" do
      zypper_package.run_action(:install)
      expect(zypper_package.updated_by_last_action?).to be true
      expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.10-1.#{pkg_arch}$")
    end

    it "does not install if the package is installed" do
      preinstall("chef_rpm-1.10-1.#{pkg_arch}.rpm")
      zypper_package.run_action(:install)
      expect(zypper_package.updated_by_last_action?).to be false
      expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.10-1.#{pkg_arch}$")
    end

    it "does not install twice" do
      zypper_package.run_action(:install)
      expect(zypper_package.updated_by_last_action?).to be true
      expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.10-1.#{pkg_arch}$")
      zypper_package.run_action(:install)
      expect(zypper_package.updated_by_last_action?).to be false
      expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.10-1.#{pkg_arch}$")
    end

    it "does not install if the prior version package is installed" do
      preinstall("chef_rpm-1.2-1.#{pkg_arch}.rpm")
      zypper_package.run_action(:install)
      expect(zypper_package.updated_by_last_action?).to be false
      expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.2-1.#{pkg_arch}$")
    end
  end

  context "with versions" do
    it "works with a version" do
      zypper_package.package_name("chef_rpm")
      zypper_package.version("1.10")
      zypper_package.run_action(:install)
      expect(zypper_package.updated_by_last_action?).to be true
      expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.10-1.#{pkg_arch}$")
    end

    it "works with an older version" do
      zypper_package.package_name("chef_rpm")
      zypper_package.version("1.2")
      zypper_package.run_action(:install)
      expect(zypper_package.updated_by_last_action?).to be true
      expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.2-1.#{pkg_arch}$")
    end

    it "works with version and release" do
      zypper_package.package_name("chef_rpm")
      zypper_package.version("1.2-1")
      zypper_package.run_action(:install)
      expect(zypper_package.updated_by_last_action?).to be true
      expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.2-1.#{pkg_arch}$")
    end

    it "downgrades when the installed version is higher than the package_name version" do
      preinstall("chef_rpm-1.10-1.#{pkg_arch}.rpm")
      zypper_package.allow_downgrade true
      zypper_package.package_name("chef_rpm")
      zypper_package.version("1.2-1")
      zypper_package.run_action(:install)
      expect(zypper_package.updated_by_last_action?).to be true
      expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^chef_rpm-1.2-1.#{pkg_arch}$")
    end
  end

  describe ":remove" do
    context "vanilla use case" do
      let(:package_name) { "chef_rpm" }
      it "does nothing if the package is not installed" do
        zypper_package.run_action(:remove)
        expect(zypper_package.updated_by_last_action?).to be false
        expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^package chef_rpm is not installed$")
      end

      it "removes the package if the package is installed" do
        preinstall("chef_rpm-1.10-1.#{pkg_arch}.rpm")
        zypper_package.run_action(:remove)
        expect(zypper_package.updated_by_last_action?).to be true
        expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^package chef_rpm is not installed$")
      end

      it "does not remove the package twice" do
        preinstall("chef_rpm-1.10-1.#{pkg_arch}.rpm")
        zypper_package.run_action(:remove)
        expect(zypper_package.updated_by_last_action?).to be true
        expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^package chef_rpm is not installed$")
        zypper_package.run_action(:remove)
        expect(zypper_package.updated_by_last_action?).to be false
        expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^package chef_rpm is not installed$")
      end

      it "removes the package if the prior version package is installed" do
        preinstall("chef_rpm-1.2-1.#{pkg_arch}.rpm")
        zypper_package.run_action(:remove)
        expect(zypper_package.updated_by_last_action?).to be true
        expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^package chef_rpm is not installed$")
      end
    end

    context "with no available version" do
      it "works when a package is installed" do
        FileUtils.rm_f "/etc/zypp/repos.d/chef-zypp-localtesting.repo"
        preinstall("chef_rpm-1.2-1.#{pkg_arch}.rpm")
        zypper_package.run_action(:remove)
        expect(zypper_package.updated_by_last_action?).to be true
        expect(shell_out("rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' chef_rpm").stdout.chomp).to match("^package chef_rpm is not installed$")
      end
    end
  end

  describe ":lock and :unlock" do
    before(:each) do
      shell_out("zypper removelock chef_rpm") # will exit with error when nothing is locked, we don't care
    end

    it "locks an rpm" do
      zypper_package.package_name("chef_rpm")
      zypper_package.run_action(:lock)
      expect(zypper_package.updated_by_last_action?).to be true
      expect(shell_out("zypper locks | grep chef_rpm").stdout.chomp).to match("chef_rpm")
    end

    it "does not lock if its already locked" do
      shell_out!("zypper addlock chef_rpm")
      zypper_package.package_name("chef_rpm")
      zypper_package.run_action(:lock)
      expect(zypper_package.updated_by_last_action?).to be false
      expect(shell_out("zypper locks | grep chef_rpm").stdout.chomp).to match("chef_rpm")
    end

    it "unlocks an rpm" do
      shell_out!("zypper addlock chef_rpm")
      zypper_package.package_name("chef_rpm")
      zypper_package.run_action(:unlock)
      expect(zypper_package.updated_by_last_action?).to be true
      expect(shell_out("zypper locks | grep chef_rpm").stdout.chomp).not_to match("chef_rpm")
    end

    it "does not unlock an already locked rpm" do
      zypper_package.package_name("chef_rpm")
      zypper_package.run_action(:unlock)
      expect(zypper_package.updated_by_last_action?).to be false
      expect(shell_out("zypper locks | grep chef_rpm").stdout.chomp).not_to match("chef_rpm")
    end

    it "check that we can lock based on provides" do
      zypper_package.package_name("chef_rpm_provides")
      zypper_package.run_action(:lock)
      expect(zypper_package.updated_by_last_action?).to be true
      expect(shell_out("zypper locks | grep chef_rpm_provides").stdout.chomp).to match("chef_rpm_provides")
    end

    it "check that we can unlock based on provides" do
      shell_out!("zypper addlock chef_rpm_provides")
      zypper_package.package_name("chef_rpm_provides")
      zypper_package.run_action(:unlock)
      expect(zypper_package.updated_by_last_action?).to be true
      expect(shell_out("zypper locks | grep chef_rpm_provides").stdout.chomp).not_to match("chef_rpm_provides")
    end
  end
  def remove_package
    pkg_to_remove = Chef::Resource::ZypperPackage.new(package_name, run_context)
    pkg_to_remove.run_action(:remove)
  end
end