|
1 |
| -import errno |
2 | 1 | import os
|
3 | 2 | import shutil
|
4 | 3 | import signal
|
5 |
| -import struct |
6 | 4 | import tempfile
|
7 | 5 |
|
8 | 6 | import docker
|
@@ -950,66 +948,30 @@ def test_run_container_streaming(self):
|
950 | 948 | container = self.client.create_container(BUSYBOX, '/bin/sh',
|
951 | 949 | detach=True, stdin_open=True)
|
952 | 950 | id = container['Id']
|
953 |
| - self.client.start(id) |
954 | 951 | self.tmp_containers.append(id)
|
| 952 | + self.client.start(id) |
955 | 953 | sock = self.client.attach_socket(container, ws=False)
|
956 | 954 | self.assertTrue(sock.fileno() > -1)
|
957 | 955 |
|
958 | 956 | def test_run_container_reading_socket(self):
|
959 | 957 | line = 'hi there and stuff and things, words!'
|
960 |
| - command = "echo '{0}'".format(line) |
| 958 | + # `echo` appends CRLF, `printf` doesn't |
| 959 | + command = "printf '{0}'".format(line) |
961 | 960 | container = self.client.create_container(BUSYBOX, command,
|
962 | 961 | detach=True, tty=False)
|
963 | 962 | ident = container['Id']
|
964 | 963 | self.tmp_containers.append(ident)
|
965 | 964 |
|
966 | 965 | opts = {"stdout": 1, "stream": 1, "logs": 1}
|
967 | 966 | pty_stdout = self.client.attach_socket(ident, opts)
|
| 967 | + self.addCleanup(pty_stdout.close) |
| 968 | + |
968 | 969 | self.client.start(ident)
|
969 | 970 |
|
970 |
| - recoverable_errors = (errno.EINTR, errno.EDEADLK, errno.EWOULDBLOCK) |
971 |
| - |
972 |
| - def read(n=4096): |
973 |
| - """Code stolen from dockerpty to read the socket""" |
974 |
| - try: |
975 |
| - if hasattr(pty_stdout, 'recv'): |
976 |
| - return pty_stdout.recv(n) |
977 |
| - return os.read(pty_stdout.fileno(), n) |
978 |
| - except EnvironmentError as e: |
979 |
| - if e.errno not in recoverable_errors: |
980 |
| - raise |
981 |
| - |
982 |
| - def next_packet_size(): |
983 |
| - """Code stolen from dockerpty to get the next packet size""" |
984 |
| - data = six.binary_type() |
985 |
| - while len(data) < 8: |
986 |
| - next_data = read(8 - len(data)) |
987 |
| - if not next_data: |
988 |
| - return 0 |
989 |
| - data = data + next_data |
990 |
| - |
991 |
| - if data is None: |
992 |
| - return 0 |
993 |
| - |
994 |
| - if len(data) == 8: |
995 |
| - _, actual = struct.unpack('>BxxxL', data) |
996 |
| - return actual |
997 |
| - |
998 |
| - next_size = next_packet_size() |
999 |
| - self.assertEqual(next_size, len(line) + 1) |
1000 |
| - |
1001 |
| - data = six.binary_type() |
1002 |
| - while len(data) < next_size: |
1003 |
| - next_data = read(next_size - len(data)) |
1004 |
| - if not next_data: |
1005 |
| - assert False, "Failed trying to read in the dataz" |
1006 |
| - data += next_data |
1007 |
| - self.assertEqual(data.decode('utf-8'), "{0}\n".format(line)) |
1008 |
| - pty_stdout.close() |
1009 |
| - |
1010 |
| - # Prevent segfault at the end of the test run |
1011 |
| - if hasattr(pty_stdout, "_response"): |
1012 |
| - del pty_stdout._response |
| 971 | + next_size = helpers.next_packet_size(pty_stdout) |
| 972 | + self.assertEqual(next_size, len(line)) |
| 973 | + data = helpers.read_data(pty_stdout, next_size) |
| 974 | + self.assertEqual(data.decode('utf-8'), line) |
1013 | 975 |
|
1014 | 976 |
|
1015 | 977 | class PauseTest(helpers.BaseTestCase):
|
|
0 commit comments