mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-08-01 11:36:10 -04:00
[node operator] patching util functions
Signed-off-by: Malte Poll <mp@edgeless.systems>
This commit is contained in:
parent
614447495d
commit
3932581f2a
6 changed files with 212 additions and 5 deletions
|
@ -0,0 +1,99 @@
|
|||
package patch
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
var annotationsTestObject = corev1.Node{
|
||||
TypeMeta: v1.TypeMeta{
|
||||
Kind: "Node",
|
||||
APIVersion: "v1",
|
||||
},
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
ResourceVersion: "0",
|
||||
},
|
||||
}
|
||||
|
||||
func TestSetAnnotations(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
oldAnnotations map[string]string
|
||||
newAnnotations map[string]string
|
||||
wantPatch []byte
|
||||
}{
|
||||
"empty patch only contains resource version": {
|
||||
wantPatch: []byte(`{"metadata":{"resourceVersion":"0"}}`),
|
||||
},
|
||||
"patch on node without existing annotations": {
|
||||
newAnnotations: map[string]string{"key": "value"},
|
||||
wantPatch: []byte(`{"metadata":{"annotations":{"key":"value"},"resourceVersion":"0"}}`),
|
||||
},
|
||||
"patch on node with same existing annotations": {
|
||||
oldAnnotations: map[string]string{"key": "value"},
|
||||
newAnnotations: map[string]string{"key": "value"},
|
||||
wantPatch: []byte(`{"metadata":{"resourceVersion":"0"}}`),
|
||||
},
|
||||
"patch on node with same key but different value": {
|
||||
oldAnnotations: map[string]string{"key": "oldvalue"},
|
||||
newAnnotations: map[string]string{"key": "newvalue"},
|
||||
wantPatch: []byte(`{"metadata":{"annotations":{"key":"newvalue"},"resourceVersion":"0"}}`),
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
original := annotationsTestObject.DeepCopy()
|
||||
original.SetAnnotations(tc.oldAnnotations)
|
||||
patched := original.DeepCopy()
|
||||
patch := SetAnnotations(original, patched, tc.newAnnotations)
|
||||
data, err := patch.Data(patched)
|
||||
assert.NoError(err)
|
||||
assert.Equal(tc.wantPatch, data)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnsetAnnotations(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
oldAnnotations map[string]string
|
||||
annotationKeys []string
|
||||
wantPatch []byte
|
||||
}{
|
||||
"empty patch only contains resource version": {
|
||||
wantPatch: []byte(`{"metadata":{"resourceVersion":"0"}}`),
|
||||
},
|
||||
"patch on node without existing annotations": {
|
||||
annotationKeys: []string{"key"},
|
||||
wantPatch: []byte(`{"metadata":{"resourceVersion":"0"}}`),
|
||||
},
|
||||
"patch on node with existing annotations": {
|
||||
oldAnnotations: map[string]string{"key": "value"},
|
||||
annotationKeys: []string{"key"},
|
||||
wantPatch: []byte(`{"metadata":{"annotations":null,"resourceVersion":"0"}}`),
|
||||
},
|
||||
"patch on node with existing annotations delete one of multiple": {
|
||||
oldAnnotations: map[string]string{"key": "value", "otherkey": "othervalue"},
|
||||
annotationKeys: []string{"key"},
|
||||
wantPatch: []byte(`{"metadata":{"annotations":{"key":null},"resourceVersion":"0"}}`),
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
original := annotationsTestObject.DeepCopy()
|
||||
original.SetAnnotations(tc.oldAnnotations)
|
||||
patched := original.DeepCopy()
|
||||
patch := UnsetAnnotations(original, patched, tc.annotationKeys)
|
||||
data, err := patch.Data(patched)
|
||||
assert.NoError(err)
|
||||
assert.Equal(tc.wantPatch, data)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue