Livio Amstutz 438daebdb9
fix: cherry pick fixes from master (#1339)
* fix: return api config in ApplicationView (#1330)


(cherry picked from commit 16a47c65421e97d2151a76b3272efa598041e0ce)

* fix tests
2021-02-23 17:05:47 +01:00

122 lines
3.5 KiB
Go

package database
import (
"errors"
"github.com/caos/orbos/mntr"
kubernetesmock "github.com/caos/orbos/pkg/kubernetes/mock"
databasemock "github.com/caos/zitadel/operator/zitadel/kinds/iam/zitadel/database/mock"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"testing"
)
func TestDatabase_Adapt(t *testing.T) {
dbClient := databasemock.NewMockClientInt(gomock.NewController(t))
k8sClient := kubernetesmock.NewMockClientInt(gomock.NewController(t))
host := "host"
port := "port"
users := []string{"test"}
monitor := mntr.Monitor{}
queried := map[string]interface{}{}
dbClient.EXPECT().GetConnectionInfo(monitor, k8sClient).Return(host, port, nil)
dbClient.EXPECT().ListUsers(monitor, k8sClient).Return([]string{"test"}, nil)
_, err := GetDatabaseInQueried(queried)
assert.Error(t, err)
query, err := AdaptFunc(monitor, dbClient)
assert.NoError(t, err)
ensure, err := query(k8sClient, queried)
assert.NoError(t, err)
assert.NoError(t, ensure(k8sClient))
current, err := GetDatabaseInQueried(queried)
assert.NoError(t, err)
assert.Equal(t, host, current.Host)
assert.Equal(t, port, current.Port)
assert.Equal(t, users, current.Users)
}
func TestDatabase_Adapt2(t *testing.T) {
dbClient := databasemock.NewMockClientInt(gomock.NewController(t))
k8sClient := kubernetesmock.NewMockClientInt(gomock.NewController(t))
host := "host2"
port := "port2"
users := []string{"test2"}
monitor := mntr.Monitor{}
queried := map[string]interface{}{}
dbClient.EXPECT().GetConnectionInfo(monitor, k8sClient).Return(host, port, nil)
dbClient.EXPECT().ListUsers(monitor, k8sClient).Return(users, nil)
_, err := GetDatabaseInQueried(queried)
assert.Error(t, err)
query, err := AdaptFunc(monitor, dbClient)
assert.NoError(t, err)
ensure, err := query(k8sClient, queried)
assert.NoError(t, err)
assert.NoError(t, ensure(k8sClient))
current, err := GetDatabaseInQueried(queried)
assert.NoError(t, err)
assert.Equal(t, host, current.Host)
assert.Equal(t, port, current.Port)
assert.Equal(t, users, current.Users)
}
func TestDatabase_AdaptFailConnection(t *testing.T) {
dbClient := databasemock.NewMockClientInt(gomock.NewController(t))
k8sClient := kubernetesmock.NewMockClientInt(gomock.NewController(t))
monitor := mntr.Monitor{}
queried := map[string]interface{}{}
dbClient.EXPECT().GetConnectionInfo(monitor, k8sClient).MinTimes(1).MaxTimes(1).Return("", "", errors.New("fail"))
dbClient.EXPECT().ListUsers(monitor, k8sClient).MinTimes(1).MaxTimes(1).Return([]string{"test"}, nil)
_, err := GetDatabaseInQueried(queried)
assert.Error(t, err)
query, err := AdaptFunc(monitor, dbClient)
assert.NoError(t, err)
ensure, err := query(k8sClient, queried)
assert.Error(t, err)
assert.Nil(t, ensure)
current, err := GetDatabaseInQueried(queried)
assert.Error(t, err)
assert.Nil(t, current)
}
func TestDatabase_AdaptFailUsers(t *testing.T) {
dbClient := databasemock.NewMockClientInt(gomock.NewController(t))
k8sClient := kubernetesmock.NewMockClientInt(gomock.NewController(t))
host := "host"
port := "port"
monitor := mntr.Monitor{}
queried := map[string]interface{}{}
dbClient.EXPECT().GetConnectionInfo(monitor, k8sClient).Return(host, port, nil)
dbClient.EXPECT().ListUsers(monitor, k8sClient).Return(nil, errors.New("fail"))
_, err := GetDatabaseInQueried(queried)
assert.Error(t, err)
query, err := AdaptFunc(monitor, dbClient)
assert.NoError(t, err)
ensure, err := query(k8sClient, queried)
assert.Error(t, err)
assert.Nil(t, ensure)
current, err := GetDatabaseInQueried(queried)
assert.Error(t, err)
assert.Nil(t, current)
}