Skip to content

Commit 54244d0

Browse files
committed
Add service_logs integration test
Signed-off-by: Joffrey F <[email protected]>
1 parent 672a9ee commit 54244d0

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

docker/api/service.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ def services(self, filters=None):
167167
return self._result(self._get(url, params=params), True)
168168

169169
@utils.minimum_version('1.25')
170+
@utils.check_resource
170171
def service_logs(self, service, details=False, follow=False, stdout=False,
171172
stderr=False, since=0, timestamps=False, tail='all',
172173
is_tty=None):
@@ -176,7 +177,7 @@ def service_logs(self, service, details=False, follow=False, stdout=False,
176177
or ``journald`` logging drivers.
177178
178179
Args:
179-
service (str): ID or name of the container
180+
service (str): ID or name of the service
180181
details (bool): Show extra details provided to logs.
181182
Default: ``False``
182183
follow (bool): Keep connection open to read logs as they are

tests/helpers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import os
23
import os.path
34
import random
@@ -53,6 +54,15 @@ def requires_api_version(version):
5354
)
5455

5556

57+
def requires_experimental(f):
58+
@functools.wraps(f)
59+
def wrapped(self, *args, **kwargs):
60+
if not self.client.info()['ExperimentalBuild']:
61+
pytest.skip('Feature requires Docker Engine experimental mode')
62+
return f(self, *args, **kwargs)
63+
return wrapped
64+
65+
5666
def wait_on_condition(condition, delay=0.1, timeout=40):
5767
start_time = time.time()
5868
while not condition():

tests/integration/api_service_test.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
import docker
77

8-
from ..helpers import force_leave_swarm, requires_api_version
8+
from ..helpers import (
9+
force_leave_swarm, requires_api_version, requires_experimental
10+
)
911
from .base import BaseAPIIntegrationTest, BUSYBOX
1012

1113

@@ -27,13 +29,15 @@ def tearDown(self):
2729
def get_service_name(self):
2830
return 'dockerpytest_{0:x}'.format(random.getrandbits(64))
2931

30-
def get_service_container(self, service_name, attempts=20, interval=0.5):
32+
def get_service_container(self, service_name, attempts=20, interval=0.5,
33+
include_stopped=False):
3134
# There is some delay between the service's creation and the creation
3235
# of the service's containers. This method deals with the uncertainty
3336
# when trying to retrieve the container associated with a service.
3437
while True:
3538
containers = self.client.containers(
36-
filters={'name': [service_name]}, quiet=True
39+
filters={'name': [service_name]}, quiet=True,
40+
all=include_stopped
3741
)
3842
if len(containers) > 0:
3943
return containers[0]
@@ -97,6 +101,18 @@ def test_create_service_simple(self):
97101
assert len(services) == 1
98102
assert services[0]['ID'] == svc_id['ID']
99103

104+
@requires_api_version('1.25')
105+
@requires_experimental
106+
def test_service_logs(self):
107+
name, svc_id = self.create_simple_service()
108+
assert self.get_service_container(name, include_stopped=True)
109+
logs = self.client.service_logs(svc_id, stdout=True, is_tty=False)
110+
log_line = next(logs)
111+
assert 'hello\n' in log_line
112+
assert 'com.docker.swarm.service.id={}'.format(
113+
svc_id['ID']
114+
) in log_line
115+
100116
def test_create_service_custom_log_driver(self):
101117
container_spec = docker.types.ContainerSpec(
102118
BUSYBOX, ['echo', 'hello']

0 commit comments

Comments
 (0)