#! /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 expect(@provider).to respond_to(:install) end let(:bff_showres_output) { <> +-----------------------------------------------------------------------------+ 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 = < '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) expect(Set.new(packages.keys)).to eq(Set.new(['mypackage.foo'])) versions = packages['mypackage.foo'] ['1.2.3.1', '1.2.3.4', '1.2.3.8'].each do |version| expect(versions.has_key?(version)).to eq(true) expect(versions[version]).to eq(:installp) end end it "should be able to parse RPM package listings" do packages = subject.send(:parse_showres_output, rpm_showres_output) expect(Set.new(packages.keys)).to eq(Set.new(['mypackage.foo'])) versions = packages['mypackage.foo'] ['1.2.3-1', '1.2.3-4', '1.2.3-8'].each do |version| expect(versions.has_key?(version)).to eq(true) expect(versions[version]).to eq(:rpm) end end end describe "#determine_latest_version" do context "when there are multiple versions" do it "should return the latest version" do expect(subject.send(:determine_latest_version, rpm_showres_output, 'mypackage.foo')).to eq([: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 = <