Negative index regression test for json-patch (#122625)

* add testcase with negative index

* exercise successful negative index patching

* use different values for testing

Co-authored-by: Chris Bandy <bandy.chris@gmail.com>

---------

Co-authored-by: Chris Bandy <bandy.chris@gmail.com>
This commit is contained in:
Vandit Singh 2024-01-18 13:14:19 +05:30 committed by GitHub
parent a02a00ecd7
commit 83ff8a2f49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 0 deletions

View File

@ -244,6 +244,10 @@ func TestJSONPatch(t *testing.T) {
expectedError: "the server rejected our request due to an error in our request",
expectedErrorType: metav1.StatusReasonInvalid,
},
{
name: "valid-negative-index-patch",
patch: `[{"op": "test", "value": "foo", "path": "/metadata/finalizers/-1"}]`,
},
} {
p := &patcher{
patchType: types.JSONPatchType,
@ -253,6 +257,7 @@ func TestJSONPatch(t *testing.T) {
codec := codecs.LegacyCodec(examplev1.SchemeGroupVersion)
pod := &examplev1.Pod{}
pod.Name = "podA"
pod.ObjectMeta.Finalizers = []string{"foo"}
versionedJS, err := runtime.Encode(codec, pod)
if err != nil {
t.Errorf("%s: unexpected error: %v", test.name, err)

View File

@ -289,6 +289,26 @@ func TestJSONPatch(t *testing.T) {
fragment: `[ {"op": "add", "path": "/metadata/labels/foo", "value": "bar"} ]`,
expectErr: true,
},
{
obj: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Finalizers: []string{"foo", "bar", "test"},
},
},
fragment: `[ {"op": "replace", "path": "/metadata/finalizers/-1", "value": "baz"} ]`,
expected: &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Finalizers: []string{"foo", "bar", "baz"},
},
Spec: corev1.PodSpec{},
},
},
}
codec := runtime.NewCodec(scheme.DefaultJSONEncoder(),