From fa13ea2388d798e1298ab91baf33c05e26a58b67 Mon Sep 17 00:00:00 2001 From: Till Maas Date: Wed, 23 May 2018 00:27:43 +0200 Subject: [PATCH] Fix exception on Python3 in _link_read_permaddress Bytestrings need to be decoded. Just use UTF-8 here, since everyone should be using it. Also add a test case to catch this. The exception was: TypeError: cannot use a string pattern on a bytes-like object --- library/network_connections.py | 3 ++- tests/helpers/ethtool | 6 ++++++ tests/test_network_connections.py | 16 ++++++++++++++-- tests/tests_unit.yml | 10 ++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100755 tests/helpers/ethtool diff --git a/library/network_connections.py b/library/network_connections.py index 271ab40..1ac8aa4 100644 --- a/library/network_connections.py +++ b/library/network_connections.py @@ -74,7 +74,8 @@ class Util: ev = os.environ.copy() ev['LANG'] = lang if lang is not None else 'C' p = subprocess.Popen(argv, stdout=subprocess.PIPE, stderr=DEVNULL, env=ev) - out = p.communicate()[0] + # FIXME: Can we assume this to always be UTF-8? + out = p.communicate()[0].decode("UTF-8") if p.returncode != 0: raise MyError('failure calling %s: exit with %s' % (argv, p.returncode)) return out diff --git a/tests/helpers/ethtool b/tests/helpers/ethtool new file mode 100755 index 0000000..874561f --- /dev/null +++ b/tests/helpers/ethtool @@ -0,0 +1,6 @@ +#! /bin/bash + +if [ "${1}" == "-P" ] && [ "${2}" != "" ] +then + echo "Permanent address: 23:00:00:00:00:00" +fi diff --git a/tests/test_network_connections.py b/tests/test_network_connections.py index ddbd1bc..2c76a8d 100755 --- a/tests/test_network_connections.py +++ b/tests/test_network_connections.py @@ -7,10 +7,12 @@ import unittest import socket import itertools -sys.path.insert(1, os.path.join(os.path.dirname(os.path.abspath(__file__)), - "..", "library")) +TESTS_BASEDIR = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(1, os.path.join(TESTS_BASEDIR, "..", "library")) import network_connections as n +from network_connections import SysUtil + try: my_test_skipIf = unittest.skipIf @@ -1873,5 +1875,15 @@ class TestNM(unittest.TestCase): self.assertIs(s, s2) self.assertTrue(GObject.type_is_a(s, NM.SettingWired)) + +class TestSysUtils(unittest.TestCase): + def test_link_read_permaddress(self): + # Manipulate PATH to use ethtool mock script to avoid hard dependency on + # ethtool + os.environ["PATH"] = TESTS_BASEDIR + "/helpers:" + os.environ["PATH"] + self.assertEqual(SysUtil._link_read_permaddress("lo"), + "23:00:00:00:00:00") + + if __name__ == '__main__': unittest.main() diff --git a/tests/tests_unit.yml b/tests/tests_unit.yml index 68fd9ec..3f34ecd 100644 --- a/tests/tests_unit.yml +++ b/tests/tests_unit.yml @@ -11,6 +11,16 @@ src: test_network_connections.py dest: /tmp/test-unit-1/ + - file: + state: directory + dest: /tmp/test-unit-1/helpers + + - copy: + src: "{{ item }}" + dest: /tmp/test-unit-1/helpers + mode: 0755 + with_fileglob: + - helpers/* - package: name: python2