From 4532f8cfe9bd8938502dc36b3cbf76cb7944f7e8 Mon Sep 17 00:00:00 2001 From: Trey Dockendorf Date: Mon, 29 Oct 2018 11:16:44 -0400 Subject: [PATCH] Add splunkforwarder_version fact --- lib/facter/splunkforwarder_version.rb | 19 +++++++++++++++ spec/spec_helper.rb | 3 +++ .../facter/splunkforwarder_version_spec.rb | 24 +++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100755 lib/facter/splunkforwarder_version.rb create mode 100644 spec/unit/facter/splunkforwarder_version_spec.rb diff --git a/lib/facter/splunkforwarder_version.rb b/lib/facter/splunkforwarder_version.rb new file mode 100755 index 00000000..15e61eef --- /dev/null +++ b/lib/facter/splunkforwarder_version.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 490c67a3..04258d28 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/spec/unit/facter/splunkforwarder_version_spec.rb b/spec/unit/facter/splunkforwarder_version_spec.rb new file mode 100644 index 00000000..1b943c47 --- /dev/null +++ b/spec/unit/facter/splunkforwarder_version_spec.rb @@ -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