Skip to content

Commit

Permalink
unittest with non default label
Browse files Browse the repository at this point in the history
  • Loading branch information
idanovinda committed Nov 27, 2024
1 parent b433066 commit 1410daa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
31 changes: 16 additions & 15 deletions pkg/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
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"}}}}}`
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")
Expand All @@ -55,7 +56,7 @@ var cl = New(
},
Resources: config.Resources{
DownscalerAnnotations: []string{"downscaler/*"},
PodLeaderLabelValue: "master",
PodLeaderLabelValue: leaderLabelValue,
},
ConnectionPooler: config.ConnectionPooler{
User: poolerUserName,
Expand Down Expand Up @@ -127,7 +128,7 @@ func TestCreate(t *testing.T) {
Labels: map[string]string{
"application": "spilo",
"cluster-name": clusterName,
"spilo-role": "master",
"spilo-role": leaderLabelValue,
},
},
}
Expand All @@ -148,7 +149,7 @@ func TestCreate(t *testing.T) {
DefaultMemoryRequest: "300Mi",
DefaultMemoryLimit: "300Mi",
PodRoleLabel: "spilo-role",
PodLeaderLabelValue: "master",
PodLeaderLabelValue: leaderLabelValue,
ResourceCheckInterval: time.Duration(3),
ResourceCheckTimeout: time.Duration(10),
},
Expand Down Expand Up @@ -665,7 +666,7 @@ func TestServiceAnnotations(t *testing.T) {
//MASTER
{
about: "Master with no annotations and EnableMasterLoadBalancer disabled on spec and OperatorConfig",
role: "master",
role: leaderLabelValue,
enableMasterLoadBalancerSpec: &disabled,
enableMasterLoadBalancerOC: false,
enableTeamIdClusterPrefix: false,
Expand All @@ -675,7 +676,7 @@ func TestServiceAnnotations(t *testing.T) {
},
{
about: "Master with no annotations and EnableMasterLoadBalancer enabled on spec",
role: "master",
role: leaderLabelValue,
enableMasterLoadBalancerSpec: &enabled,
enableMasterLoadBalancerOC: false,
enableTeamIdClusterPrefix: false,
Expand All @@ -688,7 +689,7 @@ func TestServiceAnnotations(t *testing.T) {
},
{
about: "Master with no annotations and EnableMasterLoadBalancer enabled only on operator config",
role: "master",
role: leaderLabelValue,
enableMasterLoadBalancerSpec: &disabled,
enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false,
Expand All @@ -698,7 +699,7 @@ func TestServiceAnnotations(t *testing.T) {
},
{
about: "Master with no annotations and EnableMasterLoadBalancer defined only on operator config",
role: "master",
role: leaderLabelValue,
enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false,
operatorAnnotations: make(map[string]string),
Expand All @@ -710,7 +711,7 @@ func TestServiceAnnotations(t *testing.T) {
},
{
about: "Master with cluster annotations and load balancer enabled",
role: "master",
role: leaderLabelValue,
enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false,
operatorAnnotations: make(map[string]string),
Expand All @@ -723,7 +724,7 @@ func TestServiceAnnotations(t *testing.T) {
},
{
about: "Master with cluster annotations and load balancer disabled",
role: "master",
role: leaderLabelValue,
enableMasterLoadBalancerSpec: &disabled,
enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false,
Expand All @@ -733,7 +734,7 @@ func TestServiceAnnotations(t *testing.T) {
},
{
about: "Master with operator annotations and load balancer enabled",
role: "master",
role: leaderLabelValue,
enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false,
operatorAnnotations: map[string]string{"foo": "bar"},
Expand All @@ -746,7 +747,7 @@ func TestServiceAnnotations(t *testing.T) {
},
{
about: "Master with operator annotations override default annotations",
role: "master",
role: leaderLabelValue,
enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false,
operatorAnnotations: map[string]string{
Expand All @@ -760,7 +761,7 @@ func TestServiceAnnotations(t *testing.T) {
},
{
about: "Master with cluster annotations override default annotations",
role: "master",
role: leaderLabelValue,
enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false,
operatorAnnotations: make(map[string]string),
Expand All @@ -774,7 +775,7 @@ func TestServiceAnnotations(t *testing.T) {
},
{
about: "Master with cluster annotations do not override external-dns annotations",
role: "master",
role: leaderLabelValue,
enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false,
operatorAnnotations: make(map[string]string),
Expand All @@ -788,7 +789,7 @@ func TestServiceAnnotations(t *testing.T) {
},
{
about: "Master with cluster name teamId prefix enabled",
role: "master",
role: leaderLabelValue,
enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: true,
serviceAnnotations: make(map[string]string),
Expand All @@ -800,7 +801,7 @@ func TestServiceAnnotations(t *testing.T) {
},
{
about: "Master with master service annotations override service annotations",
role: "master",
role: leaderLabelValue,
enableMasterLoadBalancerOC: true,
enableTeamIdClusterPrefix: false,
operatorAnnotations: make(map[string]string),
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/connection_pooler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func TestConnectionPoolerSync(t *testing.T) {
DefaultMemoryRequest: "300Mi",
DefaultMemoryLimit: "300Mi",
PodRoleLabel: "spilo-role",
PodLeaderLabelValue: "master",
PodLeaderLabelValue: leaderLabelValue,
},
},
}, client, pg, logger, eventRecorder)
Expand Down
16 changes: 8 additions & 8 deletions pkg/cluster/k8sres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2359,7 +2359,7 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
}
if !masterLabelSelectorDisabled &&
!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.")
}
Expand Down Expand Up @@ -2389,7 +2389,7 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
{
scenario: "With multiple instances",
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{},
acidv1.Postgresql{
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
Expand All @@ -2406,7 +2406,7 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
{
scenario: "With zero instances",
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{},
acidv1.Postgresql{
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
Expand All @@ -2423,7 +2423,7 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
{
scenario: "With PodDisruptionBudget disabled",
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{},
acidv1.Postgresql{
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
Expand All @@ -2440,7 +2440,7 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
{
scenario: "With non-default PDBNameFormat and PodDisruptionBudget explicitly enabled",
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{},
acidv1.Postgresql{
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
Expand All @@ -2457,7 +2457,7 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
{
scenario: "With PDBMasterLabelSelector disabled",
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{},
acidv1.Postgresql{
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
Expand All @@ -2474,7 +2474,7 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
{
scenario: "With OwnerReference enabled",
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{},
acidv1.Postgresql{
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
Expand Down Expand Up @@ -2550,7 +2550,7 @@ func TestGenerateService(t *testing.T) {
DefaultMemoryRequest: "0.7Gi",
MaxMemoryRequest: "1.0Gi",
DefaultMemoryLimit: "1.3Gi",
PodLeaderLabelValue: "master",
PodLeaderLabelValue: leaderLabelValue,
},
SidecarImages: map[string]string{
"deprecated-global-sidecar": "image:123",
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ func TestSyncStandbyClusterConfiguration(t *testing.T) {
podLabels := map[string]string{
"cluster-name": clusterName,
"application": applicationLabel,
"spilo-role": "master",
"spilo-role": leaderLabelValue,
}
mockPod.Labels = podLabels
client.PodsGetter.Pods(namespace).Create(context.TODO(), mockPod, metav1.CreateOptions{})
Expand Down

0 comments on commit 1410daa

Please sign in to comment.