From c82a551e77faa228da0a9e445cd0eb7f0417537d Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Tue, 12 Apr 2016 00:03:48 +0300 Subject: [PATCH 01/18] os-release parsing WIP --- letsencrypt/le_util.py | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/letsencrypt/le_util.py b/letsencrypt/le_util.py index cb1c61074..7ee5e33db 100644 --- a/letsencrypt/le_util.py +++ b/letsencrypt/le_util.py @@ -210,8 +210,56 @@ def safely_remove(path): def get_os_info(): + """ + Get OS name and version + + :returns: (os_name, os_version) + :rtype: `tuple` of `str` + """ + + +def get_systemd_os_info(): + """ + Parse systemd /etc/os-release for distribution information + + :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") + + return (os_name, os_version) + + +def _get_systemd_os_release_var(varname): + + OS_RELEASE_FILEPATH = "/etc/os-release" + var_string = varname+"=" + if not os.path.isfile(OS_RELEASE_FILEPATH): + return "" + with open(OS_RELEASE_FILEPATH, 'r') as fh: + contents = fh.readlines() + + for line in contents: + if line.strip().startswith(var_string): + # Return the value of var, normalized + return _normalize_string(line.strip()[len(var_string):]) + return "" + + +def _normalize_string(orig): + """ + Helper function for _get_systemd_os_release_var() to remove quotes + and whitespaces + """ + return orig.replace('"', '').replace("'", "").strip() + + +def get_python_os_info(): """ Get Operating System type/distribution and major version + using python platform module :returns: (os_name, os_version) :rtype: `tuple` of `str` From bbb300eb229bcca0938b50fef9421f02b3b6d88b Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Tue, 12 Apr 2016 14:27:00 +0300 Subject: [PATCH 02/18] Finalized parsing and fixed test case --- letsencrypt/le_util.py | 16 ++++++++++++++++ letsencrypt/tests/cli_test.py | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/letsencrypt/le_util.py b/letsencrypt/le_util.py index 7ee5e33db..60cdd0314 100644 --- a/letsencrypt/le_util.py +++ b/letsencrypt/le_util.py @@ -217,6 +217,15 @@ def get_os_info(): :rtype: `tuple` of `str` """ + if os.path.isfile('/etc/os-release'): + # Systemd os-release parsing might be viable + os_name, os_version = get_systemd_os_info() + if os_name: + return (os_name, os_version) + + # Fallback to platform module + return get_python_os_info() + def get_systemd_os_info(): """ @@ -233,6 +242,13 @@ def get_systemd_os_info(): def _get_systemd_os_release_var(varname): + """ + Get single value from systemd /etc/os-release + + :param str varname: Name of variable to fetch + :returns: requested value + :rtype: `str` + """ OS_RELEASE_FILEPATH = "/etc/os-release" var_string = varname+"=" diff --git a/letsencrypt/tests/cli_test.py b/letsencrypt/tests/cli_test.py index eb3f48308..03ad45514 100644 --- a/letsencrypt/tests/cli_test.py +++ b/letsencrypt/tests/cli_test.py @@ -169,7 +169,7 @@ class CLITest(unittest.TestCase): # pylint: disable=too-many-public-methods import platform plat = platform.platform() if "linux" in plat.lower(): - self.assertTrue(platform.linux_distribution()[0] in ua) + self.assertTrue(le_util.get_os_info()[0] in ua) with mock.patch('letsencrypt.main.client.acme_client.ClientNetwork') as acme_net: ua = "bandersnatch" From 7ff8440b8f49d527f3a61055aa8fb311ca90063b Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Tue, 12 Apr 2016 18:12:47 +0300 Subject: [PATCH 03/18] Tests for systemd os-release. Fix for darwin OS version info and tests for it --- letsencrypt/le_util.py | 1 - letsencrypt/tests/le_util_test.py | 34 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/letsencrypt/le_util.py b/letsencrypt/le_util.py index 60cdd0314..71dff7575 100644 --- a/letsencrypt/le_util.py +++ b/letsencrypt/le_util.py @@ -300,7 +300,6 @@ def get_python_os_info(): ["sw_vers", "-productVersion"], stdout=subprocess.PIPE ).communicate()[0] - os_ver = os_ver.partition(".")[0] elif os_type.startswith('freebsd'): # eg "9.3-RC3-p1" os_ver = os_ver.partition("-")[0] diff --git a/letsencrypt/tests/le_util_test.py b/letsencrypt/tests/le_util_test.py index 0f9464c6f..e1770eaed 100644 --- a/letsencrypt/tests/le_util_test.py +++ b/letsencrypt/tests/le_util_test.py @@ -339,5 +339,39 @@ class EnforceDomainSanityTest(unittest.TestCase): u"eichh\u00f6rnchen.example.com") +class OsInfoTest(unittest.TestCase): + """Test OS / distribution detection""" + def _call(self): + from letsencrypt.le_util import get_os_info + return get_os_info() + + def test_systemd_os_release(self): + from letsencrypt.le_util import get_os_info + os_release = 'VERSION_ID=42\nID=doobian\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], 'doobian') + self.assertEqual(get_os_info()[1], '42') + + @mock.patch("letsencrypt.le_util.subprocess.Popen") + def test_non_systemd_os_info(self, popen_mock): + from letsencrypt.le_util import get_os_info + with mock.patch('os.path.isfile', return_value=False): + with mock.patch('platform.system_alias', + return_value=('NonSystemD','42','42')): + self.assertEqual(get_os_info()[0], 'nonsystemd') + + with mock.patch('platform.system_alias', + return_value=('darwin', '', '')): + comm_mock = mock.Mock() + comm_attrs = {'communicate.return_value': + ('42.42.42', 'error')} + comm_mock.configure_mock(**comm_attrs) + popen_mock.return_value = comm_mock + self.assertEqual(get_os_info()[0], 'darwin') + self.assertEqual(get_os_info()[1], '42.42.42') + + if __name__ == "__main__": unittest.main() # pragma: no cover From 34f0e260f1ee7c900330d60f47b7e6a06aabd15b Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Tue, 12 Apr 2016 18:49:08 +0300 Subject: [PATCH 04/18] Linter fixes --- letsencrypt/tests/le_util_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/letsencrypt/tests/le_util_test.py b/letsencrypt/tests/le_util_test.py index e1770eaed..10d2c91ad 100644 --- a/letsencrypt/tests/le_util_test.py +++ b/letsencrypt/tests/le_util_test.py @@ -359,7 +359,7 @@ class OsInfoTest(unittest.TestCase): from letsencrypt.le_util import get_os_info with mock.patch('os.path.isfile', return_value=False): with mock.patch('platform.system_alias', - return_value=('NonSystemD','42','42')): + return_value=('NonSystemD', '42', '42')): self.assertEqual(get_os_info()[0], 'nonsystemd') with mock.patch('platform.system_alias', @@ -367,7 +367,7 @@ class OsInfoTest(unittest.TestCase): comm_mock = mock.Mock() comm_attrs = {'communicate.return_value': ('42.42.42', 'error')} - comm_mock.configure_mock(**comm_attrs) + comm_mock.configure_mock(**comm_attrs) # pylint disable=star-args popen_mock.return_value = comm_mock self.assertEqual(get_os_info()[0], 'darwin') self.assertEqual(get_os_info()[1], '42.42.42') From 57738142e28c6841aab9a909883ecf166e6b9560 Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Tue, 12 Apr 2016 18:59:27 +0300 Subject: [PATCH 05/18] Added constants for os-release names --- letsencrypt-apache/letsencrypt_apache/constants.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/letsencrypt-apache/letsencrypt_apache/constants.py b/letsencrypt-apache/letsencrypt_apache/constants.py index 8b502b4d8..72bbdca6d 100644 --- a/letsencrypt-apache/letsencrypt_apache/constants.py +++ b/letsencrypt-apache/letsencrypt_apache/constants.py @@ -78,6 +78,8 @@ CLI_DEFAULTS = { "centos linux": CLI_DEFAULTS_CENTOS, "fedora": CLI_DEFAULTS_CENTOS, "red hat enterprise linux server": CLI_DEFAULTS_CENTOS, + "rhel": CLI_DEFAULTS_CENTOS, + "amazon": CLI_DEFAULTS_CENTOS, "gentoo base system": CLI_DEFAULTS_GENTOO, "darwin": CLI_DEFAULTS_DARWIN, } From 67c60ab406e7b1b34637371e7392d8ce51889d82 Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Tue, 12 Apr 2016 19:41:39 +0300 Subject: [PATCH 06/18] Disabled linter error --- letsencrypt/tests/le_util_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/letsencrypt/tests/le_util_test.py b/letsencrypt/tests/le_util_test.py index 10d2c91ad..74b1bb703 100644 --- a/letsencrypt/tests/le_util_test.py +++ b/letsencrypt/tests/le_util_test.py @@ -347,11 +347,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=doobian\n' + 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], 'doobian') + self.assertEqual(get_os_info()[0], 'systemdos') self.assertEqual(get_os_info()[1], '42') @mock.patch("letsencrypt.le_util.subprocess.Popen") @@ -367,7 +367,7 @@ class OsInfoTest(unittest.TestCase): comm_mock = mock.Mock() comm_attrs = {'communicate.return_value': ('42.42.42', 'error')} - comm_mock.configure_mock(**comm_attrs) # pylint disable=star-args + comm_mock.configure_mock(**comm_attrs) # pylint: disable=star-args popen_mock.return_value = comm_mock self.assertEqual(get_os_info()[0], 'darwin') self.assertEqual(get_os_info()[1], '42.42.42') From 6fc63de5a587c91f3ba87d51b4e38ef8d9f0b44a Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Wed, 13 Apr 2016 00:49:51 +0300 Subject: [PATCH 07/18] Using mocked os-release file --- letsencrypt/le_util.py | 22 ++++++++++++---------- letsencrypt/tests/le_util_test.py | 13 +++++++------ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/letsencrypt/le_util.py b/letsencrypt/le_util.py index 71dff7575..927d8f2e8 100644 --- a/letsencrypt/le_util.py +++ b/letsencrypt/le_util.py @@ -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: diff --git a/letsencrypt/tests/le_util_test.py b/letsencrypt/tests/le_util_test.py index 74b1bb703..c43dbfe2c 100644 --- a/letsencrypt/tests/le_util_test.py +++ b/letsencrypt/tests/le_util_test.py @@ -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): From 608352157c3fb618e4e345509e0163481596dcba Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Wed, 13 Apr 2016 00:55:21 +0300 Subject: [PATCH 08/18] ..and the test file of course --- letsencrypt/tests/testdata/os-release | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 letsencrypt/tests/testdata/os-release diff --git a/letsencrypt/tests/testdata/os-release b/letsencrypt/tests/testdata/os-release new file mode 100644 index 000000000..b7c3ceb1b --- /dev/null +++ b/letsencrypt/tests/testdata/os-release @@ -0,0 +1,8 @@ +NAME="SystemdOS" +VERSION="42.42.42 LTS, Unreal" +ID=systemdos +ID_LIKE=debian +PRETTY_NAME="SystemdOS 42.42.42 Unreal" +VERSION_ID="42" +HOME_URL="http://www.example.com/" +SUPPORT_URL="http://help.example.com/" From 096873ca1c7d2c998283fa0cd3835db6bac3ea9c Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Wed, 13 Apr 2016 01:21:34 +0300 Subject: [PATCH 09/18] Removed unused import --- letsencrypt/tests/le_util_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/letsencrypt/tests/le_util_test.py b/letsencrypt/tests/le_util_test.py index c43dbfe2c..bab711ded 100644 --- a/letsencrypt/tests/le_util_test.py +++ b/letsencrypt/tests/le_util_test.py @@ -4,7 +4,6 @@ import errno import os import shutil import stat -import sys import tempfile import unittest From 995b22684f20a5de2f5b48dc34816f54f9af654c Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Thu, 14 Apr 2016 08:08:08 +0300 Subject: [PATCH 10/18] Removed unused test method --- letsencrypt/tests/le_util_test.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/letsencrypt/tests/le_util_test.py b/letsencrypt/tests/le_util_test.py index bab711ded..5187964a4 100644 --- a/letsencrypt/tests/le_util_test.py +++ b/letsencrypt/tests/le_util_test.py @@ -342,9 +342,6 @@ class EnforceDomainSanityTest(unittest.TestCase): class OsInfoTest(unittest.TestCase): """Test OS / distribution detection""" - def _call(self): - from letsencrypt.le_util import get_os_info - return get_os_info() def test_systemd_os_release(self): from letsencrypt.le_util import get_os_info From b1d7bd318e649474dd763667920e8e8744b961b9 Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Thu, 14 Apr 2016 08:39:39 +0300 Subject: [PATCH 11/18] Full test coverage for le_util and os detection --- letsencrypt/tests/le_util_test.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/letsencrypt/tests/le_util_test.py b/letsencrypt/tests/le_util_test.py index 5187964a4..435760828 100644 --- a/letsencrypt/tests/le_util_test.py +++ b/letsencrypt/tests/le_util_test.py @@ -344,16 +344,19 @@ class OsInfoTest(unittest.TestCase): """Test OS / distribution detection""" def test_systemd_os_release(self): - from letsencrypt.le_util import get_os_info + from letsencrypt.le_util import get_os_info, get_systemd_os_info 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') + self.assertEqual(get_systemd_os_info("/dev/null"), ("", "")) + with mock.patch('os.path.isfile', return_value=False): + self.assertEqual(get_systemd_os_info(), ("", "")) @mock.patch("letsencrypt.le_util.subprocess.Popen") def test_non_systemd_os_info(self, popen_mock): - from letsencrypt.le_util import get_os_info + from letsencrypt.le_util import get_os_info, get_python_os_info with mock.patch('os.path.isfile', return_value=False): with mock.patch('platform.system_alias', return_value=('NonSystemD', '42', '42')): @@ -364,11 +367,32 @@ class OsInfoTest(unittest.TestCase): comm_mock = mock.Mock() comm_attrs = {'communicate.return_value': ('42.42.42', 'error')} - comm_mock.configure_mock(**comm_attrs) # pylint: disable=star-args + comm_mock.configure_mock(**comm_attrs) # pylint: disable=star-args popen_mock.return_value = comm_mock self.assertEqual(get_os_info()[0], 'darwin') self.assertEqual(get_os_info()[1], '42.42.42') + with mock.patch('platform.system_alias', + return_value=('linux', '', '')): + with mock.patch('platform.linux_distribution', + return_value=('', '', '')): + self.assertEqual(get_python_os_info(), ("linux", "")) + + with mock.patch('platform.linux_distribution', + return_value=('testdist', '42', '')): + self.assertEqual(get_python_os_info(), ("testdist", "42")) + + with mock.patch('platform.system_alias', + return_value=('freebsd', '9.3-RC3-p1', '')): + self.assertEqual(get_python_os_info(), ("freebsd", "9")) + + with mock.patch('platform.system_alias', + return_value=('windows', '', '')): + with mock.patch('platform.win32_ver', + return_value=('4242', '95', '2', '')): + self.assertEqual(get_python_os_info(), + ("windows", "95")) + if __name__ == "__main__": unittest.main() # pragma: no cover From 7563d65cb324c5702f55593276e941e036e585b8 Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Sat, 16 Apr 2016 15:39:31 +0300 Subject: [PATCH 12/18] Name change for tests --- certbot/tests/le_util_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/certbot/tests/le_util_test.py b/certbot/tests/le_util_test.py index 752e66a63..23ea40987 100644 --- a/certbot/tests/le_util_test.py +++ b/certbot/tests/le_util_test.py @@ -344,7 +344,7 @@ class OsInfoTest(unittest.TestCase): """Test OS / distribution detection""" def test_systemd_os_release(self): - from letsencrypt.le_util import get_os_info, get_systemd_os_info + from certbot.le_util import get_os_info, get_systemd_os_info with mock.patch('os.path.isfile', return_value=True): self.assertEqual(get_os_info( test_util.vector_path("os-release"))[0], 'systemdos') @@ -354,9 +354,9 @@ class OsInfoTest(unittest.TestCase): with mock.patch('os.path.isfile', return_value=False): self.assertEqual(get_systemd_os_info(), ("", "")) - @mock.patch("letsencrypt.le_util.subprocess.Popen") + @mock.patch("certbot.le_util.subprocess.Popen") def test_non_systemd_os_info(self, popen_mock): - from letsencrypt.le_util import get_os_info, get_python_os_info + from certbot.le_util import get_os_info, get_python_os_info with mock.patch('os.path.isfile', return_value=False): with mock.patch('platform.system_alias', return_value=('NonSystemD', '42', '42')): From c2a5fb7f21c04af7a3664e14b4d7e73af23d6db9 Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Sat, 16 Apr 2016 16:03:46 +0300 Subject: [PATCH 13/18] Re-add test file --- certbot/tests/testdata/os-release | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 certbot/tests/testdata/os-release diff --git a/certbot/tests/testdata/os-release b/certbot/tests/testdata/os-release new file mode 100644 index 000000000..b7c3ceb1b --- /dev/null +++ b/certbot/tests/testdata/os-release @@ -0,0 +1,8 @@ +NAME="SystemdOS" +VERSION="42.42.42 LTS, Unreal" +ID=systemdos +ID_LIKE=debian +PRETTY_NAME="SystemdOS 42.42.42 Unreal" +VERSION_ID="42" +HOME_URL="http://www.example.com/" +SUPPORT_URL="http://help.example.com/" From ff30fb71d2cabeae6951d3c77ed536fe1070c415 Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Fri, 29 Apr 2016 15:31:59 +0300 Subject: [PATCH 14/18] New method for determining UA string --- certbot/client.py | 2 +- certbot/le_util.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/certbot/client.py b/certbot/client.py index 60e37a787..4c90a84ca 100644 --- a/certbot/client.py +++ b/certbot/client.py @@ -52,7 +52,7 @@ def _determine_user_agent(config): if config.user_agent is None: ua = "CertbotACMEClient/{0} ({1}) Authenticator/{2} Installer/{3}" - ua = ua.format(certbot.__version__, " ".join(le_util.get_os_info()), + ua = ua.format(certbot.__version__, le_util.get_os_info_ua(), config.authenticator, config.installer) else: ua = config.user_agent diff --git a/certbot/le_util.py b/certbot/le_util.py index c1f4d1acd..fcde840c9 100644 --- a/certbot/le_util.py +++ b/certbot/le_util.py @@ -228,6 +228,24 @@ def get_os_info(filepath="/etc/os-release"): return get_python_os_info() +def get_os_info_ua(filepath="/etc/os-release"): + """ + Get OS name and version string for User Agent + + :param str filepath: File path of os-release file + :returns: os_ua + :rtype: `str` + """ + + if os.path.isfile(filepath): + os_ua = _get_systemd_os_release_var("NAME", filepath=filepath) + if os_ua: + return os_ua + + # Fallback + return " ".join(get_python_os_info()) + + def get_systemd_os_info(filepath="/etc/os-release"): """ Parse systemd /etc/os-release for distribution information From 1b5efc842719312fbb22cd19f27ab8011ace3f81 Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Fri, 29 Apr 2016 15:52:24 +0300 Subject: [PATCH 15/18] Added tests for new UA method --- certbot/tests/le_util_test.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/certbot/tests/le_util_test.py b/certbot/tests/le_util_test.py index 23ea40987..99aaba44e 100644 --- a/certbot/tests/le_util_test.py +++ b/certbot/tests/le_util_test.py @@ -344,23 +344,31 @@ class OsInfoTest(unittest.TestCase): """Test OS / distribution detection""" def test_systemd_os_release(self): - from certbot.le_util import get_os_info, get_systemd_os_info + from certbot.le_util import (get_os_info, get_systemd_os_info, + get_os_info_ua) + 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') self.assertEqual(get_systemd_os_info("/dev/null"), ("", "")) + self.assertEqual(get_os_info_ua( + test_util.vector_path("os-release")), + "SystemdOS") with mock.patch('os.path.isfile', return_value=False): self.assertEqual(get_systemd_os_info(), ("", "")) @mock.patch("certbot.le_util.subprocess.Popen") def test_non_systemd_os_info(self, popen_mock): - from certbot.le_util import get_os_info, get_python_os_info + from certbot.le_util import (get_os_info, get_python_os_info, + get_os_info_ua) with mock.patch('os.path.isfile', return_value=False): with mock.patch('platform.system_alias', return_value=('NonSystemD', '42', '42')): self.assertEqual(get_os_info()[0], 'nonsystemd') + self.assertEqual(get_os_info_ua(), + " ".join(get_python_os_info())) with mock.patch('platform.system_alias', return_value=('darwin', '', '')): From 9af0994ca6144daa45903e182f48e7036bd9047b Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Fri, 29 Apr 2016 16:23:52 +0300 Subject: [PATCH 16/18] More verbose UA & test UA test fixes --- certbot/le_util.py | 9 ++++++--- certbot/tests/cli_test.py | 4 ++-- certbot/tests/testdata/os-release | 1 - 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/certbot/le_util.py b/certbot/le_util.py index fcde840c9..d18032d9c 100644 --- a/certbot/le_util.py +++ b/certbot/le_util.py @@ -238,7 +238,9 @@ def get_os_info_ua(filepath="/etc/os-release"): """ if os.path.isfile(filepath): - os_ua = _get_systemd_os_release_var("NAME", filepath=filepath) + os_ua = _get_systemd_os_release_var("PRETTY_NAME", filepath=filepath) + if not os_ua: + os_ua = _get_systemd_os_release_var("NAME", filepath=filepath) if os_ua: return os_ua @@ -396,7 +398,7 @@ def enforce_domain_sanity(domain): domain = domain.encode('ascii').lower() except UnicodeError: error_fmt = (u"Internationalized domain names " - "are not presently supported: {0}") + "are not presently supported: {0}") if isinstance(domain, six.text_type): raise errors.ConfigurationError(error_fmt.format(domain)) else: @@ -423,5 +425,6 @@ def enforce_domain_sanity(domain): # first and last char is not "-" fqdn = re.compile("^((?!-)[A-Za-z0-9-]{1,63}(? Date: Fri, 27 May 2016 15:04:59 -0700 Subject: [PATCH 17/18] Fix stray merge error --- certbot/tests/cli_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/certbot/tests/cli_test.py b/certbot/tests/cli_test.py index 09a882c89..671da16f0 100644 --- a/certbot/tests/cli_test.py +++ b/certbot/tests/cli_test.py @@ -177,7 +177,7 @@ class CLITest(unittest.TestCase): # pylint: disable=too-many-public-methods import platform plat = platform.platform() if "linux" in plat.lower(): - self.assertTrue(le_util.get_os_info_ua() in ua) + self.assertTrue(util.get_os_info_ua() in ua) with mock.patch('certbot.main.client.acme_client.ClientNetwork') as acme_net: ua = "bandersnatch" From 24915f6d008e50c2a242e9ac7e752ad28cb0f623 Mon Sep 17 00:00:00 2001 From: Peter Eckersley Date: Fri, 27 May 2016 16:45:12 -0700 Subject: [PATCH 18/18] Lint --- certbot/tests/util_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/certbot/tests/util_test.py b/certbot/tests/util_test.py index 4cbc9b663..8e1b330ed 100644 --- a/certbot/tests/util_test.py +++ b/certbot/tests/util_test.py @@ -10,7 +10,6 @@ import unittest import mock import six -import certbot from certbot import errors from certbot.tests import test_util