package clientutils import ( "context" "testing" assertapi "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" ) func TestCreateOrUpdateConfigmap(t *testing.T) { assert := assertapi.New(t) cl := fake.NewClientBuilder().Build() ctx := context.Background() old := getConfigMap(map[string]string{ "foo": "foo1", "bar": "bar1", "other": "test", "empty": "", }) newCfg := getConfigMap(map[string]string{ "foo": "foo2", "bar": "", "other": "test", "empty": "", }) assert.NoError(cl.Create(ctx, old)) assert.NoError(CreateOrUpdateConfigmap(ctx, cl, newCfg)) updated := &corev1.ConfigMap{} assert.NoError(cl.Get(ctx, client.ObjectKeyFromObject(old), updated)) assert.Equal(map[string]string{ "foo": "foo2", "bar": "bar1", "other": "test", "empty": "", }, updated.Data) } func getConfigMap(data map[string]string) *corev1.ConfigMap { return &corev1.ConfigMap{ TypeMeta: metav1.TypeMeta{ Kind: "ConfigMap", APIVersion: "v1", }, ObjectMeta: metav1.ObjectMeta{ Name: "bsl-info", Namespace: "kube-public", }, Data: data, } }