From b06d491204b10cd3c75d12807ecc772a6bdcd324 Mon Sep 17 00:00:00 2001 From: Jian Li Date: Wed, 24 Nov 2021 15:53:03 +0800 Subject: [PATCH] add unit tests for TestSetNestedStringSlice, TestSetNestedSlice etc. --- .../apis/meta/v1/unstructured/helpers_test.go | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers_test.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers_test.go index eb1cd65795b..c6c0358d018 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers_test.go @@ -165,3 +165,63 @@ func TestNestedFieldCopy(t *testing.T) { func TestCacheableObject(t *testing.T) { runtimetesting.CacheableObjectTest(t, UnstructuredJSONScheme) } + +func TestSetNestedStringSlice(t *testing.T) { + obj := map[string]interface{}{ + "x": map[string]interface{}{ + "y": 1, + "a": "foo", + }, + } + + err := SetNestedStringSlice(obj, []string{"bar"}, "x", "z") + assert.NoError(t, err) + assert.Len(t, obj["x"], 3) + assert.Len(t, obj["x"].(map[string]interface{})["z"], 1) + assert.Equal(t, obj["x"].(map[string]interface{})["z"].([]interface{})[0], "bar") +} + +func TestSetNestedSlice(t *testing.T) { + obj := map[string]interface{}{ + "x": map[string]interface{}{ + "y": 1, + "a": "foo", + }, + } + + err := SetNestedSlice(obj, []interface{}{"bar"}, "x", "z") + assert.NoError(t, err) + assert.Len(t, obj["x"], 3) + assert.Len(t, obj["x"].(map[string]interface{})["z"], 1) + assert.Equal(t, obj["x"].(map[string]interface{})["z"].([]interface{})[0], "bar") +} + +func TestSetNestedStringMap(t *testing.T) { + obj := map[string]interface{}{ + "x": map[string]interface{}{ + "y": 1, + "a": "foo", + }, + } + + err := SetNestedStringMap(obj, map[string]string{"b": "bar"}, "x", "z") + assert.NoError(t, err) + assert.Len(t, obj["x"], 3) + assert.Len(t, obj["x"].(map[string]interface{})["z"], 1) + assert.Equal(t, obj["x"].(map[string]interface{})["z"].(map[string]interface{})["b"], "bar") +} + +func TestSetNestedMap(t *testing.T) { + obj := map[string]interface{}{ + "x": map[string]interface{}{ + "y": 1, + "a": "foo", + }, + } + + err := SetNestedMap(obj, map[string]interface{}{"b": "bar"}, "x", "z") + assert.NoError(t, err) + assert.Len(t, obj["x"], 3) + assert.Len(t, obj["x"].(map[string]interface{})["z"], 1) + assert.Equal(t, obj["x"].(map[string]interface{})["z"].(map[string]interface{})["b"], "bar") +}