Skip to content

Commit

Permalink
unit tests work
Browse files Browse the repository at this point in the history
  • Loading branch information
buzzdeee committed Apr 14, 2024
1 parent fd5f36b commit ad23bf2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 67 deletions.
58 changes: 33 additions & 25 deletions lib/puppet/provider/package/openbsd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,46 @@
has_feature :supports_flavors

def self.instances
final = []
packages = []

begin
packages = listcmd
packages.each { |package, value|
unless package.empty?()
value[:provider] = name
final << new(value)
end
}
final
execpipe(listcmd) do |process|
# our regex for matching pkg_info output
regex = /^(.*)--([\w-]+)?(%[^w]+)?$/
fields = [:name, :flavor, :branch]
hash = {}

# now turn each returned line into a package object
process.each_line { |line|
match = regex.match(line.split("\n")[0])
if match
fields.zip(match.captures) { |field, value|
hash[field] = value
}

hash[:name] = "#{hash[:name]}#{hash[:branch]}" if hash[:branch]

hash[:provider] = name
packages << new(hash)
hash = {}
else
unless line =~ /Updating the pkgdb/
# Print a warning on lines we can't match, but move
# on, since it should be non-fatal
warning(_("Failed to match line %{line}") % { line: line })
end
end
}
end

packages
rescue Puppet::ExecutionFailure
nil
end
end

def self.listcmd
regex_fuzzy = /^(.*)--([\w-]+)?(%[^w]+)?$/
f = pkginfo("-a", "-z").split("\n")
packages = {}
f.each do |line|
match = regex_fuzzy.match(line.split[0])
name = match.captures[0]
flavor = match.captures[1]
branch = match.captures[2]
if branch.nil?
pname = name
else
pname = name + branch
end
packages[pname] = { :name => pname, :flavor => flavor, :branch => branch, :ensure => "present" }
end
packages
[command(:pkginfo), "-a", "-z"]
end

def install
Expand Down
19 changes: 0 additions & 19 deletions spec/fixtures/unit/provider/package/openbsd/pkginfo.detail

This file was deleted.

1 change: 0 additions & 1 deletion spec/fixtures/unit/provider/package/openbsd/pkginfo.query

This file was deleted.

44 changes: 22 additions & 22 deletions spec/unit/provider/package/openbsd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,43 @@
before :each do
# Stub some provider methods to avoid needing the actual software
# installed, so we can test on whatever platform we want.
#allow(Puppet::Util).to receive(:which).with("pkg_info").and_return("/usr/sbin/pkg_info")
allow(described_class).to receive(:yalla).with("pkg_info").and_return("/usr/sbin/pkg_info")
allow(described_class).to receive(:command).with(:pkginfo).and_return('/usr/sbin/pkg_info')
allow(described_class).to receive(:command).with(:pkgadd).and_return('/usr/sbin/pkg_add')
allow(described_class).to receive(:command).with(:pkgdelete).and_return('/usr/sbin/pkg_delete')
allow(described_class).to receive(:command).with(:pkginfo).and_return('/bin/pkg_info')
allow(described_class).to receive(:command).with(:pkgadd).and_return('/bin/pkg_add')
allow(described_class).to receive(:command).with(:pkgdelete).and_return('/bin/pkg_delete')
end

context "#instances" do
it "should return nil if execution failed" do
allow(provider).to receive(:command).with(:pkginfo).and_return('/usr/sbin/pkg_info')
expect(provider).to receive(:pkginfo).and_raise(Puppet::ExecutionFailure, 'wawawa')
#expect(provider).to receive(:pkginfo).and_raise(Puppet::ExecutionFailure, 'wawawa')
#expect(provider).to receive(:pkginfo).with(['-a', '-z'])
expect(described_class.instances).to be_nil
end

it "should return the empty set if no packages are listed" do
expect(described_class).to receive(:execpipe).with(%w{/usr/sbin/pkg_info -a}).and_yield(StringIO.new(''))
expect(described_class).to receive(:execpipe).with(%w{/bin/pkg_info -a -z}).and_yield(StringIO.new(''))
expect(described_class.instances).to be_empty
end

it "should return all packages when invoked" do
fixture = File.read(my_fixture('pkginfo.list'))
expect(described_class).to receive(:execpipe).with(%w{/usr/sbin/pkg_info -a -z}).and_yield(fixture)
expect(described_class).to receive(:execpipe).with(%w{/bin/pkg_info -a -z}).and_yield(fixture)
expect(described_class.instances.map(&:name).sort).to eq(
%w{autoconf%2.13 autoconf%2.56 bash postfix%stable puppet%8}.sort
%w{autoconf%2.13 autoconf%2.56 bash postfix%stable puppet%8 zstd}.sort
)
end

it "should return all flavors if set" do
fixture = File.read(my_fixture('pkginfo.list'))
expect(provider).to receive(:pkgadd).with(['-a', '-z']).and_yield(fixture)
expect(described_class).to receive(:execpipe).with(%w{/bin/pkg_info -a -z}).and_yield(fixture)
instances = described_class.instances.map {|p| {:name => p.get(:name),
:ensure => p.get(:ensure), :flavor => p.get(:flavor)}}
expect(instances.size).to eq(5)
expect(instances[0]).to eq({:name => 'autoconf%2.13', :ensure => 'present', :flavor => nil})
expect(instances[1]).to eq({:name => 'autoconf%2.56', :ensure => 'present', :flavor => nil})
expect(instances[2]).to eq({:name => 'bash', :ensure => 'present', :flavor => nil})
expect(instances[3]).to eq({:name => 'postfix%stable', :ensure => 'present', :flavor => 'ldap'})
expect(instances[4]).to eq({:name => 'puppet%8', :ensure => 'present', :flavor => nil})
:flavor => p.get(:flavor), :branch => p.get(:branch)}}
expect(instances.size).to eq(6)
expect(instances[0]).to eq({:name => 'autoconf%2.13', :flavor => :absent, :branch => '%2.13'})
expect(instances[1]).to eq({:name => 'autoconf%2.56', :flavor => :absent, :branch => '%2.56'})
expect(instances[2]).to eq({:name => 'bash', :flavor => :absent, :branch => :absent})
expect(instances[3]).to eq({:name => 'postfix%stable', :flavor => 'ldap', :branch => '%stable'})
expect(instances[4]).to eq({:name => 'puppet%8', :flavor => :absent, :branch => '%8'})
expect(instances[5]).to eq({:name => 'zstd', :flavor => :absent, :branch => :absent})
end
end

Expand Down Expand Up @@ -97,14 +95,16 @@
end

context "#query" do
it "should return the installed version if present" do
#fixture = File.read(my_fixture('pkginfo.detail'))
#expect(provider).to receive(:pkginfo).with('bash').and_return(fixture)
expect(provider.query).to eq({:branch=>nil, :ensure=>"present", :flavor=>nil, :name=>"bash", :provider=>:openbsd})
it "should return package info if present" do
fixture = File.read(my_fixture('pkginfo.list'))
expect(described_class).to receive(:execpipe).with(%w{/bin/pkg_info -a -z}).and_yield(fixture)
expect(provider.query).to eq({:branch=>nil, :flavor=>nil, :name=>"bash", :provider=>:openbsd})
end

it "should return nothing if not present" do
fixture = File.read(my_fixture('pkginfo.list'))
provider.resource[:name] = 'zsh'
expect(described_class).to receive(:execpipe).with(%w{/bin/pkg_info -a -z}).and_yield(fixture)
expect(provider.query).to be_nil
end
end
Expand Down

0 comments on commit ad23bf2

Please sign in to comment.