def test_get_project_config_when_not_found(self, configuration_mock, io_mock): io_mock.exists.return_value = False configuration_mock.get_config_project_filename.return_value = "project.yaml" with self.assertRaises(FileNotFoundError) as context: config.get_project_config("/some/path") self.assertEqual( "[Errno 2] No such file or directory: '/some/path/project.yaml'", str(context.exception))
def advance_workflow(current_step): """Advance the workflow for all applications able to be advanced.""" Logger.info( {"current_step": current_step}, "[/api/workflow/progress/<current_step>] Workflow advance started", ) try: # Creating a copy of the working configuration directory config_dir = config_lib.create_temporary_config_copy() config_lib.change_environment( Configuration.get_config_default_branch(), config_dir) project_config = config_lib.get_project_config(config_dir) report_status, report = workflow_lib.advance_workflow( config_dir, project_config, current_step) status, message = _get_status_and_message(report_status) # Clean up non_blocking_clean( config_dir, message_prefix="[/api/workflow/progress/<current_step>]") # HTTP Response Logger.info( { "current_step": current_step, "report": report }, f"[/api/workflow/progress/<current_step>] {message}", ) return ( { "current_step": current_step, "report": report, "message": message }, status, ) # pylint: disable=broad-except except Exception as err: Logger.error( { "current_step": current_step, "err": str(err) }, "[/api/workflow/progress/<current_step>] Workflow advance failed", ) # HTTP Response return ( { "current_step": current_step, "err": str(err), "message": "Workflow advance failed", }, HTTPStatus.INTERNAL_SERVER_ERROR, )
def test_get_project_config(self, configuration_mock, read_yaml_mock, io_mock): # Mocks configuration_mock.get_config_project_filename.return_value = "project.yaml" io_mock.exists.return_value = True read_yaml_mock.return_value = { "domain": "website.com", "variables": { "ope": { "VARIABLE_OPE_1": "ope_1", "VARIABLE_OPE_2": "ope_2" }, "app": { "VARIABLE_APP_1": "app_1", "VARIABLE_APP_2": "app_2" }, }, } # Test environment_config = config.get_project_config( "tests/__fixtures__/config") # Assertions io_mock.exists.assert_called_once_with( "tests/__fixtures__/config/project.yaml") read_yaml_mock.assert_called_once_with( "tests/__fixtures__/config/project.yaml") self.assertEqual( environment_config, { "domain": "website.com", "variables": { "ope": { "VARIABLE_OPE_1": "ope_1", "VARIABLE_OPE_2": "ope_2" }, "app": { "VARIABLE_APP_1": "app_1", "VARIABLE_APP_2": "app_2" }, }, }, )
def test_get_project_config(mocker): mocker.patch.object(Configuration, "get_config_path", return_value="tests/__fixtures__/config") environment_config = config.get_project_config() assert environment_config == { "domain": "website.com", "variables": { "ope": { "VARIABLE_OPE_1": "ope_1", "VARIABLE_OPE_2": "ope_2" }, "app": { "VARIABLE_APP_1": "app_1", "VARIABLE_APP_2": "app_2" }, }, }
def test_get_project_config_when_not_found(mocker): mocker.patch.object(io, "exists", return_value=False) with pytest.raises(FileNotFoundError): config.get_project_config()
def init_workflow(organization, app): """Initialize the workflow of an application.""" Logger.info( {"app": app, "organization": organization}, "[/api/workflow/init/:org/:app] Workflow initialization started", ) report = None try: # Retrieve project configuration config_dir = config_lib.create_temporary_config_copy() config_lib.change_environment(Configuration.get_config_default_branch(), config_dir) project_config = config_lib.get_project_config(config_dir) git_provider = get_git_provider(project_config) report_status, report = workflow_lib.init_workflow(organization, app, git_provider) if report_status == workflow_lib.WorkflowInitStatus.FAIL: status = HTTPStatus.INTERNAL_SERVER_ERROR message = "Workflow initialization failed" elif report_status == workflow_lib.WorkflowInitStatus.SUCCESS: status = HTTPStatus.OK message = "Workflow initialization succeeded" else: raise ValueError(f"Unexpected status: '{report_status}'") # Clean the temporary directory try: io_lib.remove(config_dir) # pylint: disable=broad-except except Exception as err: Logger.warn( {"config_dir": config_dir, "err": err}, "Failed to clean temporary config dir" ) # HTTP Response Logger.info( {"organization": organization, "app": app, "report": report}, f"[/api/workflow/init/:org/:app] {message}", ) return ( {"organization": organization, "app": app, "report": report, "message": message}, status, ) # pylint: disable=broad-except except Exception as err: Logger.error( {"organization": organization, "app": app, "report": report, "err": str(err)}, "[/api/workflow/init/:org/:app] Workflow initialization failed", ) # HTTP Response return ( { "organization": organization, "app": app, "err": str(err), "message": "Workflow initialization failed", }, HTTPStatus.INTERNAL_SERVER_ERROR, )