[node operator] patching util functions

Signed-off-by: Malte Poll <mp@edgeless.systems>
This commit is contained in:
Malte Poll 2022-06-28 09:55:05 +02:00 committed by Malte Poll
parent 614447495d
commit 3932581f2a
6 changed files with 212 additions and 5 deletions

View file

@ -0,0 +1,59 @@
package patch
import (
"testing"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var labelsTestObject = corev1.Node{
TypeMeta: v1.TypeMeta{
Kind: "Node",
APIVersion: "v1",
},
ObjectMeta: v1.ObjectMeta{
ResourceVersion: "0",
},
}
func TestSetLabels(t *testing.T) {
testCases := map[string]struct {
oldLabels map[string]string
newLabels map[string]string
wantPatch []byte
}{
"empty patch only contains resource version": {
wantPatch: []byte(`{"metadata":{"resourceVersion":"0"}}`),
},
"patch on node without existing labels": {
newLabels: map[string]string{"key": "value"},
wantPatch: []byte(`{"metadata":{"labels":{"key":"value"},"resourceVersion":"0"}}`),
},
"patch on node with same existing labels": {
oldLabels: map[string]string{"key": "value"},
newLabels: map[string]string{"key": "value"},
wantPatch: []byte(`{"metadata":{"resourceVersion":"0"}}`),
},
"patch on node with same key but different value": {
oldLabels: map[string]string{"key": "oldvalue"},
newLabels: map[string]string{"key": "newvalue"},
wantPatch: []byte(`{"metadata":{"labels":{"key":"newvalue"},"resourceVersion":"0"}}`),
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
original := labelsTestObject.DeepCopy()
original.SetLabels(tc.oldLabels)
patched := original.DeepCopy()
patch := SetLabels(original, patched, tc.newLabels)
data, err := patch.Data(patched)
assert.NoError(err)
assert.Equal(tc.wantPatch, data)
})
}
}