Skip to content

Commit 7dc328b

Browse files
committed
Zero-units: continuous writes ON, deploy 3 units, check, scale to 0 units, check no errors, scale to 3, check
1 parent 938035f commit 7dc328b

File tree

2 files changed

+70
-50
lines changed

2 files changed

+70
-50
lines changed

tests/integration/ha_tests/helpers.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,3 +794,42 @@ async def reused_full_cluster_recovery_storage(ops_test: OpsTest, unit_name) ->
794794
"/var/snap/charmed-postgresql/common/var/log/patroni/patroni.log*",
795795
)
796796
return True
797+
798+
799+
async def get_db_connection(ops_test, dbname, is_primary=True, replica_unit_name=""):
800+
unit_name = await get_primary(ops_test, APP_NAME)
801+
password = await get_password(ops_test, APP_NAME)
802+
address = get_unit_address(ops_test, unit_name)
803+
if not is_primary and unit_name != "":
804+
unit_name = replica_unit_name
805+
address = ops_test.model.applications[APP_NAME].units[unit_name].public_address
806+
connection_string = (
807+
f"dbname='{dbname}' user='operator'"
808+
f" host='{address}' password='{password}' connect_timeout=10"
809+
)
810+
return connection_string, unit_name
811+
812+
async def validate_test_data(connection_string):
813+
with psycopg2.connect(connection_string) as connection:
814+
connection.autocommit = True
815+
with connection.cursor() as cursor:
816+
cursor.execute("SELECT data FROM test;")
817+
data = cursor.fetchone()
818+
assert data[0] == "some data"
819+
connection.close()
820+
821+
822+
async def create_test_data(connection_string):
823+
with psycopg2.connect(connection_string) as connection:
824+
connection.autocommit = True
825+
with connection.cursor() as cursor:
826+
# Check that it's possible to write and read data from the database that
827+
# was created for the application.
828+
cursor.execute("DROP TABLE IF EXISTS test;")
829+
cursor.execute("CREATE TABLE test(data TEXT);")
830+
cursor.execute("INSERT INTO test(data) VALUES('some data');")
831+
cursor.execute("SELECT data FROM test;")
832+
data = cursor.fetchone()
833+
logger.info("check test data")
834+
assert data[0] == "some data"
835+
connection.close()

tests/integration/ha_tests/test_self_healing.py

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
storage_id,
5252
storage_type,
5353
update_restart_condition,
54-
wait_network_restore,
54+
wait_network_restore, get_db_connection, validate_test_data,
5555
)
5656

5757
logger = logging.getLogger(__name__)
@@ -543,41 +543,30 @@ async def test_network_cut_without_ip_change(
543543

544544
await is_cluster_updated(ops_test, primary_name)
545545

546+
546547
@pytest.mark.group(1)
547548
async def test_deploy_zero_units(ops_test: OpsTest):
548549
"""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)
550554

551555
# Start an application that continuously writes data to the database.
552556
await start_continuous_writes(ops_test, app)
553557

554558
logger.info("checking whether writes are increasing")
555559
await are_writes_increasing(ops_test)
556560

557-
connection_string = await build_connection_string(
558-
ops_test, APPLICATION_NAME, FIRST_DATABASE_RELATION_NAME
559-
)
560-
561561
# 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)
575565

576566
unit_ip_addresses = []
577567
storage_id_list = []
578-
primary_name = await get_primary(ops_test, APP_NAME)
579568
primary_storage = ""
580-
for unit in ops_test.model.applications[APP_NAME].units:
569+
for unit in ops_test.model.applications[app].units:
581570
# Save IP addresses of units
582571
unit_ip_addresses.append(await get_unit_ip(ops_test, unit.name))
583572

@@ -589,9 +578,9 @@ async def test_deploy_zero_units(ops_test: OpsTest):
589578

590579
# Scale the database to zero units.
591580
logger.info("scaling database to zero units")
592-
await scale_application(ops_test, APP_NAME, 0)
581+
await scale_application(ops_test, app, 0)
593582

594-
# Checking shutdown units
583+
# Checking shutdown units.
595584
for unit_ip in unit_ip_addresses:
596585
try:
597586
resp = requests.get(f"http://{unit_ip}:8008")
@@ -603,37 +592,29 @@ async def test_deploy_zero_units(ops_test: OpsTest):
603592

604593
# Scale the database to one unit.
605594
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)
607599
logger.info("checking whether writes are increasing")
608600
await are_writes_increasing(ops_test)
609601

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)
624604

625605
# 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)
638619

639620
await check_writes(ops_test)

0 commit comments

Comments
 (0)