forked from fedbiomed/fedbiomed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_round.py
877 lines (736 loc) · 39.3 KB
/
test_round.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
import builtins
import copy
import inspect
import logging
import os
from typing import Any, Dict
import unittest
from unittest.mock import MagicMock, patch
#############################################################
# Import NodeTestCase before importing FedBioMed Module
from testsupport.base_case import NodeTestCase
#############################################################
from testsupport.fake_training_plan import FakeModel
from testsupport.fake_message import FakeMessages
from testsupport.fake_uuid import FakeUuid
from fedbiomed.node.environ import environ
from fedbiomed.node.round import Round
from fedbiomed.common.exceptions import FedbiomedRoundError
from fedbiomed.common.logger import logger
from fedbiomed.common.data import DataManager, DataLoadingPlanMixin, DataLoadingPlan
from fedbiomed.common.constants import DatasetTypes
from testsupport.testing_data_loading_block import ModifyGetItemDP, LoadingBlockTypesForTesting
# Needed to access length of dataset from Round class
class FakeLoader:
dataset = [1, 2, 3, 4, 5]
class TestRound(NodeTestCase):
# values and attributes for dummy classes
URL_MSG = 'http://url/where/my/file?is=True'
@classmethod
def setUpClass(cls):
"""Sets up values in the test once """
# Sets mock environ for the test -------------------
super().setUpClass()
# --------------------------------------------------
# we define here common side effect functions
def node_msg_side_effect(msg: Dict[str, Any]) -> Dict[str, Any]:
fake_node_msg = FakeMessages(msg)
return fake_node_msg
cls.node_msg_side_effect = node_msg_side_effect
@patch('fedbiomed.common.repository.Repository.__init__')
@patch('fedbiomed.node.training_plan_security_manager.TrainingPlanSecurityManager.__init__')
def setUp(self,
tp_security_manager_patch,
repository_patch):
tp_security_manager_patch.return_value = None
repository_patch.return_value = None
# instantiate logger (we will see if exceptions are logged)
# we are setting the logger level to "ERROR" to output
# logs messages
logger.setLevel("ERROR")
# instanciate Round class
self.r1 = Round(training_plan_url='http://somewhere/where/my/model?is_stored=True',
training_plan_class='MyTrainingPlan',
params_url='https://url/to/model/params?ok=True',
training_kwargs={},
training=True
)
params = {'path': 'my/dataset/path',
'dataset_id': 'id_1234'}
self.r1.dataset = params
self.r1.job_id = '1234'
self.r1.researcher_id = '1234'
dummy_monitor = MagicMock()
self.r1.history_monitor = dummy_monitor
self.r2 = Round(training_plan_url='http://a/b/c/model',
training_plan_class='another_training_plan',
params_url='https://to/my/model/params',
training_kwargs={},
training=True
)
self.r2.dataset = params
self.r2.history_monitor = dummy_monitor
@patch('fedbiomed.node.round.Round._split_train_and_test_data')
@patch('fedbiomed.common.message.NodeMessages.format_outgoing_message')
@patch('fedbiomed.common.repository.Repository.upload_file')
@patch('fedbiomed.common.serializer.Serializer.load')
@patch('importlib.import_module')
@patch('fedbiomed.node.training_plan_security_manager.TrainingPlanSecurityManager.check_training_plan_status')
@patch('fedbiomed.common.repository.Repository.download_file')
@patch('uuid.uuid4')
def test_round_01_run_model_training_normal_case(self,
uuid_patch,
repository_download_patch,
tp_security_manager_patch,
import_module_patch,
serialize_load_patch,
repository_upload_patch,
node_msg_patch,
mock_split_test_train_data,
):
"""tests correct execution and message parameters.
Besides tests the training time.
"""
# Tests details:
# - Test 1: normal case scenario where no model_kwargs has been passed during model instantiation
# - Test 2: normal case scenario where model_kwargs has been passed when during model instantiation
FakeModel.SLEEPING_TIME = 1
# initalisation of side effect function
def repository_side_effect(training_plan_url: str, model_name: str):
return 200, 'my_python_model'
class FakeModule:
MyTrainingPlan = FakeModel
another_training_plan = FakeModel
# initialisation of patchers
uuid_patch.return_value = FakeUuid()
repository_download_patch.side_effect = repository_side_effect
tp_security_manager_patch.return_value = (True, {'name': "model_name"})
import_module_patch.return_value = FakeModule
repository_upload_patch.return_value = {'file': TestRound.URL_MSG}
node_msg_patch.side_effect = TestRound.node_msg_side_effect
mock_split_test_train_data.return_value = (FakeLoader, FakeLoader)
# test 1: case where argument `model_kwargs` = None
# action!
msg_test1 = self.r1.run_model_training()
# check results
self.assertTrue(msg_test1.get('success', False))
serialize_load_patch.assert_called_once()
self.assertEqual(msg_test1.get('params_url', False), TestRound.URL_MSG)
self.assertEqual(msg_test1.get('command', False), 'train')
# This test is not relevant since it just tests SLEEPING_TIME added in FakeModel
# and it fails in macosx-m1
# timing test - does not always work with self.assertAlmostEqual
# self.assertGreaterEqual(
# msg_test1.get('timing', {'rtime_training': 0}).get('rtime_training'),
# FakeModel.SLEEPING_TIME
# )
# self.assertLess(
# msg_test1.get('timing', {'rtime_training': 0}).get('rtime_training'),
# FakeModel.SLEEPING_TIME * 1.1
# )
# test 2: redo test 1 but with the case where `model_kwargs` != None
FakeModel.SLEEPING_TIME = 0
self.r2.model_kwargs = {'param1': 1234,
'param2': [1, 2, 3, 4],
'param3': None}
serialize_load_patch.reset_mock()
msg_test2 = self.r2.run_model_training()
# check values in message (output of `run_model_training`)
self.assertTrue(msg_test2.get('success', False))
serialize_load_patch.assert_called_once()
self.assertEqual(TestRound.URL_MSG, msg_test2.get('params_url', False))
self.assertEqual('train', msg_test2.get('command', False))
@patch('fedbiomed.node.round.Round._split_train_and_test_data')
@patch('fedbiomed.common.message.NodeMessages.format_incoming_message')
@patch('fedbiomed.common.repository.Repository.upload_file')
@patch('fedbiomed.common.serializer.Serializer.dump')
@patch('fedbiomed.common.serializer.Serializer.load')
@patch('importlib.import_module')
@patch('fedbiomed.node.training_plan_security_manager.TrainingPlanSecurityManager.check_training_plan_status')
@patch('fedbiomed.common.repository.Repository.download_file')
@patch('uuid.uuid4')
def test_round_02_run_model_training_correct_model_calls(self,
uuid_patch,
repository_download_patch,
tp_security_manager_patch,
import_module_patch,
serialize_load_patch,
serialize_dump_patch,
repository_upload_patch,
node_msg_patch,
mock_split_train_and_test_data):
"""tests if all methods of `model` have been called after instanciating
(in run_model_training)"""
# `run_model_training`, when no issues are found
# methods tested:
# - model.load
# - model.save
# - model.training_routine
# - model.after_training_params
# - model.set_dataset_path
FakeModel.SLEEPING_TIME = 0
MODEL_NAME = "my_model"
MODEL_PARAMS = {"coef": [1, 2, 3, 4]}
class FakeModule:
MyTrainingPlan = FakeModel
uuid_patch.return_value = FakeUuid()
repository_download_patch.return_value = (200, MODEL_NAME)
tp_security_manager_patch.return_value = (True, {'name': "model_name"})
import_module_patch.return_value = FakeModule
repository_upload_patch.return_value = {'file': TestRound.URL_MSG}
node_msg_patch.side_effect = TestRound.node_msg_side_effect
mock_split_train_and_test_data.return_value = (FakeLoader, FakeLoader)
self.r1.training_kwargs = {}
self.r1.dataset = {'path': 'my/dataset/path',
'dataset_id': 'id_1234'}
# arguments of `save` method
_model_filename = os.path.join(environ["TMP_DIR"], "node_params_1234.mpk")
_model_results = {
'researcher_id': self.r1.researcher_id,
'job_id': self.r1.job_id,
'model_weights': MODEL_PARAMS,
'node_id': environ['NODE_ID'],
'optimizer_args': {},
'encrypted': False,
}
# define context managers for each model method
# we are mocking every methods of our dummy model FakeModel,
# and we will check if there are called when running
# `run_model_training`
with (
patch.object(FakeModel, 'set_dataset_path') as mock_set_dataset,
patch.object(FakeModel, 'training_routine') as mock_training_routine,
patch.object(FakeModel, 'after_training_params', return_value=MODEL_PARAMS) as mock_after_training_params, # noqa
):
msg = self.r1.run_model_training()
self.assertTrue(msg.get("success"))
# Check that the model weights were loaded.
serialize_load_patch.assert_called_once()
# Check set train and test data split function is called
# Set dataset is called in set_train_and_test_data
# mock_set_dataset.assert_called_once_with(self.r1.dataset.get('path'))
mock_split_train_and_test_data.assert_called_once()
# Since set training data return None, training_routine should be called as None
mock_training_routine.assert_called_once_with( history_monitor=self.r1.history_monitor,
node_args=None)
# Check that the model weights were saved.
mock_after_training_params.assert_called_once()
serialize_dump_patch.assert_called_once_with(_model_results, _model_filename)
@patch('fedbiomed.node.round.Round._split_train_and_test_data')
@patch('fedbiomed.common.message.NodeMessages.format_incoming_message')
@patch('fedbiomed.common.repository.Repository.upload_file')
@patch('fedbiomed.node.training_plan_security_manager.TrainingPlanSecurityManager.check_training_plan_status')
@patch('fedbiomed.common.serializer.Serializer.load')
@patch('fedbiomed.common.repository.Repository.download_file')
@patch('uuid.uuid4')
def test_round_03_test_run_model_training_with_real_model(self,
uuid_patch,
repository_download_patch,
serialize_load_patch,
tp_security_manager_patch,
repository_upload_patch,
node_msg_patch,
mock_split_train_and_test_data):
"""tests normal case scenario with a real model file"""
FakeModel.SLEEPING_TIME = 0
# initialisation of patchers
uuid_patch.return_value = FakeUuid()
repository_download_patch.return_value = (200, 'my_python_model')
tp_security_manager_patch.return_value = (True, {'name': "model_name"})
repository_upload_patch.return_value = {'file': TestRound.URL_MSG}
node_msg_patch.side_effect = TestRound.node_msg_side_effect
mock_split_train_and_test_data.return_value = (True, True)
# create dummy_model
dummy_training_plan_test = "\n".join([
"from testsupport.fake_training_plan import FakeModel",
"class MyTrainingPlan(FakeModel):",
" dataset = [1, 2, 3, 4]",
" def set_data_loaders(self, *args, **kwargs):",
" self.testing_data_loader = MyTrainingPlan",
" self.training_data_loader = MyTrainingPlan",
])
module_file_path = os.path.join(environ['TMP_DIR'],
'training_plan_' + str(FakeUuid.VALUE) + '.py')
# creating file for toring dummy training plan
with open(module_file_path, "w", encoding="utf-8") as file:
file.write(dummy_training_plan_test)
# action
msg_test = self.r1.run_model_training()
# checks
serialize_load_patch.assert_called_once_with('my_python_model')
self.assertTrue(msg_test.get('success', False))
self.assertEqual(TestRound.URL_MSG, msg_test.get('params_url', False))
self.assertEqual('train', msg_test.get('command', False))
# remove model file
os.remove(module_file_path)
@patch('fedbiomed.node.round.Round._split_train_and_test_data')
@patch('fedbiomed.common.message.NodeMessages.format_incoming_message')
@patch('fedbiomed.common.repository.Repository.upload_file')
@patch('fedbiomed.node.training_plan_security_manager.TrainingPlanSecurityManager.check_training_plan_status')
@patch('fedbiomed.common.repository.Repository.download_file')
@patch('uuid.uuid4')
def test_rounds_04_run_model_training_bad_http_status(self,
uuid_patch,
repository_download_patch,
tp_security_manager_patch,
repository_upload_patch,
node_msg_patch,
mock_split_train_and_test_data):
"""tests failures and exceptions during the download file process
(in run_model_training)"""
# Tests details:
# Test 1: tests case where downloading model file fails
# Test 2: tests case where downloading model paraeters fails
FakeModel.SLEEPING_TIME = 0
# initalisation of side effects functions
def download_repo_answers_gene() -> int:
"""Generates different values of connections:
First one is HTTP code 200, second one is HTTP code 404
Raises: StopIteration, if called more than twice
"""
for i in [200, 404]:
yield i
def repository_side_effect_test_1(training_plan_url: str, model_name: str):
"""Returns HTTP 404 error, mimicking an error happened during
download process"""
return 404, 'my_python_model'
download_repo_answers_iter = iter(download_repo_answers_gene())
# initialisation of patchers
uuid_patch.return_value = FakeUuid()
repository_download_patch.side_effect = repository_side_effect_test_1
tp_security_manager_patch.return_value = (True, {'name': "model_name"})
repository_upload_patch.return_value = {'file': TestRound.URL_MSG}
node_msg_patch.side_effect = TestRound.node_msg_side_effect
mock_split_train_and_test_data.return_value = None
# test 1: case where first call to `Repository.download` generates HTTP
# status 404 (when downloading model_file)
with self.assertLogs('fedbiomed', logging.ERROR) as captured:
msg_test_1 = self.r1.run_model_training()
# checks:
# check if error message generated and logged is the same as the one
# collected in the output of `run_model_training`
self.assertEqual(
captured.records[-1].getMessage(),
msg_test_1.get('msg'))
self.assertFalse(msg_test_1.get('success', True))
# test 2: case where second call to `Repository.download` generates HTTP
# status 404 (when downloading params_file)
# overwriting side effect function for second test:
def repository_side_effect_2(training_plan_url: str, model_name: str):
"""Returns different values when called
First call: returns (200, 'my_python_model') mimicking a first download
that happened without encoutering any issues
Second call: returns (404, 'my_python_model') mimicking a second download
that failed
Third Call (or more): raises StopIteration (due to generator)
"""
val = next(download_repo_answers_iter)
return val, 'my_python_model'
repository_download_patch.side_effect = repository_side_effect_2
# action
with self.assertLogs('fedbiomed', logging.ERROR) as captured:
msg_test_2 = self.r1.run_model_training()
# checks:
# check if error message generated and logged is the same as the one
# collected in the output of `run_model_training`
self.assertEqual(
captured.records[-1].getMessage(),
msg_test_2.get('msg'))
self.assertFalse(msg_test_2.get('success', True))
# test 3: check if unknown exception is raised and caught during the download
# files process
def repository_side_effect_3(training_plan_url: str, model_name: str):
raise Exception('mimicking an error during download files process')
repository_download_patch.side_effect = repository_side_effect_3
tp_security_manager_patch.return_value = (True, {'name': "model_name"})
# action
with self.assertLogs('fedbiomed', logging.ERROR) as captured:
msg_test_3 = self.r1.run_model_training()
# checks
self.assertEqual(
captured.records[-1].getMessage(),
msg_test_3.get('msg'))
self.assertFalse(msg_test_3.get('success', True))
@patch('fedbiomed.node.training_plan_security_manager.TrainingPlanSecurityManager.check_training_plan_status')
@patch('fedbiomed.common.repository.Repository.download_file')
@patch('uuid.uuid4')
def test_round_05_run_model_training_model_not_approved(self,
uuid_patch,
repository_download_patch,
tp_security_manager_patch):
FakeModel.SLEEPING_TIME = 0
# initialisation of patchers
uuid_patch.return_value = FakeUuid()
repository_download_patch.return_value = (200, 'my_python_model')
tp_security_manager_patch.return_value = (False, {'name': "model_name"})
# action
with self.assertLogs('fedbiomed', logging.ERROR) as captured:
msg_test = self.r1.run_model_training()
# checks
self.assertEqual(
captured.records[-1].getMessage(),
msg_test.get('msg'))
self.assertFalse(msg_test.get('success', True))
@patch('fedbiomed.node.round.Round._split_train_and_test_data')
@patch('fedbiomed.common.message.NodeMessages.format_incoming_message')
@patch('fedbiomed.common.repository.Repository.upload_file')
@patch('fedbiomed.node.training_plan_security_manager.TrainingPlanSecurityManager.check_training_plan_status')
@patch('fedbiomed.common.repository.Repository.download_file')
@patch('uuid.uuid4')
def test_round_06_run_model_training_import_error(self,
uuid_patch,
repository_download_patch,
tp_security_manager_patch,
repository_upload_patch,
node_msg_patch,
mock_split_train_and_test_data):
"""tests case where the import/loading of the model have failed"""
FakeModel.SLEEPING_TIME = 0
# initialisation of patchers
uuid_patch.return_value = FakeUuid()
repository_download_patch.return_value = (200, 'my_python_model')
tp_security_manager_patch.return_value = (True, {'name': "model_name"})
repository_upload_patch.return_value = {'file': TestRound.URL_MSG}
node_msg_patch.side_effect = TestRound.node_msg_side_effect
mock_split_train_and_test_data.return_value = None
# test 1: tests raise of exception during model import
def exec_side_effect(*args, **kwargs):
"""Overriding the behaviour of `exec` builitin function,
and raises an Exception"""
raise Exception("mimicking an exception happening when loading file")
# patching builtin objects & looking for generated logs
# NB: this is the only way I have found to use
# both patched bulitins functions and assertLogs
with (self.assertLogs('fedbiomed', logging.ERROR) as captured,
patch.object(builtins, 'exec', return_value = None),
patch.object(builtins, 'eval') as eval_patch):
eval_patch.side_effect = exec_side_effect
msg_test_1 = self.r1.run_model_training()
# checks:
# check if error message generated and logged is the same as the one
# collected in the output of `run_model_training`
self.assertEqual(
captured.records[-1].getMessage(),
msg_test_1.get('msg'))
self.assertFalse(msg_test_1.get('success', True))
# test 2: tests raise of Exception during loading parameters
# into model instance
# Here we creating a new class inheriting from the FakeModel,
# but overriding `load` through classes inheritance
# when `load` is called, an Exception will be raised
#
class FakeModelRaiseExceptionWhenLoading(FakeModel):
def load(self, **kwargs):
"""Mimicks an exception happening in the `load`
method
Raises:
Exception:
"""
raise Exception('mimicking an error happening during model training')
# action
with (self.assertLogs('fedbiomed', logging.ERROR) as captured,
patch.object(builtins, 'exec', return_value=None),
patch.object(builtins, 'eval', return_value=FakeModelRaiseExceptionWhenLoading)
):
msg_test_2 = self.r1.run_model_training()
# checks:
# check if error message generated and logged is the same as the one
# collected in the output of `run_model_training`
self.assertEqual(
captured.records[-1].getMessage(),
msg_test_2.get('msg'))
self.assertFalse(msg_test_2.get('success', True))
# test 3: tests raise of Exception during model training
# into model instance
# Here we are creating a new class inheriting from the FakeModel,
# but overriding `training_routine` through classes inheritance
# when `training_routine` is called, an Exception will be raised
#
class FakeModelRaiseExceptionInTraining(FakeModel):
def training_routine(self, **kwargs):
"""Mimicks an exception happening in the `training_routine`
method
Raises:
Exception:
"""
raise Exception('mimicking an error happening during model training')
# action
with (self.assertLogs('fedbiomed', logging.ERROR) as captured,
patch.object(builtins, 'exec', return_value=None),
patch.object(builtins, 'eval', return_value= FakeModelRaiseExceptionInTraining)):
msg_test_3 = self.r1.run_model_training()
# checks :
# check if error message generated and logged is the same as the one
# collected in the output of `run_model_training``
self.assertEqual(
captured.records[-1].getMessage(),
msg_test_3.get('msg'))
self.assertFalse(msg_test_3.get('success', True))
@patch('fedbiomed.node.round.Round._split_train_and_test_data')
@patch('fedbiomed.common.message.NodeMessages.format_incoming_message')
@patch('fedbiomed.common.repository.Repository.upload_file')
@patch('fedbiomed.node.training_plan_security_manager.TrainingPlanSecurityManager.check_training_plan_status')
@patch('fedbiomed.common.repository.Repository.download_file')
@patch('uuid.uuid4')
def test_round_07_run_model_training_upload_file_fails(self,
uuid_patch,
repository_download_patch,
tp_security_manager_patch,
repository_upload_patch,
node_msg_patch,
mock_split_train_and_test_data):
"""tests case where uploading model parameters file fails"""
FakeModel.SLEEPING_TIME = 0
# declaration of side effect functions
def upload_side_effect(*args, **kwargs):
"""Raises an exception when calling this function"""
raise Exception("mimicking an error happening during upload")
# initialisation of patchers
uuid_patch.return_value = FakeUuid()
repository_download_patch.return_value = (200, 'my_python_model')
tp_security_manager_patch.return_value = (True, {'name': "model_name"})
repository_upload_patch.side_effect = upload_side_effect
node_msg_patch.side_effect = TestRound.node_msg_side_effect
mock_split_train_and_test_data.return_value = None
# action
with (self.assertLogs('fedbiomed', logging.ERROR) as captured,
patch.object(builtins, 'exec', return_value=None),
patch.object(builtins, 'eval', return_value=FakeModel)
):
msg_test = self.r1.run_model_training()
# checks if message logged is the message returned as a reply
self.assertEqual(
captured.records[-1].getMessage(),
msg_test.get('msg'))
self.assertFalse(msg_test.get('success', True))
@patch('fedbiomed.node.round.Round._split_train_and_test_data')
@patch('fedbiomed.common.message.NodeMessages.format_incoming_message')
@patch('fedbiomed.common.repository.Repository.upload_file')
@patch('builtins.eval')
@patch('builtins.exec')
@patch('fedbiomed.node.training_plan_security_manager.TrainingPlanSecurityManager.check_training_plan_status')
@patch('fedbiomed.common.repository.Repository.download_file')
@patch('uuid.uuid4')
def test_round_08_run_model_training_bad_training_argument(self,
uuid_patch,
repository_download_patch,
tp_security_manager_patch,
builtin_exec_patch,
builtin_eval_patch,
repository_upload_patch,
node_msg_patch,
mock_split_train_and_test_data):
"""tests case where training plan contains node_side arguments"""
FakeModel.SLEEPING_TIME = 1
# initialisation of patchers
uuid_patch.return_value = FakeUuid()
repository_download_patch.return_value = (200, "my_model")
tp_security_manager_patch.return_value = (True, {'name': "model_name"})
builtin_exec_patch.return_value = None
builtin_eval_patch.return_value = FakeModel
repository_upload_patch.return_value = {'file': TestRound.URL_MSG}
node_msg_patch.side_effect = TestRound.node_msg_side_effect
mock_split_train_and_test_data.return_value = None
@patch('inspect.signature')
def test_round_09_data_loading_plan(self,
patch_inspect_signature,
):
"""Test that Round correctly handles a DataLoadingPlan during training"""
class MyDataset(DataLoadingPlanMixin):
def __init__(self):
super().__init__()
def __getitem__(self, item):
return self.apply_dlb('orig-value', LoadingBlockTypesForTesting.MODIFY_GETITEM)
@staticmethod
def get_dataset_type() -> DatasetTypes:
return DatasetTypes.TEST
patch_inspect_signature.return_value = inspect.Signature(parameters={})
my_dataset = MyDataset()
data_loader_mock = MagicMock()
data_loader_mock.dataset = my_dataset
data_manager_mock = MagicMock(spec=DataManager)
data_manager_mock.split = MagicMock()
data_manager_mock.split.return_value = (data_loader_mock, None)
data_manager_mock.dataset = my_dataset
r3 = Round(training_kwargs={})
r3.training_plan = MagicMock()
r3.training_plan.training_data.return_value = data_manager_mock
training_data_loader, _ = r3._split_train_and_test_data(test_ratio=0.)
dataset = training_data_loader.dataset
self.assertEqual(dataset[0], 'orig-value')
dlp = DataLoadingPlan({LoadingBlockTypesForTesting.MODIFY_GETITEM: ModifyGetItemDP()})
r4 = Round(training_kwargs={},
dlp_and_loading_block_metadata=dlp.serialize()
)
r4.training_plan = MagicMock()
r4.training_plan.training_data.return_value = data_manager_mock
training_data_loader, _ = r4._split_train_and_test_data(test_ratio=0.)
dataset = training_data_loader.dataset
self.assertEqual(dataset[0], 'modified-value')
@patch('fedbiomed.common.serializer.Serializer.load')
@patch('fedbiomed.common.repository.Repository.download_file')
@patch('uuid.uuid4')
def test_round_10_download_aggregator_args(
self, uuid_patch, repository_download_patch, serializer_load_patch,
):
uuid_patch.return_value = FakeUuid()
repository_download_patch.side_effect = ((200, "my_model_var"+ str(i)) for i in range(3, 5))
serializer_load_patch.side_effect = (i for i in range(3, 5))
success, _ = self.r1.download_aggregator_args()
self.assertEqual(success, True)
# if attribute `aggregator_args` is None, then do nothing
repository_download_patch.assert_not_called()
aggregator_args = {'var1': 1,
'var2': [1, 2, 3, 4],
'var3': {'url': 'http://to/var/3',},
'var4': {'url': 'http://to/var/4'}}
self.r1.aggregator_args = copy.deepcopy(aggregator_args)
success, error_msg = self.r1.download_aggregator_args()
self.assertEqual(success, True)
self.assertEqual(error_msg, '')
for var in ('var1', 'var2'):
self.assertEqual(self.r1.aggregator_args[var], aggregator_args[var])
for var in ('var3', 'var4'):
serializer_load_patch.assert_any_call(f"my_model_{var}")
self.assertEqual(self.r1.aggregator_args[var], int(var[-1]))
@patch('fedbiomed.common.repository.Repository.download_file')
@patch('uuid.uuid4')
def test_round_11_download_file(self, uuid_patch, repository_download_patch):
uuid_patch.return_value = FakeUuid()
repository_download_patch.return_value = (200, "my_model")
file_path = 'path/to/my/downloaded/files'
success, param_path, msg = self.r1.download_file('http://some/url/to/some/files', file_path)
self.assertEqual(success, True)
self.assertEqual(param_path, 'my_model')
self.assertEqual(msg, '')
@patch("fedbiomed.node.round.BPrimeManager.get")
@patch("fedbiomed.node.round.SKManager.get")
def test_round_12_configure_secagg(self,
servkey_get,
biprime_get
):
"""Tests round secure aggregation configuration"""
servkey_get.return_value = {"context": {}}
biprime_get.return_value = {"context": {}}
environ["SECURE_AGGREGATION"] = True
result = self.r1._configure_secagg(
secagg_random=1.5,
secagg_biprime_id='123',
secagg_servkey_id='123'
)
self.assertTrue(result)
result = self.r1._configure_secagg(
secagg_random=None,
secagg_biprime_id=None,
secagg_servkey_id=None
)
self.assertFalse(result)
with self.assertRaises(FedbiomedRoundError):
self.r1._configure_secagg(
secagg_random=None,
secagg_biprime_id="1234",
secagg_servkey_id=None)
with self.assertRaises(FedbiomedRoundError):
self.r1._configure_secagg(
secagg_random=None,
secagg_biprime_id="1234",
secagg_servkey_id="1223")
with self.assertRaises(FedbiomedRoundError):
self.r1._configure_secagg(
secagg_random=None,
secagg_biprime_id=None,
secagg_servkey_id="1223")
with self.assertRaises(FedbiomedRoundError):
servkey_get.return_value = None
biprime_get.return_value = {"context": {}}
self.r1._configure_secagg(
secagg_random=1.5,
secagg_biprime_id='123',
secagg_servkey_id='123'
)
with self.assertRaises(FedbiomedRoundError):
servkey_get.return_value = {"context": {}}
biprime_get.return_value = None
self.r1._configure_secagg(
secagg_random=1.5,
secagg_biprime_id='123',
secagg_servkey_id='123'
)
# If node forces using secagg
environ["SECURE_AGGREGATION"] = True
environ["FORCE_SECURE_AGGREGATION"] = True
with self.assertRaises(FedbiomedRoundError):
self.r1._configure_secagg(
secagg_random=None,
secagg_biprime_id=None,
secagg_servkey_id=None
)
# If secagg is not activated
environ["SECURE_AGGREGATION"] = False
environ["FORCE_SECURE_AGGREGATION"] = False
with self.assertRaises(FedbiomedRoundError):
self.r1._configure_secagg(
secagg_random=1.5,
secagg_biprime_id='123',
secagg_servkey_id='123'
)
@patch('fedbiomed.node.round.Round._split_train_and_test_data')
@patch('fedbiomed.common.message.NodeMessages.format_incoming_message')
@patch('fedbiomed.common.repository.Repository.upload_file')
@patch('fedbiomed.common.serializer.Serializer.load')
@patch('importlib.import_module')
@patch('fedbiomed.node.training_plan_security_manager.TrainingPlanSecurityManager.check_training_plan_status')
@patch('fedbiomed.common.repository.Repository.download_file')
@patch('uuid.uuid4')
@patch("fedbiomed.node.round.BPrimeManager.get")
@patch("fedbiomed.node.round.SKManager.get")
def test_round_13_run_model_training_secagg(self,
servkey_get,
biprime_get,
uuid_patch,
repository_download_patch,
tp_security_manager_patch,
import_module_patch,
serialize_load_patch,
repository_upload_patch,
node_msg_patch,
mock_split_test_train_data):
"""tests correct execution and message parameters.
Besides tests the training time.
"""
# Tests details:
# - Test 1: normal case scenario where no model_kwargs has been passed during model instantiation
# - Test 2: normal case scenario where model_kwargs has been passed when during model instantiation
FakeModel.SLEEPING_TIME = 1
# initalisation of side effect function
def repository_side_effect(training_plan_url: str, model_name: str):
return 200, 'my_python_model'
class M(FakeModel):
def after_training_params(self, flatten):
return [0.1,0.2,0.3,0.4,0.5]
class FakeModule:
MyTrainingPlan = M
another_training_plan = M
# initialisation of patchers
uuid_patch.return_value = FakeUuid()
repository_download_patch.side_effect = repository_side_effect
tp_security_manager_patch.return_value = (True, {'name': "model_name"})
import_module_patch.return_value = FakeModule
repository_upload_patch.return_value = {'file': TestRound.URL_MSG}
node_msg_patch.side_effect = TestRound.node_msg_side_effect
mock_split_test_train_data.return_value = (FakeLoader, FakeLoader)
# Secagg configuration
servkey_get.return_value = {"parties": ["r-1", "n-1", "n-2"], "context" : {"server_key": 123445}}
biprime_get.return_value = {"parties": ["r-1", "n-1", "n-2"], "context" : {"biprime": 123445}}
environ["SECURE_AGGREGATION"] = True
environ["FORCE_SECURE_AGGREGATION"] = True
msg_test1 = self.r1.run_model_training(secagg_arguments={
'secagg_random': 1.12,
'secagg_servkey_id': '1234',
'secagg_biprime_id': '1234',
})
# Back to normal
environ["SECURE_AGGREGATION"] = False
environ["FORCE_SECURE_AGGREGATION"] = False
if __name__ == '__main__': # pragma: no cover
unittest.main()