|
2 | 2 | import re
|
3 | 3 | import signal
|
4 | 4 | import tempfile
|
| 5 | +import threading |
5 | 6 | from datetime import datetime
|
6 | 7 |
|
7 | 8 | import docker
|
@@ -880,6 +881,30 @@ def test_logs_streaming_and_follow(self):
|
880 | 881 |
|
881 | 882 | assert logs == (snippet + '\n').encode(encoding='ascii')
|
882 | 883 |
|
| 884 | + def test_logs_streaming_and_follow_and_cancel(self): |
| 885 | + snippet = 'Flowering Nights (Sakuya Iyazoi)' |
| 886 | + container = self.client.create_container( |
| 887 | + BUSYBOX, 'sh -c "echo \\"{0}\\" && sleep 3"'.format(snippet) |
| 888 | + ) |
| 889 | + id = container['Id'] |
| 890 | + self.tmp_containers.append(id) |
| 891 | + self.client.start(id) |
| 892 | + logs = six.binary_type() |
| 893 | + |
| 894 | + generator = self.client.logs(id, stream=True, follow=True) |
| 895 | + |
| 896 | + exit_timer = threading.Timer(3, os._exit, args=[1]) |
| 897 | + exit_timer.start() |
| 898 | + |
| 899 | + threading.Timer(1, generator.close).start() |
| 900 | + |
| 901 | + for chunk in generator: |
| 902 | + logs += chunk |
| 903 | + |
| 904 | + exit_timer.cancel() |
| 905 | + |
| 906 | + assert logs == (snippet + '\n').encode(encoding='ascii') |
| 907 | + |
883 | 908 | def test_logs_with_dict_instead_of_id(self):
|
884 | 909 | snippet = 'Flowering Nights (Sakuya Iyazoi)'
|
885 | 910 | container = self.client.create_container(
|
@@ -1226,6 +1251,29 @@ def test_attach_no_stream(self):
|
1226 | 1251 | output = self.client.attach(container, stream=False, logs=True)
|
1227 | 1252 | assert output == 'hello\n'.encode(encoding='ascii')
|
1228 | 1253 |
|
| 1254 | + def test_attach_stream_and_cancel(self): |
| 1255 | + container = self.client.create_container( |
| 1256 | + BUSYBOX, 'sh -c "echo hello && sleep 60"', |
| 1257 | + tty=True |
| 1258 | + ) |
| 1259 | + self.tmp_containers.append(container) |
| 1260 | + self.client.start(container) |
| 1261 | + output = self.client.attach(container, stream=True, logs=True) |
| 1262 | + |
| 1263 | + exit_timer = threading.Timer(3, os._exit, args=[1]) |
| 1264 | + exit_timer.start() |
| 1265 | + |
| 1266 | + threading.Timer(1, output.close).start() |
| 1267 | + |
| 1268 | + lines = [] |
| 1269 | + for line in output: |
| 1270 | + lines.append(line) |
| 1271 | + |
| 1272 | + exit_timer.cancel() |
| 1273 | + |
| 1274 | + assert len(lines) == 1 |
| 1275 | + assert lines[0] == 'hello\r\n'.encode(encoding='ascii') |
| 1276 | + |
1229 | 1277 | def test_detach_with_default(self):
|
1230 | 1278 | container = self.client.create_container(
|
1231 | 1279 | BUSYBOX, 'cat',
|
|
0 commit comments