Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion lib/puppet/provider/package/pacman.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'puppet/provider/package'
require 'uri'

Puppet::Type.type(:package).provide :pacman, :parent => Puppet::Provider::Package do
desc "Support for the Package Manager Utility (pacman) used in Archlinux."
Expand All @@ -12,13 +13,45 @@
# Installs quietly, without confirmation or progressbar, updates package
# list from servers defined in pacman.conf.
def install
pacman "--noconfirm", "--noprogressbar", "-Sy", @resource[:name]
if @resource[:source]
install_from_file
else
install_from_repo
end

unless self.query
raise Puppet::ExecutionFailure.new("Could not find package %s" % self.name)
end
end

def install_from_repo
pacman "--noconfirm", "--noprogressbar", "-Sy", @resource[:name]
end
private :install_from_repo

def install_from_file
source = @resource[:source]
begin
source_uri = URI.parse source
rescue => detail
fail "Invalid source '#{source}': #{detail}"
end

source = case source_uri.scheme
when nil then source
when /https?/i then source
when /ftp/i then source
when /file/i then source_uri.path
when /puppet/i
fail "puppet:// URL is not supported by pacman"
else
fail "Source #{source} is not supported by pacman"
end
pacman "--noconfirm", "--noprogressbar", "-Sy"
pacman "--noconfirm", "--noprogressbar", "-U", source
end
private :install_from_file

def self.listcmd
[command(:pacman), " -Q"]
end
Expand Down
102 changes: 85 additions & 17 deletions spec/unit/provider/package/pacman_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
describe provider do
before do
provider.stubs(:command).with(:pacman).returns('/usr/bin/pacman')
@resource = stub 'resource'
@resource.stubs(:[]).returns("package")
@resource.stubs(:name).returns("name")
@resource = Puppet::Type.type(:package).new(:name => 'package')
@provider = provider.new(@resource)
end

Expand Down Expand Up @@ -46,7 +44,7 @@
provider.
expects(:execute).
with { |args|
args[3,4] == ["-Sy", @resource[0]]
args[3,4] == ["-Sy", @resource[:name]]
}.
returns("")

Expand All @@ -59,6 +57,78 @@

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

context "when :source is specified" do
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
install = sequence('install')

provider.expects(:execute).
with(all_of(includes("-Sy"), includes("--noprogressbar"))).
in_sequence(install).
returns("")

provider.expects(:execute).
with(all_of(includes("-U"), includes(source))).
in_sequence(install).
returns("")

@provider.install
end
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this would be simpler using sequences as in http://mocha.rubyforge.org/classes/Mocha/Expectation.html#M000053

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well personally I'd keep the state explicitly specified as such. If it really is a merge-stopper I'll do it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd really prefer the sequence, since we're not actually testing a state machine. We're just testing whether one method is called after another one in sequence. If you'd prefer, we can make this change when getting things merged in. We need to change some things around anyway, since ec09f60 really shouldn't be it's own commit, and should be squashed into the commits that introduce the typos.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end
end

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

it "should install from the path segment of the URL" do
install = sequence("instrall")

provider.expects(:execute).
with(all_of(includes("-Sy"), includes("--noprogressbar"),
includes("--noconfirm"))).
in_sequence(install).
returns("")

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

@provider.install
end
end

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

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

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

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

describe "when updating" do
Expand Down Expand Up @@ -95,7 +165,7 @@
provider.
expects(:execute).
with { |args|
args[3,4] == ["-R", @resource[0]]
args[3,4] == ["-R", @resource[:name]]
}.
returns("")

Expand All @@ -107,7 +177,7 @@
it "should query pacman" do
provider.
expects(:execute).
with(["/usr/bin/pacman", "-Qi", @resource[0]])
with(["/usr/bin/pacman", "-Qi", @resource[:name]])
@provider.query
end

Expand Down Expand Up @@ -149,7 +219,7 @@
@provider.query.should == {
:ensure => :purged,
:status => 'missing',
:name => @resource[0],
:name => @resource[:name],
:error => 'ok',
}
end
Expand Down Expand Up @@ -194,32 +264,30 @@

describe "when determining the latest version" do
it "should refresh package list" do
refreshed = states('refreshed').starts_as('unrefreshed')
get_latest_version = sequence("get_latest_version")
provider.
expects(:execute).
when(refreshed.is('unrefreshed')).
with(['/usr/bin/pacman', '-Sy']).
then(refreshed.is('refreshed'))
in_sequence(get_latest_version).
with(['/usr/bin/pacman', '-Sy'])

provider.
stubs(:execute).
when(refreshed.is('refreshed')).
in_sequence(get_latest_version).
returns("")

@provider.latest
end

it "should get query pacman for the latest version" do
refreshed = states('refreshed').starts_as('unrefreshed')
get_latest_version = sequence("get_latest_version")
provider.
stubs(:execute).
when(refreshed.is('unrefreshed')).
then(refreshed.is('refreshed'))
in_sequence(get_latest_version)

provider.
expects(:execute).
when(refreshed.is('refreshed')).
with(['/usr/bin/pacman', '-Sp', '--print-format', '%v', @resource[0]]).
in_sequence(get_latest_version).
with(['/usr/bin/pacman', '-Sp', '--print-format', '%v', @resource[:name]]).
returns("")

@provider.latest
Expand Down