Skip to content

Commit

Permalink
Upgrade unit test to be compatible with Python 3.8 (#1860)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgombar authored Apr 23, 2020
1 parent 92b652e commit f08dd83
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion azurelinuxagent/common/osutil/bigip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion azurelinuxagent/common/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', '', '']
Expand Down
3 changes: 2 additions & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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'
nose-timer; python_version >= '2.7'
37 changes: 29 additions & 8 deletions tests/common/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down
5 changes: 2 additions & 3 deletions tests/protocol/test_protocol_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand Down

0 comments on commit f08dd83

Please sign in to comment.