unittest with non default label

This commit is contained in:
inovindasari 2024-11-27 16:20:27 +01:00
parent b433066577
commit 1410daa1aa
4 changed files with 26 additions and 25 deletions

View File

@ -35,6 +35,7 @@ const (
adminUserName = "admin" adminUserName = "admin"
exampleSpiloConfig = `{"postgresql":{"bin_dir":"/usr/lib/postgresql/12/bin","parameters":{"autovacuum_analyze_scale_factor":"0.1"},"pg_hba":["hostssl all all 0.0.0.0/0 md5","host all all 0.0.0.0/0 md5"]},"bootstrap":{"initdb":[{"auth-host":"md5"},{"auth-local":"trust"},"data-checksums",{"encoding":"UTF8"},{"locale":"en_US.UTF-8"}],"dcs":{"ttl":30,"loop_wait":10,"retry_timeout":10,"maximum_lag_on_failover":33554432,"postgresql":{"parameters":{"max_connections":"100","max_locks_per_transaction":"64","max_worker_processes":"4"}}}}}` exampleSpiloConfig = `{"postgresql":{"bin_dir":"/usr/lib/postgresql/12/bin","parameters":{"autovacuum_analyze_scale_factor":"0.1"},"pg_hba":["hostssl all all 0.0.0.0/0 md5","host all all 0.0.0.0/0 md5"]},"bootstrap":{"initdb":[{"auth-host":"md5"},{"auth-local":"trust"},"data-checksums",{"encoding":"UTF8"},{"locale":"en_US.UTF-8"}],"dcs":{"ttl":30,"loop_wait":10,"retry_timeout":10,"maximum_lag_on_failover":33554432,"postgresql":{"parameters":{"max_connections":"100","max_locks_per_transaction":"64","max_worker_processes":"4"}}}}}`
spiloConfigDiff = `{"postgresql":{"bin_dir":"/usr/lib/postgresql/12/bin","parameters":{"autovacuum_analyze_scale_factor":"0.1"},"pg_hba":["hostssl all all 0.0.0.0/0 md5","host all all 0.0.0.0/0 md5"]},"bootstrap":{"initdb":[{"auth-host":"md5"},{"auth-local":"trust"},"data-checksums",{"encoding":"UTF8"},{"locale":"en_US.UTF-8"}],"dcs":{"loop_wait":10,"retry_timeout":10,"maximum_lag_on_failover":33554432,"postgresql":{"parameters":{"max_locks_per_transaction":"64","max_worker_processes":"4"}}}}}` spiloConfigDiff = `{"postgresql":{"bin_dir":"/usr/lib/postgresql/12/bin","parameters":{"autovacuum_analyze_scale_factor":"0.1"},"pg_hba":["hostssl all all 0.0.0.0/0 md5","host all all 0.0.0.0/0 md5"]},"bootstrap":{"initdb":[{"auth-host":"md5"},{"auth-local":"trust"},"data-checksums",{"encoding":"UTF8"},{"locale":"en_US.UTF-8"}],"dcs":{"loop_wait":10,"retry_timeout":10,"maximum_lag_on_failover":33554432,"postgresql":{"parameters":{"max_locks_per_transaction":"64","max_worker_processes":"4"}}}}}`
leaderLabelValue = "primary"
) )
var logger = logrus.New().WithField("test", "cluster") var logger = logrus.New().WithField("test", "cluster")
@ -55,7 +56,7 @@ var cl = New(
}, },
Resources: config.Resources{ Resources: config.Resources{
DownscalerAnnotations: []string{"downscaler/*"}, DownscalerAnnotations: []string{"downscaler/*"},
PodLeaderLabelValue: "master", PodLeaderLabelValue: leaderLabelValue,
}, },
ConnectionPooler: config.ConnectionPooler{ ConnectionPooler: config.ConnectionPooler{
User: poolerUserName, User: poolerUserName,
@ -127,7 +128,7 @@ func TestCreate(t *testing.T) {
Labels: map[string]string{ Labels: map[string]string{
"application": "spilo", "application": "spilo",
"cluster-name": clusterName, "cluster-name": clusterName,
"spilo-role": "master", "spilo-role": leaderLabelValue,
}, },
}, },
} }
@ -148,7 +149,7 @@ func TestCreate(t *testing.T) {
DefaultMemoryRequest: "300Mi", DefaultMemoryRequest: "300Mi",
DefaultMemoryLimit: "300Mi", DefaultMemoryLimit: "300Mi",
PodRoleLabel: "spilo-role", PodRoleLabel: "spilo-role",
PodLeaderLabelValue: "master", PodLeaderLabelValue: leaderLabelValue,
ResourceCheckInterval: time.Duration(3), ResourceCheckInterval: time.Duration(3),
ResourceCheckTimeout: time.Duration(10), ResourceCheckTimeout: time.Duration(10),
}, },
@ -665,7 +666,7 @@ func TestServiceAnnotations(t *testing.T) {
//MASTER //MASTER
{ {
about: "Master with no annotations and EnableMasterLoadBalancer disabled on spec and OperatorConfig", about: "Master with no annotations and EnableMasterLoadBalancer disabled on spec and OperatorConfig",
role: "master", role: leaderLabelValue,
enableMasterLoadBalancerSpec: &disabled, enableMasterLoadBalancerSpec: &disabled,
enableMasterLoadBalancerOC: false, enableMasterLoadBalancerOC: false,
enableTeamIdClusterPrefix: false, enableTeamIdClusterPrefix: false,
@ -675,7 +676,7 @@ func TestServiceAnnotations(t *testing.T) {
}, },
{ {
about: "Master with no annotations and EnableMasterLoadBalancer enabled on spec", about: "Master with no annotations and EnableMasterLoadBalancer enabled on spec",
role: "master", role: leaderLabelValue,
enableMasterLoadBalancerSpec: &enabled, enableMasterLoadBalancerSpec: &enabled,
enableMasterLoadBalancerOC: false, enableMasterLoadBalancerOC: false,
enableTeamIdClusterPrefix: false, enableTeamIdClusterPrefix: false,
@ -688,7 +689,7 @@ func TestServiceAnnotations(t *testing.T) {
}, },
{ {
about: "Master with no annotations and EnableMasterLoadBalancer enabled only on operator config", about: "Master with no annotations and EnableMasterLoadBalancer enabled only on operator config",
role: "master", role: leaderLabelValue,
enableMasterLoadBalancerSpec: &disabled, enableMasterLoadBalancerSpec: &disabled,
enableMasterLoadBalancerOC: true, enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false, enableTeamIdClusterPrefix: false,
@ -698,7 +699,7 @@ func TestServiceAnnotations(t *testing.T) {
}, },
{ {
about: "Master with no annotations and EnableMasterLoadBalancer defined only on operator config", about: "Master with no annotations and EnableMasterLoadBalancer defined only on operator config",
role: "master", role: leaderLabelValue,
enableMasterLoadBalancerOC: true, enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false, enableTeamIdClusterPrefix: false,
operatorAnnotations: make(map[string]string), operatorAnnotations: make(map[string]string),
@ -710,7 +711,7 @@ func TestServiceAnnotations(t *testing.T) {
}, },
{ {
about: "Master with cluster annotations and load balancer enabled", about: "Master with cluster annotations and load balancer enabled",
role: "master", role: leaderLabelValue,
enableMasterLoadBalancerOC: true, enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false, enableTeamIdClusterPrefix: false,
operatorAnnotations: make(map[string]string), operatorAnnotations: make(map[string]string),
@ -723,7 +724,7 @@ func TestServiceAnnotations(t *testing.T) {
}, },
{ {
about: "Master with cluster annotations and load balancer disabled", about: "Master with cluster annotations and load balancer disabled",
role: "master", role: leaderLabelValue,
enableMasterLoadBalancerSpec: &disabled, enableMasterLoadBalancerSpec: &disabled,
enableMasterLoadBalancerOC: true, enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false, enableTeamIdClusterPrefix: false,
@ -733,7 +734,7 @@ func TestServiceAnnotations(t *testing.T) {
}, },
{ {
about: "Master with operator annotations and load balancer enabled", about: "Master with operator annotations and load balancer enabled",
role: "master", role: leaderLabelValue,
enableMasterLoadBalancerOC: true, enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false, enableTeamIdClusterPrefix: false,
operatorAnnotations: map[string]string{"foo": "bar"}, operatorAnnotations: map[string]string{"foo": "bar"},
@ -746,7 +747,7 @@ func TestServiceAnnotations(t *testing.T) {
}, },
{ {
about: "Master with operator annotations override default annotations", about: "Master with operator annotations override default annotations",
role: "master", role: leaderLabelValue,
enableMasterLoadBalancerOC: true, enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false, enableTeamIdClusterPrefix: false,
operatorAnnotations: map[string]string{ operatorAnnotations: map[string]string{
@ -760,7 +761,7 @@ func TestServiceAnnotations(t *testing.T) {
}, },
{ {
about: "Master with cluster annotations override default annotations", about: "Master with cluster annotations override default annotations",
role: "master", role: leaderLabelValue,
enableMasterLoadBalancerOC: true, enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false, enableTeamIdClusterPrefix: false,
operatorAnnotations: make(map[string]string), operatorAnnotations: make(map[string]string),
@ -774,7 +775,7 @@ func TestServiceAnnotations(t *testing.T) {
}, },
{ {
about: "Master with cluster annotations do not override external-dns annotations", about: "Master with cluster annotations do not override external-dns annotations",
role: "master", role: leaderLabelValue,
enableMasterLoadBalancerOC: true, enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false, enableTeamIdClusterPrefix: false,
operatorAnnotations: make(map[string]string), operatorAnnotations: make(map[string]string),
@ -788,7 +789,7 @@ func TestServiceAnnotations(t *testing.T) {
}, },
{ {
about: "Master with cluster name teamId prefix enabled", about: "Master with cluster name teamId prefix enabled",
role: "master", role: leaderLabelValue,
enableMasterLoadBalancerOC: true, enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: true, enableTeamIdClusterPrefix: true,
serviceAnnotations: make(map[string]string), serviceAnnotations: make(map[string]string),
@ -800,7 +801,7 @@ func TestServiceAnnotations(t *testing.T) {
}, },
{ {
about: "Master with master service annotations override service annotations", about: "Master with master service annotations override service annotations",
role: "master", role: leaderLabelValue,
enableMasterLoadBalancerOC: true, enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false, enableTeamIdClusterPrefix: false,
operatorAnnotations: make(map[string]string), operatorAnnotations: make(map[string]string),

View File

@ -424,7 +424,7 @@ func TestConnectionPoolerSync(t *testing.T) {
DefaultMemoryRequest: "300Mi", DefaultMemoryRequest: "300Mi",
DefaultMemoryLimit: "300Mi", DefaultMemoryLimit: "300Mi",
PodRoleLabel: "spilo-role", PodRoleLabel: "spilo-role",
PodLeaderLabelValue: "master", PodLeaderLabelValue: leaderLabelValue,
}, },
}, },
}, client, pg, logger, eventRecorder) }, client, pg, logger, eventRecorder)

View File

@ -2359,7 +2359,7 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
} }
if !masterLabelSelectorDisabled && if !masterLabelSelectorDisabled &&
!reflect.DeepEqual(podDisruptionBudget.Spec.Selector, &metav1.LabelSelector{ !reflect.DeepEqual(podDisruptionBudget.Spec.Selector, &metav1.LabelSelector{
MatchLabels: map[string]string{"spilo-role": "master", "cluster-name": "myapp-database"}}) { MatchLabels: map[string]string{"spilo-role": leaderLabelValue, "cluster-name": "myapp-database"}}) {
return fmt.Errorf("MatchLabels incorrect.") return fmt.Errorf("MatchLabels incorrect.")
} }
@ -2389,7 +2389,7 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
{ {
scenario: "With multiple instances", scenario: "With multiple instances",
spec: New( spec: New(
Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role", PodLeaderLabelValue: "master"}, PDBNameFormat: "postgres-{cluster}-pdb"}}, Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role", PodLeaderLabelValue: leaderLabelValue}, PDBNameFormat: "postgres-{cluster}-pdb"}},
k8sutil.KubernetesClient{}, k8sutil.KubernetesClient{},
acidv1.Postgresql{ acidv1.Postgresql{
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"}, ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
@ -2406,7 +2406,7 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
{ {
scenario: "With zero instances", scenario: "With zero instances",
spec: New( spec: New(
Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role", PodLeaderLabelValue: "master"}, PDBNameFormat: "postgres-{cluster}-pdb"}}, Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role", PodLeaderLabelValue: leaderLabelValue}, PDBNameFormat: "postgres-{cluster}-pdb"}},
k8sutil.KubernetesClient{}, k8sutil.KubernetesClient{},
acidv1.Postgresql{ acidv1.Postgresql{
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"}, ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
@ -2423,7 +2423,7 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
{ {
scenario: "With PodDisruptionBudget disabled", scenario: "With PodDisruptionBudget disabled",
spec: New( spec: New(
Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role", PodLeaderLabelValue: "master"}, PDBNameFormat: "postgres-{cluster}-pdb", EnablePodDisruptionBudget: util.False()}}, Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role", PodLeaderLabelValue: leaderLabelValue}, PDBNameFormat: "postgres-{cluster}-pdb", EnablePodDisruptionBudget: util.False()}},
k8sutil.KubernetesClient{}, k8sutil.KubernetesClient{},
acidv1.Postgresql{ acidv1.Postgresql{
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"}, ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
@ -2440,7 +2440,7 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
{ {
scenario: "With non-default PDBNameFormat and PodDisruptionBudget explicitly enabled", scenario: "With non-default PDBNameFormat and PodDisruptionBudget explicitly enabled",
spec: New( spec: New(
Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role", PodLeaderLabelValue: "master"}, PDBNameFormat: "postgres-{cluster}-databass-budget", EnablePodDisruptionBudget: util.True()}}, Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role", PodLeaderLabelValue: leaderLabelValue}, PDBNameFormat: "postgres-{cluster}-databass-budget", EnablePodDisruptionBudget: util.True()}},
k8sutil.KubernetesClient{}, k8sutil.KubernetesClient{},
acidv1.Postgresql{ acidv1.Postgresql{
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"}, ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
@ -2457,7 +2457,7 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
{ {
scenario: "With PDBMasterLabelSelector disabled", scenario: "With PDBMasterLabelSelector disabled",
spec: New( spec: New(
Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role", PodLeaderLabelValue: "master"}, PDBNameFormat: "postgres-{cluster}-pdb", EnablePodDisruptionBudget: util.True(), PDBMasterLabelSelector: util.False()}}, Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role", PodLeaderLabelValue: leaderLabelValue}, PDBNameFormat: "postgres-{cluster}-pdb", EnablePodDisruptionBudget: util.True(), PDBMasterLabelSelector: util.False()}},
k8sutil.KubernetesClient{}, k8sutil.KubernetesClient{},
acidv1.Postgresql{ acidv1.Postgresql{
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"}, ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
@ -2474,7 +2474,7 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
{ {
scenario: "With OwnerReference enabled", scenario: "With OwnerReference enabled",
spec: New( spec: New(
Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role", PodLeaderLabelValue: "master", EnableOwnerReferences: util.True()}, PDBNameFormat: "postgres-{cluster}-pdb", EnablePodDisruptionBudget: util.True()}}, Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role", PodLeaderLabelValue: leaderLabelValue, EnableOwnerReferences: util.True()}, PDBNameFormat: "postgres-{cluster}-pdb", EnablePodDisruptionBudget: util.True()}},
k8sutil.KubernetesClient{}, k8sutil.KubernetesClient{},
acidv1.Postgresql{ acidv1.Postgresql{
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"}, ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
@ -2550,7 +2550,7 @@ func TestGenerateService(t *testing.T) {
DefaultMemoryRequest: "0.7Gi", DefaultMemoryRequest: "0.7Gi",
MaxMemoryRequest: "1.0Gi", MaxMemoryRequest: "1.0Gi",
DefaultMemoryLimit: "1.3Gi", DefaultMemoryLimit: "1.3Gi",
PodLeaderLabelValue: "master", PodLeaderLabelValue: leaderLabelValue,
}, },
SidecarImages: map[string]string{ SidecarImages: map[string]string{
"deprecated-global-sidecar": "image:123", "deprecated-global-sidecar": "image:123",

View File

@ -556,7 +556,7 @@ func TestSyncStandbyClusterConfiguration(t *testing.T) {
podLabels := map[string]string{ podLabels := map[string]string{
"cluster-name": clusterName, "cluster-name": clusterName,
"application": applicationLabel, "application": applicationLabel,
"spilo-role": "master", "spilo-role": leaderLabelValue,
} }
mockPod.Labels = podLabels mockPod.Labels = podLabels
client.PodsGetter.Pods(namespace).Create(context.TODO(), mockPod, metav1.CreateOptions{}) client.PodsGetter.Pods(namespace).Create(context.TODO(), mockPod, metav1.CreateOptions{})