File: pip_spec.rb

package info (click to toggle)
puppet 5.5.10-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 21,116 kB
  • sloc: ruby: 250,669; sh: 1,620; xml: 218; makefile: 151; sql: 103
file content (379 lines) | stat: -rw-r--r-- 15,436 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
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
require 'spec_helper'

osfamilies = { 'windows' => ['pip.exe'], 'other' => ['pip', 'pip-python'] }

describe Puppet::Type.type(:package).provider(:pip) do
  before do  
    @resource = Puppet::Resource.new(:package, "fake_package")
    @provider = described_class.new(@resource)
    @client = stub_everything('client')
    @client.stubs(:call).with('package_releases', 'real_package').returns(["1.3", "1.2.5", "1.2.4"])
    @client.stubs(:call).with('package_releases', 'fake_package').returns([])
  end

  context "parse" do
    it "should return a hash on valid input" do
      expect(described_class.parse("real_package==1.2.5")).to eq({
        :ensure   => "1.2.5",
        :name     => "real_package",
        :provider => :pip,
      })
    end

    it "should return nil on invalid input" do
      expect(described_class.parse("foo")).to eq(nil)
    end
  end

  context "cmd" do
    it "should return 'pip.exe' by default on Windows systems" do
      Puppet.features.stubs(:microsoft_windows?).returns true
      expect(described_class.cmd[0]).to eq('pip.exe')
    end

    it "could return pip-python on legacy redhat systems which rename pip" do
      Puppet.features.stubs(:microsoft_windows?).returns false
      expect(described_class.cmd[1]).to eq('pip-python')
    end

    it "should return pip by default on other systems" do
      Puppet.features.stubs(:microsoft_windows?).returns false
      expect(described_class.cmd[0]).to eq('pip')
    end
  end

  context "instances" do
    osfamilies.each do |osfamily, pip_cmds|
      it "should return an array on #{osfamily} systems when #{pip_cmds.join(' or ')} is present" do
        Puppet.features.stubs(:microsoft_windows?).returns (osfamily == 'windows')
        pip_cmds.each do |pip_cmd|  
          pip_cmds.each do |cmd|
            unless cmd == pip_cmd
              described_class.expects(:which).with(cmd).returns(nil)
            end
          end
          described_class.stubs(:pip_version).returns('8.0.1')
          described_class.expects(:which).with(pip_cmd).returns("/fake/bin/#{pip_cmd}")
          p = stub("process")
          p.expects(:collect).yields("real_package==1.2.5")
          described_class.expects(:execpipe).with(["/fake/bin/#{pip_cmd}", "freeze"]).yields(p)
          described_class.instances
        end
      end

      context "with pip version >= 8.1.0" do
        versions = ['8.1.0', '9.0.1']
        versions.each do |version|
          it "should use the --all option when version is '#{version}'" do
            Puppet.features.stubs(:microsoft_windows?).returns (osfamily == 'windows')
            described_class.stubs(:pip_cmd).returns('/fake/bin/pip')
            described_class.stubs(:pip_version).returns(version)
            p = stub("process")
            p.expects(:collect).yields("real_package==1.2.5")
            described_class.expects(:execpipe).with(["/fake/bin/pip", "freeze", "--all"]).yields(p)
            described_class.instances
          end
        end
      end

      it "should return an empty array on #{osfamily} systems when #{pip_cmds.join(' and ')} are missing" do
        Puppet.features.stubs(:microsoft_windows?).returns (osfamily == 'windows')
        pip_cmds.each do |cmd|
          described_class.expects(:which).with(cmd).returns nil
        end
        expect(described_class.instances).to eq([])
      end
    end
  end

  context "query" do
    before do
      @resource[:name] = "real_package"
    end

    it "should return a hash when pip and the package are present" do
      described_class.expects(:instances).returns [described_class.new({
        :ensure   => "1.2.5",
        :name     => "real_package",
        :provider => :pip,
      })]

      expect(@provider.query).to eq({
        :ensure   => "1.2.5",
        :name     => "real_package",
        :provider => :pip,
      })
    end

    it "should return nil when the package is missing" do
      described_class.expects(:instances).returns []
      expect(@provider.query).to eq(nil)
    end

    it "should be case insensitive" do
      @resource[:name] = "Real_Package"

      described_class.expects(:instances).returns [described_class.new({
        :ensure   => "1.2.5",
        :name     => "real_package",
        :provider => :pip,
      })]

      expect(@provider.query).to eq({
        :ensure   => "1.2.5",
        :name     => "real_package",
        :provider => :pip,
      })
    end
  end

  context "latest" do
    context "with pip version < 1.5.4" do
      before :each do
        described_class.stubs(:pip_version).returns('1.0.1')
        described_class.stubs(:which).with('pip').returns("/fake/bin/pip")
        described_class.stubs(:which).with('pip-python').returns("/fake/bin/pip")
        described_class.stubs(:which).with('pip.exe').returns("/fake/bin/pip")
      end

      it "should find a version number for new_pip_package" do
        p = StringIO.new(
          <<-EOS
          Downloading/unpacking fake-package
            Using version 0.10.1 (newest of versions: 0.10.1, 0.10, 0.9, 0.8.1, 0.8, 0.7.2, 0.7.1, 0.7, 0.6.1, 0.6, 0.5.2, 0.5.1, 0.5, 0.4, 0.3.1, 0.3, 0.2, 0.1)
            Downloading real-package-0.10.1.tar.gz (544Kb): 544Kb downloaded
          Saved ./foo/real-package-0.10.1.tar.gz
          Successfully downloaded real-package
          EOS
        )
        Puppet::Util::Execution.expects(:execpipe).yields(p).once
        @resource[:name] = "real_package"
        expect(@provider.latest).to eq('0.10.1')
      end

      it "should not find a version number for fake_package" do
        p = StringIO.new(
          <<-EOS
          Downloading/unpacking fake-package
            Could not fetch URL http://pypi.python.org/simple/fake_package: HTTP Error 404: Not Found
            Will skip URL http://pypi.python.org/simple/fake_package when looking for download links for fake-package
            Could not fetch URL http://pypi.python.org/simple/fake_package/: HTTP Error 404: Not Found
            Will skip URL http://pypi.python.org/simple/fake_package/ when looking for download links for fake-package
            Could not find any downloads that satisfy the requirement fake-package
          No distributions at all found for fake-package
          Exception information:
          Traceback (most recent call last):
            File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 126, in main
              self.run(options, args)
            File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 223, in run
              requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
            File "/usr/lib/python2.7/dist-packages/pip/req.py", line 948, in prepare_files
              url = finder.find_requirement(req_to_install, upgrade=self.upgrade)
            File "/usr/lib/python2.7/dist-packages/pip/index.py", line 152, in find_requirement
              raise DistributionNotFound('No distributions at all found for %s' % req)
          DistributionNotFound: No distributions at all found for fake-package

          Storing complete log in /root/.pip/pip.log
          EOS
        )
        Puppet::Util::Execution.expects(:execpipe).yields(p).once
        @resource[:name] = "fake_package"
        expect(@provider.latest).to eq(nil)
      end
    end

    context "with pip version >= 1.5.4" do
      # For Pip 1.5.4 and above, you can get a version list from CLI - which allows for native pip behavior
      # with regards to custom repositories, proxies and the like
      before :each do
        described_class.stubs(:pip_version).returns('1.5.4')
        described_class.stubs(:which).with('pip').returns("/fake/bin/pip")
        described_class.stubs(:which).with('pip-python').returns("/fake/bin/pip")
        described_class.stubs(:which).with('pip.exe').returns("/fake/bin/pip")
      end

      it "should find a version number for real_package" do
        p = StringIO.new(
          <<-EOS
          Collecting real-package==versionplease
            Could not find a version that satisfies the requirement real-package==versionplease (from versions: 1.1.3, 1.2, 1.9b1)
          No matching distribution found for real-package==versionplease
          EOS
        )
        Puppet::Util::Execution.expects(:execpipe).with(["/fake/bin/pip", "install", "real_package==versionplease"]).yields(p).once
        @resource[:name] = "real_package"
        latest = @provider.latest
        expect(latest).to eq('1.9b1')
      end

      it "should not find a version number for fake_package" do
        p = StringIO.new(
          <<-EOS
          Collecting fake-package==versionplease
            Could not find a version that satisfies the requirement fake-package==versionplease (from versions: )
          No matching distribution found for fake-package==versionplease
          EOS
        )
        Puppet::Util::Execution.expects(:execpipe).with(["/fake/bin/pip", "install", "fake_package==versionplease"]).yields(p).once
        @resource[:name] = "fake_package"
        expect(@provider.latest).to eq(nil)
      end

      it "should handle out-of-order version numbers for real_package" do
        p = StringIO.new(
          <<-EOS
          Collecting real-package==versionplease
            Could not find a version that satisfies the requirement real-package==versionplease (from versions: 1.11, 13.0.3, 1.6, 1.9, 1.3.2, 14.0.1, 12.0.7, 13.0.3, 1.7.2, 1.8.4, 1.6.1, 0.9.2, 1.3, 1.8.3, 12.1.1, 1.1, 1.11.6, 1.4.8, 1.6.3, 1.10.1, 14.0.2, 1.11.3, 14.0.3, 1.4rc1, 0.8.4, 1.0, 12.0.5, 14.0.6, 1.11.5, 1.7.1.1, 1.11.4, 13.0.1, 13.1.2, 1.3.3, 0.8.2, 14.0.0, 12.0, 1.8, 1.3.4, 12.0, 1.2, 12.0.6, 0.9.1, 13.1.1, 14.0.5, 15.0.2, 15.0.0, 1.4.5, 1.4.3, 13.1.1, 1.11.2, 13.1.2, 1.3.1, 13.1.0, 12.0.2, 1.11.1, 12.0.1, 12.1.0, 0.9, 1.4.4, 13.0.0, 1.4.9, 12.1.0, 1.7.1, 1.4.2, 14.0.5, 0.8.1, 1.4.6, 0.8.3, 1.11.3, 1.5.1, 1.4.7, 13.0.2, 12.0.7, 13.0.0, 1.9.1, 1.8.2, 14.0.1, 14.0.0, 14.0.4, 1.6.2, 15.0.1, 13.1.0, 0.8, 1.7, 15.0.2, 12.0.5, 13.0.1, 1.8.1, 1.11.6, 15.0.1, 12.0.4, 12.1.1, 13.0.2, 1.11.4, 1.10, 14.0.4, 14.0.6, 1.4.1, 1.4, 1.5.2, 12.0.2, 12.0.1, 14.0.3, 14.0.2, 1.11.1, 1.7.1.2, 15.0.0, 12.0.4, 1.6.4, 1.11.2, 1.5)
          No distributions matching the version for real-package==versionplease
          EOS
        )
        Puppet::Util::Execution.expects(:execpipe).with(["/fake/bin/pip", "install", "real_package==versionplease"]).yields(p).once
        @resource[:name] = "real_package"
        latest = @provider.latest
        expect(latest).to eq('15.0.2')
      end
    end
  end

  context "install" do
    before do
      @resource[:name] = "fake_package"
      @url = "git+https://example.com/fake_package.git"
    end

    it "should install" do
      @resource[:ensure] = :installed
      @resource[:source] = nil
      @provider.expects(:lazy_pip).
        with("install", '-q', "fake_package")
      @provider.install
    end

    it "omits the -e flag (GH-1256)" do
      # The -e flag makes the provider non-idempotent
      @resource[:ensure] = :installed
      @resource[:source] = @url
      @provider.expects(:lazy_pip).with() do |*args|
        not args.include?("-e")
      end
      @provider.install
    end

    it "should install from SCM" do
      @resource[:ensure] = :installed
      @resource[:source] = @url
      @provider.expects(:lazy_pip).
        with("install", '-q', "#{@url}#egg=fake_package")
      @provider.install
    end

    it "should install a particular SCM revision" do
      @resource[:ensure] = "0123456"
      @resource[:source] = @url
      @provider.expects(:lazy_pip).
        with("install", "-q", "#{@url}@0123456#egg=fake_package")
      @provider.install
    end

    it "should install a particular version" do
      @resource[:ensure] = "0.0.0"
      @resource[:source] = nil
      @provider.expects(:lazy_pip).with("install", "-q", "fake_package==0.0.0")
      @provider.install
    end

    it "should upgrade" do
      @resource[:ensure] = :latest
      @resource[:source] = nil
      @provider.expects(:lazy_pip).
        with("install", "-q", "--upgrade", "fake_package")
      @provider.install
    end

    it "should handle install options" do
      @resource[:ensure] = :installed
      @resource[:source] = nil
      @resource[:install_options] = [{"--timeout" => "10"}, "--no-index"]
      @provider.expects(:lazy_pip).
        with("install", "-q", "--timeout=10", "--no-index", "fake_package")
      @provider.install
    end
  end

  context "uninstall" do
    it "should uninstall" do
      @resource[:name] = "fake_package"
      @provider.expects(:lazy_pip).
        with('uninstall', '-y', '-q', 'fake_package')
      @provider.uninstall
    end
  end

  context "update" do
    it "should just call install" do
      @provider.expects(:install).returns(nil)
      @provider.update
    end
  end

  context "pip_version" do
    it "should return nil on missing pip" do
      described_class.stubs(:pip_cmd).returns(nil)
      expect(described_class.pip_version).to eq(nil)
    end

    it "should look up version if pip is present" do
      described_class.stubs(:pip_cmd).returns('/fake/bin/pip')
      p = stub("process")
      p.expects(:collect).yields('pip 8.0.2 from /usr/local/lib/python2.7/dist-packages (python 2.7)')
      described_class.expects(:execpipe).with(['/fake/bin/pip', '--version']).yields(p)
      expect(described_class.pip_version).to eq('8.0.2')
    end
  end

  context "lazy_pip" do
    after(:each) do
      Puppet::Type::Package::ProviderPip.instance_variable_set(:@confine_collection, nil)
    end

    it "should succeed if pip is present" do
      @provider.stubs(:pip).returns(nil)
      @provider.method(:lazy_pip).call "freeze"
    end

    osfamilies.each do |osfamily, pip_cmds|
      pip_cmds.each do |pip_cmd|
        it "should retry on #{osfamily} systems if #{pip_cmd} has not yet been found" do
          Puppet.features.stubs(:microsoft_windows?).returns (osfamily == 'windows')
          @provider.expects(:pip).twice.with('freeze').raises(NoMethodError).then.returns(nil)
          pip_cmds.each do |cmd|
            unless cmd == pip_cmd
              @provider.expects(:which).with(cmd).returns(nil)
            end
          end
          @provider.expects(:which).with(pip_cmd).returns("/fake/bin/#{pip_cmd}")
          @provider.method(:lazy_pip).call "freeze"
        end
      end

      it "should fail on #{osfamily} systems if #{pip_cmds.join(' and ')} are missing" do
        Puppet.features.stubs(:microsoft_windows?).returns (osfamily == 'windows')
        @provider.expects(:pip).with('freeze').raises(NoMethodError)
        pip_cmds.each do |pip_cmd|
          @provider.expects(:which).with(pip_cmd).returns(nil)
        end
        expect { @provider.method(:lazy_pip).call("freeze") }.to raise_error(NoMethodError)
      end

      it "should output a useful error message on #{osfamily} systems if #{pip_cmds.join(' and ')} are missing" do
        Puppet.features.stubs(:microsoft_windows?).returns (osfamily == 'windows')
        @provider.expects(:pip).with('freeze').raises(NoMethodError)
        pip_cmds.each do |pip_cmd|
          @provider.expects(:which).with(pip_cmd).returns(nil)
        end
        expect { @provider.method(:lazy_pip).call("freeze") }.
          to raise_error(NoMethodError, "Could not locate command #{pip_cmds.join(' and ')}.")
      end
    end
  end
end