From 44343c671c08efc7813b164d7f92c722148b1cdf Mon Sep 17 00:00:00 2001 From: Tim Meusel Date: Sun, 10 Jul 2022 13:34:58 +0200 Subject: [PATCH] Add version fact --- README.md | 16 +++++++++++++++ lib/facter/borgbackup.rb | 11 +++++++++++ spec/unit/facter/borgbackup_spec.rb | 30 +++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 lib/facter/borgbackup.rb create mode 100644 spec/unit/facter/borgbackup_spec.rb diff --git a/README.md b/README.md index c150eeb..d772f45 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ * [Examples](#examples) * [Restore Script](#restore-script) * [Prometheus Exporter](#prometheus-exporter) +* [Facts](#facts) * [Limitations](#limitations) * [Tests](#tests) * [Contributions](#contributions) @@ -102,6 +103,21 @@ More and more people use prometheus. We vendor a bash script that can provide you metrics about your backups in the prometheus format. They are written to disk and the node\_exporter can collect them. +## Facts + +This module provides you a structed fact, named `borgbackup`. It currently only +lists the borgbackup version. It's a structured so it can easily be extended +without breaking changes and without further topscope pollution. + +``` +# puppet facts show borgbackup +{ + "borgbackup": { + "version": "1.2.1" + } +} +``` + ## Limitations On CentOS 8, the PowerTools repository needs to be enabled by the user. diff --git a/lib/facter/borgbackup.rb b/lib/facter/borgbackup.rb new file mode 100644 index 0000000..3c209f1 --- /dev/null +++ b/lib/facter/borgbackup.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +Facter.add(:borgbackup) do + confine do + Facter::Util::Resolution.which('borg') + end + setcode do + version = Facter::Util::Resolution.exec('borg --version').split.last + { 'version' => version } + end +end diff --git a/spec/unit/facter/borgbackup_spec.rb b/spec/unit/facter/borgbackup_spec.rb new file mode 100644 index 0000000..33bb9e9 --- /dev/null +++ b/spec/unit/facter/borgbackup_spec.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require 'spec_helper' +command_output = 'borg 1.1.16' +fact_output = { 'version' => '1.1.16' } +describe Facter::Util::Fact.to_s do + before { Facter.clear } + + context 'borg not in path' do + before do + allow(Facter::Util::Resolution).to receive(:which).with('borg').and_return(nil) + end + + it { expect(Facter.fact(:borgbackup).value).to eq(nil) } + end + + context 'valid run' do + before do + allow(Facter::Util::Resolution).to receive(:which).with('borg').and_return('/usr/bin/borg') + end + + context 'borgbackup version' do + before do + allow(Facter::Util::Resolution).to receive(:exec).with('borg --version') { command_output } + end + + it { expect(Facter.fact(:borgbackup).value).to eq fact_output } + end + end +end