16
16
logger = logging .getLogger (__name__ )
17
17
18
18
DB_METADATA = yaml .safe_load (Path ("./metadata.yaml" ).read_text ())
19
- DATABASE_APP_NAME = DB_METADATA ["name" ]
20
- CLUSTER_NAME = "test_cluster"
21
19
20
+ DATABASE_APP_NAME = DB_METADATA ["name" ]
21
+ DATABASE_ENDPOINT = "database"
22
22
APPLICATION_APP_NAME = "mysql-test-app"
23
+ APPLICATION_ENDPOINT = "database"
23
24
24
25
APPS = [DATABASE_APP_NAME , APPLICATION_APP_NAME ]
25
26
26
- ENDPOINT = "database"
27
-
28
27
29
28
@pytest .mark .abort_on_fail
30
29
@pytest .mark .skip_if_deployed
31
30
async def test_build_and_deploy (ops_test : OpsTest , charm ):
32
31
"""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" }
34
33
resources = {"mysql-image" : DB_METADATA ["resources" ]["mysql-image" ]["upstream-source" ]}
35
34
36
35
await asyncio .gather (
@@ -52,12 +51,26 @@ async def test_build_and_deploy(ops_test: OpsTest, charm):
52
51
),
53
52
)
54
53
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
+
55
69
# Reduce the update_status frequency until the cluster is deployed
56
70
async with ops_test .fast_forward ("60s" ):
57
71
await ops_test .model .block_until (
58
72
lambda : len (ops_test .model .applications [DATABASE_APP_NAME ].units ) == 3
59
73
)
60
-
61
74
await ops_test .model .block_until (
62
75
lambda : len (ops_test .model .applications [APPLICATION_APP_NAME ].units ) == 2
63
76
)
@@ -78,26 +91,14 @@ async def test_build_and_deploy(ops_test: OpsTest, charm):
78
91
),
79
92
)
80
93
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
-
88
94
89
95
@pytest .mark .abort_on_fail
90
96
@markers .only_without_juju_secrets
91
97
async def test_relation_creation_databag (ops_test : OpsTest ):
92
98
"""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
-
95
99
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
-
100
100
await ops_test .model .wait_for_idle (apps = APPS , status = "active" )
101
+
101
102
relation_data = await get_relation_data (ops_test , APPLICATION_APP_NAME , "database" )
102
103
assert {"password" , "username" } <= set (relation_data [0 ]["application-data" ])
103
104
@@ -106,14 +107,9 @@ async def test_relation_creation_databag(ops_test: OpsTest):
106
107
@markers .only_with_juju_secrets
107
108
async def test_relation_creation (ops_test : OpsTest ):
108
109
"""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
-
111
110
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
-
116
111
await ops_test .model .wait_for_idle (apps = APPS , status = "active" )
112
+
117
113
relation_data = await get_relation_data (ops_test , APPLICATION_APP_NAME , "database" )
118
114
assert not {"password" , "username" } <= set (relation_data [0 ]["application-data" ])
119
115
assert "secret-user" in relation_data [0 ]["application-data" ]
@@ -123,11 +119,12 @@ async def test_relation_creation(ops_test: OpsTest):
123
119
async def test_relation_broken (ops_test : OpsTest ):
124
120
"""Remove relation and wait for the expected changes in status."""
125
121
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 } " ,
127
124
)
128
125
129
126
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
131
128
)
132
129
133
130
async with ops_test .fast_forward ("60s" ):
0 commit comments