Skip to content

Commit

Permalink
Improve error messages for parse failures (#732)
Browse files Browse the repository at this point in the history
Improve command output for cases like parsing helm values that contain
invalid values.

#731
  • Loading branch information
allenporter authored Jun 29, 2024
1 parent 410d14e commit ca50383
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
7 changes: 6 additions & 1 deletion flux_local/kustomize.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ async def objects(
self, target_namespace: str | None = None
) -> list[dict[str, Any]]:
"""Run the kustomize command and return the result cluster objects as a list."""
return [doc async for doc in self._docs(target_namespace=target_namespace)]
try:
return [doc async for doc in self._docs(target_namespace=target_namespace)]
except yaml.YAMLError as err:
raise KustomizeException(
f"Unable to parse command output: {self._cmds}: {err}"
) from err

def skip_resources(self, kinds: list[str]) -> "Kustomize":
"""Skip resources kinds of the specified types."""
Expand Down
27 changes: 24 additions & 3 deletions tests/test_kustomize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from syrupy.assertion import SnapshotAssertion
import yaml

from flux_local import kustomize, exceptions, manifest
from flux_local import kustomize, exceptions, manifest, command

TESTDATA_DIR = Path("tests/testdata")

Expand Down Expand Up @@ -44,6 +44,28 @@ async def test_objects(path: Path, snapshot: SnapshotAssertion) -> None:
assert result == snapshot


INVALID_YAML = """
---
foo: !bar
"""


async def test_objects_failure() -> None:
"""Test loading yaml documents."""

class FakeTask(command.Task):
async def run(self, stdin: bytes | None = None) -> bytes:
"""Execute the task and return the result."""
return INVALID_YAML.encode()

cmd = kustomize.Kustomize([FakeTask()])
with pytest.raises(
exceptions.KustomizeException,
match=r"Unable to parse.*could not determine a constructor",
):
await cmd.objects()


@pytest.mark.parametrize(
"path",
[TESTDATA_DIR / "repo", (TESTDATA_DIR / "repo").absolute()],
Expand Down Expand Up @@ -80,8 +102,7 @@ async def test_validate_pass(path: Path) -> None:
async def test_validate_fail(path: Path) -> None:
"""Test applying policies to validate resources."""
cmd = kustomize.grep("kind=ConfigMap", path)
with pytest.raises(
exceptions.CommandException, match="fail: 1"):
with pytest.raises(exceptions.CommandException, match="fail: 1"):
await cmd.validate(TESTDATA_DIR / "policies/fail.yaml")


Expand Down

0 comments on commit ca50383

Please sign in to comment.