]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
test: Fix mtr-packet tests for Python 3
authorMatt Kimball <matt.kimball@gmail.com>
Sun, 4 Dec 2016 18:03:44 +0000 (10:03 -0800)
committerMatt Kimball <matt.kimball@gmail.com>
Sun, 4 Dec 2016 18:03:44 +0000 (10:03 -0800)
The mtr-packet test suite was written to run with Python 2, and lacked
the Unicode to bytes and bytes to Unicode conversions required to
run with Python 3.  This change adds those conversions, and the result
works with either Python 2 or Python 3.

Also, close the subprocess's stdin and stdout explicitly.  Python 3's
unittest complains about unclosed file descriptors otherwise.

packet/testpacket.py [changed mode: 0755->0644]

old mode 100755 (executable)
new mode 100644 (file)
index 67bb82c..b145bb4
@@ -78,10 +78,16 @@ class TestProbe(unittest.TestCase):
         except OSError:
             return
 
+        self.packet_process.stdin.close()
+        self.packet_process.stdout.close()
+
     def write_command(self, cmd):  # type: (str) -> None
         'Send a command string to the mtr-packet instance'
 
-        self.packet_process.stdin.write(cmd + '\n')
+        command_str = cmd + '\n'
+        command_bytes = command_str.encode('utf-8')
+
+        self.packet_process.stdin.write(command_bytes)
         self.packet_process.stdin.flush()
 
     def read_reply(self, timeout=10.0):  # type: (float) -> str
@@ -106,11 +112,16 @@ class TestProbe(unittest.TestCase):
 
             select.select([self.stdout_fd], [], [], select_time)
 
+            reply_bytes = None
+
             try:
-                self.reply_buffer += os.read(self.stdout_fd, 1024)
+                reply_bytes = os.read(self.stdout_fd, 1024)
             except OSError:
                 pass
 
+            if reply_bytes:
+                self.reply_buffer += reply_bytes.decode('utf-8')
+
             #  If we have read a newline character, we can stop waiting
             #  for more input.
             newline_ix = self.reply_buffer.find('\n')