From 8c91416d61a38ceb7eb4346d3ccc8b89c95450ea Mon Sep 17 00:00:00 2001 From: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com> Date: Tue, 17 Sep 2024 07:33:30 -0400 Subject: [PATCH] test unknown field case Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com> --- pkg/diff/diff_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/diff/diff_test.go b/pkg/diff/diff_test.go index 922db590e..9305beb5a 100644 --- a/pkg/diff/diff_test.go +++ b/pkg/diff/diff_test.go @@ -1152,6 +1152,29 @@ spec: assert.Equal(t, "200m", requestsAfter["cpu"]) } +func TestRemarshalResources_stripTypes_unknownField(t *testing.T) { + un := unstructured.Unstructured{Object: map[string]interface{}{ + "apiVersion": "v1", + "kind": "Pod", + "spec": map[string]interface{}{ + "containers": []interface{}{ + map[string]interface{}{ + "resources": map[string]interface{}{ + "requests": map[string]interface{}{ + "cpu": 0.2, + }, + }, + }, + }, + }, + "unknownField": "unknown", + }} + afterUn := remarshal(&un, applyOptions(diffOptionsForTest())) + requestsAfter := afterUn.Object["spec"].(map[string]interface{})["containers"].([]interface{})[0].(map[string]interface{})["resources"].(map[string]interface{})["requests"].(map[string]interface{}) + // Since there's an unknown field, we can't unmarshal into the Pod type, so the field doesn't get normalized to "200m". + assert.Equal(t, float64(0.2), requestsAfter["cpu"]) +} + func ExampleDiff() { expectedResource := unstructured.Unstructured{} if err := yaml.Unmarshal([]byte(`