Skip to content

Commit

Permalink
unbound_version: add spec tests to the unbound_version fact
Browse files Browse the repository at this point in the history
  • Loading branch information
b4ldr committed Oct 8, 2019
1 parent 146a5de commit a0c4844
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/facter/unbound_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
'/usr/sbin/unbound'
end
if File.exist? unbound_bin
unbound_version = Facter::Util::Resolution.exec(
"#{unbound_bin} -V 2>&1"
).match(%r{Version\s+(\d+(?:\.\d+)*)})[1]
begin
unbound_version = Facter::Util::Resolution.exec(
"#{unbound_bin} -V 2>&1"
).match(%r{Version\s+(\d+(?:\.\d+){2})\s+})[1]
rescue
unbound_version = 'UNKNOWN'
end
Facter.add(:unbound_version) do
setcode do
unbound_version
Expand Down
43 changes: 43 additions & 0 deletions spec/unit/unbound/facter/unbound_version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'spec_helper'

version_string = <<-VERSION
unbound: invalid option -- 'V'
usage: unbound [options]
start unbound daemon DNS resolver.
-h this help
-c file config file to read instead of /etc/unbound/unbound.conf
file format is described in unbound.conf(5).
-d do not fork into the background.
-v verbose (more times to increase verbosity)
Version %s
linked libs: libevent 2.0.21-stable (it uses epoll), OpenSSL 1.1.0l 10 Sep 2019
linked modules: dns64 python validator iterator
BSD licensed, see LICENSE in source package for details.
Report bugs to unbound-bugs@nlnetlabs.nl
VERSION

tests = {
'valid' => ['1.1.1', '1.90.24', '1.0.404', '2.5.6'],
'invalid' => ['1', '1.1', '1.1.1.1', '1,1,1', '2:5.1', 'foobar']
}
describe Facter::Util::Fact.to_s do
before { Facter.clear }
tests.each_pair do |test, versions|
describe "test #{test} versions" do
versions.each do |version|
context "test version #{version}" do
before do
Facter::Util::Resolution.stubs(:exec).with('/usr/sbin/unbound -V 2>&1').returns(
version_string % version
)
end
if test == 'valid'
it { expect(Facter.fact(:unbound_version).value).to eq(version) }
else
it { expect(Facter.fact(:unbound_version).value).to eq('UNKNOWN') }
end
end
end
end
end
end

0 comments on commit a0c4844

Please sign in to comment.