4
4
import asyncio
5
5
import logging
6
6
7
+ import psycopg2
7
8
import pytest
8
9
from pip ._vendor import requests
9
10
from pytest_operator .plugin import OpsTest
15
16
get_machine_from_unit ,
16
17
get_password ,
17
18
get_unit_address ,
18
- run_command_on_unit , scale_application ,
19
+ run_command_on_unit , scale_application , build_connection_string , FIRST_DATABASE_RELATION_NAME ,
19
20
)
20
21
from .conftest import APPLICATION_NAME
21
22
from .helpers import (
@@ -545,39 +546,33 @@ async def test_network_cut_without_ip_change(
545
546
@pytest .mark .group (1 )
546
547
async def test_deploy_zero_units (ops_test : OpsTest ):
547
548
"""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 )
574
550
575
551
# 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 )
577
553
578
554
logger .info ("checking whether writes are increasing" )
579
555
await are_writes_increasing (ops_test )
580
556
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
+
581
576
unit_ip_addresses = []
582
577
storage_id_list = []
583
578
primary_name = await get_primary (ops_test , APP_NAME )
@@ -608,11 +603,37 @@ async def test_deploy_zero_units(ops_test: OpsTest):
608
603
609
604
# Scale the database to one unit.
610
605
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 ] )
612
607
logger .info ("checking whether writes are increasing" )
613
608
await are_writes_increasing (ops_test )
614
609
615
610
# 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
+
618
639
await check_writes (ops_test )
0 commit comments