Add more tests
sync logic system users init needConnectionPool
This commit is contained in:
		
							parent
							
								
									619c543b61
								
							
						
					
					
						commit
						3e98832703
					
				|  | @ -9,6 +9,7 @@ import ( | |||
| 	acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1" | ||||
| 	"github.com/zalando/postgres-operator/pkg/spec" | ||||
| 	"github.com/zalando/postgres-operator/pkg/util/config" | ||||
| 	"github.com/zalando/postgres-operator/pkg/util/constants" | ||||
| 	"github.com/zalando/postgres-operator/pkg/util/k8sutil" | ||||
| 	"github.com/zalando/postgres-operator/pkg/util/teams" | ||||
| 	v1 "k8s.io/api/core/v1" | ||||
|  | @ -704,3 +705,20 @@ func TestServiceAnnotations(t *testing.T) { | |||
| 		}) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestInitSystemUsers(t *testing.T) { | ||||
| 	testName := "Test system users initialization" | ||||
| 
 | ||||
| 	// default cluster without connection pool
 | ||||
| 	cl.initSystemUsers() | ||||
| 	if _, exist := cl.systemUsers[constants.ConnectionPoolUserKeyName]; exist { | ||||
| 		t.Errorf("%s, connection pool user is present", testName) | ||||
| 	} | ||||
| 
 | ||||
| 	// cluster with connection pool
 | ||||
| 	cl.Spec.EnableConnectionPool = boolToPointer(true) | ||||
| 	cl.initSystemUsers() | ||||
| 	if _, exist := cl.systemUsers[constants.ConnectionPoolUserKeyName]; !exist { | ||||
| 		t.Errorf("%s, connection pool user is not present", testName) | ||||
| 	} | ||||
| } | ||||
|  |  | |||
|  | @ -104,4 +104,24 @@ func TestNeedConnPool(t *testing.T) { | |||
| 		t.Errorf("%s: Connection pool is not enabled with flag", | ||||
| 			testName) | ||||
| 	} | ||||
| 
 | ||||
| 	cluster.Spec = acidv1.PostgresSpec{ | ||||
| 		EnableConnectionPool: boolToPointer(false), | ||||
| 		ConnectionPool:       &acidv1.ConnectionPool{}, | ||||
| 	} | ||||
| 
 | ||||
| 	if cluster.needConnectionPool() { | ||||
| 		t.Errorf("%s: Connection pool is still enabled with flag being false", | ||||
| 			testName) | ||||
| 	} | ||||
| 
 | ||||
| 	cluster.Spec = acidv1.PostgresSpec{ | ||||
| 		EnableConnectionPool: boolToPointer(true), | ||||
| 		ConnectionPool:       &acidv1.ConnectionPool{}, | ||||
| 	} | ||||
| 
 | ||||
| 	if !cluster.needConnectionPool() { | ||||
| 		t.Errorf("%s: Connection pool is not enabled with flag and full", | ||||
| 			testName) | ||||
| 	} | ||||
| } | ||||
|  |  | |||
|  | @ -9,6 +9,7 @@ import ( | |||
| 	"github.com/zalando/postgres-operator/pkg/util/k8sutil" | ||||
| 
 | ||||
| 	appsv1 "k8s.io/api/apps/v1" | ||||
| 	v1 "k8s.io/api/core/v1" | ||||
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||
| ) | ||||
| 
 | ||||
|  | @ -41,6 +42,14 @@ func objectsAreSaved(cluster *Cluster, err error) error { | |||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| func objectsAreDeleted(cluster *Cluster, err error) error { | ||||
| 	if cluster.ConnectionPool != nil { | ||||
| 		return fmt.Errorf("Connection pool was not deleted") | ||||
| 	} | ||||
| 
 | ||||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| func TestConnPoolSynchronization(t *testing.T) { | ||||
| 	testName := "Test connection pool synchronization" | ||||
| 	var cluster = New( | ||||
|  | @ -72,6 +81,13 @@ func TestConnPoolSynchronization(t *testing.T) { | |||
| 	clusterMock := *cluster | ||||
| 	clusterMock.KubeClient = k8sutil.NewMockKubernetesClient() | ||||
| 
 | ||||
| 	clusterDirtyMock := *cluster | ||||
| 	clusterDirtyMock.KubeClient = k8sutil.NewMockKubernetesClient() | ||||
| 	clusterDirtyMock.ConnectionPool = &ConnectionPoolObjects{ | ||||
| 		Deployment: &appsv1.Deployment{}, | ||||
| 		Service:    &v1.Service{}, | ||||
| 	} | ||||
| 
 | ||||
| 	tests := []struct { | ||||
| 		subTest string | ||||
| 		oldSpec *acidv1.Postgresql | ||||
|  | @ -94,6 +110,43 @@ func TestConnPoolSynchronization(t *testing.T) { | |||
| 			cluster: &clusterMissingObjects, | ||||
| 			check:   objectsAreSaved, | ||||
| 		}, | ||||
| 		{ | ||||
| 			subTest: "create from scratch", | ||||
| 			oldSpec: &acidv1.Postgresql{ | ||||
| 				Spec: acidv1.PostgresSpec{}, | ||||
| 			}, | ||||
| 			newSpec: &acidv1.Postgresql{ | ||||
| 				Spec: acidv1.PostgresSpec{ | ||||
| 					ConnectionPool: &acidv1.ConnectionPool{}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			cluster: &clusterMissingObjects, | ||||
| 			check:   objectsAreSaved, | ||||
| 		}, | ||||
| 		{ | ||||
| 			subTest: "delete if not needed", | ||||
| 			oldSpec: &acidv1.Postgresql{ | ||||
| 				Spec: acidv1.PostgresSpec{ | ||||
| 					ConnectionPool: &acidv1.ConnectionPool{}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			newSpec: &acidv1.Postgresql{ | ||||
| 				Spec: acidv1.PostgresSpec{}, | ||||
| 			}, | ||||
| 			cluster: &clusterMock, | ||||
| 			check:   objectsAreDeleted, | ||||
| 		}, | ||||
| 		{ | ||||
| 			subTest: "cleanup if still there", | ||||
| 			oldSpec: &acidv1.Postgresql{ | ||||
| 				Spec: acidv1.PostgresSpec{}, | ||||
| 			}, | ||||
| 			newSpec: &acidv1.Postgresql{ | ||||
| 				Spec: acidv1.PostgresSpec{}, | ||||
| 			}, | ||||
| 			cluster: &clusterDirtyMock, | ||||
| 			check:   objectsAreDeleted, | ||||
| 		}, | ||||
| 		{ | ||||
| 			subTest: "update deployment", | ||||
| 			oldSpec: &acidv1.Postgresql{ | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue