def test_local_folder(self, _, compute_job_raises): channel_name = 'dummy_channel' compute_plan_tag = 'cp1' compute_plan_key = str(uuid.uuid4()) file = 'model.txt' initial_value = 'initial value' updated_value = 'updated value' subtuple = { 'key': str(uuid.uuid4()), 'compute_plan_key': compute_plan_key, 'rank': 1, 'algo': { 'key': 'some key' } } # Write an initial value into the compute plan local folder cp_local_folder = get_cp_local_folder(compute_plan_key) os.makedirs(cp_local_folder, exist_ok=True) with open(os.path.join(cp_local_folder, file), 'w') as x: x.write(initial_value) # Call `do_task`, which will: # 1. write a new value to the subtuple local folder # 2. and then: # - complete successfully (compute_job_raises == False) # - or raise an exception (compute_job_raises == True) with mock.patch('substrapp.ledger.api.query_ledger') as mquery_ledger,\ mock.patch('substrapp.tasks.tasks.generate_command'),\ mock.patch('substrapp.tasks.tasks.save_models'),\ mock.patch('substrapp.tasks.tasks.compute_job') as mcompute_job: def compute_job(*args, **kwargs): for vol in kwargs['volumes']: if vol.endswith('/local'): with open(os.path.join(vol, file), 'w') as x: x.write(updated_value) if compute_job_raises: raise Exception('Boom!') mquery_ledger.return_value = {'tag': compute_plan_tag} mcompute_job.side_effect = compute_job try: build_subtuple_folders(subtuple) do_task(channel_name, subtuple, TRAINTUPLE_TYPE) except Exception: if compute_job_raises: # exception expected pass # Check the compute plan local folder value is correct: # - If do_task did raise an exception then the local value should be unchanged # - If do_task did not raise an exception then the local value should be updated with open(os.path.join(cp_local_folder, file), 'r') as x: content = x.read() self.assertEqual( content, initial_value if compute_job_raises else updated_value)
def test_prepare_data_sample_zip_fail(self): data_sample = DataSample(key=uuid.uuid4(), path=self.data_sample_filename) data_sample.save() subtuple = {'key': 'bar', 'dataset': {'data_sample_keys': ['fake_pk']}} subtuple2 = { 'key': 'bar', 'dataset': { 'data_sample_keys': [data_sample.key] } } with mock.patch('substrapp.models.DataSample.objects.get') as mget: mget.return_value = data_sample subtuple_directory = build_subtuple_folders(subtuple) with self.assertRaises(Exception): prepare_data_sample(subtuple_directory, subtuple) with self.assertRaises(Exception): prepare_data_sample('/fake/directory/failure', subtuple2)
def test_put_data_tar(self): checksum, datasamples_path_from_file = store_datasamples_archive( self.data_sample_tar) key = str(uuid.uuid4()) data_sample = DataSample(key=key, path=datasamples_path_from_file, checksum=checksum) data_sample.save() subtuple = { 'key': 'bar', 'dataset': { 'data_sample_keys': [str(data_sample.key)] } } with mock.patch('substrapp.models.DataSample.objects.get') as mget: mget.return_value = data_sample subtuple_directory = build_subtuple_folders(subtuple) prepare_data_sample(subtuple_directory, subtuple) # check folder has been correctly renamed with key of directory containing uncompressed data_sample self.assertFalse( os.path.exists(os.path.join(MEDIA_ROOT, 'datasamples', 'foo'))) self.assertTrue( os.path.exists(os.path.join(MEDIA_ROOT, 'datasamples', key))) # check subtuple folder has been created and sym links exists self.assertTrue( os.path.exists( os.path.join(MEDIA_ROOT, 'subtuple/bar/data', data_sample.key))) self.assertTrue( os.path.islink( os.path.join(MEDIA_ROOT, 'subtuple/bar/data', data_sample.key))) self.assertTrue( os.path.exists( os.path.join(MEDIA_ROOT, 'subtuple/bar/data', data_sample.key, 'LABEL_0024900.csv'))) self.assertTrue( os.path.exists( os.path.join(MEDIA_ROOT, 'subtuple/bar/data', data_sample.key, 'IMG_0024900.jpg')))
def test_compute_task(self): subtuple_key = 'test_owkin' subtuple = {'key': subtuple_key, 'inModels': None} subtuple_directory = build_subtuple_folders(subtuple) with mock.patch( 'substrapp.tasks.tasks.log_start_tuple') as mlog_start_tuple: mlog_start_tuple.return_value = 'data', 200 for name in ['opener', 'metrics']: with open( os.path.join(subtuple_directory, f'{name}/{name}.py'), 'w') as f: f.write('Hello World') perf = 0.3141592 with open(os.path.join(subtuple_directory, 'pred/perf.json'), 'w') as f: f.write(f'{{"all": {perf}}}') with open(os.path.join(subtuple_directory, 'model/model'), 'w') as f: f.write("MODEL") with mock.patch('substrapp.tasks.tasks.compute_docker') as mcompute_docker, \ mock.patch('substrapp.tasks.tasks.do_task') as mdo_task,\ mock.patch('substrapp.tasks.tasks.prepare_materials') as mprepare_materials, \ mock.patch('substrapp.tasks.tasks.log_success_tuple') as mlog_success_tuple: mcompute_docker.return_value = 'DONE' mprepare_materials.return_value = 'DONE' mdo_task.return_value = 'DONE' mlog_success_tuple.return_value = 'data', 201 compute_task('traintuple', subtuple, None) mlog_success_tuple.return_value = 'data', 404 compute_task('traintuple', subtuple, None) with mock.patch('substrapp.tasks.tasks.log_fail_tuple' ) as mlog_fail_tuple: mdo_task.side_effect = Exception("Test") mlog_fail_tuple.return_value = 'data', 404 with self.assertRaises(Exception) as exc: compute_task('traintuple', subtuple, None) self.assertEqual(str(exc.exception), "Test")
def test_build_subtuple_folders(self): with mock.patch('substrapp.tasks.tasks.getattr') as getattr: getattr.return_value = self.subtuple_path subtuple_key = 'test1234' subtuple = {'key': subtuple_key} subtuple_directory = build_subtuple_folders(subtuple) self.assertTrue(os.path.exists(subtuple_directory)) self.assertEqual( os.path.join(self.subtuple_path, f'subtuple/{subtuple["key"]}'), subtuple_directory) for root, dirs, files in os.walk(subtuple_directory): nb_subfolders = len(dirs) self.assertTrue(5, nb_subfolders)
def test_do_task(self): class FakeSettings(object): def __init__(self): self.MEDIA_ROOT = MEDIA_ROOT subtuple_key = 'test_owkin' subtuple = { 'key': subtuple_key, 'in_models': None, 'algo': { 'key': 'mykey', 'checksum': 'myhash' }, 'dataset': { 'data_sample_keys': [] }, } subtuple_directory = build_subtuple_folders(subtuple) with mock.patch('substrapp.tasks.tasks.settings') as msettings, \ mock.patch('substrapp.tasks.tasks.getattr') as mgetattr: msettings.return_value = FakeSettings() mgetattr.return_value = self.subtuple_path for name in ['opener', 'metrics']: with open( os.path.join(subtuple_directory, f'{name}/{name}.py'), 'w') as f: f.write('Hello World') perf = 0.3141592 with open(os.path.join(subtuple_directory, 'pred/perf.json'), 'w') as f: f.write(f'{{"all": {perf}}}') with open(os.path.join(subtuple_directory, 'output_model/model'), 'w') as f: f.write("MODEL") with mock.patch( 'substrapp.tasks.tasks.compute_job') as mcompute_job: mcompute_job.return_value = 'DONE' do_task(CHANNEL, subtuple, 'traintuple')
def test_prepare_data_sample_zip(self): dir_pkhash, datasamples_path_from_file = store_datasamples_archive( self.data_sample) data_sample = DataSample(pkhash=dir_pkhash, path=datasamples_path_from_file) data_sample.save() subtuple = {'key': 'bar', 'dataset': {'keys': [data_sample.pk]}} with mock.patch('substrapp.models.DataSample.objects.get') as mget: mget.return_value = data_sample subtuple_directory = build_subtuple_folders(subtuple) prepare_data_sample(subtuple_directory, subtuple) # check folder has been correctly renamed with pk of directory containing uncompressed data sample self.assertFalse( os.path.exists(os.path.join(MEDIA_ROOT, 'datasamples', 'foo'))) self.assertTrue( os.path.exists( os.path.join(MEDIA_ROOT, 'datasamples', dir_pkhash))) # check subtuple folder has been created and sym links exists self.assertTrue( os.path.exists( os.path.join(MEDIA_ROOT, 'subtuple/bar/data', data_sample.pk))) self.assertTrue( os.path.islink( os.path.join(MEDIA_ROOT, 'subtuple/bar/data', data_sample.pk))) self.assertTrue( os.path.exists( os.path.join(MEDIA_ROOT, 'subtuple/bar/data', data_sample.pk, 'LABEL_0024900.csv'))) self.assertTrue( os.path.exists( os.path.join(MEDIA_ROOT, 'subtuple/bar/data', data_sample.pk, 'IMG_0024900.jpg')))
def test_do_task(self): class FakeSettings(object): def __init__(self): self.LEDGER = { 'signcert': 'signcert', 'org': 'owkin', 'peer': 'peer' } self.MEDIA_ROOT = MEDIA_ROOT subtuple_key = 'test_owkin' subtuple = {'key': subtuple_key, 'inModels': None} subtuple_directory = build_subtuple_folders(subtuple) with mock.patch('substrapp.tasks.tasks.settings') as msettings, \ mock.patch('substrapp.tasks.tasks.getattr') as mgetattr: msettings.return_value = FakeSettings() mgetattr.return_value = self.subtuple_path for name in ['opener', 'metrics']: with open( os.path.join(subtuple_directory, f'{name}/{name}.py'), 'w') as f: f.write('Hello World') perf = 0.3141592 with open(os.path.join(subtuple_directory, 'pred/perf.json'), 'w') as f: f.write(f'{{"all": {perf}}}') with open(os.path.join(subtuple_directory, 'model/model'), 'w') as f: f.write("MODEL") with mock.patch( 'substrapp.tasks.tasks.compute_docker') as mcompute_docker: mcompute_docker.return_value = 'DONE' do_task(subtuple, 'traintuple')