Skip to content

Commit

Permalink
Parse 'docker context ls --format=json' correctly (#34711)
Browse files Browse the repository at this point in the history
(cherry picked from commit 1928498)
  • Loading branch information
uranusjr authored and ephraimbuddy committed Oct 5, 2023
1 parent ed36b28 commit f8fc3d6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
7 changes: 2 additions & 5 deletions dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,11 +826,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"
Expand Down
19 changes: 8 additions & 11 deletions dev/breeze/tests/test_docker_command_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
Expand Down

0 comments on commit f8fc3d6

Please sign in to comment.