diff --git a/lib/facter/grafana_version.rb b/lib/facter/grafana_version.rb new file mode 100644 index 00000000..5e5c728f --- /dev/null +++ b/lib/facter/grafana_version.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +Facter.add(:grafana_version) do + confine { Facter::Util::Resolution.which('grafana-server') } + + setcode do + version_path = '/usr/share/grafana/VERSION' + File.read(version_path).strip if File.exist?(version_path) + end +end diff --git a/spec/facter/grafana_version_spec.rb b/spec/facter/grafana_version_spec.rb new file mode 100644 index 00000000..3be2a647 --- /dev/null +++ b/spec/facter/grafana_version_spec.rb @@ -0,0 +1,27 @@ +require 'facter' + +describe 'grafana_version' do + before(:each) do + Facter.clear + allow(Facter::Util::Resolution).to receive(:which).with('grafana-server').and_return('/usr/sbin/grafana-server') + end + + context 'when VERSION file exists' do + it 'returns the version from the VERSION file' do + version = '1.2.3' + allow(File).to receive(:exist?).with('/usr/share/grafana/VERSION').and_return(true) + allow(File).to receive(:read).with('/usr/share/grafana/VERSION').and_return(version) + + expect(Facter.fact(:grafana_version).value).to eq(version.strip) + end + end + + context 'when VERSION file does not exist' do + it 'returns nil' do + allow(File).to receive(:exist?).with('/usr/share/grafana/VERSION').and_return(false) + + expect(Facter.fact(:grafana_version).value).to be_nil + end + end + +end