From 7e2c6c39adab33c1e283673a74cd8b756c1c00a6 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Sun, 10 Nov 2024 18:25:21 +0000 Subject: [PATCH] Harden item handling --- flux_local/tool/visitor.py | 6 ++++-- tests/tool/test_visitor.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/flux_local/tool/visitor.py b/flux_local/tool/visitor.py index 38f9b20b..009f1e09 100644 --- a/flux_local/tool/visitor.py +++ b/flux_local/tool/visitor.py @@ -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): diff --git a/tests/tool/test_visitor.py b/tests/tool/test_visitor.py index a295a378..81cbc22a 100644 --- a/tests/tool/test_visitor.py +++ b/tests/tool/test_visitor.py @@ -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 @@ -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 +""" + )