Skip to content

Commit ad23bf2

Browse files
committed
unit tests work
1 parent fd5f36b commit ad23bf2

File tree

4 files changed

+55
-67
lines changed

4 files changed

+55
-67
lines changed

lib/puppet/provider/package/openbsd.rb

+33-25
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,46 @@
2727
has_feature :supports_flavors
2828

2929
def self.instances
30-
final = []
30+
packages = []
31+
3132
begin
32-
packages = listcmd
33-
packages.each { |package, value|
34-
unless package.empty?()
35-
value[:provider] = name
36-
final << new(value)
37-
end
38-
}
39-
final
33+
execpipe(listcmd) do |process|
34+
# our regex for matching pkg_info output
35+
regex = /^(.*)--([\w-]+)?(%[^w]+)?$/
36+
fields = [:name, :flavor, :branch]
37+
hash = {}
38+
39+
# now turn each returned line into a package object
40+
process.each_line { |line|
41+
match = regex.match(line.split("\n")[0])
42+
if match
43+
fields.zip(match.captures) { |field, value|
44+
hash[field] = value
45+
}
46+
47+
hash[:name] = "#{hash[:name]}#{hash[:branch]}" if hash[:branch]
48+
49+
hash[:provider] = name
50+
packages << new(hash)
51+
hash = {}
52+
else
53+
unless line =~ /Updating the pkgdb/
54+
# Print a warning on lines we can't match, but move
55+
# on, since it should be non-fatal
56+
warning(_("Failed to match line %{line}") % { line: line })
57+
end
58+
end
59+
}
60+
end
61+
62+
packages
4063
rescue Puppet::ExecutionFailure
4164
nil
4265
end
4366
end
4467

4568
def self.listcmd
46-
regex_fuzzy = /^(.*)--([\w-]+)?(%[^w]+)?$/
47-
f = pkginfo("-a", "-z").split("\n")
48-
packages = {}
49-
f.each do |line|
50-
match = regex_fuzzy.match(line.split[0])
51-
name = match.captures[0]
52-
flavor = match.captures[1]
53-
branch = match.captures[2]
54-
if branch.nil?
55-
pname = name
56-
else
57-
pname = name + branch
58-
end
59-
packages[pname] = { :name => pname, :flavor => flavor, :branch => branch, :ensure => "present" }
60-
end
61-
packages
69+
[command(:pkginfo), "-a", "-z"]
6270
end
6371

6472
def install

spec/fixtures/unit/provider/package/openbsd/pkginfo.detail

-19
This file was deleted.

spec/fixtures/unit/provider/package/openbsd/pkginfo.query

-1
This file was deleted.

spec/unit/provider/package/openbsd_spec.rb

+22-22
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,43 @@
1717
before :each do
1818
# Stub some provider methods to avoid needing the actual software
1919
# installed, so we can test on whatever platform we want.
20-
#allow(Puppet::Util).to receive(:which).with("pkg_info").and_return("/usr/sbin/pkg_info")
21-
allow(described_class).to receive(:yalla).with("pkg_info").and_return("/usr/sbin/pkg_info")
22-
allow(described_class).to receive(:command).with(:pkginfo).and_return('/usr/sbin/pkg_info')
23-
allow(described_class).to receive(:command).with(:pkgadd).and_return('/usr/sbin/pkg_add')
24-
allow(described_class).to receive(:command).with(:pkgdelete).and_return('/usr/sbin/pkg_delete')
20+
allow(described_class).to receive(:command).with(:pkginfo).and_return('/bin/pkg_info')
21+
allow(described_class).to receive(:command).with(:pkgadd).and_return('/bin/pkg_add')
22+
allow(described_class).to receive(:command).with(:pkgdelete).and_return('/bin/pkg_delete')
2523
end
2624

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

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

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

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

@@ -97,14 +95,16 @@
9795
end
9896

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

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

0 commit comments

Comments
 (0)