Skip to content

Commit

Permalink
Merge pull request puppetlabs#2918 from adrienthebo/fixup/master/pup-…
Browse files Browse the repository at this point in the history
…2871-install-uninstall-options-pacman

Fixup/master/pup 2871 install uninstall options pacman
  • Loading branch information
peterhuene committed Jul 29, 2014
2 parents fd7cf5f + d5cc540 commit 2e844f4
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 74 deletions.
27 changes: 24 additions & 3 deletions lib/puppet/provider/package/pacman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

confine :operatingsystem => :archlinux
defaultfor :operatingsystem => :archlinux
has_feature :install_options
has_feature :uninstall_options
has_feature :upgradeable

# If yaourt is installed, we can make use of it
Expand All @@ -35,9 +37,15 @@ def install

def install_from_repo
if yaourt?
yaourt "--noconfirm", "-S", @resource[:name]
cmd = %w{--noconfirm}
cmd += install_options if @resource[:install_options]
cmd << "-S" << @resource[:name]
yaourt *cmd
else
pacman "--noconfirm", "--noprogressbar", "-Sy", @resource[:name]
cmd = %w{--noconfirm --noprogressbar}
cmd += install_options if @resource[:install_options]
cmd << "-Sy" << @resource[:name]
pacman *cmd
end
end
private :install_from_repo
Expand Down Expand Up @@ -204,6 +212,19 @@ def query

# Removes a package from the system.
def uninstall
pacman "--noconfirm", "--noprogressbar", "-R", @resource[:name]
cmd = %w{--noconfirm --noprogressbar}
cmd += uninstall_options if @resource[:uninstall_options]
cmd << "-R" << @resource[:name]
pacman *cmd
end

private

def install_options
join_options(@resource[:install_options])
end

def uninstall_options
join_options(@resource[:uninstall_options])
end
end
161 changes: 90 additions & 71 deletions spec/unit/provider/package/pacman_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,102 @@
require 'spec_helper'
require 'stringio'

provider = Puppet::Type.type(:package).provider(:pacman)

describe provider do
describe Puppet::Type.type(:package).provider(:pacman) do
let(:no_extra_options) { { :failonfail => true, :combine => true, :custom_environment => {} } }
let(:executor) { Puppet::Util::Execution }
let(:resolver) { Puppet::Util }

let(:resource) { Puppet::Type.type(:package).new(:name => 'package', :provider => 'pacman') }
let(:provider) { described_class.new(resource) }

before do
resolver.stubs(:which).with('/usr/bin/pacman').returns('/usr/bin/pacman')
provider.stubs(:which).with('/usr/bin/pacman').returns('/usr/bin/pacman')
described_class.stubs(:which).with('/usr/bin/pacman').returns('/usr/bin/pacman')
resolver.stubs(:which).with('/usr/bin/yaourt').returns('/usr/bin/yaourt')
provider.stubs(:which).with('/usr/bin/yaourt').returns('/usr/bin/yaourt')
@resource = Puppet::Type.type(:package).new(:name => 'package')
@provider = provider.new(@resource)
described_class.stubs(:which).with('/usr/bin/yaourt').returns('/usr/bin/yaourt')
end

describe "when installing" do
before do
@provider.stubs(:query).returns({
provider.stubs(:query).returns({
:ensure => '1.0'
})
end

it "should call pacman to install the right package quietly" do

if @provider.yaourt?
args = ['/usr/bin/yaourt', '--noconfirm', '-S', @resource[:name]]
else
args = ['/usr/bin/pacman', '--noconfirm', '--noprogressbar', '-Sy', @resource[:name]]
end

executor.
expects(:execute).
at_least_once.
with(args, no_extra_options).
returns ''
it "should call pacman to install the right package quietly when yaourt is not installed" do
provider.stubs(:yaourt?).returns(false)
args = ['--noconfirm', '--noprogressbar', '-Sy', resource[:name]]
provider.expects(:pacman).at_least_once.with(*args).returns ''
provider.install
end

@provider.install
it "should call yaourt to install the right package quietly when yaourt is installed" do
provider.stubs(:yaourt?).returns(true)
args = ['--noconfirm', '-S', resource[:name]]
provider.expects(:yaourt).at_least_once.with(*args).returns ''
provider.install
end

it "should raise an ExecutionFailure if the installation failed" do
executor.stubs(:execute).returns("")
@provider.expects(:query).returns(nil)
provider.expects(:query).returns(nil)

lambda { @provider.install }.should raise_exception(Puppet::ExecutionFailure)
lambda { provider.install }.should raise_exception(Puppet::ExecutionFailure)
end

context "when :source is specified" do
before :each do
@install = sequence("install")
describe "and install_options are given" do
before do
resource[:install_options] = ['-x', {'--arg' => 'value'}]
end

it "should call pacman to install the right package quietly when yaourt is not installed" do
provider.stubs(:yaourt?).returns(false)
args = ['--noconfirm', '--noprogressbar', '-x', '--arg=value', '-Sy', resource[:name]]
provider.expects(:pacman).at_least_once.with(*args).returns ''
provider.install
end

it "should call yaourt to install the right package quietly when yaourt is installed" do
provider.stubs(:yaourt?).returns(true)
args = ['--noconfirm', '-x', '--arg=value', '-S', resource[:name]]
provider.expects(:yaourt).at_least_once.with(*args).returns ''
provider.install
end
end

context "when :source is specified" do
let(:install_seq) { sequence("install") }

context "recognizable by pacman" do
%w{
/some/package/file
http://some.package.in/the/air
ftp://some.package.in/the/air
}.each do |source|
it "should install #{source} directly" do
@resource[:source] = source
resource[:source] = source

executor.expects(:execute).
with(all_of(includes("-Sy"), includes("--noprogressbar")), no_extra_options).
in_sequence(@install).
in_sequence(install_seq).
returns("")

executor.expects(:execute).
with(all_of(includes("-U"), includes(source)), no_extra_options).
in_sequence(@install).
in_sequence(install_seq).
returns("")

@provider.install
provider.install
end
end
end

context "as a file:// URL" do
let(:actual_file_path) { "/some/package/file" }

before do
@package_file = "file:///some/package/file"
@actual_file_path = "/some/package/file"
@resource[:source] = @package_file
resource[:source] = "file:///some/package/file"
end

it "should install from the path segment of the URL" do
Expand All @@ -91,64 +106,68 @@
includes("--noprogressbar"),
includes("--noconfirm")),
no_extra_options).
in_sequence(@install).
in_sequence(install_seq).
returns("")

executor.expects(:execute).
with(all_of(includes("-U"), includes(@actual_file_path)), no_extra_options).
in_sequence(@install).
with(all_of(includes("-U"), includes(actual_file_path)), no_extra_options).
in_sequence(install_seq).
returns("")

@provider.install
provider.install
end
end

context "as a puppet URL" do
before do
@resource[:source] = "puppet://server/whatever"
resource[:source] = "puppet://server/whatever"
end

it "should fail" do
lambda { @provider.install }.should raise_error(Puppet::Error)
lambda { provider.install }.should raise_error(Puppet::Error)
end
end

context "as a malformed URL" do
before do
@resource[:source] = "blah://"
resource[:source] = "blah://"
end

it "should fail" do
lambda { @provider.install }.should raise_error(Puppet::Error)
lambda { provider.install }.should raise_error(Puppet::Error)
end
end
end
end

describe "when updating" do
it "should call install" do
@provider.expects(:install).returns("install return value")
@provider.update.should == "install return value"
provider.expects(:install).returns("install return value")
provider.update.should == "install return value"
end
end

describe "when uninstalling" do
it "should call pacman to remove the right package quietly" do
executor.
expects(:execute).
with(["/usr/bin/pacman", "--noconfirm", "--noprogressbar", "-R", @resource[:name]], no_extra_options).
returns ""
args = ["/usr/bin/pacman", "--noconfirm", "--noprogressbar", "-R", resource[:name]]
executor.expects(:execute).with(args, no_extra_options).returns ""
provider.uninstall
end

@provider.uninstall
it "adds any uninstall_options" do
resource[:uninstall_options] = ['-x', {'--arg' => 'value'}]
args = ["/usr/bin/pacman", "--noconfirm", "--noprogressbar", "-x", "--arg=value", "-R", resource[:name]]
executor.expects(:execute).with(args, no_extra_options).returns ""
provider.uninstall
end
end

describe "when querying" do
it "should query pacman" do
executor.
expects(:execute).
with(["/usr/bin/pacman", "-Qi", @resource[:name]], no_extra_options)
@provider.query
with(["/usr/bin/pacman", "-Qi", resource[:name]], no_extra_options)
provider.query
end

it "should return the version" do
Expand Down Expand Up @@ -176,20 +195,20 @@
EOF

executor.expects(:execute).returns(query_output)
@provider.query.should == {:ensure => "1.01.3-2"}
provider.query.should == {:ensure => "1.01.3-2"}
end

it "should return a nil if the package isn't found" do
executor.expects(:execute).returns("")
@provider.query.should be_nil
provider.query.should be_nil
end

it "should return a hash indicating that the package is missing on error" do
executor.expects(:execute).raises(Puppet::ExecutionFailure.new("ERROR!"))
@provider.query.should == {
provider.query.should == {
:ensure => :purged,
:status => 'missing',
:name => @resource[:name],
:name => resource[:name],
:error => 'ok',
}
end
Expand All @@ -199,18 +218,18 @@

describe "when fetching a package list" do
it "should retrieve installed packages" do
provider.expects(:execpipe).with(["/usr/bin/pacman", '-Q'])
provider.installedpkgs
described_class.expects(:execpipe).with(["/usr/bin/pacman", '-Q'])
described_class.installedpkgs
end

it "should retrieve installed package groups" do
provider.expects(:execpipe).with(["/usr/bin/pacman", '-Qg'])
provider.installedgroups
described_class.expects(:execpipe).with(["/usr/bin/pacman", '-Qg'])
described_class.installedgroups
end

it "should return installed packages with their versions" do
provider.expects(:execpipe).yields(StringIO.new("package1 1.23-4\npackage2 2.00\n"))
packages = provider.installedpkgs
described_class.expects(:execpipe).yields(StringIO.new("package1 1.23-4\npackage2 2.00\n"))
packages = described_class.installedpkgs

packages.length.should == 2

Expand All @@ -228,8 +247,8 @@
end

it "should return installed groups with a dummy version" do
provider.expects(:execpipe).yields(StringIO.new("group1 pkg1\ngroup1 pkg2"))
groups = provider.installedgroups
described_class.expects(:execpipe).yields(StringIO.new("group1 pkg1\ngroup1 pkg2"))
groups = described_class.installedgroups

groups.length.should == 1

Expand All @@ -241,14 +260,14 @@
end

it "should return nil on error" do
provider.expects(:execpipe).twice.raises(Puppet::ExecutionFailure.new("ERROR!"))
provider.instances.should be_nil
described_class.expects(:execpipe).twice.raises(Puppet::ExecutionFailure.new("ERROR!"))
described_class.instances.should be_nil
end

it "should warn on invalid input" do
provider.expects(:execpipe).yields(StringIO.new("blah"))
provider.expects(:warning).with("Failed to match line blah")
provider.installedpkgs == []
described_class.expects(:execpipe).yields(StringIO.new("blah"))
described_class.expects(:warning).with("Failed to match line blah")
described_class.installedpkgs == []
end
end

Expand All @@ -265,7 +284,7 @@
in_sequence(get_latest_version).
returns("")

@provider.latest
provider.latest
end

it "should get query pacman for the latest version" do
Expand All @@ -277,10 +296,10 @@
executor.
expects(:execute).
in_sequence(get_latest_version).
with(['/usr/bin/pacman', '-Sp', '--print-format', '%v', @resource[:name]], no_extra_options).
with(['/usr/bin/pacman', '-Sp', '--print-format', '%v', resource[:name]], no_extra_options).
returns("")

@provider.latest
provider.latest
end

it "should return the version number from pacman" do
Expand All @@ -289,7 +308,7 @@
at_least_once().
returns("1.00.2-3\n")

@provider.latest.should == "1.00.2-3"
provider.latest.should == "1.00.2-3"
end
end
end

0 comments on commit 2e844f4

Please sign in to comment.