51
51
storage_id ,
52
52
storage_type ,
53
53
update_restart_condition ,
54
- wait_network_restore ,
54
+ wait_network_restore , get_db_connection , validate_test_data ,
55
55
)
56
56
57
57
logger = logging .getLogger (__name__ )
@@ -543,41 +543,30 @@ async def test_network_cut_without_ip_change(
543
543
544
544
await is_cluster_updated (ops_test , primary_name )
545
545
546
+
546
547
@pytest .mark .group (1 )
547
548
async def test_deploy_zero_units (ops_test : OpsTest ):
548
549
"""Scale the database to zero units and scale up again."""
549
- app = await app_name (ops_test , APP_NAME )
550
+ app = await app_name (ops_test )
551
+
552
+ dbname = f"{ APPLICATION_NAME .replace ('-' , '_' )} _first_database"
553
+ connection_string , primary_name = await get_db_connection (ops_test , dbname = dbname )
550
554
551
555
# Start an application that continuously writes data to the database.
552
556
await start_continuous_writes (ops_test , app )
553
557
554
558
logger .info ("checking whether writes are increasing" )
555
559
await are_writes_increasing (ops_test )
556
560
557
- connection_string = await build_connection_string (
558
- ops_test , APPLICATION_NAME , FIRST_DATABASE_RELATION_NAME
559
- )
560
-
561
561
# Connect to the database.
562
- # Create test data
563
- with psycopg2 .connect (connection_string ) as connection :
564
- connection .autocommit = True
565
- with connection .cursor () as cursor :
566
- # Check that it's possible to write and read data from the database that
567
- # was created for the application.
568
- cursor .execute ("DROP TABLE IF EXISTS test;" )
569
- cursor .execute ("CREATE TABLE test(data TEXT);" )
570
- cursor .execute ("INSERT INTO test(data) VALUES('some data');" )
571
- cursor .execute ("SELECT data FROM test;" )
572
- data = cursor .fetchone ()
573
- assert data [0 ] == "some data"
574
- connection .close ()
562
+ # Create test data.
563
+ logger .info ("connect to DB and create test table" )
564
+ await create_test_data (connection_string )
575
565
576
566
unit_ip_addresses = []
577
567
storage_id_list = []
578
- primary_name = await get_primary (ops_test , APP_NAME )
579
568
primary_storage = ""
580
- for unit in ops_test .model .applications [APP_NAME ].units :
569
+ for unit in ops_test .model .applications [app ].units :
581
570
# Save IP addresses of units
582
571
unit_ip_addresses .append (await get_unit_ip (ops_test , unit .name ))
583
572
@@ -589,9 +578,9 @@ async def test_deploy_zero_units(ops_test: OpsTest):
589
578
590
579
# Scale the database to zero units.
591
580
logger .info ("scaling database to zero units" )
592
- await scale_application (ops_test , APP_NAME , 0 )
581
+ await scale_application (ops_test , app , 0 )
593
582
594
- # Checking shutdown units
583
+ # Checking shutdown units.
595
584
for unit_ip in unit_ip_addresses :
596
585
try :
597
586
resp = requests .get (f"http://{ unit_ip } :8008" )
@@ -603,37 +592,29 @@ async def test_deploy_zero_units(ops_test: OpsTest):
603
592
604
593
# Scale the database to one unit.
605
594
logger .info ("scaling database to one unit" )
606
- await ops_test .model .applications [app ].add_unit (attach_storage = [primary_storage ])
595
+ await add_unit_with_storage (ops_test , app = app , storage = primary_storage )
596
+ await ops_test .model .wait_for_idle (status = "active" , timeout = 3000 )
597
+
598
+ connection_string , primary_name = await get_db_connection (ops_test , dbname = dbname )
607
599
logger .info ("checking whether writes are increasing" )
608
600
await are_writes_increasing (ops_test )
609
601
610
- # Scale the database to three units.
611
- await ops_test .model .applications [app ].add_unit ()
612
-
613
- # Connect to the database.
614
- # Create test data
615
- with psycopg2 .connect (connection_string ) as connection :
616
- connection .autocommit = True
617
- with connection .cursor () as cursor :
618
- # Check that it's possible to write and read data from the database that
619
- # was created for the application.
620
- cursor .execute ("SELECT data FROM test;" )
621
- data = cursor .fetchone ()
622
- assert data [0 ] == "some data"
623
- connection .close ()
602
+ logger .info ("check test database data" )
603
+ await validate_test_data (connection_string )
624
604
625
605
# Scale the database to three units.
626
- await ops_test .model .applications [app ].add_unit ()
627
- # Connect to the database.
628
- # Create test data
629
- with psycopg2 .connect (connection_string ) as connection :
630
- connection .autocommit = True
631
- with connection .cursor () as cursor :
632
- # Check that it's possible to write and read data from the database that
633
- # was created for the application.
634
- cursor .execute ("SELECT data FROM test;" )
635
- data = cursor .fetchone ()
636
- assert data [0 ] == "some data"
637
- connection .close ()
606
+ logger .info ("scaling database to two unit" )
607
+ await scale_application (ops_test , application_name = app , count = 2 )
608
+ await ops_test .model .wait_for_idle (status = "active" , timeout = 3000 )
609
+ for unit in ops_test .model .applications [app ].units :
610
+ if not await unit .is_leader_from_status ():
611
+ assert await reused_replica_storage (ops_test , unit_name = unit .name
612
+ ), "attached storage not properly re-used by Postgresql."
613
+ logger .info (f"check test database data of unit name { unit .name } " )
614
+ connection_string , _ = await get_db_connection (ops_test ,
615
+ dbname = dbname ,
616
+ is_primary = False ,
617
+ replica_unit_name = unit .name )
618
+ await validate_test_data (connection_string )
638
619
639
620
await check_writes (ops_test )
0 commit comments