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

uranusjr 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 19284981f8 Parse 'docker context ls --format=json' correctly (#34711)
19284981f8 is described below

commit 19284981f88e45dca4c4003837e3cead1723caf1
Author: Tzu-ping Chung <[email protected]>
AuthorDate: Tue Oct 3 17:42:50 2023 +0800

    Parse 'docker context ls --format=json' correctly (#34711)
---
 .../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 176d5067ab..c77289383a 100644
--- a/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
+++ b/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
@@ -828,11 +828,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 d16dada590..234904a668 100644
--- a/dev/breeze/tests/test_docker_command_utils.py
+++ b/dev/breeze/tests/test_docker_command_utils.py
@@ -195,39 +195,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",
         ),

Reply via email to