26
26
generate_random_string ,
27
27
get_cluster_status ,
28
28
get_primary_unit ,
29
- get_server_config_credentials ,
30
29
get_unit_address ,
31
30
is_relation_joined ,
32
31
scale_application ,
47
46
logger = logging .getLogger (__name__ )
48
47
49
48
50
- async def get_max_written_value_in_database (ops_test : OpsTest , unit : Unit ) -> int :
49
+ async def get_max_written_value_in_database (
50
+ ops_test : OpsTest , unit : Unit , credentials : dict
51
+ ) -> int :
51
52
"""Retrieve the max written value in the MySQL database.
52
53
53
54
Args:
54
55
ops_test: The ops test framework
55
56
unit: The MySQL unit on which to execute queries on
57
+ credentials: The credentials to use to connect to the MySQL database
56
58
"""
57
- server_config_credentials = await get_server_config_credentials (unit )
58
59
unit_address = await get_unit_address (ops_test , unit .name )
59
60
60
61
select_max_written_value_sql = [f"SELECT MAX(number) FROM `{ DATABASE_NAME } `.`{ TABLE_NAME } `;" ]
61
62
62
- output = await execute_queries_on_unit (
63
- unit_address ,
64
- server_config_credentials ["username" ],
65
- server_config_credentials ["password" ],
66
- select_max_written_value_sql ,
63
+ output = execute_queries_on_unit (
64
+ unit_address = unit_address ,
65
+ username = credentials ["username" ],
66
+ password = credentials ["password" ],
67
+ queries = select_max_written_value_sql ,
67
68
)
68
69
69
70
return output [0 ]
70
71
71
72
72
73
def get_application_name (ops_test : OpsTest , application_name_substring : str ) -> Optional [str ]:
73
- """Returns the name of the application witt the provided application name.
74
+ """Returns the name of the application with the provided application name.
74
75
75
76
This enables us to retrieve the name of the deployed application in an existing model.
76
77
@@ -354,6 +355,7 @@ async def insert_data_into_mysql_and_validate_replication(
354
355
ops_test : OpsTest ,
355
356
database_name : str ,
356
357
table_name : str ,
358
+ credentials : dict ,
357
359
mysql_units : Optional [List [Unit ]] = None ,
358
360
mysql_application_substring : Optional [str ] = "mysql" ,
359
361
) -> str :
@@ -370,7 +372,6 @@ async def insert_data_into_mysql_and_validate_replication(
370
372
primary = await get_primary_unit (ops_test , mysql_units [0 ], mysql_application_name )
371
373
372
374
# insert some data into the new primary and ensure that the writes get replicated
373
- server_config_credentials = await get_server_config_credentials (primary )
374
375
primary_address = await get_unit_address (ops_test , primary .name )
375
376
376
377
value = generate_random_string (255 )
@@ -380,10 +381,10 @@ async def insert_data_into_mysql_and_validate_replication(
380
381
f"INSERT INTO `{ database_name } `.`{ table_name } ` (id) VALUES ('{ value } ')" ,
381
382
]
382
383
383
- await execute_queries_on_unit (
384
+ execute_queries_on_unit (
384
385
primary_address ,
385
- server_config_credentials ["username" ],
386
- server_config_credentials ["password" ],
386
+ credentials ["username" ],
387
+ credentials ["password" ],
387
388
insert_value_sql ,
388
389
commit = True ,
389
390
)
@@ -398,10 +399,10 @@ async def insert_data_into_mysql_and_validate_replication(
398
399
for unit in mysql_units :
399
400
unit_address = await get_unit_address (ops_test , unit .name )
400
401
401
- output = await execute_queries_on_unit (
402
+ output = execute_queries_on_unit (
402
403
unit_address ,
403
- server_config_credentials ["username" ],
404
- server_config_credentials ["password" ],
404
+ credentials ["username" ],
405
+ credentials ["password" ],
405
406
select_value_sql ,
406
407
)
407
408
assert output [0 ] == value
@@ -412,20 +413,21 @@ async def insert_data_into_mysql_and_validate_replication(
412
413
413
414
414
415
async def clean_up_database_and_table (
415
- ops_test : OpsTest , database_name : str , table_name : str
416
+ ops_test : OpsTest , database_name : str , table_name : str , credentials : dict
416
417
) -> None :
417
418
"""Cleans the database and table created by insert_data_into_mysql_and_validate_replication.
418
419
419
420
Args:
420
421
ops_test: The ops test framework
421
422
database_name: The name of the database to drop
422
423
table_name: The name of the table to drop
424
+ credentials: The credentials to use to connect to the MySQL database
423
425
"""
424
426
mysql_application_name = get_application_name (ops_test , "mysql" )
425
427
426
- mysql_unit = ops_test . model . applications [ mysql_application_name ]. units [ 0 ]
428
+ assert mysql_application_name , "MySQL application not found"
427
429
428
- server_config_credentials = await get_server_config_credentials ( mysql_unit )
430
+ mysql_unit = ops_test . model . applications [ mysql_application_name ]. units [ 0 ]
429
431
430
432
primary = await get_primary_unit (ops_test , mysql_unit , mysql_application_name )
431
433
primary_address = await get_unit_address (ops_test , primary .name )
@@ -435,17 +437,18 @@ async def clean_up_database_and_table(
435
437
f"DROP DATABASE IF EXISTS `{ database_name } `" ,
436
438
]
437
439
438
- await execute_queries_on_unit (
440
+ execute_queries_on_unit (
439
441
primary_address ,
440
- server_config_credentials ["username" ],
441
- server_config_credentials ["password" ],
442
+ credentials ["username" ],
443
+ credentials ["password" ],
442
444
clean_up_database_and_table_sql ,
443
445
commit = True ,
444
446
)
445
447
446
448
447
449
async def ensure_all_units_continuous_writes_incrementing (
448
450
ops_test : OpsTest ,
451
+ credentials : dict ,
449
452
mysql_units : Optional [List [Unit ]] = None ,
450
453
mysql_application_name : Optional [str ] = None ,
451
454
) -> None :
@@ -464,36 +467,24 @@ async def ensure_all_units_continuous_writes_incrementing(
464
467
465
468
assert primary , "Primary unit not found"
466
469
467
- last_max_written_value = await get_max_written_value_in_database (ops_test , primary )
468
-
469
- select_all_continuous_writes_sql = [f"SELECT * FROM `{ DATABASE_NAME } `.`{ TABLE_NAME } `" ]
470
- server_config_credentials = await get_server_config_credentials (mysql_units [0 ])
470
+ last_max_written_value = await get_max_written_value_in_database (
471
+ ops_test , primary , credentials
472
+ )
471
473
472
- async with ops_test .fast_forward ():
474
+ async with ops_test .fast_forward (fast_interval = "15s" ):
473
475
for attempt in Retrying (stop = stop_after_delay (15 * 60 ), wait = wait_fixed (10 )):
474
476
with attempt :
475
477
# ensure that all units are up to date (including the previous primary)
476
478
for unit in mysql_units :
477
- unit_address = await get_unit_address (ops_test , unit .name )
478
-
479
479
# ensure the max written value is incrementing (continuous writes is active)
480
- max_written_value = await get_max_written_value_in_database (ops_test , unit )
480
+ max_written_value = await get_max_written_value_in_database (
481
+ ops_test , unit , credentials
482
+ )
483
+ logger .info (f"{ max_written_value = } on unit { unit .name } " )
481
484
assert (
482
485
max_written_value > last_max_written_value
483
486
), "Continuous writes not incrementing"
484
487
485
- # ensure that the unit contains all values up to the max written value
486
- all_written_values = await execute_queries_on_unit (
487
- unit_address ,
488
- server_config_credentials ["username" ],
489
- server_config_credentials ["password" ],
490
- select_all_continuous_writes_sql ,
491
- )
492
- for number in range (1 , max_written_value ):
493
- assert (
494
- number in all_written_values
495
- ), f"Missing { number } in database for unit { unit .name } "
496
-
497
488
last_max_written_value = max_written_value
498
489
499
490
0 commit comments