summaryrefslogtreecommitdiff
path: root/spec/unit/provider/package/nim_spec.rb
blob: 95754fcfc2c66e6169d4fdf744fa3d2b52e0802c (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
248
249
250
#! /usr/bin/env ruby
require 'spec_helper'

provider_class = Puppet::Type.type(:package).provider(:nim)

describe provider_class do

  before(:each) do
    # Create a mock resource
    @resource = stub 'resource'

    # A catch all; no parameters set
    @resource.stubs(:[]).returns(nil)

    # But set name and source
    @resource.stubs(:[]).with(:name).returns "mypackage.foo"
    @resource.stubs(:[]).with(:source).returns "mysource"
    @resource.stubs(:[]).with(:ensure).returns :installed

    @provider = provider_class.new
    @provider.resource = @resource
  end

  it "should have an install method" do
    @provider = provider_class.new
    @provider.should respond_to(:install)
  end

  let(:bff_showres_output) {
    <<END
mypackage.foo                                                           ALL  @@I:mypackage.foo _all_filesets
 @ 1.2.3.1  MyPackage Runtime Environment                       @@I:mypackage.foo 1.2.3.1
 + 1.2.3.4  MyPackage Runtime Environment                       @@I:mypackage.foo 1.2.3.4
 + 1.2.3.8  MyPackage Runtime Environment                       @@I:mypackage.foo 1.2.3.8

END
  }

  let(:rpm_showres_output) {
    <<END
mypackage.foo                                                                ALL  @@R:mypackage.foo _all_filesets
 @@R:mypackage.foo-1.2.3-1 1.2.3-1
 @@R:mypackage.foo-1.2.3-4 1.2.3-4
 @@R:mypackage.foo-1.2.3-8 1.2.3-8

END
  }

  context "when installing" do
    it "should install a package" do

      @resource.stubs(:should).with(:ensure).returns(:installed)
      Puppet::Util.expects(:execute).with("/usr/sbin/nimclient -o showres -a resource=mysource |/usr/bin/grep -p -E 'mypackage\\.foo'").returns(bff_showres_output)
      @provider.expects(:nimclient).with("-o", "cust", "-a", "installp_flags=acgwXY", "-a", "lpp_source=mysource", "-a", "filesets=mypackage.foo 1.2.3.8")
      @provider.install
    end

    context "when installing versioned packages" do

      it "should fail if the package is not available on the lpp source" do
        nimclient_showres_output = ""

        @resource.stubs(:should).with(:ensure).returns("1.2.3.4")
        Puppet::Util.expects(:execute).with("/usr/sbin/nimclient -o showres -a resource=mysource |/usr/bin/grep -p -E 'mypackage\\.foo( |-)1\\.2\\.3\\.4'").returns(nimclient_showres_output)
        expect {
          @provider.install
        }.to raise_error(Puppet::Error, "Unable to find package 'mypackage.foo' with version '1.2.3.4' on lpp_source 'mysource'")
      end

      it "should succeed if a BFF/installp package is available on the lpp source" do
        nimclient_sequence = sequence('nimclient')

        @resource.stubs(:should).with(:ensure).returns("1.2.3.4")
        Puppet::Util.expects(:execute).with("/usr/sbin/nimclient -o showres -a resource=mysource |/usr/bin/grep -p -E 'mypackage\\.foo( |-)1\\.2\\.3\\.4'").returns(bff_showres_output).in_sequence(nimclient_sequence)
        @provider.expects(:nimclient).with("-o", "cust", "-a", "installp_flags=acgwXY", "-a", "lpp_source=mysource", "-a", "filesets=mypackage.foo 1.2.3.4").in_sequence(nimclient_sequence)
        @provider.install
      end

      it "should fail if the specified version of a BFF package is superseded" do
        nimclient_sequence = sequence('nimclient')

        install_output = <<OUTPUT
+-----------------------------------------------------------------------------+
                    Pre-installation Verification...
+-----------------------------------------------------------------------------+
Verifying selections...done
Verifying requisites...done
Results...

WARNINGS
--------
  Problems described in this section are not likely to be the source of any
  immediate or serious failures, but further actions may be necessary or
  desired.

  Already Installed
  -----------------
  The number of selected filesets that are either already installed
  or effectively installed through superseding filesets is 1.  See
  the summaries at the end of this installation for details.

  NOTE:  Base level filesets may be reinstalled using the "Force"
  option (-F flag), or they may be removed, using the deinstall or
  "Remove Software Products" facility (-u flag), and then reinstalled.

  << End of Warning Section >>

+-----------------------------------------------------------------------------+
                   BUILDDATE Verification ...
+-----------------------------------------------------------------------------+
Verifying build dates...done
FILESET STATISTICS
------------------
    1  Selected to be installed, of which:
        1  Already installed (directly or via superseding filesets)
  ----
    0  Total to be installed


Pre-installation Failure/Warning Summary
----------------------------------------
Name                      Level           Pre-installation Failure/Warning
-------------------------------------------------------------------------------
mypackage.foo              1.2.3.1         Already superseded by 1.2.3.4
OUTPUT

        @resource.stubs(:should).with(:ensure).returns("1.2.3.1")
        Puppet::Util.expects(:execute).with("/usr/sbin/nimclient -o showres -a resource=mysource |/usr/bin/grep -p -E 'mypackage\\.foo( |-)1\\.2\\.3\\.1'").returns(bff_showres_output).in_sequence(nimclient_sequence)
        @provider.expects(:nimclient).with("-o", "cust", "-a", "installp_flags=acgwXY", "-a", "lpp_source=mysource", "-a", "filesets=mypackage.foo 1.2.3.1").in_sequence(nimclient_sequence).returns(install_output)

        expect { @provider.install }.to raise_error(Puppet::Error, "NIM package provider is unable to downgrade packages")
    end


    it "should succeed if an RPM package is available on the lpp source" do
        nimclient_sequence = sequence('nimclient')

        @resource.stubs(:should).with(:ensure).returns("1.2.3-4")
        Puppet::Util.expects(:execute).with("/usr/sbin/nimclient -o showres -a resource=mysource |/usr/bin/grep -p -E 'mypackage\\.foo( |-)1\\.2\\.3\\-4'").returns(rpm_showres_output).in_sequence(nimclient_sequence)
        @provider.expects(:nimclient).with("-o", "cust", "-a", "installp_flags=acgwXY", "-a", "lpp_source=mysource", "-a", "filesets=mypackage.foo-1.2.3-4").in_sequence(nimclient_sequence)
        @provider.install
      end
    end

    it "should fail if the specified version of a RPM package is superseded" do
      nimclient_sequence = sequence('nimclient')

      install_output = <<OUTPUT


Validating RPM package selections ...

Please wait...
+-----------------------------------------------------------------------------+
                          RPM  Error Summary:
+-----------------------------------------------------------------------------+
The following RPM packages were requested for installation
but they are already installed or superseded by a package installed
at a higher level:
mypackage.foo-1.2.3-1 is superseded by mypackage.foo-1.2.3-4


OUTPUT

      @resource.stubs(:should).with(:ensure).returns("1.2.3-1")
      Puppet::Util.expects(:execute).with("/usr/sbin/nimclient -o showres -a resource=mysource |/usr/bin/grep -p -E 'mypackage\\.foo( |-)1\\.2\\.3\\-1'").returns(rpm_showres_output).in_sequence(nimclient_sequence)
      @provider.expects(:nimclient).with("-o", "cust", "-a", "installp_flags=acgwXY", "-a", "lpp_source=mysource", "-a", "filesets=mypackage.foo-1.2.3-1").in_sequence(nimclient_sequence).returns(install_output)

      expect { @provider.install }.to raise_error(Puppet::Error, "NIM package provider is unable to downgrade packages")
    end



  end

  context "when uninstalling" do
    it "should call installp to uninstall a bff package" do
      @provider.expects(:lslpp).with("-qLc", "mypackage.foo").returns("#bos.atm:bos.atm.atmle:7.1.2.0: : :C: :ATM LAN Emulation Client Support : : : : : : :0:0:/:1241")
      @provider.expects(:installp).with("-gu", "mypackage.foo")
      @provider.class.expects(:pkglist).with(:pkgname => 'mypackage.foo').returns(nil)
      @provider.uninstall
    end

    it "should call rpm to uninstall an rpm package" do
      @provider.expects(:lslpp).with("-qLc", "mypackage.foo").returns("cdrecord:cdrecord-1.9-6:1.9-6: : :C:R:A command line CD/DVD recording program.: :/bin/rpm -e cdrecord: : : : :0: :/opt/freeware:Wed Jun 29 09:41:32 PDT 2005")
      @provider.expects(:rpm).with("-e", "mypackage.foo")
      @provider.class.expects(:pkglist).with(:pkgname => 'mypackage.foo').returns(nil)
      @provider.uninstall
    end

  end


  context "when parsing nimclient showres output" do
    describe "#parse_showres_output" do
      it "should be able to parse installp/BFF package listings" do
        packages = subject.send(:parse_showres_output, bff_showres_output)
        Set.new(packages.keys).should == Set.new(['mypackage.foo'])
        versions = packages['mypackage.foo']
        ['1.2.3.1', '1.2.3.4', '1.2.3.8'].each do |version|
          versions.has_key?(version).should == true
          versions[version].should == :installp
        end
      end

      it "should be able to parse RPM package listings" do
        packages = subject.send(:parse_showres_output, rpm_showres_output)
        Set.new(packages.keys).should == Set.new(['mypackage.foo'])
        versions = packages['mypackage.foo']
        ['1.2.3-1', '1.2.3-4', '1.2.3-8'].each do |version|
          versions.has_key?(version).should == true
          versions[version].should == :rpm
        end
      end
    end

    describe "#determine_latest_version" do
      context "when there are multiple versions" do
        it "should return the latest version" do
          subject.send(:determine_latest_version, rpm_showres_output, 'mypackage.foo').should == [:rpm, '1.2.3-8']
        end
      end

      context "when there is only one version" do
        it "should return the type specifier and `nil` for the version number" do
          nimclient_showres_output = <<END
mypackage.foo                                                                ALL  @@R:mypackage.foo _all_filesets
 @@R:mypackage.foo-1.2.3-4 1.2.3-4

END
          subject.send(:determine_latest_version, nimclient_showres_output, 'mypackage.foo').should == [:rpm, nil]
        end
      end

    end

    describe "#determine_package_type" do
      it "should return :rpm for rpm packages" do
        subject.send(:determine_package_type, rpm_showres_output, 'mypackage.foo', '1.2.3-4').should == :rpm
      end

      it "should return :installp for installp/bff packages" do
        subject.send(:determine_package_type, bff_showres_output, 'mypackage.foo', '1.2.3.4').should == :installp
      end
    end
  end



end