Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse 'docker context ls --format=json' correctly #34711

Merged
merged 1 commit into from
Oct 3, 2023
Merged
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
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 @@ -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"
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