Exemplo n.º 1
0
    def test_input_location_is_not_a_tar___exception_is_raised(self):
        with TemporaryDirectory() as media_root:
            with SettingsPatcher(MEDIA_ROOT=media_root):
                Path(media_root, 'not-tar-file.tar').touch()

                self.assertRaises(InvalidInputsException, start_analysis, {},
                                  'not-tar-file.tar')
Exemplo n.º 2
0
    def test_custom_model_runner_does_not_exist___generate_losses_is_called_output_files_are_tared_up(
            self):
        with TemporaryDirectory() as media_root, \
                TemporaryDirectory() as model_data_dir, \
                TemporaryDirectory() as run_dir, \
                TemporaryDirectory() as work_dir:
            with SettingsPatcher(
                    MODEL_SUPPLIER_ID='supplier',
                    MODEL_ID='model',
                    MODEL_VERSION_ID='version',
                    MEDIA_ROOT=media_root,
                    MODEL_DATA_DIRECTORY=model_data_dir,
                    WORKING_DIRECTORY=work_dir,
            ):
                self.create_tar(str(Path(media_root, 'location.tar')))
                Path(media_root, 'analysis_settings.json').touch()
                Path(run_dir, 'output').mkdir(parents=True)
                Path(model_data_dir, 'supplier', 'model',
                     'version').mkdir(parents=True)

                cmd_instance = Mock()
                cmd_instance.stdout = b'output'
                cmd_instance.stderr = b'errors'

                @contextmanager
                def fake_run_dir(*args, **kwargs):
                    yield run_dir

                with patch('src.model_execution_worker.tasks.subprocess.run', Mock(return_value=cmd_instance)) as cmd_mock, \
                        patch('src.model_execution_worker.tasks.get_worker_versions', Mock(return_value='')), \
                        patch('src.model_execution_worker.tasks.filestore.compress') as tarfile, \
                        patch('src.model_execution_worker.tasks.TemporaryDir', fake_run_dir):

                    output_location, log_location, error_location, returncode = start_analysis(
                        os.path.join(media_root, 'analysis_settings.json'),
                        os.path.join(media_root, 'location.tar'),
                    )
                    test_env = os.environ.copy()
                    cmd_mock.assert_called_once_with([
                        'oasislmf',
                        'model',
                        'generate-losses',
                        '--oasis-files-dir',
                        os.path.join(run_dir, 'input'),
                        '--config',
                        get_oasislmf_config_path(
                            settings.get('worker', 'model_id')),
                        '--model-run-dir',
                        run_dir,
                        '--analysis-settings-json',
                        os.path.join(media_root, 'analysis_settings.json'),
                        '--ktools-fifo-relative',
                        '--verbose',
                    ],
                                                     stderr=subprocess.PIPE,
                                                     stdout=subprocess.PIPE,
                                                     env=test_env)
                    tarfile.assert_called_once_with(
                        output_location, os.path.join(run_dir, 'output'),
                        'output')
Exemplo n.º 3
0
 def test_settings_file_does_not_exist___exception_is_raised(self):
     with TemporaryDirectory() as media_root:
         with SettingsPatcher(MEDIA_ROOT=media_root):
             self.create_tar(str(Path(media_root, 'location.tar')))
             with self.assertRaises(MissingInputsException):
                 start_analysis(input_location=os.path.join(
                     media_root, 'location.tar'),
                                analysis_settings=os.path.join(
                                    media_root, 'analysis_settings.json'))
Exemplo n.º 4
0
    def test_custom_model_runner_does_not_exist___generate_losses_is_called_output_files_are_tared_up(
            self):
        with TemporaryDirectory() as media_root, \
                TemporaryDirectory() as model_data_dir, \
                TemporaryDirectory() as work_dir:
            with SettingsPatcher(
                    MODEL_SUPPLIER_ID='supplier',
                    MODEL_ID='model',
                    MODEL_VERSION_ID='version',
                    MEDIA_ROOT=media_root,
                    MODEL_DATA_DIRECTORY=model_data_dir,
                    WORKING_DIRECTORY=work_dir,
            ):
                self.create_tar(str(Path(media_root, 'location.tar')))
                Path(model_data_dir, 'supplier', 'model',
                     'version').mkdir(parents=True)

                cmd_instance = Mock()
                with patch('src.model_execution_worker.tasks.GenerateLossesCmd', Mock(return_value=cmd_instance)) as cmd_mock, \
                        patch('src.model_execution_worker.tasks.tarfile') as tarfile:
                    output_location = start_analysis(
                        'analysis_settings.json',
                        'location.tar',
                    )
                    cmd_mock.assert_called_once_with(argv=[
                        '--oasis-files-dir', ANY, '--config',
                        get_oasislmf_config_path(
                            settings.get('worker', 'model_id')),
                        '--model-run-dir', ANY, '--analysis-settings-json',
                        'analysis_settings.json', '--ktools-num-processes',
                        settings.get('worker', 'KTOOLS_BATCH_COUNT'),
                        '--ktools-alloc-rule-gul',
                        settings.get('worker', 'KTOOLS_ALLOC_RULE_GUL'),
                        '--ktools-alloc-rule-il',
                        settings.get('worker', 'KTOOLS_ALLOC_RULE_IL'),
                        '--ktools-alloc-rule-ri',
                        settings.get('worker', 'KTOOLS_ALLOC_RULE_RI'),
                        '--ktools-fifo-relative'
                    ])
                    cmd_instance.run.assert_called_once_with()
                    self.assertEqual(
                        tarfile.open.call_args_list[1][0],
                        (str(Path(media_root, output_location)), 'w:gz'))
Exemplo n.º 5
0
 def test_input_tar_file_does_not_exist___exception_is_raised(self):
     with TemporaryDirectory() as media_root:
         with SettingsPatcher(MEDIA_ROOT=media_root):
             self.assertRaises(MissingInputsException, start_analysis, {},
                               'non-existant-location.tar')