Skip to content

Commit

Permalink
Add fact to get grafana version
Browse files Browse the repository at this point in the history
Signed-off-by: Robert Waffen <[email protected]>
  • Loading branch information
rwaffen committed Jun 20, 2024
1 parent 03789fc commit 925d712
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/facter/grafana_version.rb
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions spec/facter/grafana_version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'facter'

Check failure on line 1 in spec/facter/grafana_version_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/FrozenStringLiteralComment: Missing frozen string literal comment.

describe 'grafana_version' do
before(:each) do

Check failure on line 4 in spec/facter/grafana_version_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

RSpec/HookArgument: Omit the default `:each` argument for RSpec hooks. (https://rspec.rubystyle.guide/#redundant-beforeeach, https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/HookArgument)
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

Check failure on line 26 in spec/facter/grafana_version_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Layout/EmptyLinesAroundBlockBody: Extra empty line detected at block body end. (https://rubystyle.guide#empty-lines-around-bodies)
end

0 comments on commit 925d712

Please sign in to comment.