[Pkg-puppet-devel] [facter] 96/180: (FACT-451) Add helper for reading /etc/os-release

Stig Sandbeck Mathisen ssm at debian.org
Mon Jun 30 15:06:35 UTC 2014


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository facter.

commit b5d97851097306760da16373b329f08e05c671a7
Author: Adrien Thebo <git at somethingsinistral.net>
Date:   Tue May 13 11:09:34 2014 -0700

    (FACT-451) Add helper for reading /etc/os-release
---
 lib/facter/util/operatingsystem.rb                 | 21 +++++
 .../unit/util/operatingsystem/cumuluslinux.txt     |  8 ++
 .../unit/util/operatingsystem/redhat-7.txt         | 12 +++
 .../fixtures/unit/util/operatingsystem/sabayon.txt |  7 ++
 spec/fixtures/unit/util/operatingsystem/wheezy.txt |  9 +++
 spec/unit/util/operatingsystem_spec.rb             | 92 ++++++++++++++++++++++
 6 files changed, 149 insertions(+)

diff --git a/lib/facter/util/operatingsystem.rb b/lib/facter/util/operatingsystem.rb
new file mode 100644
index 0000000..13f2ca6
--- /dev/null
+++ b/lib/facter/util/operatingsystem.rb
@@ -0,0 +1,21 @@
+module Facter
+  module Util
+    module Operatingsystem
+
+      # @see http://www.freedesktop.org/software/systemd/man/os-release.html
+      def self.os_release(file = '/etc/os-release')
+        values = {}
+
+        if File.readable?(file)
+          File.readlines(file).each do |line|
+            if (match = line.match(/^(\w+)=["']?(.+?)["']?$/))
+              values[match[1]] = match[2]
+            end
+          end
+        end
+
+        values
+      end
+    end
+  end
+end
diff --git a/spec/fixtures/unit/util/operatingsystem/cumuluslinux.txt b/spec/fixtures/unit/util/operatingsystem/cumuluslinux.txt
new file mode 100644
index 0000000..0e41ebd
--- /dev/null
+++ b/spec/fixtures/unit/util/operatingsystem/cumuluslinux.txt
@@ -0,0 +1,8 @@
+NAME="Cumulus Linux"
+VERSION_ID=1.5.2
+VERSION="1.5.2-28283a7-201311181623-final"
+PRETTY_NAME="Cumulus Linux"
+ID=cumulus-linux
+ID_LIKE=debian
+CPE_NAME=cpe:/o:cumulusnetworks:cumulus_linux:1.5.2-28283a7-201311181623-final
+HOME_URL="http://www.cumulusnetworks.com/"
diff --git a/spec/fixtures/unit/util/operatingsystem/redhat-7.txt b/spec/fixtures/unit/util/operatingsystem/redhat-7.txt
new file mode 100644
index 0000000..41e3f3d
--- /dev/null
+++ b/spec/fixtures/unit/util/operatingsystem/redhat-7.txt
@@ -0,0 +1,12 @@
+NAME="Red Hat Enterprise Linux Everything"
+VERSION="7.0 (Maipo)"
+ID="rhel"
+VERSION_ID="7.0"
+PRETTY_NAME="Red Hat Enterprise Linux Everything 7.0 (Maipo)"
+ANSI_COLOR="0;31"
+CPE_NAME="cpe:/o:redhat:enterprise_linux:7.0:beta:everything"
+
+REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7"
+REDHAT_BUGZILLA_PRODUCT_VERSION=7.0
+REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
+REDHAT_SUPPORT_PRODUCT_VERSION=7.0
diff --git a/spec/fixtures/unit/util/operatingsystem/sabayon.txt b/spec/fixtures/unit/util/operatingsystem/sabayon.txt
new file mode 100644
index 0000000..ed82641
--- /dev/null
+++ b/spec/fixtures/unit/util/operatingsystem/sabayon.txt
@@ -0,0 +1,7 @@
+NAME=Sabayon
+ID=sabayon
+PRETTY_NAME="Sabayon/Linux"
+ANSI_COLOR="1;32"
+HOME_URL="http://www.sabayon.org/"
+SUPPORT_URL="http://forum.sabayon.org/"
+BUG_REPORT_URL="https://bugs.sabayon.org/"
diff --git a/spec/fixtures/unit/util/operatingsystem/wheezy.txt b/spec/fixtures/unit/util/operatingsystem/wheezy.txt
new file mode 100644
index 0000000..c110e3b
--- /dev/null
+++ b/spec/fixtures/unit/util/operatingsystem/wheezy.txt
@@ -0,0 +1,9 @@
+PRETTY_NAME="Debian GNU/Linux 7 (wheezy)"
+NAME="Debian GNU/Linux"
+VERSION_ID="7"
+VERSION="7 (wheezy)"
+ID=debian
+ANSI_COLOR="1;31"
+HOME_URL="http://www.debian.org/"
+SUPPORT_URL="http://www.debian.org/support/"
+BUG_REPORT_URL="http://bugs.debian.org/"
diff --git a/spec/unit/util/operatingsystem_spec.rb b/spec/unit/util/operatingsystem_spec.rb
new file mode 100644
index 0000000..a2b4c51
--- /dev/null
+++ b/spec/unit/util/operatingsystem_spec.rb
@@ -0,0 +1,92 @@
+require 'spec_helper'
+require 'facter/util/operatingsystem'
+
+describe Facter::Util::Operatingsystem do
+  describe "reading the os-release file" do
+
+    it "correctly parses the file on Cumulus Linux"  do
+      values = described_class.os_release(my_fixture('cumuluslinux.txt'))
+
+      expect(values).to eq({
+        'NAME' => "Cumulus Linux",
+        'VERSION_ID' => "1.5.2",
+        'VERSION' => "1.5.2-28283a7-201311181623-final",
+        'PRETTY_NAME' => "Cumulus Linux",
+        'ID' => "cumulus-linux",
+        'ID_LIKE' => "debian",
+        'CPE_NAME' => "cpe:/o:cumulusnetworks:cumulus_linux:1.5.2-28283a7-201311181623-final",
+        'HOME_URL' => "http://www.cumulusnetworks.com/",
+      })
+    end
+
+    it "correctly parses the file on Sabayon" do
+      values = described_class.os_release(my_fixture('sabayon.txt'))
+
+      expect(values).to eq({
+        "NAME" => "Sabayon",
+        "ID" => "sabayon",
+        "PRETTY_NAME" => "Sabayon/Linux",
+        "ANSI_COLOR" => "1;32",
+        "HOME_URL" => "http://www.sabayon.org/",
+        "SUPPORT_URL" => "http://forum.sabayon.org/",
+        "BUG_REPORT_URL" => "https://bugs.sabayon.org/",
+      })
+    end
+
+    it "correctly parses the file on Debian Wheezy" do
+      values = described_class.os_release(my_fixture('wheezy.txt'))
+
+      expect(values).to eq({
+        "PRETTY_NAME" => "Debian GNU/Linux 7 (wheezy)",
+        "NAME" => "Debian GNU/Linux",
+        "VERSION_ID" => "7",
+        "VERSION" => "7 (wheezy)",
+        "ID" => "debian",
+        "ANSI_COLOR" => "1;31",
+        "HOME_URL" => "http://www.debian.org/",
+        "SUPPORT_URL" => "http://www.debian.org/support/",
+        "BUG_REPORT_URL" => "http://bugs.debian.org/",
+      })
+    end
+
+    it "correctly parses the file on Debian Wheezy" do
+      values = described_class.os_release(my_fixture('wheezy.txt'))
+
+      expect(values).to eq({
+        "PRETTY_NAME" => "Debian GNU/Linux 7 (wheezy)",
+        "NAME" => "Debian GNU/Linux",
+        "VERSION_ID" => "7",
+        "VERSION" => "7 (wheezy)",
+        "ID" => "debian",
+        "ANSI_COLOR" => "1;31",
+        "HOME_URL" => "http://www.debian.org/",
+        "SUPPORT_URL" => "http://www.debian.org/support/",
+        "BUG_REPORT_URL" => "http://bugs.debian.org/",
+      })
+    end
+
+
+    it "correctly parses the file on RedHat 7" do
+      values = described_class.os_release(my_fixture('redhat-7.txt'))
+      expect(values).to eq({
+        "NAME" => "Red Hat Enterprise Linux Everything",
+        "VERSION" => "7.0 (Maipo)",
+        "ID" => "rhel",
+        "VERSION_ID" => "7.0",
+        "PRETTY_NAME" => "Red Hat Enterprise Linux Everything 7.0 (Maipo)",
+        "ANSI_COLOR" => "0;31",
+        "CPE_NAME" => "cpe:/o:redhat:enterprise_linux:7.0:beta:everything",
+        "REDHAT_BUGZILLA_PRODUCT" => "Red Hat Enterprise Linux 7",
+        "REDHAT_BUGZILLA_PRODUCT_VERSION" => "7.0",
+        "REDHAT_SUPPORT_PRODUCT" => "Red Hat Enterprise Linux",
+        "REDHAT_SUPPORT_PRODUCT_VERSION" => "7.0",
+      })
+    end
+
+    it "does not try to read an unreadable '/etc/os-release' file" do
+      File.expects(:readable?).with('/some/nonexistent/file').returns false
+
+      expect(described_class.os_release('/some/nonexistent/file')).to be_empty
+    end
+  end
+end

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git



More information about the Pkg-puppet-devel mailing list