5
5
import logging
6
6
7
7
import pytest
8
+ from pip ._vendor import requests
8
9
from pytest_operator .plugin import OpsTest
9
10
from tenacity import Retrying , stop_after_delay , wait_fixed
10
11
14
15
get_machine_from_unit ,
15
16
get_password ,
16
17
get_unit_address ,
17
- run_command_on_unit ,
18
+ run_command_on_unit , scale_application ,
18
19
)
19
20
from .conftest import APPLICATION_NAME
20
21
from .helpers import (
@@ -540,3 +541,78 @@ async def test_network_cut_without_ip_change(
540
541
), "Connection is not possible after network restore"
541
542
542
543
await is_cluster_updated (ops_test , primary_name )
544
+
545
+ @pytest .mark .group (1 )
546
+ async def test_deploy_zero_units (ops_test : OpsTest ):
547
+ """Scale the database to zero units and scale up again."""
548
+ wait_for_apps = False
549
+ if not await app_name (ops_test ):
550
+ wait_for_apps = True
551
+ async with ops_test .fast_forward ():
552
+ await ops_test .model .deploy (
553
+ APP_NAME ,
554
+ application_name = APP_NAME ,
555
+ num_units = 3 ,
556
+ storage = {"pgdata" : {"pool" : "lxd-btrfs" , "size" : 2048 }},
557
+ series = CHARM_SERIES ,
558
+ channel = "edge" ,
559
+ )
560
+
561
+ # Deploy the continuous writes application charm if it wasn't already deployed.
562
+ if not await app_name (ops_test , APPLICATION_NAME ):
563
+ wait_for_apps = True
564
+ async with ops_test .fast_forward ():
565
+ await ops_test .model .deploy (
566
+ APPLICATION_NAME ,
567
+ application_name = APPLICATION_NAME ,
568
+ series = CHARM_SERIES ,
569
+ channel = "edge" ,
570
+ )
571
+
572
+ if wait_for_apps :
573
+ await ops_test .model .wait_for_idle (status = "active" , timeout = 3000 )
574
+
575
+ # Start an application that continuously writes data to the database.
576
+ await start_continuous_writes (ops_test , APP_NAME )
577
+
578
+ logger .info ("checking whether writes are increasing" )
579
+ await are_writes_increasing (ops_test )
580
+
581
+ unit_ip_addresses = []
582
+ storage_id_list = []
583
+ primary_name = await get_primary (ops_test , APP_NAME )
584
+ primary_storage = ""
585
+ for unit in ops_test .model .applications [APP_NAME ].units :
586
+ # Save IP addresses of units
587
+ unit_ip_addresses .append (await get_unit_ip (ops_test , unit .name ))
588
+
589
+ # Save detached storage ID
590
+ if primary_name != unit .name :
591
+ storage_id_list .append (storage_id (ops_test , unit .name ))
592
+ else :
593
+ primary_storage = storage_id (ops_test , unit .name )
594
+
595
+ # Scale the database to zero units.
596
+ logger .info ("scaling database to zero units" )
597
+ await scale_application (ops_test , APP_NAME , 0 )
598
+
599
+ # Checking shutdown units
600
+ for unit_ip in unit_ip_addresses :
601
+ try :
602
+ resp = requests .get (f"http://{ unit_ip } :8008" )
603
+ assert resp .status_code != 200 , f"status code = { resp .status_code } , message = { resp .text } "
604
+ except requests .exceptions .ConnectionError as e :
605
+ assert True , f"unit host = http://{ unit_ip } :8008, all units shutdown"
606
+ except Exception as e :
607
+ assert False , f"{ e } unit host = http://{ unit_ip } :8008, something went wrong"
608
+
609
+ # Scale the database to one unit.
610
+ logger .info ("scaling database to one unit" )
611
+ await add_unit_with_storage (ops_test , storage = primary_storage , app = APP_NAME )
612
+ logger .info ("checking whether writes are increasing" )
613
+ await are_writes_increasing (ops_test )
614
+
615
+ # Scale the database to three units.
616
+ for store_id in storage_id_list :
617
+ await add_unit_with_storage (ops_test , storage = store_id , app = APP_NAME )
618
+ await check_writes (ops_test )
0 commit comments