@@ -1491,9 +1491,9 @@ func TestPodAffinity(t *testing.T) {
14911491func testDeploymentOwnerReference (cluster * Cluster , deployment * appsv1.Deployment ) error {
14921492 owner := deployment .ObjectMeta .OwnerReferences [0 ]
14931493
1494- if owner .Name != cluster .Statefulset .ObjectMeta .Name {
1495- return fmt .Errorf ("Ownere reference is incorrect, got %s, expected %s" ,
1496- owner .Name , cluster .Statefulset .ObjectMeta .Name )
1494+ if owner .Name != cluster .Postgresql .ObjectMeta .Name {
1495+ return fmt .Errorf ("Owner reference is incorrect, got %s, expected %s" ,
1496+ owner .Name , cluster .Postgresql .ObjectMeta .Name )
14971497 }
14981498
14991499 return nil
@@ -1502,9 +1502,9 @@ func testDeploymentOwnerReference(cluster *Cluster, deployment *appsv1.Deploymen
15021502func testServiceOwnerReference (cluster * Cluster , service * v1.Service , role PostgresRole ) error {
15031503 owner := service .ObjectMeta .OwnerReferences [0 ]
15041504
1505- if owner .Name != cluster .Statefulset .ObjectMeta .Name {
1506- return fmt .Errorf ("Ownere reference is incorrect, got %s, expected %s" ,
1507- owner .Name , cluster .Statefulset .ObjectMeta .Name )
1505+ if owner .Name != cluster .Postgresql .ObjectMeta .Name {
1506+ return fmt .Errorf ("Owner reference is incorrect, got %s, expected %s" ,
1507+ owner .Name , cluster .Postgresql .ObjectMeta .Name )
15081508 }
15091509
15101510 return nil
@@ -2201,112 +2201,140 @@ func TestSidecars(t *testing.T) {
22012201}
22022202
22032203func 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- },
2224- Spec : policyv1.PodDisruptionBudgetSpec {
2225- MinAvailable : util .ToIntStr (1 ),
2226- Selector : & metav1.LabelSelector {
2227- MatchLabels : map [string ]string {"spilo-role" : "master" , "cluster-name" : "myapp-database" },
2228- },
2229- },
2270+ check : []func (cluster * Cluster , podDisruptionBudget * policyv1.PodDisruptionBudget ) error {
2271+ testPodDisruptionBudgetOwnerReference ,
2272+ hasName ("postgres-myapp-database-pdb" ),
2273+ hasMinAvailable (1 ),
2274+ testLabelsAndSelectors ,
22302275 },
22312276 },
2232- // With zero instances.
22332277 {
2234- New (
2278+ scenario : "With zero instances" ,
2279+ spec : New (
22352280 Config {OpConfig : config.Config {Resources : config.Resources {ClusterNameLabel : "cluster-name" , PodRoleLabel : "spilo-role" }, PDBNameFormat : "postgres-{cluster}-pdb" }},
22362281 k8sutil.KubernetesClient {},
22372282 acidv1.Postgresql {
22382283 ObjectMeta : metav1.ObjectMeta {Name : "myapp-database" , Namespace : "myapp" },
22392284 Spec : acidv1.PostgresSpec {TeamID : "myapp" , NumberOfInstances : 0 }},
22402285 logger ,
22412286 eventRecorder ),
2242- policyv1.PodDisruptionBudget {
2243- ObjectMeta : metav1.ObjectMeta {
2244- Name : "postgres-myapp-database-pdb" ,
2245- Namespace : "myapp" ,
2246- Labels : map [string ]string {"team" : "myapp" , "cluster-name" : "myapp-database" },
2247- },
2248- Spec : policyv1.PodDisruptionBudgetSpec {
2249- MinAvailable : util .ToIntStr (0 ),
2250- Selector : & metav1.LabelSelector {
2251- MatchLabels : map [string ]string {"spilo-role" : "master" , "cluster-name" : "myapp-database" },
2252- },
2253- },
2287+ check : []func (cluster * Cluster , podDisruptionBudget * policyv1.PodDisruptionBudget ) error {
2288+ testPodDisruptionBudgetOwnerReference ,
2289+ hasName ("postgres-myapp-database-pdb" ),
2290+ hasMinAvailable (0 ),
2291+ testLabelsAndSelectors ,
22542292 },
22552293 },
2256- // With PodDisruptionBudget disabled.
22572294 {
2258- New (
2295+ scenario : "With PodDisruptionBudget disabled" ,
2296+ spec : New (
22592297 Config {OpConfig : config.Config {Resources : config.Resources {ClusterNameLabel : "cluster-name" , PodRoleLabel : "spilo-role" }, PDBNameFormat : "postgres-{cluster}-pdb" , EnablePodDisruptionBudget : util .False ()}},
22602298 k8sutil.KubernetesClient {},
22612299 acidv1.Postgresql {
22622300 ObjectMeta : metav1.ObjectMeta {Name : "myapp-database" , Namespace : "myapp" },
22632301 Spec : acidv1.PostgresSpec {TeamID : "myapp" , NumberOfInstances : 3 }},
22642302 logger ,
22652303 eventRecorder ),
2266- policyv1.PodDisruptionBudget {
2267- ObjectMeta : metav1.ObjectMeta {
2268- Name : "postgres-myapp-database-pdb" ,
2269- Namespace : "myapp" ,
2270- Labels : map [string ]string {"team" : "myapp" , "cluster-name" : "myapp-database" },
2271- },
2272- Spec : policyv1.PodDisruptionBudgetSpec {
2273- MinAvailable : util .ToIntStr (0 ),
2274- Selector : & metav1.LabelSelector {
2275- MatchLabels : map [string ]string {"spilo-role" : "master" , "cluster-name" : "myapp-database" },
2276- },
2277- },
2304+ check : []func (cluster * Cluster , podDisruptionBudget * policyv1.PodDisruptionBudget ) error {
2305+ testPodDisruptionBudgetOwnerReference ,
2306+ hasName ("postgres-myapp-database-pdb" ),
2307+ hasMinAvailable (0 ),
2308+ testLabelsAndSelectors ,
22782309 },
22792310 },
2280- // With non-default PDBNameFormat and PodDisruptionBudget explicitly enabled.
22812311 {
2282- New (
2312+ scenario : "With non-default PDBNameFormat and PodDisruptionBudget explicitly enabled" ,
2313+ spec : New (
22832314 Config {OpConfig : config.Config {Resources : config.Resources {ClusterNameLabel : "cluster-name" , PodRoleLabel : "spilo-role" }, PDBNameFormat : "postgres-{cluster}-databass-budget" , EnablePodDisruptionBudget : util .True ()}},
22842315 k8sutil.KubernetesClient {},
22852316 acidv1.Postgresql {
22862317 ObjectMeta : metav1.ObjectMeta {Name : "myapp-database" , Namespace : "myapp" },
22872318 Spec : acidv1.PostgresSpec {TeamID : "myapp" , NumberOfInstances : 3 }},
22882319 logger ,
22892320 eventRecorder ),
2290- policyv1.PodDisruptionBudget {
2291- ObjectMeta : metav1.ObjectMeta {
2292- Name : "postgres-myapp-database-databass-budget" ,
2293- Namespace : "myapp" ,
2294- Labels : map [string ]string {"team" : "myapp" , "cluster-name" : "myapp-database" },
2295- },
2296- Spec : policyv1.PodDisruptionBudgetSpec {
2297- MinAvailable : util .ToIntStr (1 ),
2298- Selector : & metav1.LabelSelector {
2299- MatchLabels : map [string ]string {"spilo-role" : "master" , "cluster-name" : "myapp-database" },
2300- },
2301- },
2321+ check : []func (cluster * Cluster , podDisruptionBudget * policyv1.PodDisruptionBudget ) error {
2322+ testPodDisruptionBudgetOwnerReference ,
2323+ hasName ("postgres-myapp-database-databass-budget" ),
2324+ hasMinAvailable (1 ),
2325+ testLabelsAndSelectors ,
23022326 },
23032327 },
23042328 }
23052329
23062330 for _ , tt := range tests {
2307- result := tt .c .generatePodDisruptionBudget ()
2308- if ! reflect .DeepEqual (* result , tt .out ) {
2309- 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+ }
23102338 }
23112339 }
23122340}
0 commit comments