...
1 package clientutils
2
3 import (
4 "context"
5 "testing"
6
7 assertapi "github.com/stretchr/testify/assert"
8
9 corev1 "k8s.io/api/core/v1"
10 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11 "sigs.k8s.io/controller-runtime/pkg/client"
12 "sigs.k8s.io/controller-runtime/pkg/client/fake"
13 )
14
15 func TestCreateOrUpdateConfigmap(t *testing.T) {
16 assert := assertapi.New(t)
17 cl := fake.NewClientBuilder().Build()
18 ctx := context.Background()
19 old := getConfigMap(map[string]string{
20 "foo": "foo1",
21 "bar": "bar1",
22 "other": "test",
23 "empty": "",
24 })
25 newCfg := getConfigMap(map[string]string{
26 "foo": "foo2",
27 "bar": "",
28 "other": "test",
29 "empty": "",
30 })
31 assert.NoError(cl.Create(ctx, old))
32 assert.NoError(CreateOrUpdateConfigmap(ctx, cl, newCfg))
33 updated := &corev1.ConfigMap{}
34 assert.NoError(cl.Get(ctx, client.ObjectKeyFromObject(old), updated))
35 assert.Equal(map[string]string{
36 "foo": "foo2",
37 "bar": "bar1",
38 "other": "test",
39 "empty": "",
40 }, updated.Data)
41 }
42
43 func getConfigMap(data map[string]string) *corev1.ConfigMap {
44 return &corev1.ConfigMap{
45 TypeMeta: metav1.TypeMeta{
46 Kind: "ConfigMap",
47 APIVersion: "v1",
48 },
49 ObjectMeta: metav1.ObjectMeta{
50 Name: "bsl-info",
51 Namespace: "kube-public",
52 },
53 Data: data,
54 }
55 }
56
View as plain text