You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by ep...@apache.org on 2023/10/05 09:40:27 UTC
[airflow] 03/03: Parse 'docker context ls --format=json' correctly (#34711)
This is an automated email from the ASF dual-hosted git repository.
ephraimanierobi pushed a commit to branch v2-7-test
in repository https://gitbox.apache.org/repos/asf/airflow.git
commit c2173d05a87f521c6661b8998658e0d97fb415d7
Author: Tzu-ping Chung <ur...@gmail.com>
AuthorDate: Tue Oct 3 17:42:50 2023 +0800
Parse 'docker context ls --format=json' correctly (#34711)
(cherry picked from commit 19284981f88e45dca4c4003837e3cead1723caf1)
---
.../src/airflow_breeze/utils/docker_command_utils.py | 7 ++-----
dev/breeze/tests/test_docker_command_utils.py | 19 ++++++++-----------
2 files changed, 10 insertions(+), 16 deletions(-)
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 6aae84fb5f..b6bcea335a 100644
--- a/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
+++ b/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
@@ -832,11 +832,8 @@ def autodetect_docker_context():
if result.returncode != 0:
get_console().print("[warning]Could not detect docker builder. Using default.[/]")
return "default"
- context_json = json.loads(result.stdout)
- if isinstance(context_json, dict):
- # In case there is one context it is returned as dict not array of dicts ¯\_(ツ)_/¯
- context_json = [context_json]
- known_contexts = {info["Name"]: info for info in context_json}
+ context_dicts = (json.loads(line) for line in result.stdout.splitlines() if line.strip())
+ known_contexts = {info["Name"]: info for info in context_dicts}
if not known_contexts:
get_console().print("[warning]Could not detect docker builder. Using default.[/]")
return "default"
diff --git a/dev/breeze/tests/test_docker_command_utils.py b/dev/breeze/tests/test_docker_command_utils.py
index b125fb2bd7..8cd7924445 100644
--- a/dev/breeze/tests/test_docker_command_utils.py
+++ b/dev/breeze/tests/test_docker_command_utils.py
@@ -191,39 +191,36 @@ def test_check_docker_compose_version_ok(mock_get_console, mock_run_command):
)
-def _fake_ctx(name: str) -> dict[str, str]:
- return {
- "Name": name,
- "DockerEndpoint": f"unix://{name}",
- }
+def _fake_ctx_output(*names: str) -> str:
+ return "\n".join(json.dumps({"Name": name, "DockerEndpoint": f"unix://{name}"}) for name in names)
@pytest.mark.parametrize(
"context_output, selected_context, console_output",
[
(
- json.dumps([_fake_ctx("default")]),
+ _fake_ctx_output("default"),
"default",
"[info]Using default as context",
),
- ("[]", "default", "[warning]Could not detect docker builder"),
+ ("\n", "default", "[warning]Could not detect docker builder"),
(
- json.dumps([_fake_ctx("a"), _fake_ctx("b")]),
+ _fake_ctx_output("a", "b"),
"a",
"[warning]Could not use any of the preferred docker contexts",
),
(
- json.dumps([_fake_ctx("a"), _fake_ctx("desktop-linux")]),
+ _fake_ctx_output("a", "desktop-linux"),
"desktop-linux",
"[info]Using desktop-linux as context",
),
(
- json.dumps([_fake_ctx("a"), _fake_ctx("default")]),
+ _fake_ctx_output("a", "default"),
"default",
"[info]Using default as context",
),
(
- json.dumps([_fake_ctx("a"), _fake_ctx("default"), _fake_ctx("desktop-linux")]),
+ _fake_ctx_output("a", "default", "desktop-linux"),
"desktop-linux",
"[info]Using desktop-linux as context",
),