Skip to content

Commit 938035f

Browse files
committed
deployment test "zero-units"
1 parent 9d7bfed commit 938035f

File tree

2 files changed

+53
-31
lines changed

2 files changed

+53
-31
lines changed

tests/integration/ha_tests/test_self_healing.py

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import asyncio
55
import logging
66

7+
import psycopg2
78
import pytest
89
from pip._vendor import requests
910
from pytest_operator.plugin import OpsTest
@@ -15,7 +16,7 @@
1516
get_machine_from_unit,
1617
get_password,
1718
get_unit_address,
18-
run_command_on_unit, scale_application,
19+
run_command_on_unit, scale_application, build_connection_string, FIRST_DATABASE_RELATION_NAME,
1920
)
2021
from .conftest import APPLICATION_NAME
2122
from .helpers import (
@@ -545,39 +546,33 @@ async def test_network_cut_without_ip_change(
545546
@pytest.mark.group(1)
546547
async def test_deploy_zero_units(ops_test: OpsTest):
547548
"""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)
549+
app = await app_name(ops_test, APP_NAME)
574550

575551
# Start an application that continuously writes data to the database.
576-
await start_continuous_writes(ops_test, APP_NAME)
552+
await start_continuous_writes(ops_test, app)
577553

578554
logger.info("checking whether writes are increasing")
579555
await are_writes_increasing(ops_test)
580556

557+
connection_string = await build_connection_string(
558+
ops_test, APPLICATION_NAME, FIRST_DATABASE_RELATION_NAME
559+
)
560+
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()
575+
581576
unit_ip_addresses = []
582577
storage_id_list = []
583578
primary_name = await get_primary(ops_test, APP_NAME)
@@ -608,11 +603,37 @@ async def test_deploy_zero_units(ops_test: OpsTest):
608603

609604
# Scale the database to one unit.
610605
logger.info("scaling database to one unit")
611-
await add_unit_with_storage(ops_test, storage=primary_storage, app=APP_NAME)
606+
await ops_test.model.applications[app].add_unit(attach_storage=[primary_storage])
612607
logger.info("checking whether writes are increasing")
613608
await are_writes_increasing(ops_test)
614609

615610
# 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)
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()
624+
625+
# 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()
638+
618639
await check_writes(ops_test)

tests/integration/helpers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
DATABASE_APP_NAME = METADATA["name"]
3636
STORAGE_PATH = METADATA["storage"]["pgdata"]["location"]
3737
APPLICATION_NAME = "postgresql-test-app"
38+
FIRST_DATABASE_RELATION_NAME = "first-database"
3839

3940

4041
async def build_connection_string(

0 commit comments

Comments
 (0)