Skip to content

Commit

Permalink
Add splunkforwarder_version fact
Browse files Browse the repository at this point in the history
  • Loading branch information
treydock committed Oct 29, 2018
1 parent 0fe4771 commit 4532f8c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/facter/splunkforwarder_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Facter.add('splunkforwarder_version') do
setcode do
value = nil
if File.exists?('C:\Program Files\SplunkUniveralForwarder\bin\splunk.exe')
cmd = 'C:\Program Files\SplunkUniveralForwarder\bin\splunk.exe --version'
elsif File.exists?('/opt/splunkforwarder/bin/splunk')
cmd = '/opt/splunkforwarder/bin/splunk --version'
else
cmd = nil
end
if cmd
output = Facter::Util::Resolution.exec(cmd)
if output =~ /^Splunk Universal Forwarder ([0-9\.]+) \(/
value = $1
end
end
value
end
end
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# This file is managed via modulesync
# https://github.com/voxpupuli/modulesync
# https://github.com/voxpupuli/modulesync_config
RSpec.configure do |c|
c.mock_with :rspec
end
require 'puppetlabs_spec_helper/module_spec_helper'
require 'rspec-puppet-facts'
include RspecPuppetFacts
Expand Down
24 changes: 24 additions & 0 deletions spec/unit/facter/splunkforwarder_version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'spec_helper'

describe 'splunkforwarder_version Fact' do
before(:each) { Facter.clear }

it 'returns version for Windows' do
allow(File).to receive(:exists?).with('C:\Program Files\SplunkUniveralForwarder\bin\splunk.exe').and_return(true)
allow(Facter::Util::Resolution).to receive(:exec).with('C:\Program Files\SplunkUniveralForwarder\bin\splunk.exe --version').and_return('Splunk Universal Forwarder 7.0.2 (build 03bbabbd5c0f)')
expect(Facter.fact(:splunkforwarder_version).value).to eq('7.0.2')
end

it 'returns version for Linux' do
allow(File).to receive(:exists?).with('C:\Program Files\SplunkUniveralForwarder\bin\splunk.exe').and_return(false)
allow(File).to receive(:exists?).with('/opt/splunkforwarder/bin/splunk').and_return(true)
allow(Facter::Util::Resolution).to receive(:exec).with('/opt/splunkforwarder/bin/splunk --version').and_return('Splunk Universal Forwarder 7.0.2 (build 03bbabbd5c0f)')
expect(Facter.fact(:splunkforwarder_version).value).to eq('7.0.2')
end

it 'returns nil' do
allow(File).to receive(:exists?).with('C:\Program Files\SplunkUniveralForwarder\bin\splunk.exe').and_return(false)
allow(File).to receive(:exists?).with('/opt/splunkforwarder/bin/splunk').and_return(false)
expect(Facter.fact(:splunkforwarder_version).value).to be_nil
end
end

0 comments on commit 4532f8c

Please sign in to comment.