Skip to content

Commit

Permalink
Merge pull request #2672 from peterhuene/pup/2182
Browse files Browse the repository at this point in the history
(PUP-2182) Fix yum and zypper providers virtual_package feature.
  • Loading branch information
joshcooper committed May 20, 2014
2 parents 8e473d1 + 21322de commit a901afd
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 37 deletions.
20 changes: 14 additions & 6 deletions lib/puppet/provider/package/yum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,14 @@ def self.clear
end

def install
wanted = @resource[:name]
# If not allowing virtual packages, do a query to ensure a real package exists
unless @resource.allow_virtual?
yum '-d', '0', '-e', '0', '-y', :list, wanted
end

should = @resource.should(:ensure)
self.debug "Ensuring => #{should}"
wanted = @resource[:name]
operation = :install

case should
Expand All @@ -113,12 +118,15 @@ def install
yum *args


is = self.query
raise Puppet::Error, "Could not find package #{self.name}" unless is
# If a version was specified, query again to see if it is a matching version
if should
is = self.query
raise Puppet::Error, "Could not find package #{self.name}" unless is

# FIXME: Should we raise an exception even if should == :latest
# and yum updated us to a version other than @param_hash[:ensure] ?
raise Puppet::Error, "Failed to update to version #{should}, got version #{is[:ensure]} instead" if should && should != is[:ensure]
# FIXME: Should we raise an exception even if should == :latest
# and yum updated us to a version other than @param_hash[:ensure] ?
raise Puppet::Error, "Failed to update to version #{should}, got version #{is[:ensure]} instead" if should != is[:ensure]
end
end

# What's the latest package version available?
Expand Down
5 changes: 3 additions & 2 deletions lib/puppet/provider/package/zypper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Puppet::Type.type(:package).provide :zypper, :parent => :rpm do
desc "Support for SuSE `zypper` package manager. Found in SLES10sp2+ and SLES11"

has_feature :versionable, :install_options
has_feature :versionable, :install_options, :virtual_packages

commands :zypper => "/usr/bin/zypper"

Expand All @@ -23,7 +23,7 @@ def install
# XXX: We don't actually deal with epochs here.
case should
when true, false, Symbol
# pass
should = nil
else
# Add the package version
wanted = "#{wanted}-#{should}"
Expand Down Expand Up @@ -52,6 +52,7 @@ def install
#zypper 0.6.13 (OpenSuSE 10.2) does not support auto agree with licenses
options << '--auto-agree-with-licenses' unless major < 1 and minor <= 6 and patch <= 13
options << '--no-confirm'
options << '--name' unless @resource.allow_virtual? || should
options += install_options if resource[:install_options]
options << wanted

Expand Down
57 changes: 34 additions & 23 deletions spec/unit/provider/package/yum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@

it 'should call yum install for :installed' do
resource.stubs(:should).with(:ensure).returns :installed
provider.expects(:yum).with('-d', '0', '-e', '0', '-y', :install, 'mypackage')
provider.expects(:yum).with('-d', '0', '-e', '0', '-y', :list, name)
provider.expects(:yum).with('-d', '0', '-e', '0', '-y', :install, name)
provider.install
end

Expand All @@ -59,33 +60,43 @@
end

it 'should be able to set version' do
resource[:ensure] = '1.2'

provider.expects(:yum).with('-d', '0', '-e', '0', '-y', :install, 'mypackage-1.2')
provider.stubs(:query).returns :ensure => '1.2'
version = '1.2'
resource[:ensure] = version
provider.expects(:yum).with('-d', '0', '-e', '0', '-y', :list, name)
provider.expects(:yum).with('-d', '0', '-e', '0', '-y', :install, "#{name}-#{version}")
provider.stubs(:query).returns :ensure => version
provider.install
end

it 'should be able to downgrade' do
current_version = '1.2'
version = '1.0'
resource[:ensure] = '1.0'

provider.expects(:yum).with('-d', '0', '-e', '0', '-y', :downgrade, 'mypackage-1.0')
provider.stubs(:query).returns(:ensure => '1.2').then.returns(:ensure => '1.0')
provider.expects(:yum).with('-d', '0', '-e', '0', '-y', :downgrade, "#{name}-#{version}")
provider.stubs(:query).returns(:ensure => current_version).then.returns(:ensure => version)
provider.install
end

it 'should accept install options' do
resource[:ensure] = :installed
resource[:install_options] = ['-t', {'-x' => 'expackage'}]

provider.expects(:yum).with('-d', '0', '-e', '0', '-y', ['-t', '-x=expackage'], :install, 'mypackage')
provider.expects(:yum).with('-d', '0', '-e', '0', '-y', ['-t', '-x=expackage'], :install, name)
provider.install
end

it 'allow virtual packages' do
resource[:ensure] = :installed
resource[:allow_virtual] = true
provider.expects(:yum).with('-d', '0', '-e', '0', '-y', :list, name).never
provider.expects(:yum).with('-d', '0', '-e', '0', '-y', :install, name)
provider.install
end
end

describe 'when uninstalling' do
it 'should use erase to purge' do
provider.expects(:yum).with('-y', :erase, 'mypackage')
provider.expects(:yum).with('-y', :erase, name)
provider.purge
end
end
Expand All @@ -103,7 +114,7 @@
]
provider.stubs(:properties).returns({:ensure => '3.4.5'})

described_class.expects(:latest_package_version).with('mypackage', ['contrib', 'centosplus'], [])
described_class.expects(:latest_package_version).with(name, ['contrib', 'centosplus'], [])
provider.latest
end

Expand All @@ -114,13 +125,13 @@
]
provider.stubs(:properties).returns({:ensure => '3.4.5'})

described_class.expects(:latest_package_version).with('mypackage', [], ['updates', 'centosplus'])
described_class.expects(:latest_package_version).with(name, [], ['updates', 'centosplus'])
provider.latest
end

describe 'and a newer version is not available' do
before :each do
described_class.stubs(:latest_package_version).with('mypackage', [], []).returns nil
described_class.stubs(:latest_package_version).with(name, [], []).returns nil
end

it 'raises an error the package is not installed' do
Expand All @@ -139,7 +150,7 @@
describe 'and a newer version is available' do
let(:latest_version) do
{
:name => 'mypackage',
:name => name,
:epoch => '1',
:version => '2.3.4',
:release => '5',
Expand All @@ -148,7 +159,7 @@
end

it 'includes the epoch in the version string' do
described_class.stubs(:latest_package_version).with('mypackage', [], []).returns(latest_version)
described_class.stubs(:latest_package_version).with(name, [], []).returns(latest_version)
provider.latest.should == '1:2.3.4-5'
end
end
Expand All @@ -160,7 +171,7 @@

let(:mypackage_version) do
{
:name => 'mypackage',
:name => name,
:epoch => '1',
:version => '2.3.4',
:release => '5',
Expand All @@ -170,20 +181,20 @@

let(:mypackage_newerversion) do
{
:name => 'mypackage',
:name => name,
:epoch => '1',
:version => '4.5.6',
:release => '7',
:arch => 'i686',
}
end

let(:latest_versions) { {'mypackage' => [mypackage_version]} }
let(:enabled_versions) { {'mypackage' => [mypackage_newerversion]} }
let(:latest_versions) { {name => [mypackage_version]} }
let(:enabled_versions) { {name => [mypackage_newerversion]} }

it "returns the version hash if the package was found" do
described_class.expects(:fetch_latest_versions).with([], []).once.returns(latest_versions)
version = described_class.latest_package_version('mypackage', [], [])
version = described_class.latest_package_version(name, [], [])
expect(version).to eq(mypackage_version)
end

Expand All @@ -197,7 +208,7 @@
described_class.expects(:fetch_latest_versions).with([], []).once.returns(latest_versions)

2.times {
version = described_class.latest_package_version('mypackage', [], [])
version = described_class.latest_package_version(name, [], [])
expect(version).to eq mypackage_version
}
end
Expand All @@ -207,12 +218,12 @@
described_class.expects(:fetch_latest_versions).with(['enabled'], ['disabled']).once.returns(enabled_versions)

2.times {
version = described_class.latest_package_version('mypackage', [], [])
version = described_class.latest_package_version(name, [], [])
expect(version).to eq mypackage_version
}

2.times {
version = described_class.latest_package_version('mypackage', ['enabled'], ['disabled'])
version = described_class.latest_package_version(name, ['enabled'], ['disabled'])
expect(version).to eq(mypackage_newerversion)
}
end
Expand Down
31 changes: 25 additions & 6 deletions spec/unit/provider/package/zypper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
describe "when installing with zypper version >= 1.0" do
it "should use a command-line with versioned package'" do
@resource.stubs(:should).with(:ensure).returns "1.2.3-4.5.6"
@resource.stubs(:allow_virtual?).returns false
@provider.stubs(:zypper_version).returns "1.2.8"

@provider.expects(:zypper).with('--quiet', :install, '--auto-agree-with-licenses', '--no-confirm', 'mypackage-1.2.3-4.5.6')
Expand All @@ -56,8 +57,9 @@

it "should use a command-line without versioned package" do
@resource.stubs(:should).with(:ensure).returns :latest
@resource.stubs(:allow_virtual?).returns false
@provider.stubs(:zypper_version).returns "1.2.8"
@provider.expects(:zypper).with('--quiet', :install, '--auto-agree-with-licenses', '--no-confirm', 'mypackage')
@provider.expects(:zypper).with('--quiet', :install, '--auto-agree-with-licenses', '--no-confirm', '--name', 'mypackage')
@provider.expects(:query).returns "mypackage 0 1.2.3 4.5.6 x86_64"
@provider.install
end
Expand All @@ -66,6 +68,7 @@
describe "when installing with zypper version = 0.6.104" do
it "should use a command-line with versioned package'" do
@resource.stubs(:should).with(:ensure).returns "1.2.3-4.5.6"
@resource.stubs(:allow_virtual?).returns false
@provider.stubs(:zypper_version).returns "0.6.104"

@provider.expects(:zypper).with('--terse', :install, '--auto-agree-with-licenses', '--no-confirm', 'mypackage-1.2.3-4.5.6')
Expand All @@ -75,8 +78,9 @@

it "should use a command-line without versioned package" do
@resource.stubs(:should).with(:ensure).returns :latest
@resource.stubs(:allow_virtual?).returns false
@provider.stubs(:zypper_version).returns "0.6.104"
@provider.expects(:zypper).with('--terse', :install, '--auto-agree-with-licenses', '--no-confirm', 'mypackage')
@provider.expects(:zypper).with('--terse', :install, '--auto-agree-with-licenses', '--no-confirm', '--name', 'mypackage')
@provider.expects(:query).returns "mypackage 0 1.2.3 4.5.6 x86_64"
@provider.install
end
Expand All @@ -85,6 +89,7 @@
describe "when installing with zypper version = 0.6.13" do
it "should use a command-line with versioned package'" do
@resource.stubs(:should).with(:ensure).returns "1.2.3-4.5.6"
@resource.stubs(:allow_virtual?).returns false
@provider.stubs(:zypper_version).returns "0.6.13"

@provider.expects(:zypper).with('--terse', :install, '--no-confirm', 'mypackage-1.2.3-4.5.6')
Expand All @@ -94,8 +99,9 @@

it "should use a command-line without versioned package" do
@resource.stubs(:should).with(:ensure).returns :latest
@resource.stubs(:allow_virtual?).returns false
@provider.stubs(:zypper_version).returns "0.6.13"
@provider.expects(:zypper).with('--terse', :install, '--no-confirm', 'mypackage')
@provider.expects(:zypper).with('--terse', :install, '--no-confirm', '--name', 'mypackage')
@provider.expects(:query).returns "mypackage 0 1.2.3 4.5.6 x86_64"
@provider.install
end
Expand All @@ -118,11 +124,21 @@
end
end

it "should install a virtual package" do
@resource.stubs(:should).with(:ensure).returns :installed
@resource.stubs(:allow_virtual?).returns true
@provider.stubs(:zypper_version).returns "0.6.13"
@provider.expects(:zypper).with('--terse', :install, '--no-confirm', 'mypackage')
@provider.expects(:query).returns "mypackage 0 1.2.3 4.5.6 x86_64"
@provider.install
end

describe "when installing with zypper install options" do
it "should install the package without checking keys" do
@resource.stubs(:[]).with(:name).returns "php5"
@resource.stubs(:[]).with(:install_options).returns ['--no-gpg-check', {'-p' => '/vagrant/files/localrepo/'}]
@resource.stubs(:should).with(:ensure).returns "5.4.10-4.5.6"
@resource.stubs(:allow_virtual?).returns false
@provider.stubs(:zypper_version).returns "1.2.8"

@provider.expects(:zypper).with('--quiet', :install,
Expand All @@ -135,9 +151,10 @@
@resource.stubs(:[]).with(:name).returns 'vim'
@resource.stubs(:[]).with(:install_options).returns([{ '--a' => 'foo', '--b' => '"quoted bar"' }])
@resource.stubs(:should).with(:ensure).returns :present
@resource.stubs(:allow_virtual?).returns false

@provider.stubs(:zypper_version).returns '1.2.8'
@provider.expects(:zypper).with('--quiet', :install, '--auto-agree-with-licenses', '--no-confirm', '--a=foo', '--b="quoted bar"', 'vim')
@provider.expects(:zypper).with('--quiet', :install, '--auto-agree-with-licenses', '--no-confirm', '--name', '--a=foo', '--b="quoted bar"', 'vim')
@provider.expects(:query).returns 'package vim is not installed'
@provider.install
end
Expand All @@ -146,9 +163,10 @@
@resource.stubs(:[]).with(:name).returns 'vim'
@resource.stubs(:[]).with(:install_options).returns([['--a', '--b', '--c']])
@resource.stubs(:should).with(:ensure).returns :present
@resource.stubs(:allow_virtual?).returns false

@provider.stubs(:zypper_version).returns '1.2.8'
@provider.expects(:zypper).with('--quiet', :install, '--auto-agree-with-licenses', '--no-confirm', '--a', '--b', '--c', 'vim')
@provider.expects(:zypper).with('--quiet', :install, '--auto-agree-with-licenses', '--no-confirm', '--name', '--a', '--b', '--c', 'vim')
@provider.expects(:query).returns 'package vim is not installed'
@provider.install
end
Expand All @@ -157,9 +175,10 @@
@resource.stubs(:[]).with(:name).returns 'vim'
@resource.stubs(:[]).with(:install_options).returns(['--a --b --c'])
@resource.stubs(:should).with(:ensure).returns :present
@resource.stubs(:allow_virtual?).returns false

@provider.stubs(:zypper_version).returns '1.2.8'
@provider.expects(:zypper).with('--quiet', :install, '--auto-agree-with-licenses', '--no-confirm', '--a --b --c', 'vim')
@provider.expects(:zypper).with('--quiet', :install, '--auto-agree-with-licenses', '--no-confirm', '--name', '--a --b --c', 'vim')
@provider.expects(:query).returns 'package vim is not installed'
@provider.install
end
Expand Down

0 comments on commit a901afd

Please sign in to comment.