mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-12-03 08:17:52 +00:00
fix(ci): make sufficient_storage JSON-safe and stabilize test matrix generation
Switch sufficient_storage CLI to support JSON output and update CI matrix generation to use JSON-only pipelines, preventing roles from collapsing into a single matrix entry.
This commit is contained in:
3 files changed
+92
-21
No files matched your search
@@ -1,7 +1,9 @@
|
||||
#!/usr/bin/env python3
|
||||
# cli/meta/applications/sufficient_storage/__main__.py
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
@@ -38,6 +40,14 @@ def parse_args() -> argparse.Namespace:
|
||||
help="Emit GitHub Actions warnings for missing configs/keys",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"-f",
|
||||
"--format",
|
||||
choices=("text", "json"),
|
||||
default="text",
|
||||
help="Output format (default: text). text = one role per line; json = JSON array.",
|
||||
)
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
@@ -50,9 +60,14 @@ def main() -> int:
|
||||
emit_warnings=args.warnings,
|
||||
)
|
||||
|
||||
# Print space-separated for easy CI consumption
|
||||
if matching:
|
||||
print(" ".join(matching))
|
||||
if args.format == "json":
|
||||
# Always output valid JSON
|
||||
print(json.dumps(matching, indent=2))
|
||||
return 0
|
||||
|
||||
# text: one role per line (robust for shell pipelines)
|
||||
for role in matching:
|
||||
print(role)
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
@@ -73,9 +73,7 @@ apps_json="$(
|
||||
)"
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# 3) CI storage filter (still JSON-safe)
|
||||
# NOTE: sufficient_storage currently outputs TEXT (one role per line).
|
||||
# We convert that text to JSON array safely without whitespace collapsing.
|
||||
# 3) CI storage filter (JSON-only)
|
||||
# ------------------------------------------------------------
|
||||
if [[ -n "${GITHUB_ACTIONS:-}" && -z "${ACT:-}" ]]; then
|
||||
required_storage="60GB"
|
||||
@@ -83,30 +81,24 @@ if [[ -n "${GITHUB_ACTIONS:-}" && -z "${ACT:-}" ]]; then
|
||||
# Extract roles from JSON to pass as args (safe: one per line -> bash array)
|
||||
mapfile -t roles < <(printf '%s\n' "${apps_json}" | jq -r '.[]')
|
||||
if [[ "${#roles[@]}" -gt 0 ]]; then
|
||||
# Warnings pass (best-effort)
|
||||
docker compose --profile ci exec -T infinito \
|
||||
"${PYTHON}" -m cli.meta.applications.sufficient_storage \
|
||||
--roles "${roles[@]}" \
|
||||
--required-storage "${required_storage}" \
|
||||
--warnings \
|
||||
--format json \
|
||||
>/dev/null || true
|
||||
|
||||
kept_txt="$(
|
||||
# Real filter (JSON output)
|
||||
apps_json="$(
|
||||
docker compose --profile ci exec -T infinito \
|
||||
"${PYTHON}" -m cli.meta.applications.sufficient_storage \
|
||||
--roles "${roles[@]}" \
|
||||
--required-storage "${required_storage}" \
|
||||
| sed -e 's/[[:space:]]\+$//' -e '/^$/d'
|
||||
--format json \
|
||||
| json_compact_array
|
||||
)"
|
||||
|
||||
if [[ -z "${kept_txt}" ]]; then
|
||||
apps_json='[]'
|
||||
else
|
||||
# Convert "one per line" to JSON array WITHOUT joining by spaces.
|
||||
apps_json="$(
|
||||
printf '%s\n' "${kept_txt}" \
|
||||
| jq -R -s -c 'split("\n") | map(select(length>0))'
|
||||
)"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import json
|
||||
import unittest
|
||||
from contextlib import redirect_stdout
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
class TestSufficientStorageCLI(unittest.TestCase):
|
||||
def test_prints_space_separated_matching_roles(self) -> None:
|
||||
def test_prints_one_per_line_matching_roles_by_default(self) -> None:
|
||||
from cli.meta.applications.sufficient_storage import __main__ as cli_main
|
||||
|
||||
fake_roles = ["web-app-a", "web-app-b"]
|
||||
@@ -36,9 +37,42 @@ class TestSufficientStorageCLI(unittest.TestCase):
|
||||
rc = cli_main.main()
|
||||
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(out.getvalue().strip(), "web-app-a web-app-b")
|
||||
self.assertEqual(out.getvalue().strip(), "web-app-a\nweb-app-b")
|
||||
|
||||
def test_prints_nothing_when_no_matches(self) -> None:
|
||||
def test_prints_json_array_when_format_json(self) -> None:
|
||||
from cli.meta.applications.sufficient_storage import __main__ as cli_main
|
||||
|
||||
fake_roles = ["web-app-a", "web-app-b"]
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
cli_main,
|
||||
"filter_roles_by_min_storage",
|
||||
return_value=fake_roles,
|
||||
),
|
||||
patch.object(
|
||||
cli_main.sys,
|
||||
"argv",
|
||||
[
|
||||
"prog",
|
||||
"--roles",
|
||||
"web-app-a",
|
||||
"web-app-b",
|
||||
"--required-storage",
|
||||
"10G",
|
||||
"--format",
|
||||
"json",
|
||||
],
|
||||
),
|
||||
):
|
||||
out = io.StringIO()
|
||||
with redirect_stdout(out):
|
||||
rc = cli_main.main()
|
||||
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(json.loads(out.getvalue()), fake_roles)
|
||||
|
||||
def test_prints_nothing_when_no_matches_default_text(self) -> None:
|
||||
from cli.meta.applications.sufficient_storage import __main__ as cli_main
|
||||
|
||||
with (
|
||||
@@ -60,6 +94,36 @@ class TestSufficientStorageCLI(unittest.TestCase):
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(out.getvalue(), "")
|
||||
|
||||
def test_prints_empty_json_array_when_no_matches_format_json(self) -> None:
|
||||
from cli.meta.applications.sufficient_storage import __main__ as cli_main
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
cli_main,
|
||||
"filter_roles_by_min_storage",
|
||||
return_value=[],
|
||||
),
|
||||
patch.object(
|
||||
cli_main.sys,
|
||||
"argv",
|
||||
[
|
||||
"prog",
|
||||
"--roles",
|
||||
"web-app-a",
|
||||
"--required-storage",
|
||||
"10G",
|
||||
"--format",
|
||||
"json",
|
||||
],
|
||||
),
|
||||
):
|
||||
out = io.StringIO()
|
||||
with redirect_stdout(out):
|
||||
rc = cli_main.main()
|
||||
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(json.loads(out.getvalue()), [])
|
||||
|
||||
def test_passes_warnings_flag_through(self) -> None:
|
||||
from cli.meta.applications.sufficient_storage import __main__ as cli_main
|
||||
|
||||
|
||||
Reference in new issue
Block a user