Skip to content

Commit 76bb39c

Browse files
XD-DENGFokko Driesprong
authored and
Fokko Driesprong
committed
[AIRFLOW-3203] Fix DockerOperator & some operator test (#4049)
- For argument `image`, no need to explicitly add "latest" if tag is omitted.   "latest" will be used by default if no tag provided. This is handled by `docker` package itself. - Intermediate variable `cpu_shares` is not needed. - Fix wrong usage of `cpu_shares` and `cpu_shares`. Based on https://docker-py.readthedocs.io/en/stable/api.html#docker.api.container.ContainerApiMixin.create_host_config, They should be an arguments of self.cli.create_host_config() rather than APIClient.create_container(). - Change name of the corresponding test script, to ensure it can be discovered. - Fix the test itself. - Some other test scripts are not named properly, which result in failure of test discovery.
1 parent dc05842 commit 76bb39c

8 files changed

+20
-20
lines changed

airflow/operators/docker_operator.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class DockerOperator(BaseOperator):
4343
be provided with the parameter ``docker_conn_id``.
4444
4545
:param image: Docker image from which to create the container.
46+
If image tag is omitted, "latest" will be used.
4647
:type image: str
4748
:param api_version: Remote API version. Set to ``auto`` to automatically
4849
detect the server's version.
@@ -58,7 +59,7 @@ class DockerOperator(BaseOperator):
5859
:type docker_url: str
5960
:param environment: Environment variables to set in the container. (templated)
6061
:type environment: dict
61-
:param force_pull: Pull the docker image on every run. Default is false.
62+
:param force_pull: Pull the docker image on every run. Default is False.
6263
:type force_pull: bool
6364
:param mem_limit: Maximum amount of memory the container can use.
6465
Either a float value, which represents the limit in bytes,
@@ -179,33 +180,28 @@ def execute(self, context):
179180
tls=tls_config
180181
)
181182

182-
if ':' not in self.image:
183-
image = self.image + ':latest'
184-
else:
185-
image = self.image
186-
187-
if self.force_pull or len(self.cli.images(name=image)) == 0:
188-
self.log.info('Pulling docker image %s', image)
189-
for l in self.cli.pull(image, stream=True):
183+
if self.force_pull or len(self.cli.images(name=self.image)) == 0:
184+
self.log.info('Pulling docker image %s', self.image)
185+
for l in self.cli.pull(self.image, stream=True):
190186
output = json.loads(l.decode('utf-8'))
191187
self.log.info("%s", output['status'])
192188

193-
cpu_shares = int(round(self.cpus * 1024))
194-
195189
with TemporaryDirectory(prefix='airflowtmp') as host_tmp_dir:
196190
self.environment['AIRFLOW_TMP_DIR'] = self.tmp_dir
197191
self.volumes.append('{0}:{1}'.format(host_tmp_dir, self.tmp_dir))
198192

199193
self.container = self.cli.create_container(
200194
command=self.get_command(),
201-
cpu_shares=cpu_shares,
202195
environment=self.environment,
203196
host_config=self.cli.create_host_config(
204197
binds=self.volumes,
205198
network_mode=self.network_mode,
206-
shm_size=self.shm_size),
207-
image=image,
208-
mem_limit=self.mem_limit,
199+
shm_size=self.shm_size,
200+
dns=self.dns,
201+
dns_search=self.dns_search,
202+
cpu_shares=int(round(self.cpus * 1024)),
203+
mem_limit=self.mem_limit),
204+
image=self.image,
209205
user=self.user,
210206
working_dir=self.working_dir
211207
)

tests/operators/docker_operator.py tests/operators/test_docker_operator.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
# to you under the Apache License, Version 2.0 (the
88
# "License"); you may not use this file except in compliance
99
# with the License. You may obtain a copy of the License at
10-
#
10+
#
1111
# http://www.apache.org/licenses/LICENSE-2.0
12-
#
12+
#
1313
# Unless required by applicable law or agreed to in writing,
1414
# software distributed under the License is distributed on an
1515
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -64,20 +64,24 @@ def test_execute(self, client_class_mock, mkdtemp_mock):
6464
client_class_mock.assert_called_with(base_url='unix://var/run/docker.sock', tls=None,
6565
version='1.19')
6666

67-
client_mock.create_container.assert_called_with(command='env', cpu_shares=1024,
67+
client_mock.create_container.assert_called_with(command='env',
6868
environment={
6969
'AIRFLOW_TMP_DIR': '/tmp/airflow',
7070
'UNIT': 'TEST'
7171
},
7272
host_config=host_config,
7373
image='ubuntu:latest',
74-
mem_limit=None, user=None,
74+
user=None,
7575
working_dir='/container/path'
7676
)
7777
client_mock.create_host_config.assert_called_with(binds=['/host/path:/container/path',
7878
'/mkdtemp:/tmp/airflow'],
7979
network_mode='bridge',
80-
shm_size=1000)
80+
shm_size=1000,
81+
cpu_shares=1024,
82+
mem_limit=None,
83+
dns=None,
84+
dns_search=None)
8185
client_mock.images.assert_called_with(name='ubuntu:latest')
8286
client_mock.logs.assert_called_with(container='some_id', stream=True)
8387
client_mock.pull.assert_called_with('ubuntu:latest', stream=True)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)