Skip to content

Commit 6a81980

Browse files
committed
fix(test)
1 parent 6a025c0 commit 6a81980

File tree

1 file changed

+89
-64
lines changed

1 file changed

+89
-64
lines changed

pkg/cluster/k8sres_test.go

Lines changed: 89 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,115 +2201,140 @@ func TestSidecars(t *testing.T) {
22012201
}
22022202

22032203
func TestGeneratePodDisruptionBudget(t *testing.T) {
2204+
testName := "Test PodDisruptionBudget spec generation"
2205+
2206+
hasName := func(pdbName string) func(cluster *Cluster, podDisruptionBudget *policyv1.PodDisruptionBudget) error {
2207+
return func(cluster *Cluster, podDisruptionBudget *policyv1.PodDisruptionBudget) error {
2208+
if pdbName != podDisruptionBudget.ObjectMeta.Name {
2209+
return fmt.Errorf("PodDisruptionBudget name is incorrect, got %s, expected %s",
2210+
podDisruptionBudget.ObjectMeta.Name, pdbName)
2211+
}
2212+
return nil
2213+
}
2214+
}
2215+
2216+
hasMinAvailable := func(expectedMinAvailable int) func(cluster *Cluster, podDisruptionBudget *policyv1.PodDisruptionBudget) error {
2217+
return func(cluster *Cluster, podDisruptionBudget *policyv1.PodDisruptionBudget) error {
2218+
actual := podDisruptionBudget.Spec.MinAvailable.IntVal
2219+
if actual != int32(expectedMinAvailable) {
2220+
return fmt.Errorf("PodDisruptionBudget MinAvailable is incorrect, got %d, expected %d",
2221+
actual, expectedMinAvailable)
2222+
}
2223+
return nil
2224+
}
2225+
}
2226+
2227+
testLabelsAndSelectors := func(cluster *Cluster, podDisruptionBudget *policyv1.PodDisruptionBudget) error {
2228+
if podDisruptionBudget.ObjectMeta.Namespace != "myapp" {
2229+
return fmt.Errorf("Object Namespace incorrect.")
2230+
}
2231+
if !reflect.DeepEqual(podDisruptionBudget.Labels, map[string]string{"team": "myapp", "cluster-name": "myapp-database"}) {
2232+
2233+
return fmt.Errorf("Labels incorrect.")
2234+
}
2235+
if !reflect.DeepEqual(podDisruptionBudget.Spec.Selector, &metav1.LabelSelector{
2236+
MatchLabels: map[string]string{"spilo-role": "master", "cluster-name": "myapp-database"}}) {
2237+
2238+
return fmt.Errorf("MatchLabels incorrect.")
2239+
}
2240+
2241+
return nil
2242+
}
2243+
2244+
testPodDisruptionBudgetOwnerReference := func(cluster *Cluster, podDisruptionBudget *policyv1.PodDisruptionBudget) error {
2245+
owner := podDisruptionBudget.ObjectMeta.OwnerReferences[0]
2246+
2247+
if owner.Name != cluster.Postgresql.ObjectMeta.Name {
2248+
return fmt.Errorf("Owner reference is incorrect, got %s, expected %s",
2249+
owner.Name, cluster.Postgresql.ObjectMeta.Name)
2250+
}
2251+
2252+
return nil
2253+
}
2254+
22042255
tests := []struct {
2205-
c *Cluster
2206-
out policyv1.PodDisruptionBudget
2256+
scenario string
2257+
spec *Cluster
2258+
check []func(cluster *Cluster, podDisruptionBudget *policyv1.PodDisruptionBudget) error
22072259
}{
2208-
// With multiple instances.
22092260
{
2210-
New(
2261+
scenario: "With multiple instances",
2262+
spec: New(
22112263
Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role"}, PDBNameFormat: "postgres-{cluster}-pdb"}},
22122264
k8sutil.KubernetesClient{},
22132265
acidv1.Postgresql{
22142266
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
22152267
Spec: acidv1.PostgresSpec{TeamID: "myapp", NumberOfInstances: 3}},
22162268
logger,
22172269
eventRecorder),
2218-
policyv1.PodDisruptionBudget{
2219-
ObjectMeta: metav1.ObjectMeta{
2220-
Name: "postgres-myapp-database-pdb",
2221-
Namespace: "myapp",
2222-
Labels: map[string]string{"team": "myapp", "cluster-name": "myapp-database"},
2223-
OwnerReferences: []metav1.OwnerReference{{Name: "myapp-database"}},
2224-
},
2225-
Spec: policyv1.PodDisruptionBudgetSpec{
2226-
MinAvailable: util.ToIntStr(1),
2227-
Selector: &metav1.LabelSelector{
2228-
MatchLabels: map[string]string{"spilo-role": "master", "cluster-name": "myapp-database"},
2229-
},
2230-
},
2270+
check: []func(cluster *Cluster, podDisruptionBudget *policyv1.PodDisruptionBudget) error{
2271+
testPodDisruptionBudgetOwnerReference,
2272+
hasName("postgres-myapp-database-pdb"),
2273+
hasMinAvailable(1),
2274+
testLabelsAndSelectors,
22312275
},
22322276
},
2233-
// With zero instances.
22342277
{
2235-
New(
2278+
scenario: "With zero instances",
2279+
spec: New(
22362280
Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role"}, PDBNameFormat: "postgres-{cluster}-pdb"}},
22372281
k8sutil.KubernetesClient{},
22382282
acidv1.Postgresql{
22392283
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
22402284
Spec: acidv1.PostgresSpec{TeamID: "myapp", NumberOfInstances: 0}},
22412285
logger,
22422286
eventRecorder),
2243-
policyv1.PodDisruptionBudget{
2244-
ObjectMeta: metav1.ObjectMeta{
2245-
Name: "postgres-myapp-database-pdb",
2246-
Namespace: "myapp",
2247-
Labels: map[string]string{"team": "myapp", "cluster-name": "myapp-database"},
2248-
OwnerReferences: []metav1.OwnerReference{{Name: "myapp-database"}},
2249-
},
2250-
Spec: policyv1.PodDisruptionBudgetSpec{
2251-
MinAvailable: util.ToIntStr(0),
2252-
Selector: &metav1.LabelSelector{
2253-
MatchLabels: map[string]string{"spilo-role": "master", "cluster-name": "myapp-database"},
2254-
},
2255-
},
2287+
check: []func(cluster *Cluster, podDisruptionBudget *policyv1.PodDisruptionBudget) error{
2288+
testPodDisruptionBudgetOwnerReference,
2289+
hasName("postgres-myapp-database-pdb"),
2290+
hasMinAvailable(0),
2291+
testLabelsAndSelectors,
22562292
},
22572293
},
2258-
// With PodDisruptionBudget disabled.
22592294
{
2260-
New(
2295+
scenario: "With PodDisruptionBudget disabled",
2296+
spec: New(
22612297
Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role"}, PDBNameFormat: "postgres-{cluster}-pdb", EnablePodDisruptionBudget: util.False()}},
22622298
k8sutil.KubernetesClient{},
22632299
acidv1.Postgresql{
22642300
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
22652301
Spec: acidv1.PostgresSpec{TeamID: "myapp", NumberOfInstances: 3}},
22662302
logger,
22672303
eventRecorder),
2268-
policyv1.PodDisruptionBudget{
2269-
ObjectMeta: metav1.ObjectMeta{
2270-
Name: "postgres-myapp-database-pdb",
2271-
Namespace: "myapp",
2272-
Labels: map[string]string{"team": "myapp", "cluster-name": "myapp-database"},
2273-
OwnerReferences: []metav1.OwnerReference{{Name: "myapp-database"}},
2274-
},
2275-
Spec: policyv1.PodDisruptionBudgetSpec{
2276-
MinAvailable: util.ToIntStr(0),
2277-
Selector: &metav1.LabelSelector{
2278-
MatchLabels: map[string]string{"spilo-role": "master", "cluster-name": "myapp-database"},
2279-
},
2280-
},
2304+
check: []func(cluster *Cluster, podDisruptionBudget *policyv1.PodDisruptionBudget) error{
2305+
testPodDisruptionBudgetOwnerReference,
2306+
hasName("postgres-myapp-database-pdb"),
2307+
hasMinAvailable(0),
2308+
testLabelsAndSelectors,
22812309
},
22822310
},
2283-
// With non-default PDBNameFormat and PodDisruptionBudget explicitly enabled.
22842311
{
2285-
New(
2312+
scenario: "With non-default PDBNameFormat and PodDisruptionBudget explicitly enabled",
2313+
spec: New(
22862314
Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role"}, PDBNameFormat: "postgres-{cluster}-databass-budget", EnablePodDisruptionBudget: util.True()}},
22872315
k8sutil.KubernetesClient{},
22882316
acidv1.Postgresql{
22892317
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
22902318
Spec: acidv1.PostgresSpec{TeamID: "myapp", NumberOfInstances: 3}},
22912319
logger,
22922320
eventRecorder),
2293-
policyv1.PodDisruptionBudget{
2294-
ObjectMeta: metav1.ObjectMeta{
2295-
Name: "postgres-myapp-database-databass-budget",
2296-
Namespace: "myapp",
2297-
Labels: map[string]string{"team": "myapp", "cluster-name": "myapp-database"},
2298-
},
2299-
Spec: policyv1.PodDisruptionBudgetSpec{
2300-
MinAvailable: util.ToIntStr(1),
2301-
Selector: &metav1.LabelSelector{
2302-
MatchLabels: map[string]string{"spilo-role": "master", "cluster-name": "myapp-database"},
2303-
},
2304-
},
2321+
check: []func(cluster *Cluster, podDisruptionBudget *policyv1.PodDisruptionBudget) error{
2322+
testPodDisruptionBudgetOwnerReference,
2323+
hasName("postgres-myapp-database-databass-budget"),
2324+
hasMinAvailable(1),
2325+
testLabelsAndSelectors,
23052326
},
23062327
},
23072328
}
23082329

23092330
for _, tt := range tests {
2310-
result := tt.c.generatePodDisruptionBudget()
2311-
if !reflect.DeepEqual(*result, tt.out) {
2312-
t.Errorf("Expected PodDisruptionBudget: %#v, got %#v", tt.out, *result)
2331+
result := tt.spec.generatePodDisruptionBudget()
2332+
for _, check := range tt.check {
2333+
err := check(tt.spec, result)
2334+
if err != nil {
2335+
t.Errorf("%s [%s]: PodDisruptionBudget spec is incorrect, %+v",
2336+
testName, tt.scenario, err)
2337+
}
23132338
}
23142339
}
23152340
}

0 commit comments

Comments
 (0)