Using mocked os-release file

This commit is contained in:
Joona Hoikkala
2016-04-13 00:49:51 +03:00
parent 67c60ab406
commit 6fc63de5a5
2 changed files with 19 additions and 16 deletions
+12 -10
View File
@@ -209,17 +209,18 @@ def safely_remove(path):
raise raise
def get_os_info(): def get_os_info(filepath="/etc/os-release"):
""" """
Get OS name and version Get OS name and version
:param str filepath: File path of os-release file
:returns: (os_name, os_version) :returns: (os_name, os_version)
:rtype: `tuple` of `str` :rtype: `tuple` of `str`
""" """
if os.path.isfile('/etc/os-release'): if os.path.isfile(filepath):
# Systemd os-release parsing might be viable # Systemd os-release parsing might be viable
os_name, os_version = get_systemd_os_info() os_name, os_version = get_systemd_os_info(filepath=filepath)
if os_name: if os_name:
return (os_name, os_version) return (os_name, os_version)
@@ -227,34 +228,35 @@ def get_os_info():
return get_python_os_info() return get_python_os_info()
def get_systemd_os_info(): def get_systemd_os_info(filepath="/etc/os-release"):
""" """
Parse systemd /etc/os-release for distribution information Parse systemd /etc/os-release for distribution information
:param str filepath: File path of os-release file
:returns: (os_name, os_version) :returns: (os_name, os_version)
:rtype: `tuple` of `str` :rtype: `tuple` of `str`
""" """
os_name = _get_systemd_os_release_var("ID") os_name = _get_systemd_os_release_var("ID", filepath=filepath)
os_version = _get_systemd_os_release_var("VERSION_ID") os_version = _get_systemd_os_release_var("VERSION_ID", filepath=filepath)
return (os_name, os_version) return (os_name, os_version)
def _get_systemd_os_release_var(varname): def _get_systemd_os_release_var(varname, filepath="/etc/os-release"):
""" """
Get single value from systemd /etc/os-release Get single value from systemd /etc/os-release
:param str varname: Name of variable to fetch :param str varname: Name of variable to fetch
:param str filepath: File path of os-release file
:returns: requested value :returns: requested value
:rtype: `str` :rtype: `str`
""" """
OS_RELEASE_FILEPATH = "/etc/os-release"
var_string = varname+"=" var_string = varname+"="
if not os.path.isfile(OS_RELEASE_FILEPATH): if not os.path.isfile(filepath):
return "" return ""
with open(OS_RELEASE_FILEPATH, 'r') as fh: with open(filepath, 'r') as fh:
contents = fh.readlines() contents = fh.readlines()
for line in contents: for line in contents:
+7 -6
View File
@@ -4,6 +4,7 @@ import errno
import os import os
import shutil import shutil
import stat import stat
import sys
import tempfile import tempfile
import unittest import unittest
@@ -11,6 +12,7 @@ import mock
import six import six
from letsencrypt import errors from letsencrypt import errors
from letsencrypt.tests import test_util
class RunScriptTest(unittest.TestCase): class RunScriptTest(unittest.TestCase):
@@ -347,12 +349,11 @@ class OsInfoTest(unittest.TestCase):
def test_systemd_os_release(self): def test_systemd_os_release(self):
from letsencrypt.le_util import get_os_info from letsencrypt.le_util import get_os_info
os_release = 'VERSION_ID=42\nID=systemdos\n' with mock.patch('os.path.isfile', return_value=True):
with mock.patch('__builtin__.open', self.assertEqual(get_os_info(
mock.mock_open(read_data=os_release)): test_util.vector_path("os-release"))[0], 'systemdos')
with mock.patch('os.path.isfile', return_value=True): self.assertEqual(get_os_info(
self.assertEqual(get_os_info()[0], 'systemdos') test_util.vector_path("os-release"))[1], '42')
self.assertEqual(get_os_info()[1], '42')
@mock.patch("letsencrypt.le_util.subprocess.Popen") @mock.patch("letsencrypt.le_util.subprocess.Popen")
def test_non_systemd_os_info(self, popen_mock): def test_non_systemd_os_info(self, popen_mock):