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

Improve error messages for parse failures #732

Merged
merged 1 commit into from
Jun 29, 2024
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: 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
Loading