23
23
from kubetester .omtester import OMTester
24
24
from kubetester .operator import Operator
25
25
from kubetester .opsmanager import MongoDBOpsManager
26
- from pymongo .errors import ServerSelectionTimeoutError
27
26
from pytest import fixture , mark
28
- from tests .conftest import assert_data_got_restored , update_coredns_hosts
27
+ from tests .conftest import (
28
+ assert_data_got_restored ,
29
+ update_coredns_hosts ,
30
+ wait_for_primary ,
31
+ )
29
32
30
33
TEST_DATA = {"_id" : "unique_id" , "name" : "John" , "address" : "Highway 37" , "age" : 30 }
31
34
@@ -362,13 +365,20 @@ def project_one(
362
365
)
363
366
364
367
@fixture (scope = "function" )
365
- def mongodb_multi_one_collection (self , mongodb_multi_one : MongoDBMulti ):
366
- # we instantiate the pymongo client per test to avoid flakiness as the primary and secondary might swap
367
- collection = pymongo .MongoClient (
368
+ def mdb_client (self , mongodb_multi_one : MongoDBMulti ):
369
+ return pymongo .MongoClient (
368
370
mongodb_multi_one .tester (port = MONGODB_PORT ).cnx_string ,
369
371
** mongodb_multi_one .tester (port = MONGODB_PORT ).default_opts ,
370
- )["testdb" ]
371
- return collection ["testcollection" ]
372
+ readPreference = "primary" , # let's read from the primary and not stale data from the secondary
373
+ )
374
+
375
+ @fixture (scope = "function" )
376
+ def mongodb_multi_one_collection (self , mdb_client ):
377
+
378
+ # Ensure primary is available before proceeding
379
+ wait_for_primary (mdb_client )
380
+
381
+ return mdb_client ["testdb" ]["testcollection" ]
372
382
373
383
@fixture (scope = "module" )
374
384
def mongodb_multi_one (
@@ -472,6 +482,7 @@ def test_add_test_data(self, mongodb_multi_one_collection):
472
482
print (e )
473
483
max_attempts -= 1
474
484
time .sleep (6 )
485
+ raise Exception ("❌ Failed to insert test data after multiple attempts" )
475
486
476
487
@mark .e2e_multi_cluster_backup_restore
477
488
def test_mdb_backed_up (self , project_one : OMTester ):
@@ -489,14 +500,17 @@ def test_pit_restore(self, project_one: OMTester):
489
500
now_millis = time_to_millis (datetime .datetime .now ())
490
501
print ("\n Current time (millis): {}" .format (now_millis ))
491
502
492
- pit_datetme = datetime .datetime .now () - datetime .timedelta (seconds = 15 )
493
- pit_millis = time_to_millis (pit_datetme )
494
- print ("Restoring back to the moment 15 seconds ago (millis): {}" .format (pit_millis ))
503
+ backup_completion_time = project_one .get_latest_backup_completion_time ()
504
+ print ("\n backup_completion_time: {}" .format (backup_completion_time ))
505
+
506
+ pit_millis = backup_completion_time + 1500
507
+
508
+ print (f"Restoring back to: { pit_millis } " )
495
509
496
510
project_one .create_restore_job_pit (pit_millis )
497
511
498
512
@mark .e2e_multi_cluster_backup_restore
499
- def test_data_got_restored (self , mongodb_multi_one_collection ):
513
+ def test_data_got_restored (self , mongodb_multi_one_collection , mdb_client ):
500
514
assert_data_got_restored (TEST_DATA , mongodb_multi_one_collection , timeout = 1200 )
501
515
502
516
0 commit comments