Skip to content
Snippets Groups Projects
Commit 4aad1ee5 authored by Andreas Kempe's avatar Andreas Kempe
Browse files

Add simple xbps package provider

parents
No related branches found
No related tags found
No related merge requests found
LICENSE 0 → 100644
Copyright 2020, Andreas Kempe
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require 'puppet/provider/package'
Puppet::Type.type(:package).provide :xbps, :parent => Puppet::Provider::Package do
desc "A xbps provider for Void Linux."
confine :operatingsystem => [:void]
commands :xbps_install => "/usr/bin/xbps-install"
commands :xbps_query => "/usr/bin/xbps-query"
commands :xbps_remove => "/usr/bin/xbps-remove"
has_feature :installable
has_feature :uninstallable
def self.parse_xbps_query(line)
name, _, version = line.split(" ")[1].rpartition("-")
{
:ensure => version,
:name => name,
:provider => self.name,
:version => version,
}
end
def self.instances
packages = []
output = xbps_query(["-l"])
output.lines.each do |line|
package = parse_xbps_query(line)
packages << new(package)
end
packages
end
def self.prefetch(resources)
packages = instances
resources.each_key do |name|
provider = packages.find{|p| p.name == name }
if provider
resources[name].provider = provider
end
end
end
def install
xbps_install(['-y', resource[:name]])
end
def uninstall
xbps_remove(['-y', resource[:name]])
end
def query
begin
output = xbps_query(["-s", resource[:name] + ">=0"])
rescue Puppet::ExecutionFailure
return nil
end
if output.lines.length == 0
return nil
end
self.class.parse_xbps_query(output.lines[0])
end
end
require 'spec_helper'
require 'puppet/provider/package/xbps'
describe Puppet::Type.type(:package).provider(:xbps) do
let(:executor) { Puppet::Util::Execution }
let(:resource) { Puppet::Type.type(:package).new(:name => 'package', :provider => 'xbps') }
let(:resource_new) { Puppet::Type.type(:package).new(:name => 'package-new') }
let(:provider) { described_class.new(resource) }
describe 'when installing' do
before do
allow(provider).to receive(:query).and_return({
:ensure => '1.0_1'
})
end
it 'should call xbps-install to install the package' do
expect(executor).to receive(:execute).
ordered.
with(['/usr/bin/xbps-install', ['-y', 'package']], anything).
and_return('')
provider.install
end
it 'should call xbps-query to list all resources during prefetch' do
expect(executor).to receive(:execute).
ordered.
with(['/usr/bin/xbps-query', ['-l']], anything).
and_return('[ii] package-new-1.0_1 Some package\n')
expect(described_class.prefetch({'package-new' => resource_new})['package-new'].provider.properties).to eq({
:ensure => '1.0_1',
:name => 'package-new',
:provider => :xbps,
:version => '1.0_1',
})
end
end
describe 'when uninstalling' do
before do
allow(provider).to receive(:query).and_return({
:ensure => '1.0_1'
})
end
it 'should call xbps-remove to uninstall the package' do
expect(executor).to receive(:execute).
ordered.
with(['/usr/bin/xbps-remove', ['-y', 'package']], anything).
and_return('')
provider.uninstall
end
end
describe 'when querying' do
it 'should call xbps-query to query a single package' do
expect(executor).to receive(:execute).
ordered.
with(['/usr/bin/xbps-query', ['-s', resource[:name] + '>=0']], anything).
and_return('[ii] package-2.3_5 Some package\n')
expect(provider.query).to eq({
:ensure => '2.3_5',
:name => 'package',
:provider => :xbps,
:version => '2.3_5',
})
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment