From f08dd83fb1740041d05f8c0840ca49113cfab905 Mon Sep 17 00:00:00 2001 From: Paula Gombar Date: Thu, 23 Apr 2020 15:31:12 -0700 Subject: [PATCH] Upgrade unit test to be compatible with Python 3.8 (#1860) --- .travis.yml | 1 + azurelinuxagent/common/osutil/bigip.py | 2 +- azurelinuxagent/common/version.py | 2 +- test-requirements.txt | 3 ++- tests/common/test_version.py | 37 ++++++++++++++++++++------ tests/protocol/test_protocol_util.py | 5 ++-- 6 files changed, 36 insertions(+), 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index e1a250efe7..0c0813fdf4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,6 +22,7 @@ matrix: - python: 3.4 - python: 3.6 - python: 3.7 + - python: 3.8 env: - >- NOSEOPTS="--verbose --with-coverage --with-timer --cover-inclusive diff --git a/azurelinuxagent/common/osutil/bigip.py b/azurelinuxagent/common/osutil/bigip.py index 07cdb1dd65..945c89bb8a 100644 --- a/azurelinuxagent/common/osutil/bigip.py +++ b/azurelinuxagent/common/osutil/bigip.py @@ -67,7 +67,7 @@ def _wait_until_mcpd_is_initialized(self): break time.sleep(30) - if rc is 0: + if rc == 0: return True raise OSUtilError( diff --git a/azurelinuxagent/common/version.py b/azurelinuxagent/common/version.py index 854a8c66b8..fa52833691 100644 --- a/azurelinuxagent/common/version.py +++ b/azurelinuxagent/common/version.py @@ -87,7 +87,7 @@ def get_distro(): osinfo = ['nsbsd', release, '', 'nsbsd'] else: try: - # dist() removed in Python 3.7 + # dist() removed in Python 3.8 osinfo = list(platform.dist()) + [''] except: osinfo = ['UNKNOWN', 'FFFF', '', ''] diff --git a/test-requirements.txt b/test-requirements.txt index 4fc043df55..35071253eb 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -4,5 +4,6 @@ flake8; python_version >= '2.7' mock==2.0.0; python_version == '2.6' mock==3.0.5; python_version >= '2.7' and python_version <= '3.5' mock==4.0.2; python_version >= '3.6' +distro; python_version >= '3.8' nose -nose-timer; python_version >= '2.7' \ No newline at end of file +nose-timer; python_version >= '2.7' diff --git a/tests/common/test_version.py b/tests/common/test_version.py index ada8f2349c..8767828077 100644 --- a/tests/common/test_version.py +++ b/tests/common/test_version.py @@ -25,7 +25,7 @@ from azurelinuxagent.common.event import EVENTS_DIRECTORY from azurelinuxagent.common.version import set_current_agent, \ AGENT_LONG_VERSION, AGENT_VERSION, AGENT_NAME, AGENT_NAME_PATTERN, \ - get_f5_platform, get_distro + get_f5_platform, get_distro, PY_VERSION_MAJOR, PY_VERSION_MINOR from tests.tools import AgentTestCase, open_patch, patch @@ -57,6 +57,13 @@ def default_system_exception(): raise Exception +def is_platform_dist_supported(): + # platform.dist() and platform.linux_distribution() is deprecated from Python 3.8+ + if PY_VERSION_MAJOR == 3 and PY_VERSION_MINOR >= 8: + return False + else: + return True + class TestAgentVersion(AgentTestCase): def setUp(self): AgentTestCase.setUp(self) @@ -79,18 +86,32 @@ def test_distro_is_correct_format_when_openbsd(self, platform_system_name, mock_ return @mock.patch('platform.system', side_effect=default_system) - @mock.patch('platform.dist', side_effect=default_system_no_linux_distro) - def test_distro_is_correct_format_when_default_case(self, platform_system_name, default_system_no_linux): - osinfo = get_distro() + def test_distro_is_correct_format_when_default_case(self, *args): default_list = ['', '', '', ''] - self.assertListEqual(default_list, osinfo) + unknown_list = ['unknown', 'FFFF', '', ''] + + if is_platform_dist_supported(): + with patch('platform.dist', side_effect=default_system_no_linux_distro): + osinfo = get_distro() + self.assertListEqual(default_list, osinfo) + else: + # platform.dist() is deprecated in Python 3.7+ and would throw, resulting in unknown distro + osinfo = get_distro() + self.assertListEqual(unknown_list, osinfo) return @mock.patch('platform.system', side_effect=default_system) - @mock.patch('platform.dist', side_effect=default_system_exception) - def test_distro_is_correct_for_exception_case(self, platform_system_name, default_system_no_linux): - osinfo = get_distro() + def test_distro_is_correct_for_exception_case(self, *args): default_list = ['unknown', 'FFFF', '', ''] + + if is_platform_dist_supported(): + with patch('platform.dist', side_effect=default_system_exception): + osinfo = get_distro() + else: + # platform.dist() is deprecated in Python 3.7+ so we can't patch it, but it would throw + # as well, resulting in the same unknown distro + osinfo = get_distro() + self.assertListEqual(default_list, osinfo) return diff --git a/tests/protocol/test_protocol_util.py b/tests/protocol/test_protocol_util.py index 5a9e1e87f3..eb44fe3c84 100644 --- a/tests/protocol/test_protocol_util.py +++ b/tests/protocol/test_protocol_util.py @@ -281,11 +281,10 @@ def test_get_protocol_new_wireserver_agent_generates_certificates(self, mock_wir with open(os.path.join(dir, ENDPOINT_FILE_NAME), 'r') as f: self.assertEquals(f.read(), KNOWN_WIRESERVER_IP) - @patch("azurelinuxagent.common.utils.fileutil") + @patch("azurelinuxagent.common.protocol.util.fileutil") @patch("azurelinuxagent.common.conf.get_lib_dir") def test_endpoint_file_states(self, mock_get_lib_dir, mock_fileutil, _): mock_get_lib_dir.return_value = self.tmp_dir - mock_fileutil = MagicMock() protocol_util = get_protocol_util() endpoint_file = protocol_util._get_wireserver_endpoint_file_path() @@ -312,7 +311,7 @@ def test_endpoint_file_states(self, mock_get_lib_dir, mock_fileutil, _): mock_fileutil.write_file.side_effect = IOError() ep = protocol_util.get_wireserver_endpoint() - self.assertRaises(OSUtilError, protocol_util._set_wireserver_endpoint('abc')) + self.assertRaises(OSUtilError, protocol_util._set_wireserver_endpoint, 'abc') # Test clear endpoint for io error with open(endpoint_file, "w+") as ep_fd: