MCPcopy Create free account
hub / github.com/flant/shell-operator / Test_PatchOperations

Function Test_PatchOperations

pkg/kube/object_patch/patch_test.go:84–551  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

82}
83
84func Test_PatchOperations(t *testing.T) {
85 const (
86 namespace = "default"
87 name = "testcm"
88 missingName = "missing-object"
89 configMap = `
90apiVersion: v1
91kind: ConfigMap
92metadata:
93 name: testcm
94data:
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 },

Callers

nothing calls this directly

Calls 13

ExecuteOperationMethod · 0.95
ExecuteOperationsMethod · 0.95
NewMergePatchOperationFunction · 0.85
WithIgnoreMissingObjectFunction · 0.85
ParseOperationsFunction · 0.85
NewJSONPatchOperationFunction · 0.85
NewObjectPatcherFunction · 0.85
fetchObjectFunction · 0.85
RunMethod · 0.45

Tested by

no test coverage detected