1
0
mirror of https://github.com/certbot/certbot.git synced 2026-01-26 07:41:33 +03:00

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

View File

@@ -209,17 +209,18 @@ def safely_remove(path):
raise
def get_os_info():
def get_os_info(filepath="/etc/os-release"):
"""
Get OS name and version
:param str filepath: File path of os-release file
:returns: (os_name, os_version)
:rtype: `tuple` of `str`
"""
if os.path.isfile('/etc/os-release'):
if os.path.isfile(filepath):
# 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:
return (os_name, os_version)
@@ -227,34 +228,35 @@ def get_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
:param str filepath: File path of os-release file
:returns: (os_name, os_version)
:rtype: `tuple` of `str`
"""
os_name = _get_systemd_os_release_var("ID")
os_version = _get_systemd_os_release_var("VERSION_ID")
os_name = _get_systemd_os_release_var("ID", filepath=filepath)
os_version = _get_systemd_os_release_var("VERSION_ID", filepath=filepath)
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
:param str varname: Name of variable to fetch
:param str filepath: File path of os-release file
:returns: requested value
:rtype: `str`
"""
OS_RELEASE_FILEPATH = "/etc/os-release"
var_string = varname+"="
if not os.path.isfile(OS_RELEASE_FILEPATH):
if not os.path.isfile(filepath):
return ""
with open(OS_RELEASE_FILEPATH, 'r') as fh:
with open(filepath, 'r') as fh:
contents = fh.readlines()
for line in contents:

View File

@@ -4,6 +4,7 @@ import errno
import os
import shutil
import stat
import sys
import tempfile
import unittest
@@ -11,6 +12,7 @@ import mock
import six
from letsencrypt import errors
from letsencrypt.tests import test_util
class RunScriptTest(unittest.TestCase):
@@ -347,12 +349,11 @@ class OsInfoTest(unittest.TestCase):
def test_systemd_os_release(self):
from letsencrypt.le_util import get_os_info
os_release = 'VERSION_ID=42\nID=systemdos\n'
with mock.patch('__builtin__.open',
mock.mock_open(read_data=os_release)):
with mock.patch('os.path.isfile', return_value=True):
self.assertEqual(get_os_info()[0], 'systemdos')
self.assertEqual(get_os_info()[1], '42')
with mock.patch('os.path.isfile', return_value=True):
self.assertEqual(get_os_info(
test_util.vector_path("os-release"))[0], 'systemdos')
self.assertEqual(get_os_info(
test_util.vector_path("os-release"))[1], '42')
@mock.patch("letsencrypt.le_util.subprocess.Popen")
def test_non_systemd_os_info(self, popen_mock):