diff --git a/docker/client.py b/docker/client.py index ccd4a774..47ca4cd3 100644 --- a/docker/client.py +++ b/docker/client.py @@ -574,7 +574,7 @@ class Client(requests.Session): 'AttachStderr': stderr, 'Detach': detach, 'Cmd': cmd - } + } # create the command url = self._url('/containers/{0}/exec'.format(container)) @@ -738,13 +738,15 @@ class Client(requests.Session): if isinstance(container, dict): container = container.get('Id') if utils.compare_version('1.11', self._version) >= 0: - if tail != 'all' and (not isinstance(tail, int) or tail <= 0): - tail = 'all' params = {'stderr': stderr and 1 or 0, 'stdout': stdout and 1 or 0, 'timestamps': timestamps and 1 or 0, 'follow': stream and 1 or 0, - 'tail': tail} + } + if utils.compare_version('1.13', self._version) >= 0: + if tail != 'all' and (not isinstance(tail, int) or tail <= 0): + tail = 'all' + params['tail'] = tail url = self._url("/containers/{0}/logs".format(container)) res = self._get(url, params=params, stream=stream) if stream: diff --git a/tests/integration_test.py b/tests/integration_test.py index 65d974b5..f6f2c457 100644 --- a/tests/integration_test.py +++ b/tests/integration_test.py @@ -292,6 +292,22 @@ class TestLogs(BaseTestCase): self.assertEqual(logs, (snippet + '\n').encode(encoding='ascii')) +class TestLogsWithTailOption(BaseTestCase): + def runTest(self): + snippet = '''Line1 +Line2''' + container = self.client.create_container( + 'busybox', 'echo "{0}"'.format(snippet) + ) + id = container['Id'] + self.client.start(id) + self.tmp_containers.append(id) + exitcode = self.client.wait(id) + self.assertEqual(exitcode, 0) + logs = self.client.logs(id, tail=1) + self.assertEqual(logs, ('Line2\n').encode(encoding='ascii')) + + # class TestLogsStreaming(BaseTestCase): # def runTest(self): # snippet = 'Flowering Nights (Sakuya Iyazoi)' diff --git a/tests/test.py b/tests/test.py index 8291b26a..b7fe0ab4 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1069,6 +1069,20 @@ class DockerClientTest(Cleanup, unittest.TestCase): stream=True ) + def test_log_tail(self): + try: + self.client.logs(fake_api.FAKE_CONTAINER_ID, stream=False, tail=10) + except Exception as e: + self.fail('Command should not raise exception: {0}'.format(e)) + + fake_request.assert_called_with( + url_prefix + 'containers/3cc2351ab11b/logs', + params={'timestamps': 0, 'follow': 0, 'stderr': 1, 'stdout': 1, + 'tail': 10}, + timeout=docker.client.DEFAULT_TIMEOUT_SECONDS, + stream=False + ) + def test_diff(self): try: self.client.diff(fake_api.FAKE_CONTAINER_ID)