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 a57955f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
19 changes: 6 additions & 13 deletions lib/facter/unbound_version.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
# frozen_string_literal: true

unbound_bin = case Facter.value('kernel')
when 'FreeBSD'
'/usr/local/sbin/unbound'
else
'/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]
Facter.add(:unbound_version) do
setcode do
unbound_version
Facter.add(:unbound_version) do
confine { Facter.value(:kernel) != 'windows' }
setcode do
if Facter::Util::Resolution.which('unbound')
unbound_version = Facter::Util::Resolution.exec('unbound -V 2>&1')
%r{Version\s+(\d+(?:\.\d+){2})\s+}.match(unbound_version)[1]
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
end

RSpec.configure do |c|
c.mock_with :rspec
# Coverage generation
c.after(:suite) do
RSpec::Puppet::Coverage.report!
Expand Down
52 changes: 52 additions & 0 deletions spec/unit/unbound/facter/unbound_version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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 }
context 'unbound not in path' do
before do
allow(Facter::Util::Resolution).to receive(:which).with('unbound') { false }
end
it { expect(Facter.fact(:unbound_version).value).to eq(nil) }
end
tests.each_pair do |test, versions|
describe "test #{test} versions" do
before do
allow(Facter::Util::Resolution).to receive(:which).with('unbound') { true }
end
versions.each do |version|
context "test version #{version}" do
before do
allow(Facter::Util::Resolution).to receive(:exec).with('unbound -V 2>&1') do
version_string % version
end
end
if test == 'valid'
it { expect(Facter.fact(:unbound_version).value).to eq(version) }
else
it { expect(Facter.fact(:unbound_version).value).to eq(nil) }
end
end
end
end
end
end

0 comments on commit a57955f

Please sign in to comment.