(t *testing.T)
| 82 | } |
| 83 | |
| 84 | func Test_PatchOperations(t *testing.T) { |
| 85 | const ( |
| 86 | namespace = "default" |
| 87 | name = "testcm" |
| 88 | missingName = "missing-object" |
| 89 | configMap = ` |
| 90 | apiVersion: v1 |
| 91 | kind: ConfigMap |
| 92 | metadata: |
| 93 | name: testcm |
| 94 | data: |
| 95 | foo: "bar" |
| 96 | ` |
| 97 | newField = "baz" |
| 98 | newValue = "quux" |
| 99 | shouldNotAdd = false |
| 100 | shouldAdd = true |
| 101 | shouldNotBeError = false |
| 102 | shouldBeError = true |
| 103 | ) |
| 104 | |
| 105 | // Filter func to add a new field. |
| 106 | filter := func(u *unstructured.Unstructured) (*unstructured.Unstructured, error) { |
| 107 | res := u.DeepCopy() |
| 108 | data := res.Object["data"].(map[string]any) |
| 109 | data[newField] = newValue |
| 110 | res.Object["data"] = data |
| 111 | return res, nil |
| 112 | } |
| 113 | |
| 114 | tests := []struct { |
| 115 | name string |
| 116 | fn func(patcher *ObjectPatcher) error |
| 117 | expectAdd bool |
| 118 | expectError bool |
| 119 | }{ |
| 120 | { |
| 121 | "merge patch", |
| 122 | func(patcher *ObjectPatcher) error { |
| 123 | return patcher.ExecuteOperation(NewMergePatchOperation( |
| 124 | fmt.Sprintf(`{"data":{"%s":"%s"}}`, newField, newValue), |
| 125 | "v1", "ConfigMap", namespace, name, |
| 126 | )) |
| 127 | }, |
| 128 | shouldAdd, |
| 129 | shouldNotBeError, |
| 130 | }, |
| 131 | { |
| 132 | "merge patch a missing object", |
| 133 | func(patcher *ObjectPatcher) error { |
| 134 | return patcher.ExecuteOperation(NewMergePatchOperation( |
| 135 | fmt.Sprintf(`{"data":{"%s":"%s"}}`, newField, newValue), |
| 136 | "v1", "ConfigMap", namespace, missingName, |
| 137 | )) |
| 138 | }, |
| 139 | shouldNotAdd, |
| 140 | shouldBeError, |
| 141 | }, |
nothing calls this directly
no test coverage detected