File: apt_spec.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (302 lines) | stat: -rw-r--r-- 9,908 bytes parent folder | download
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
require 'spec_helper'

describe Puppet::Type.type(:package).provider(:apt) do
  let(:name) { 'asdf' }

  let(:resource) do
    Puppet::Type.type(:package).new(
      :name     => name,
      :provider => 'apt'
    )
  end

  let(:provider) do
    resource.provider
  end

  it "should be the default provider on :osfamily => Debian" do
    expect(Facter).to receive(:value).with(:osfamily).and_return("Debian")
    expect(described_class.default?).to be_truthy
  end

  it "should be versionable" do
    expect(described_class).to be_versionable
  end

  it "should use :install to update" do
    expect(provider).to receive(:install)
    provider.update
  end

  it "should use 'apt-get remove' to uninstall" do
    expect(provider).to receive(:aptget).with("-y", "-q", :remove, name)
    expect(provider).to receive(:properties).and_return({:mark => :none})
    provider.uninstall
  end

  it "should use 'apt-get purge' and 'dpkg purge' to purge" do
    expect(provider).to receive(:aptget).with("-y", "-q", :remove, "--purge", name)
    expect(provider).to receive(:dpkg).with("--purge", name)
    expect(provider).to receive(:properties).and_return({:mark => :none})
    provider.purge
  end

  it "should use 'apt-cache policy' to determine the latest version of a package" do
    expect(provider).to receive(:aptcache).with(:policy, name).and_return(<<-HERE)
#{name}:
Installed: 1:1.0
Candidate: 1:1.1
Version table:
1:1.0
  650 http://ftp.osuosl.org testing/main Packages
*** 1:1.1
  100 /var/lib/dpkg/status
    HERE

    expect(provider.latest).to eq("1:1.1")
  end

  it "should print and error and return nil if no policy is found" do
    expect(provider).to receive(:aptcache).with(:policy, name).and_return("#{name}:")

    expect(provider).to receive(:err)
    expect(provider.latest).to be_nil
  end

  it "should be able to preseed" do
    expect(provider).to respond_to(:run_preseed)
  end

  it "should preseed with the provided responsefile when preseeding is called for" do
    resource[:responsefile] = '/my/file'
    expect(Puppet::FileSystem).to receive(:exist?).with('/my/file').and_return(true)

    expect(provider).to receive(:info)
    expect(provider).to receive(:preseed).with('/my/file')

    provider.run_preseed
  end

  it "should not preseed if no responsefile is provided" do
    expect(provider).to receive(:info)
    expect(provider).not_to receive(:preseed)

    provider.run_preseed
  end

  describe ".instances" do
    before do
      allow(Puppet::Type::Package::ProviderDpkg).to receive(:instances).and_return([provider])
    end

    context "when package is manual marked" do
      before do
        allow(described_class).to receive(:aptmark).with('showmanual').and_return("#{resource.name}\n")
      end

      it 'sets mark to manual' do
        expect(described_class.instances.map(&:mark)).to eq([:manual])
      end
    end

    context 'when package is not manual marked ' do
      before do
        allow(described_class).to receive(:aptmark).with('showmanual').and_return('')
      end

      it 'does not set mark to manual' do
        expect(described_class.instances.map(&:mark)).to eq([nil])
      end
    end
  end

  describe 'query' do
    before do
      allow(provider).to receive(:dpkgquery).and_return("name: #{resource.name}" )
    end


    context 'when package is not installed on the system' do
      it 'does not set mark to manual' do
        result = provider.query

        expect(described_class).not_to receive(:aptmark)
        expect(result[:mark]).to be_nil
      end
    end
  end

  describe 'flush' do

    context "when package is manual marked" do
      before do
        provider.mark = :manual
      end

      it 'does not call aptmark' do
        expect(provider).not_to receive(:aptmark)
        provider.flush
      end
    end

    context 'when package is not manual marked ' do
      it 'calls aptmark' do
        expect(described_class).to receive(:aptmark).with('manual', resource.name)
        provider.flush
      end
    end
  end

  describe "when installing" do
    it "should preseed if a responsefile is provided" do
      resource[:responsefile] = "/my/file"
      expect(provider).to receive(:run_preseed)
      expect(provider).to receive(:properties).and_return({:mark => :none})
      allow(provider).to receive(:aptget)
      provider.install
    end

    it "should check for a cdrom" do
      expect(provider).to receive(:checkforcdrom)
      expect(provider).to receive(:properties).and_return({:mark => :none})
      allow(provider).to receive(:aptget)
      provider.install
    end

    it "should use 'apt-get install' with the package name if no version is asked for" do
      resource[:ensure] = :installed
      expect(provider).to receive(:aptget) do |*command|
        expect(command[-1]).to eq(name)
        expect(command[-2]).to eq(:install)
      end
      expect(provider).to receive(:properties).and_return({:mark => :none})

      provider.install
    end

    it "should specify the package version if one is asked for" do
      resource[:ensure] = '1.0'
      expect(provider).to receive(:aptget) do |*command|
        expect(command[-1]).to eq("#{name}=1.0")
      end
      expect(provider).to receive(:properties).and_return({:mark => :none})

      provider.install
    end

    it "should select latest available version if range is specified" do
      resource[:ensure] = '>60.0'
      expect(provider).to receive(:aptget) do |*command|
        expect(command[-1]).to eq("#{name}=72.0.1+build1-0ubuntu0.19.04.1")
      end
      expect(provider).to receive(:aptcache).with(:madison, name).and_return(<<-HERE)
   #{name} | 72.0.1+build1-0ubuntu0.19.04.1 | http://ro.archive.ubuntu.com/ubuntu disco-updates/main amd64 Packages
   #{name} | 72.0.1+build1-0ubuntu0.19.04.1 | http://security.ubuntu.com/ubuntu disco-security/main amd64 Packages
   #{name} | 66.0.3+build1-0ubuntu1 | http://ro.archive.ubuntu.com/ubuntu disco/main amd64 Packages
    HERE
      expect(provider).to receive(:properties).and_return({:mark => :none})

      provider.install
    end

    it "should pass through ensure is no version can be selected" do
      resource[:ensure] = '>74.0'
      expect(provider).to receive(:aptget) do |*command|
        expect(command[-1]).to eq("#{name}=>74.0")
      end
      expect(provider).to receive(:aptcache).with(:madison, name).and_return(<<-HERE)
   #{name} | 72.0.1+build1-0ubuntu0.19.04.1 | http://ro.archive.ubuntu.com/ubuntu disco-updates/main amd64 Packages
   #{name} | 72.0.1+build1-0ubuntu0.19.04.1 | http://security.ubuntu.com/ubuntu disco-security/main amd64 Packages
   #{name} | 66.0.3+build1-0ubuntu1 | http://ro.archive.ubuntu.com/ubuntu disco/main amd64 Packages
    HERE
      expect(provider).to receive(:properties).and_return({:mark => :none})

      provider.install
    end

    it "should use --force-yes if a package version is specified" do
      resource[:ensure] = '1.0'
      expect(provider).to receive(:aptget) do |*command|
        expect(command).to include("--force-yes")
      end
      expect(provider).to receive(:properties).and_return({:mark => :none})

      provider.install
    end

    it "should do a quiet install" do
      expect(provider).to receive(:aptget) do |*command|
        expect(command).to include("-q")
      end
      expect(provider).to receive(:properties).and_return({:mark => :none})

      provider.install
    end

    it "should default to 'yes' for all questions" do
      expect(provider).to receive(:aptget) do |*command|
        expect(command).to include("-y")
      end
      expect(provider).to receive(:properties).and_return({:mark => :none})

      provider.install
    end

    it "should keep config files if asked" do
      resource[:configfiles] = :keep
      expect(provider).to receive(:aptget) do |*command|
        expect(command).to include("DPkg::Options::=--force-confold")
      end
      expect(provider).to receive(:properties).and_return({:mark => :none})

      provider.install
    end

    it "should replace config files if asked" do
      resource[:configfiles] = :replace
      expect(provider).to receive(:aptget) do |*command|
        expect(command).to include("DPkg::Options::=--force-confnew")
      end
      expect(provider).to receive(:properties).and_return({:mark => :none})

      provider.install
    end

    it 'should support string install options' do
      resource[:install_options] = ['--foo', '--bar']
      expect(provider).to receive(:aptget).with('-q', '-y', '-o', 'DPkg::Options::=--force-confold', '--foo', '--bar', :install, name)
      expect(provider).to receive(:properties).and_return({:mark => :none})

      provider.install
    end

    it 'should support hash install options' do
      resource[:install_options] = ['--foo', { '--bar' => 'baz', '--baz' => 'foo' }]
      expect(provider).to receive(:aptget).with('-q', '-y', '-o', 'DPkg::Options::=--force-confold', '--foo', '--bar=baz', '--baz=foo', :install, name)
      expect(provider).to receive(:properties).and_return({:mark => :none})

      provider.install
    end

    it "should install using the source attribute if present" do
      resource[:ensure] = :installed
      resource[:source] = '/my/local/package/file'

      expect(provider).to receive(:aptget).with(any_args, :install, resource[:source])
      expect(provider).to receive(:properties).and_return({:mark => :none})

      provider.install
    end

    it "should install specific version using the source attribute if present" do
      resource[:ensure] = '1.2.3'
      resource[:source] = '/my/local/package/file'

      expect(provider).to receive(:aptget).with(any_args, :install, resource[:source])
      expect(provider).to receive(:properties).and_return({:mark => :none})
      expect(provider).to receive(:query).and_return({:ensure => '1.2.3'})

      provider.install
    end
  end
end