package cluster import ( "testing" acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1" "github.com/zalando/postgres-operator/pkg/util/config" "github.com/zalando/postgres-operator/pkg/util/k8sutil" appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func mockInstallLookupFunction(schema string, user string) error { return nil } func boolToPointer(value bool) *bool { return &value } func TestConnectionPoolerCreationAndDeletion(t *testing.T) { testName := "Test connection pooler creation" var cluster = New( Config{ OpConfig: config.Config{ ProtectedRoles: []string{"admin"}, Auth: config.Auth{ SuperUsername: superUserName, ReplicationUsername: replicationUserName, }, ConnectionPooler: config.ConnectionPooler{ ConnectionPoolerDefaultCPURequest: "100m", ConnectionPoolerDefaultCPULimit: "100m", ConnectionPoolerDefaultMemoryRequest: "100Mi", ConnectionPoolerDefaultMemoryLimit: "100Mi", }, }, }, k8sutil.NewMockKubernetesClient(), acidv1.Postgresql{}, logger) cluster.Statefulset = &appsv1.StatefulSet{ ObjectMeta: metav1.ObjectMeta{ Name: "test-sts", }, } cluster.Spec = acidv1.PostgresSpec{ ConnectionPooler: &acidv1.ConnectionPooler{}, } poolerResources, err := cluster.createConnectionPooler(mockInstallLookupFunction) if err != nil { t.Errorf("%s: Cannot create connection pooler, %s, %+v", testName, err, poolerResources) } if poolerResources.Deployment == nil { t.Errorf("%s: Connection pooler deployment is empty", testName) } if poolerResources.Service == nil { t.Errorf("%s: Connection pooler service is empty", testName) } err = cluster.deleteConnectionPooler() if err != nil { t.Errorf("%s: Cannot delete connection pooler, %s", testName, err) } } func TestNeedConnectionPooler(t *testing.T) { testName := "Test how connection pooler can be enabled" var cluster = New( Config{ OpConfig: config.Config{ ProtectedRoles: []string{"admin"}, Auth: config.Auth{ SuperUsername: superUserName, ReplicationUsername: replicationUserName, }, ConnectionPooler: config.ConnectionPooler{ ConnectionPoolerDefaultCPURequest: "100m", ConnectionPoolerDefaultCPULimit: "100m", ConnectionPoolerDefaultMemoryRequest: "100Mi", ConnectionPoolerDefaultMemoryLimit: "100Mi", }, }, }, k8sutil.NewMockKubernetesClient(), acidv1.Postgresql{}, logger) cluster.Spec = acidv1.PostgresSpec{ ConnectionPooler: &acidv1.ConnectionPooler{}, } if !cluster.needConnectionPooler() { t.Errorf("%s: Connection pooler is not enabled with full definition", testName) } cluster.Spec = acidv1.PostgresSpec{ EnableConnectionPooler: boolToPointer(true), } if !cluster.needConnectionPooler() { t.Errorf("%s: Connection pooler is not enabled with flag", testName) } cluster.Spec = acidv1.PostgresSpec{ EnableConnectionPooler: boolToPointer(false), ConnectionPooler: &acidv1.ConnectionPooler{}, } if cluster.needConnectionPooler() { t.Errorf("%s: Connection pooler is still enabled with flag being false", testName) } cluster.Spec = acidv1.PostgresSpec{ EnableConnectionPooler: boolToPointer(true), ConnectionPooler: &acidv1.ConnectionPooler{}, } if !cluster.needConnectionPooler() { t.Errorf("%s: Connection pooler is not enabled with flag and full", testName) } }