Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions airflow/hooks/docker_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from docker import Client
from docker import APIClient
from docker.errors import APIError

from airflow.exceptions import AirflowException
Expand Down Expand Up @@ -60,7 +60,7 @@ def __init__(self,
self.__reauth = False if extra_options.get('reauth') == 'no' else True

def get_conn(self):
client = Client(
client = APIClient(
base_url=self.__base_url,
version=self.__version,
tls=self.__tls
Expand Down
8 changes: 4 additions & 4 deletions airflow/operators/docker_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
Expand All @@ -24,7 +24,7 @@
from airflow.models import BaseOperator
from airflow.utils.decorators import apply_defaults
from airflow.utils.file import TemporaryDirectory
from docker import Client, tls
from docker import APIClient, tls
import ast


Expand Down Expand Up @@ -166,7 +166,7 @@ def execute(self, context):
if self.docker_conn_id:
self.cli = self.get_hook().get_conn()
else:
self.cli = Client(
self.cli = APIClient(
base_url=self.docker_url,
version=self.api_version,
tls=tls_config
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def write_version(filename=os.path.join(*['airflow',
'sphinx-rtd-theme>=0.1.6',
'Sphinx-PyPI-upload>=0.2.1'
]
docker = ['docker-py>=1.6.0']
docker = ['docker>=2.0.0']
druid = ['pydruid>=0.4.1']
elasticsearch = [
'elasticsearch>=5.0.0,<6.0.0',
Expand Down
7 changes: 3 additions & 4 deletions tests/hooks/test_docker_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
Expand All @@ -26,7 +26,6 @@

try:
from airflow.hooks.docker_hook import DockerHook
from docker import Client
except ImportError:
pass

Expand All @@ -39,7 +38,7 @@
mock = None


@mock.patch('airflow.hooks.docker_hook.Client', autospec=True)
@mock.patch('airflow.hooks.docker_hook.APIClient', autospec=True)
class DockerHookTest(unittest.TestCase):
def setUp(self):
configuration.load_test_config()
Expand Down
34 changes: 17 additions & 17 deletions tests/operators/docker_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
Expand All @@ -23,7 +23,7 @@
try:
from airflow.operators.docker_operator import DockerOperator
from airflow.hooks.docker_hook import DockerHook
from docker import Client
from docker import APIClient
except ImportError:
pass

Expand All @@ -40,12 +40,12 @@

class DockerOperatorTestCase(unittest.TestCase):
@mock.patch('airflow.utils.file.mkdtemp')
@mock.patch('airflow.operators.docker_operator.Client')
@mock.patch('airflow.operators.docker_operator.APIClient')
def test_execute(self, client_class_mock, mkdtemp_mock):
host_config = mock.Mock()
mkdtemp_mock.return_value = '/mkdtemp'

client_mock = mock.Mock(spec=Client)
client_mock = mock.Mock(spec=APIClient)
client_mock.create_container.return_value = {'Id': 'some_id'}
client_mock.create_host_config.return_value = host_config
client_mock.images.return_value = []
Expand Down Expand Up @@ -84,9 +84,9 @@ def test_execute(self, client_class_mock, mkdtemp_mock):
client_mock.wait.assert_called_with('some_id')

@mock.patch('airflow.operators.docker_operator.tls.TLSConfig')
@mock.patch('airflow.operators.docker_operator.Client')
@mock.patch('airflow.operators.docker_operator.APIClient')
def test_execute_tls(self, client_class_mock, tls_class_mock):
client_mock = mock.Mock(spec=Client)
client_mock = mock.Mock(spec=APIClient)
client_mock.create_container.return_value = {'Id': 'some_id'}
client_mock.create_host_config.return_value = mock.Mock()
client_mock.images.return_value = []
Expand All @@ -110,9 +110,9 @@ def test_execute_tls(self, client_class_mock, tls_class_mock):
client_class_mock.assert_called_with(base_url='https://127.0.0.1:2376', tls=tls_mock,
version=None)

@mock.patch('airflow.operators.docker_operator.Client')
@mock.patch('airflow.operators.docker_operator.APIClient')
def test_execute_unicode_logs(self, client_class_mock):
client_mock = mock.Mock(spec=Client)
client_mock = mock.Mock(spec=APIClient)
client_mock.create_container.return_value = {'Id': 'some_id'}
client_mock.create_host_config.return_value = mock.Mock()
client_mock.images.return_value = []
Expand All @@ -132,9 +132,9 @@ def test_execute_unicode_logs(self, client_class_mock):
logging.raiseExceptions = originalRaiseExceptions
print_exception_mock.assert_not_called()

@mock.patch('airflow.operators.docker_operator.Client')
@mock.patch('airflow.operators.docker_operator.APIClient')
def test_execute_container_fails(self, client_class_mock):
client_mock = mock.Mock(spec=Client)
client_mock = mock.Mock(spec=APIClient)
client_mock.create_container.return_value = {'Id': 'some_id'}
client_mock.create_host_config.return_value = mock.Mock()
client_mock.images.return_value = []
Expand All @@ -150,7 +150,7 @@ def test_execute_container_fails(self, client_class_mock):
operator.execute(None)

def test_on_kill(self):
client_mock = mock.Mock(spec=Client)
client_mock = mock.Mock(spec=APIClient)

operator = DockerOperator(image='ubuntu', owner='unittest', task_id='unittest')
operator.cli = client_mock
Expand All @@ -160,10 +160,10 @@ def test_on_kill(self):

client_mock.stop.assert_called_with('some_id')

@mock.patch('airflow.operators.docker_operator.Client')
@mock.patch('airflow.operators.docker_operator.APIClient')
def test_execute_no_docker_conn_id_no_hook(self, operator_client_mock):
# Mock out a Docker client, so operations don't raise errors
client_mock = mock.Mock(name='DockerOperator.Client mock', spec=Client)
client_mock = mock.Mock(name='DockerOperator.APIClient mock', spec=APIClient)
client_mock.images.return_value = []
client_mock.create_container.return_value = {'Id': 'some_id'}
client_mock.logs.return_value = []
Expand Down Expand Up @@ -194,11 +194,11 @@ def test_execute_no_docker_conn_id_no_hook(self, operator_client_mock):
)

@mock.patch('airflow.operators.docker_operator.DockerHook')
@mock.patch('airflow.operators.docker_operator.Client')
@mock.patch('airflow.operators.docker_operator.APIClient')
def test_execute_with_docker_conn_id_use_hook(self, operator_client_mock,
operator_docker_hook):
# Mock out a Docker client, so operations don't raise errors
client_mock = mock.Mock(name='DockerOperator.Client mock', spec=Client)
client_mock = mock.Mock(name='DockerOperator.APIClient mock', spec=APIClient)
client_mock.images.return_value = []
client_mock.create_container.return_value = {'Id': 'some_id'}
client_mock.logs.return_value = []
Expand All @@ -223,7 +223,7 @@ def test_execute_with_docker_conn_id_use_hook(self, operator_client_mock,

self.assertEqual(
operator_client_mock.call_count, 0,
'Client was called on the operator instead of the hook'
'APIClient was called on the operator instead of the hook'
)
self.assertEqual(
operator_docker_hook.call_count, 1,
Expand Down