Skip to content

Commit

Permalink
Harden item handling
Browse files Browse the repository at this point in the history
  • Loading branch information
allenporter committed Nov 10, 2024
1 parent 39bbdf0 commit 7e2c6c3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
6 changes: 4 additions & 2 deletions flux_local/tool/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,11 @@ def strip_resource_attributes(resource: dict[str, Any], strip_attributes: list[s
and (meta := templ.get("metadata"))
):
strip_attrs(meta, strip_attributes)
if resource["kind"] == "List" and (items := resource.get("items")):
if resource["kind"] == "List" and (items := resource.get("items")) and isinstance(items, list):
for item in items:
strip_attrs(item["metadata"], strip_attributes)
if not (item_meta := item.get("metadata")):
continue
strip_attrs(item_meta, strip_attributes)


class ObjectOutput(ResourceOutput):
Expand Down
30 changes: 29 additions & 1 deletion tests/tool/test_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def test_strip_list_metadata() -> None:
)


def test_strip_list_corner_cases() -> None:
def test_strip_list_null_items() -> None:
"""Test corner cases of handling metadata."""
resource = yaml.load(
"""apiVersion: v1
Expand All @@ -220,3 +220,31 @@ def test_strip_list_corner_cases() -> None:
items: null
"""
)


def test_strip_list_item_without_metdata() -> None:
"""Test corner cases of handling metadata."""
resource = yaml.load(
"""apiVersion: v1
kind: List
metadata:
resourceVersion: ''
selfLink: ''
items:
- kind: CronTab
""",
Loader=yaml.Loader,
)

strip_resource_attributes(resource, STRIP_ATTRIBUTES)
assert (
yaml.dump(resource, sort_keys=False)
== """apiVersion: v1
kind: List
metadata:
resourceVersion: ''
selfLink: ''
items:
- kind: CronTab
"""
)

0 comments on commit 7e2c6c3

Please sign in to comment.