summaryrefslogtreecommitdiff
path: root/spec/unit/provider/package/pip_spec.rb
blob: 81f61479120b3ea22c9327e16440d21b83d32b59 (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
#! /usr/bin/env ruby
require 'spec_helper'

provider_class = Puppet::Type.type(:package).provider(:pip)
osfamilies = { 'RedHat' => 'pip-python', 'Not RedHat' => 'pip' }

describe provider_class do

  before do
    @resource = Puppet::Resource.new(:package, "fake_package")
    @provider = provider_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([])
    XMLRPC::Client.stubs(:new2).returns(@client)
  end

  describe "parse" do

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

    it "should return nil on invalid input" do
      provider_class.parse("foo").should == nil
    end

  end

  describe "cmd" do
    it "should return pip-python on RedHat systems" do
      Facter.stubs(:value).with(:osfamily).returns("RedHat")
      provider_class.cmd.should == 'pip-python'
    end

    it "should return pip by default" do
      Facter.stubs(:value).with(:osfamily).returns("Not RedHat")
      provider_class.cmd.should == 'pip'
    end

  end

  describe "instances" do

    osfamilies.each do |osfamily, pip_cmd|
      it "should return an array on #{osfamily} when #{pip_cmd} is present" do
        Facter.stubs(:value).with(:osfamily).returns(osfamily)
        provider_class.expects(:which).with(pip_cmd).returns("/fake/bin/pip")
        p = stub("process")
        p.expects(:collect).yields("real_package==1.2.5")
        provider_class.expects(:execpipe).with("/fake/bin/pip freeze").yields(p)
        provider_class.instances
      end

      it "should return an empty array on #{osfamily} when #{pip_cmd} is missing" do
        Facter.stubs(:value).with(:osfamily).returns(osfamily)
        provider_class.expects(:which).with(pip_cmd).returns nil
        provider_class.instances.should == []
      end
    end

  end

  describe "query" do

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

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

      @provider.query.should == {
        :ensure   => "1.2.5",
        :name     => "real_package",
        :provider => :pip,
      }
    end

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

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

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

      @provider.query.should == {
        :ensure   => "1.2.5",
        :name     => "real_package",
        :provider => :pip,
      }
    end

  end

  describe "latest" do

    it "should find a version number for real_package" do
      @resource[:name] = "real_package"
      @provider.latest.should_not == nil
    end

    it "should not find a version number for fake_package" do
      @resource[:name] = "fake_package"
      @provider.latest.should == nil
    end

    it "should handle a timeout gracefully" do
      @resource[:name] = "fake_package"
      @client.stubs(:call).raises(Timeout::Error)
      lambda { @provider.latest }.should raise_error(Puppet::Error)
    end

  end

  describe "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

  end

  describe "uninstall" do

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

  end

  describe "update" do

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

  end

  describe "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_cmd|
      it "should retry on #{osfamily} if #{pip_cmd} has not yet been found" do
        Facter.stubs(:value).with(:osfamily).returns(osfamily)
        @provider.expects(:pip).twice.with('freeze').raises(NoMethodError).then.returns(nil)
        @provider.expects(:which).with(pip_cmd).returns("/fake/bin/pip")
        @provider.method(:lazy_pip).call "freeze"
      end

      it "should fail on #{osfamily} if #{pip_cmd} is missing" do
        Facter.stubs(:value).with(:osfamily).returns(osfamily)
        @provider.expects(:pip).with('freeze').raises(NoMethodError)
        @provider.expects(:which).with(pip_cmd).returns(nil)
        expect { @provider.method(:lazy_pip).call("freeze") }.to raise_error(NoMethodError)
      end

      it "should output a useful error message on #{osfamily} if #{pip_cmd} is missing" do
        Facter.stubs(:value).with(:osfamily).returns(osfamily)
        @provider.expects(:pip).with('freeze').raises(NoMethodError)
        @provider.expects(:which).with(pip_cmd).returns(nil)
        expect { @provider.method(:lazy_pip).call("freeze") }.
          to raise_error(NoMethodError, 'Could not locate the pip command.')
      end

    end

  end

end