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
This commit is contained in:
Till Maas 2018-05-23 00:27:43 +02:00
parent b3341f50c0
commit fa13ea2388
4 changed files with 32 additions and 3 deletions

View file

@ -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

6
tests/helpers/ethtool Executable file
View file

@ -0,0 +1,6 @@
#! /bin/bash
if [ "${1}" == "-P" ] && [ "${2}" != "" ]
then
echo "Permanent address: 23:00:00:00:00:00"
fi

View file

@ -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()

View file

@ -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