Catch APIError while printing container logs

Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
Joffrey F 2016-09-12 15:49:54 -07:00
parent 6abdd9cc32
commit 016197c16e
2 changed files with 31 additions and 2 deletions

View File

@ -6,6 +6,7 @@ from collections import namedtuple
from itertools import cycle
from threading import Thread
from docker.errors import APIError
from six.moves import _thread as thread
from six.moves.queue import Empty
from six.moves.queue import Queue
@ -176,8 +177,14 @@ def build_log_generator(container, log_args):
def wait_on_exit(container):
try:
exit_code = container.wait()
return "%s exited with code %s\n" % (container.name, exit_code)
except APIError as e:
return "Unexpected API error for %s (HTTP code %s)\nResponse body:\n%s\n" % (
container.name, e.response.status_code,
e.response.text or '[empty]'
)
def start_producer_thread(thread_args):

View File

@ -4,7 +4,9 @@ from __future__ import unicode_literals
import itertools
import pytest
import requests
import six
from docker.errors import APIError
from six.moves.queue import Queue
from compose.cli.log_printer import build_log_generator
@ -56,6 +58,26 @@ def test_wait_on_exit():
assert expected == wait_on_exit(mock_container)
def test_wait_on_exit_raises():
status_code = 500
def mock_wait():
resp = requests.Response()
resp.status_code = status_code
raise APIError('Bad server', resp)
mock_container = mock.Mock(
spec=Container,
name='cname',
wait=mock_wait
)
expected = 'Unexpected API error for {} (HTTP code {})\n'.format(
mock_container.name, status_code,
)
assert expected in wait_on_exit(mock_container)
def test_build_no_log_generator(mock_container):
mock_container.has_api_logs = False
mock_container.log_driver = 'none'