1616logger = logging .getLogger (__name__ )
1717
1818DB_METADATA = yaml .safe_load (Path ("./metadata.yaml" ).read_text ())
19- DATABASE_APP_NAME = DB_METADATA ["name" ]
20- CLUSTER_NAME = "test_cluster"
2119
20+ DATABASE_APP_NAME = DB_METADATA ["name" ]
21+ DATABASE_ENDPOINT = "database"
2222APPLICATION_APP_NAME = "mysql-test-app"
23+ APPLICATION_ENDPOINT = "database"
2324
2425APPS = [DATABASE_APP_NAME , APPLICATION_APP_NAME ]
2526
26- ENDPOINT = "database"
27-
2827
2928@pytest .mark .abort_on_fail
3029@pytest .mark .skip_if_deployed
3130async def test_build_and_deploy (ops_test : OpsTest , charm ):
3231 """Build the charm and deploy 3 units to ensure a cluster is formed."""
33- config = {"cluster-name" : CLUSTER_NAME , "profile" : "testing" }
32+ config = {"cluster-name" : "test_cluster" , "profile" : "testing" }
3433 resources = {"mysql-image" : DB_METADATA ["resources" ]["mysql-image" ]["upstream-source" ]}
3534
3635 await asyncio .gather (
@@ -52,12 +51,26 @@ async def test_build_and_deploy(ops_test: OpsTest, charm):
5251 ),
5352 )
5453
54+
55+ @pytest .mark .abort_on_fail
56+ async def test_relation_creation_eager (ops_test : OpsTest ):
57+ """Relate charms before they have time to properly start.
58+
59+ It simulates a Terraform-like deployment strategy
60+ """
61+ await ops_test .model .relate (
62+ f"{ APPLICATION_APP_NAME } :{ APPLICATION_ENDPOINT } " ,
63+ f"{ DATABASE_APP_NAME } :{ DATABASE_ENDPOINT } " ,
64+ )
65+ await ops_test .model .block_until (
66+ lambda : is_relation_joined (ops_test , APPLICATION_ENDPOINT , DATABASE_ENDPOINT ) == True # noqa: E712
67+ )
68+
5569 # Reduce the update_status frequency until the cluster is deployed
5670 async with ops_test .fast_forward ("60s" ):
5771 await ops_test .model .block_until (
5872 lambda : len (ops_test .model .applications [DATABASE_APP_NAME ].units ) == 3
5973 )
60-
6174 await ops_test .model .block_until (
6275 lambda : len (ops_test .model .applications [APPLICATION_APP_NAME ].units ) == 2
6376 )
@@ -78,26 +91,14 @@ async def test_build_and_deploy(ops_test: OpsTest, charm):
7891 ),
7992 )
8093
81- assert len (ops_test .model .applications [DATABASE_APP_NAME ].units ) == 3
82-
83- for unit in ops_test .model .applications [DATABASE_APP_NAME ].units :
84- assert unit .workload_status == "active"
85-
86- assert len (ops_test .model .applications [APPLICATION_APP_NAME ].units ) == 2
87-
8894
8995@pytest .mark .abort_on_fail
9096@markers .only_without_juju_secrets
9197async def test_relation_creation_databag (ops_test : OpsTest ):
9298 """Relate charms and wait for the expected changes in status."""
93- await ops_test .model .relate (APPLICATION_APP_NAME , f"{ DATABASE_APP_NAME } :{ ENDPOINT } " )
94-
9599 async with ops_test .fast_forward ("60s" ):
96- await ops_test .model .block_until (
97- lambda : is_relation_joined (ops_test , ENDPOINT , ENDPOINT ) == True # noqa: E712
98- )
99-
100100 await ops_test .model .wait_for_idle (apps = APPS , status = "active" )
101+
101102 relation_data = await get_relation_data (ops_test , APPLICATION_APP_NAME , "database" )
102103 assert {"password" , "username" } <= set (relation_data [0 ]["application-data" ])
103104
@@ -106,14 +107,9 @@ async def test_relation_creation_databag(ops_test: OpsTest):
106107@markers .only_with_juju_secrets
107108async def test_relation_creation (ops_test : OpsTest ):
108109 """Relate charms and wait for the expected changes in status."""
109- await ops_test .model .relate (APPLICATION_APP_NAME , f"{ DATABASE_APP_NAME } :{ ENDPOINT } " )
110-
111110 async with ops_test .fast_forward ("60s" ):
112- await ops_test .model .block_until (
113- lambda : is_relation_joined (ops_test , ENDPOINT , ENDPOINT ) == True # noqa: E712
114- )
115-
116111 await ops_test .model .wait_for_idle (apps = APPS , status = "active" )
112+
117113 relation_data = await get_relation_data (ops_test , APPLICATION_APP_NAME , "database" )
118114 assert not {"password" , "username" } <= set (relation_data [0 ]["application-data" ])
119115 assert "secret-user" in relation_data [0 ]["application-data" ]
@@ -123,11 +119,12 @@ async def test_relation_creation(ops_test: OpsTest):
123119async def test_relation_broken (ops_test : OpsTest ):
124120 """Remove relation and wait for the expected changes in status."""
125121 await ops_test .model .applications [DATABASE_APP_NAME ].remove_relation (
126- f"{ APPLICATION_APP_NAME } :{ ENDPOINT } " , f"{ DATABASE_APP_NAME } :{ ENDPOINT } "
122+ f"{ APPLICATION_APP_NAME } :{ APPLICATION_ENDPOINT } " ,
123+ f"{ DATABASE_APP_NAME } :{ DATABASE_ENDPOINT } " ,
127124 )
128125
129126 await ops_test .model .block_until (
130- lambda : is_relation_broken (ops_test , ENDPOINT , ENDPOINT ) == True # noqa: E712
127+ lambda : is_relation_broken (ops_test , APPLICATION_ENDPOINT , DATABASE_ENDPOINT ) == True # noqa: E712
131128 )
132129
133130 async with ops_test .fast_forward ("60s" ):
0 commit comments