@@ -2354,3 +2354,292 @@ func TestUpdatePITRResources(t *testing.T) {
23542354 })
23552355 }
23562356}
2357+
2358+ func TestUpdate_LifecycleBlocksDuringStopping (t * testing.T ) {
2359+ clientSet := fake .NewSimpleClientset ()
2360+ acidClientSet := fakeacidv1 .NewSimpleClientset ()
2361+
2362+ client := k8sutil.KubernetesClient {
2363+ DeploymentsGetter : clientSet .AppsV1 (),
2364+ PostgresqlsGetter : acidClientSet .AcidV1 (),
2365+ StatefulSetsGetter : clientSet .AppsV1 (),
2366+ ServicesGetter : clientSet .CoreV1 (),
2367+ SecretsGetter : clientSet .CoreV1 (),
2368+ ConfigMapsGetter : clientSet .CoreV1 (),
2369+ PodsGetter : clientSet .CoreV1 (),
2370+ EndpointsGetter : clientSet .CoreV1 (),
2371+ }
2372+
2373+ pg := acidv1.Postgresql {
2374+ ObjectMeta : metav1.ObjectMeta {
2375+ Name : "test-cluster" ,
2376+ Namespace : "default" ,
2377+ },
2378+ Spec : acidv1.PostgresSpec {
2379+ TeamID : "test-team" ,
2380+ NumberOfInstances : 3 ,
2381+ Volume : acidv1.Volume {Size : "1Gi" },
2382+ },
2383+ Status : acidv1.PostgresStatus {
2384+ PostgresClusterStatus : "Stopping" ,
2385+ },
2386+ }
2387+
2388+ cluster := New (
2389+ Config {
2390+ OpConfig : config.Config {
2391+ PodManagementPolicy : "ordered_ready" ,
2392+ },
2393+ }, client , pg , logger , eventRecorder )
2394+
2395+ cluster .Name = "test-cluster"
2396+ cluster .Namespace = "default"
2397+
2398+ oldSpec := pg .DeepCopy ()
2399+ newSpec := pg .DeepCopy ()
2400+ newSpec .Spec .NumberOfInstances = 5
2401+
2402+ err := cluster .Update (oldSpec , newSpec )
2403+
2404+ assert .Error (t , err )
2405+ assert .Contains (t , err .Error (), "cannot update cluster while it is stopping" )
2406+ }
2407+
2408+ func TestUpdate_LifecycleBlocksWhenStoppedWithPhase (t * testing.T ) {
2409+ clientSet := fake .NewSimpleClientset ()
2410+ acidClientSet := fakeacidv1 .NewSimpleClientset ()
2411+
2412+ client := k8sutil.KubernetesClient {
2413+ DeploymentsGetter : clientSet .AppsV1 (),
2414+ PostgresqlsGetter : acidClientSet .AcidV1 (),
2415+ StatefulSetsGetter : clientSet .AppsV1 (),
2416+ ServicesGetter : clientSet .CoreV1 (),
2417+ SecretsGetter : clientSet .CoreV1 (),
2418+ ConfigMapsGetter : clientSet .CoreV1 (),
2419+ PodsGetter : clientSet .CoreV1 (),
2420+ EndpointsGetter : clientSet .CoreV1 (),
2421+ }
2422+
2423+ pg := acidv1.Postgresql {
2424+ ObjectMeta : metav1.ObjectMeta {
2425+ Name : "test-cluster" ,
2426+ Namespace : "default" ,
2427+ },
2428+ Spec : acidv1.PostgresSpec {
2429+ TeamID : "test-team" ,
2430+ NumberOfInstances : 0 ,
2431+ Lifecycle : & acidv1.LifecycleSpec {Phase : "stopped" },
2432+ Volume : acidv1.Volume {Size : "1Gi" },
2433+ },
2434+ Status : acidv1.PostgresStatus {
2435+ PostgresClusterStatus : "Stopped" ,
2436+ PreviousNumberOfInstances : 3 ,
2437+ },
2438+ }
2439+
2440+ cluster := New (
2441+ Config {
2442+ OpConfig : config.Config {
2443+ PodManagementPolicy : "ordered_ready" ,
2444+ },
2445+ }, client , pg , logger , eventRecorder )
2446+
2447+ cluster .Name = "test-cluster"
2448+ cluster .Namespace = "default"
2449+
2450+ oldSpec := pg .DeepCopy ()
2451+ newSpec := pg .DeepCopy ()
2452+ newSpec .Spec .NumberOfInstances = 5
2453+
2454+ err := cluster .Update (oldSpec , newSpec )
2455+
2456+ assert .Error (t , err )
2457+ assert .Contains (t , err .Error (), "cannot update cluster while stopped" )
2458+ }
2459+
2460+ func TestUpdate_LifecycleAllowsWakeUp (t * testing.T ) {
2461+ clientSet := fake .NewSimpleClientset ()
2462+ acidClientSet := fakeacidv1 .NewSimpleClientset ()
2463+
2464+ updateCalled := false
2465+ statusUpdateCalled := false
2466+
2467+ acidClientSet .PrependReactor ("update" , "postgresqls" , func (action k8stesting.Action ) (bool , runtime.Object , error ) {
2468+ updateAction := action .(k8stesting.UpdateAction )
2469+ pg := updateAction .GetObject ().(* acidv1.Postgresql )
2470+ updateCalled = true
2471+ return true , pg , nil
2472+ })
2473+ acidClientSet .PrependReactor ("update" , "postgresqls" , func (action k8stesting.Action ) (bool , runtime.Object , error ) {
2474+ if action .GetSubresource () == "status" {
2475+ statusUpdateCalled = true
2476+ updateAction := action .(k8stesting.UpdateAction )
2477+ pg := updateAction .GetObject ().(* acidv1.Postgresql )
2478+ return true , pg , nil
2479+ }
2480+ return false , nil , nil
2481+ })
2482+
2483+ client := k8sutil.KubernetesClient {
2484+ DeploymentsGetter : clientSet .AppsV1 (),
2485+ PostgresqlsGetter : acidClientSet .AcidV1 (),
2486+ StatefulSetsGetter : clientSet .AppsV1 (),
2487+ ServicesGetter : clientSet .CoreV1 (),
2488+ SecretsGetter : clientSet .CoreV1 (),
2489+ ConfigMapsGetter : clientSet .CoreV1 (),
2490+ PodsGetter : clientSet .CoreV1 (),
2491+ EndpointsGetter : clientSet .CoreV1 (),
2492+ }
2493+
2494+ pg := acidv1.Postgresql {
2495+ ObjectMeta : metav1.ObjectMeta {
2496+ Name : "test-cluster" ,
2497+ Namespace : "default" ,
2498+ },
2499+ Spec : acidv1.PostgresSpec {
2500+ TeamID : "test-team" ,
2501+ NumberOfInstances : 0 ,
2502+ Volume : acidv1.Volume {Size : "1Gi" },
2503+ },
2504+ Status : acidv1.PostgresStatus {
2505+ PostgresClusterStatus : "Stopped" ,
2506+ PreviousNumberOfInstances : 3 ,
2507+ },
2508+ }
2509+
2510+ cluster := New (
2511+ Config {
2512+ OpConfig : config.Config {
2513+ PodManagementPolicy : "ordered_ready" ,
2514+ },
2515+ }, client , pg , logger , eventRecorder )
2516+
2517+ cluster .Name = "test-cluster"
2518+ cluster .Namespace = "default"
2519+
2520+ oldSpec := pg .DeepCopy ()
2521+ newSpec := pg .DeepCopy ()
2522+
2523+ err := cluster .Update (oldSpec , newSpec )
2524+
2525+ assert .NoError (t , err )
2526+ assert .True (t , updateCalled , "Update should have been called for wake-up" )
2527+ assert .True (t , statusUpdateCalled , "Status update should have been called for wake-up" )
2528+ }
2529+
2530+ func TestUpdate_LifecycleInitiatesHibernate (t * testing.T ) {
2531+ clientSet := fake .NewSimpleClientset ()
2532+ acidClientSet := fakeacidv1 .NewSimpleClientset ()
2533+
2534+ updateCalled := false
2535+ statusUpdateCalled := false
2536+
2537+ acidClientSet .PrependReactor ("update" , "postgresqls" , func (action k8stesting.Action ) (bool , runtime.Object , error ) {
2538+ updateAction := action .(k8stesting.UpdateAction )
2539+ pg := updateAction .GetObject ().(* acidv1.Postgresql )
2540+ updateCalled = true
2541+ return true , pg , nil
2542+ })
2543+ acidClientSet .PrependReactor ("update" , "postgresqls" , func (action k8stesting.Action ) (bool , runtime.Object , error ) {
2544+ if action .GetSubresource () == "status" {
2545+ statusUpdateCalled = true
2546+ updateAction := action .(k8stesting.UpdateAction )
2547+ pg := updateAction .GetObject ().(* acidv1.Postgresql )
2548+ return true , pg , nil
2549+ }
2550+ return false , nil , nil
2551+ })
2552+
2553+ client := k8sutil.KubernetesClient {
2554+ DeploymentsGetter : clientSet .AppsV1 (),
2555+ PostgresqlsGetter : acidClientSet .AcidV1 (),
2556+ StatefulSetsGetter : clientSet .AppsV1 (),
2557+ ServicesGetter : clientSet .CoreV1 (),
2558+ SecretsGetter : clientSet .CoreV1 (),
2559+ ConfigMapsGetter : clientSet .CoreV1 (),
2560+ PodsGetter : clientSet .CoreV1 (),
2561+ EndpointsGetter : clientSet .CoreV1 (),
2562+ }
2563+
2564+ pg := acidv1.Postgresql {
2565+ ObjectMeta : metav1.ObjectMeta {
2566+ Name : "test-cluster" ,
2567+ Namespace : "default" ,
2568+ },
2569+ Spec : acidv1.PostgresSpec {
2570+ TeamID : "test-team" ,
2571+ NumberOfInstances : 3 ,
2572+ Volume : acidv1.Volume {Size : "1Gi" },
2573+ },
2574+ Status : acidv1.PostgresStatus {
2575+ PostgresClusterStatus : "Running" ,
2576+ },
2577+ }
2578+
2579+ cluster := New (
2580+ Config {
2581+ OpConfig : config.Config {
2582+ PodManagementPolicy : "ordered_ready" ,
2583+ },
2584+ }, client , pg , logger , eventRecorder )
2585+
2586+ cluster .Name = "test-cluster"
2587+ cluster .Namespace = "default"
2588+
2589+ oldSpec := pg .DeepCopy ()
2590+ newSpec := pg .DeepCopy ()
2591+ newSpec .Spec .Lifecycle = & acidv1.LifecycleSpec {Phase : "stopped" }
2592+
2593+ err := cluster .Update (oldSpec , newSpec )
2594+
2595+ assert .NoError (t , err )
2596+ assert .True (t , updateCalled , "Update should have been called for hibernate" )
2597+ assert .True (t , statusUpdateCalled , "Status update should have been called for hibernate" )
2598+ }
2599+
2600+ func TestUpdate_LifecycleNormalUpdate (t * testing.T ) {
2601+ clientSet := fake .NewSimpleClientset ()
2602+ acidClientSet := fakeacidv1 .NewSimpleClientset ()
2603+
2604+ client := k8sutil.KubernetesClient {
2605+ DeploymentsGetter : clientSet .AppsV1 (),
2606+ PostgresqlsGetter : acidClientSet .AcidV1 (),
2607+ StatefulSetsGetter : clientSet .AppsV1 (),
2608+ ServicesGetter : clientSet .CoreV1 (),
2609+ SecretsGetter : clientSet .CoreV1 (),
2610+ ConfigMapsGetter : clientSet .CoreV1 (),
2611+ PodsGetter : clientSet .CoreV1 (),
2612+ EndpointsGetter : clientSet .CoreV1 (),
2613+ }
2614+
2615+ pg := acidv1.Postgresql {
2616+ ObjectMeta : metav1.ObjectMeta {
2617+ Name : "test-cluster" ,
2618+ Namespace : "default" ,
2619+ },
2620+ Spec : acidv1.PostgresSpec {
2621+ TeamID : "test-team" ,
2622+ NumberOfInstances : 3 ,
2623+ Volume : acidv1.Volume {Size : "1Gi" },
2624+ },
2625+ Status : acidv1.PostgresStatus {
2626+ PostgresClusterStatus : "Running" ,
2627+ },
2628+ }
2629+
2630+ cluster := New (
2631+ Config {
2632+ OpConfig : config.Config {
2633+ PodManagementPolicy : "ordered_ready" ,
2634+ },
2635+ }, client , pg , logger , eventRecorder )
2636+
2637+ cluster .Name = "test-cluster"
2638+ cluster .Namespace = "default"
2639+
2640+ newSpec := pg .DeepCopy ()
2641+ blocked , err := cluster .blockLifecycleUpdate (newSpec )
2642+
2643+ assert .False (t , blocked )
2644+ assert .NoError (t , err )
2645+ }
0 commit comments