5
5
import uuid
6
6
7
7
import boto3
8
+ import jubilant
8
9
import pytest
9
10
from pytest_operator .plugin import OpsTest
10
11
11
12
from . import architecture
12
13
from .helpers import construct_endpoint
14
+ from .jubilant_helpers import RoleAttributeValue
13
15
14
16
AWS = "AWS"
15
17
GCP = "GCP"
@@ -25,7 +27,7 @@ def charm():
25
27
return f"./[email protected] { architecture .architecture } .charm"
26
28
27
29
28
- def get_cloud_config (cloud : str ) -> tuple [dict [str , str ], dict [str , str ]]:
30
+ def get_cloud_config (cloud : str ) -> tuple [dict [str , str ], dict [str , str ]] | None :
29
31
# Define some configurations and credentials.
30
32
if cloud == AWS :
31
33
return {
@@ -47,6 +49,7 @@ def get_cloud_config(cloud: str) -> tuple[dict[str, str], dict[str, str]]:
47
49
"access-key" : os .environ ["GCP_ACCESS_KEY" ],
48
50
"secret-key" : os .environ ["GCP_SECRET_KEY" ],
49
51
}
52
+ return None
50
53
51
54
52
55
def cleanup_cloud (config : dict [str , str ], credentials : dict [str , str ]) -> None :
@@ -94,3 +97,173 @@ async def gcp_cloud_configs(ops_test: OpsTest) -> None:
94
97
yield config , credentials
95
98
96
99
cleanup_cloud (config , credentials )
100
+
101
+
102
+ @pytest .fixture (scope = "module" )
103
+ def juju (request : pytest .FixtureRequest ):
104
+ """Pytest fixture that wraps :meth:`jubilant.with_model`.
105
+
106
+ This adds command line parameter ``--keep-models`` (see help for details).
107
+ """
108
+ controller = request .config .getoption ("--controller" )
109
+ model = request .config .getoption ("--model" )
110
+ controller_and_model = None
111
+ if controller and model :
112
+ controller_and_model = f"{ controller } :{ model } "
113
+ elif controller :
114
+ controller_and_model = controller
115
+ elif model :
116
+ controller_and_model = model
117
+ keep_models = bool (request .config .getoption ("--keep-models" ))
118
+
119
+ if controller_and_model :
120
+ juju = jubilant .Juju (model = controller_and_model ) # type: ignore
121
+ yield juju
122
+ log = juju .debug_log (limit = 1000 )
123
+ else :
124
+ with jubilant .temp_model (keep = keep_models ) as juju :
125
+ yield juju
126
+ log = juju .debug_log (limit = 1000 )
127
+
128
+ if request .session .testsfailed :
129
+ print (log , end = "" )
130
+
131
+
132
+ @pytest .fixture (scope = "module" )
133
+ def predefined_roles () -> dict :
134
+ """Return a list of predefined roles with their expected permissions."""
135
+ return {
136
+ "" : {
137
+ "auto-escalate-to-database-owner" : RoleAttributeValue .REQUESTED_DATABASE ,
138
+ "permissions" : {
139
+ "connect" : RoleAttributeValue .REQUESTED_DATABASE ,
140
+ "create-databases" : RoleAttributeValue .NO ,
141
+ "create-objects" : RoleAttributeValue .NO ,
142
+ "escalate-to-database-owner" : RoleAttributeValue .REQUESTED_DATABASE ,
143
+ "read-data" : RoleAttributeValue .NO ,
144
+ "read-stats" : RoleAttributeValue .ALL_DATABASES ,
145
+ "run-backup-commands" : RoleAttributeValue .NO ,
146
+ "set-up-predefined-catalog-roles" : RoleAttributeValue .NO ,
147
+ "set-user" : RoleAttributeValue .NO ,
148
+ "write-data" : RoleAttributeValue .NO ,
149
+ },
150
+ },
151
+ "charmed_stats" : {
152
+ "auto-escalate-to-database-owner" : RoleAttributeValue .NO ,
153
+ "permissions" : {
154
+ "connect" : RoleAttributeValue .ALL_DATABASES ,
155
+ "create-databases" : RoleAttributeValue .NO ,
156
+ "create-objects" : RoleAttributeValue .NO ,
157
+ "escalate-to-database-owner" : RoleAttributeValue .NO ,
158
+ "read-data" : RoleAttributeValue .NO ,
159
+ "read-stats" : RoleAttributeValue .ALL_DATABASES ,
160
+ "run-backup-commands" : RoleAttributeValue .NO ,
161
+ "set-up-predefined-catalog-roles" : RoleAttributeValue .NO ,
162
+ "set-user" : RoleAttributeValue .NO ,
163
+ "write-data" : RoleAttributeValue .NO ,
164
+ },
165
+ },
166
+ "charmed_read" : {
167
+ "auto-escalate-to-database-owner" : RoleAttributeValue .NO ,
168
+ "permissions" : {
169
+ "connect" : RoleAttributeValue .ALL_DATABASES ,
170
+ "create-databases" : RoleAttributeValue .NO ,
171
+ "create-objects" : RoleAttributeValue .NO ,
172
+ "escalate-to-database-owner" : RoleAttributeValue .NO ,
173
+ "read-data" : RoleAttributeValue .ALL_DATABASES ,
174
+ "read-stats" : RoleAttributeValue .ALL_DATABASES ,
175
+ "run-backup-commands" : RoleAttributeValue .NO ,
176
+ "set-up-predefined-catalog-roles" : RoleAttributeValue .NO ,
177
+ "set-user" : RoleAttributeValue .NO ,
178
+ "write-data" : RoleAttributeValue .NO ,
179
+ },
180
+ },
181
+ "charmed_dml" : {
182
+ "auto-escalate-to-database-owner" : RoleAttributeValue .NO ,
183
+ "permissions" : {
184
+ "connect" : RoleAttributeValue .ALL_DATABASES ,
185
+ "create-databases" : RoleAttributeValue .NO ,
186
+ "create-objects" : RoleAttributeValue .NO ,
187
+ "escalate-to-database-owner" : RoleAttributeValue .NO ,
188
+ "read-data" : RoleAttributeValue .ALL_DATABASES ,
189
+ "read-stats" : RoleAttributeValue .ALL_DATABASES ,
190
+ "run-backup-commands" : RoleAttributeValue .NO ,
191
+ "set-up-predefined-catalog-roles" : RoleAttributeValue .NO ,
192
+ "set-user" : RoleAttributeValue .NO ,
193
+ "write-data" : RoleAttributeValue .ALL_DATABASES ,
194
+ },
195
+ },
196
+ "charmed_backup" : {
197
+ "auto-escalate-to-database-owner" : RoleAttributeValue .NO ,
198
+ "permissions" : {
199
+ "connect" : RoleAttributeValue .ALL_DATABASES ,
200
+ "create-databases" : RoleAttributeValue .NO ,
201
+ "create-objects" : RoleAttributeValue .NO ,
202
+ "escalate-to-database-owner" : RoleAttributeValue .NO ,
203
+ "read-data" : RoleAttributeValue .NO ,
204
+ "read-stats" : RoleAttributeValue .ALL_DATABASES ,
205
+ "run-backup-commands" : RoleAttributeValue .YES ,
206
+ "set-up-predefined-catalog-roles" : RoleAttributeValue .NO ,
207
+ "set-user" : RoleAttributeValue .NO ,
208
+ "write-data" : RoleAttributeValue .NO ,
209
+ },
210
+ },
211
+ "charmed_dba" : {
212
+ "auto-escalate-to-database-owner" : RoleAttributeValue .NO ,
213
+ "permissions" : {
214
+ "connect" : RoleAttributeValue .ALL_DATABASES ,
215
+ "create-databases" : RoleAttributeValue .NO ,
216
+ "create-objects" : RoleAttributeValue .NO ,
217
+ "escalate-to-database-owner" : RoleAttributeValue .NO ,
218
+ "read-data" : RoleAttributeValue .ALL_DATABASES ,
219
+ "read-stats" : RoleAttributeValue .ALL_DATABASES ,
220
+ "run-backup-commands" : RoleAttributeValue .NO ,
221
+ "set-up-predefined-catalog-roles" : RoleAttributeValue .NO ,
222
+ "set-user" : RoleAttributeValue .YES ,
223
+ "write-data" : RoleAttributeValue .ALL_DATABASES ,
224
+ },
225
+ },
226
+ "charmed_admin" : {
227
+ "auto-escalate-to-database-owner" : RoleAttributeValue .ALL_DATABASES ,
228
+ "permissions" : {
229
+ "connect" : RoleAttributeValue .ALL_DATABASES ,
230
+ "create-databases" : RoleAttributeValue .NO ,
231
+ "create-objects" : RoleAttributeValue .NO ,
232
+ "escalate-to-database-owner" : RoleAttributeValue .ALL_DATABASES ,
233
+ "read-data" : RoleAttributeValue .ALL_DATABASES ,
234
+ "read-stats" : RoleAttributeValue .ALL_DATABASES ,
235
+ "run-backup-commands" : RoleAttributeValue .NO ,
236
+ "set-up-predefined-catalog-roles" : RoleAttributeValue .NO ,
237
+ "set-user" : RoleAttributeValue .NO ,
238
+ "write-data" : RoleAttributeValue .ALL_DATABASES ,
239
+ },
240
+ },
241
+ "CREATEDB" : {
242
+ "auto-escalate-to-database-owner" : RoleAttributeValue .NO ,
243
+ "permissions" : {
244
+ "connect" : RoleAttributeValue .ALL_DATABASES ,
245
+ "create-databases" : RoleAttributeValue .YES ,
246
+ "create-objects" : RoleAttributeValue .NO ,
247
+ "escalate-to-database-owner" : RoleAttributeValue .NO ,
248
+ "read-data" : RoleAttributeValue .NO ,
249
+ "read-stats" : RoleAttributeValue .NO ,
250
+ "run-backup-commands" : RoleAttributeValue .NO ,
251
+ "set-up-predefined-catalog-roles" : RoleAttributeValue .YES ,
252
+ "set-user" : RoleAttributeValue .NO ,
253
+ "write-data" : RoleAttributeValue .NO ,
254
+ },
255
+ },
256
+ }
257
+
258
+
259
+ @pytest .fixture (scope = "module" )
260
+ def predefined_roles_combinations () -> list :
261
+ """Return a list of valid combinations of predefined roles."""
262
+ return [
263
+ ("" ,),
264
+ ("charmed_stats" ,),
265
+ ("charmed_read" ,),
266
+ ("charmed_dml" ,),
267
+ ("charmed_admin" ,),
268
+ ("charmed_admin" , "CREATEDB" ),
269
+ ]
0 commit comments