fix: 81134: fix unsafe json for ReleaseControllerRevision (#104049)

* fix: 81134: fix unsafe json for ReleaseControllerRevision

1. Ensures that ReleaseControllerRevision returns a proper json by
marshalling an object into bytes. Otherwise, it returns an error.

2. Also, refactors the code to commonize the merge type
   GenerateDeleteOwnerRefStrategicMergeBytes that returns a byte and is
   used across ReleasePod, ReleaseControllerRevison
   ReleaseReplicaSet.

* Move GeneratePatchBytesForDelete to controller_ref_manager
This commit is contained in:
Manu Gupta
2021-11-05 06:33:52 -07:00
committed by GitHub
parent 47041cd2a2
commit 79a51090f9
7 changed files with 122 additions and 72 deletions

View File

@@ -18,6 +18,7 @@ package controller
import (
"context"
"reflect"
"strings"
"testing"
@@ -202,10 +203,64 @@ func TestClaimPods(t *testing.T) {
}
for _, f := range test.manager.finalizers {
if !strings.Contains(patch, f) {
t.Errorf("Patch doesn't contain finalizer %q", f)
t.Errorf("Patch doesn't contain finalizer %s, %q", patch, f)
}
}
}
})
}
}
func TestGeneratePatchBytesForDelete(t *testing.T) {
tests := []struct {
name string
ownerUID []types.UID
dependentUID types.UID
finalizers []string
want []byte
}{
{
name: "check the structure of patch bytes",
ownerUID: []types.UID{"ss1"},
dependentUID: "ss2",
finalizers: []string{},
want: []byte(`{"metadata":{"uid":"ss2","ownerReferences":[{"$patch":"delete","uid":"ss1"}]}}`),
},
{
name: "check if parent uid is escaped",
ownerUID: []types.UID{`ss1"hello`},
dependentUID: "ss2",
finalizers: []string{},
want: []byte(`{"metadata":{"uid":"ss2","ownerReferences":[{"$patch":"delete","uid":"ss1\"hello"}]}}`),
},
{
name: "check if revision uid uid is escaped",
ownerUID: []types.UID{`ss1`},
dependentUID: `ss2"hello`,
finalizers: []string{},
want: []byte(`{"metadata":{"uid":"ss2\"hello","ownerReferences":[{"$patch":"delete","uid":"ss1"}]}}`),
},
{
name: "check the structure of patch bytes with multiple owners",
ownerUID: []types.UID{"ss1", "ss2"},
dependentUID: "ss2",
finalizers: []string{},
want: []byte(`{"metadata":{"uid":"ss2","ownerReferences":[{"$patch":"delete","uid":"ss1"},{"$patch":"delete","uid":"ss2"}]}}`),
},
{
name: "check the structure of patch bytes with a finalizer and multiple owners",
ownerUID: []types.UID{"ss1", "ss2"},
dependentUID: "ss2",
finalizers: []string{"f1"},
want: []byte(`{"metadata":{"uid":"ss2","ownerReferences":[{"$patch":"delete","uid":"ss1"},{"$patch":"delete","uid":"ss2"}],"$deleteFromPrimitiveList/finalizers":["f1"]}}`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := GenerateDeleteOwnerRefStrategicMergeBytes(tt.dependentUID, tt.ownerUID, tt.finalizers...)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("generatePatchBytesForDelete() got = %s, want %s", got, tt.want)
}
})
}
}