You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by po...@apache.org on 2022/07/04 10:46:43 UTC

[airflow] branch main updated: Adding Docker context check for breeze (#24751)

This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new e520ef5160 Adding Docker context check for breeze (#24751)
e520ef5160 is described below

commit e520ef5160bcc2d98e5ccb042ef9a6506553e7fe
Author: Alex Kruchkov <36...@users.noreply.github.com>
AuthorDate: Mon Jul 4 13:46:38 2022 +0300

    Adding Docker context check for breeze (#24751)
---
 .../airflow_breeze/utils/docker_command_utils.py   | 36 ++++++++++++
 dev/breeze/tests/test_docker_command_utils.py      | 64 +++++++++++++++++++++-
 2 files changed, 99 insertions(+), 1 deletion(-)

diff --git a/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py b/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
index c49c0f183f..d646ba0fab 100644
--- a/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
+++ b/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
@@ -307,6 +307,41 @@ Make sure docker-compose you install is first on the PATH variable of yours.
         )
 
 
+def check_docker_context(verbose: bool):
+    """
+    Checks whether Docker is using the expected context
+    :param verbose: print commands when running
+    """
+    expected_docker_context = "default"
+    response = run_command(
+        ["docker", "info", "--format", "{{json .ClientInfo.Context}}"],
+        verbose=verbose,
+        no_output_dump_on_exception=False,
+        text=True,
+        capture_output=True,
+    )
+    if response.returncode != 0:
+        get_console().print(
+            '[warning]Could not check for Docker context.[/]\n'
+            '[warning]Please make sure that Docker is using the right context by running "docker info" and '
+            'checking the active Context.[/]'
+        )
+        return
+
+    used_docker_context = response.stdout.strip().replace('"', '')
+
+    if used_docker_context == expected_docker_context:
+        get_console().print(f'[success]Good Docker context used: {used_docker_context}.[/]')
+    else:
+        get_console().print(
+            f'[error]Docker is not using the default context, used context is: {used_docker_context}[/]\n'
+            f'[warning]Please make sure Docker is using the {expected_docker_context} context.[/]\n'
+            f'[warning]You can try switching contexts by running: "docker context use '
+            f'{expected_docker_context}"[/]'
+        )
+        sys.exit(1)
+
+
 def get_env_variable_value(arg_name: str, params: Union[CommonBuildParams, ShellParams]):
     raw_value = getattr(params, arg_name, None)
     value = str(raw_value) if raw_value is not None else ''
@@ -630,6 +665,7 @@ def perform_environment_checks(verbose: bool):
     check_docker_is_running(verbose=verbose)
     check_docker_version(verbose=verbose)
     check_docker_compose_version(verbose=verbose)
+    check_docker_context(verbose=verbose)
 
 
 def get_docker_syntax_version() -> str:
diff --git a/dev/breeze/tests/test_docker_command_utils.py b/dev/breeze/tests/test_docker_command_utils.py
index 55c7c3e4d4..80fe192764 100644
--- a/dev/breeze/tests/test_docker_command_utils.py
+++ b/dev/breeze/tests/test_docker_command_utils.py
@@ -18,7 +18,13 @@
 from unittest import mock
 from unittest.mock import call
 
-from airflow_breeze.utils.docker_command_utils import check_docker_compose_version, check_docker_version
+import pytest
+
+from airflow_breeze.utils.docker_command_utils import (
+    check_docker_compose_version,
+    check_docker_context,
+    check_docker_version,
+)
 
 
 @mock.patch('airflow_breeze.utils.docker_command_utils.check_docker_permission_denied')
@@ -199,3 +205,59 @@ def test_check_docker_compose_version_higher(mock_get_console, mock_run_command)
     mock_get_console.return_value.print.assert_called_with(
         "[success]Good version of docker-compose: 1.29.2[/]"
     )
+
+
+@mock.patch('airflow_breeze.utils.docker_command_utils.run_command')
+@mock.patch('airflow_breeze.utils.docker_command_utils.get_console')
+def test_check_docker_context_default(mock_get_console, mock_run_command):
+    mock_run_command.return_value.returncode = 0
+    mock_run_command.return_value.stdout = "default"
+    check_docker_context(verbose=True)
+    mock_run_command.assert_called_with(
+        ["docker", "info", "--format", "{{json .ClientInfo.Context}}"],
+        verbose=True,
+        no_output_dump_on_exception=False,
+        text=True,
+        capture_output=True,
+    )
+    mock_get_console.return_value.print.assert_called_with('[success]Good Docker context used: default.[/]')
+
+
+@mock.patch('airflow_breeze.utils.docker_command_utils.run_command')
+@mock.patch('airflow_breeze.utils.docker_command_utils.get_console')
+def test_check_docker_context_other(mock_get_console, mock_run_command):
+    mock_run_command.return_value.returncode = 0
+    mock_run_command.return_value.stdout = "other"
+    with pytest.raises(SystemExit):
+        check_docker_context(verbose=True)
+    mock_run_command.assert_called_with(
+        ["docker", "info", "--format", "{{json .ClientInfo.Context}}"],
+        verbose=True,
+        no_output_dump_on_exception=False,
+        text=True,
+        capture_output=True,
+    )
+    mock_get_console.return_value.print.assert_called_with(
+        '[error]Docker is not using the default context, used context is: other[/]\n'
+        '[warning]Please make sure Docker is using the default context.[/]\n'
+        '[warning]You can try switching contexts by running: "docker context use default"[/]'
+    )
+
+
+@mock.patch('airflow_breeze.utils.docker_command_utils.run_command')
+@mock.patch('airflow_breeze.utils.docker_command_utils.get_console')
+def test_check_docker_context_command_failed(mock_get_console, mock_run_command):
+    mock_run_command.return_value.returncode = 1
+    check_docker_context(verbose=True)
+    mock_run_command.assert_called_with(
+        ["docker", "info", "--format", "{{json .ClientInfo.Context}}"],
+        verbose=True,
+        no_output_dump_on_exception=False,
+        text=True,
+        capture_output=True,
+    )
+    mock_get_console.return_value.print.assert_called_with(
+        '[warning]Could not check for Docker context.[/]\n'
+        '[warning]Please make sure that Docker is using the right context by running "docker info" and '
+        'checking the active Context.[/]'
+    )