def test_rerun_validations(test_case): with contextlib.nested( patch('sys.stdout', new_callable=StringIO), patch('paasta_tools.cli.cmds.rerun.figure_out_service_name', autospec=True), patch('paasta_tools.cli.cmds.rerun.list_clusters', autospec=True), patch('paasta_tools.cli.cmds.rerun.get_actual_deployments', autospec=True), patch('paasta_tools.cli.cmds.rerun.get_planned_deployments', autospec=True), patch('paasta_tools.cli.cmds.rerun.execute_chronos_rerun_on_remote_master', autospec=True), patch('paasta_tools.cli.cmds.rerun.chronos_tools.load_chronos_job_config', autospec=True), patch('paasta_tools.cli.cmds.rerun.chronos_tools.uses_time_variables', autospec=True), patch('paasta_tools.cli.cmds.rerun._get_default_execution_date', autospec=True), ) as ( mock_stdout, mock_figure_out_service_name, mock_list_clusters, mock_get_actual_deployments, mock_get_planned_deployments, mock_execute_rerun_remote, mock_load_chronos_job_config, mock_uses_time_variables, mock_get_default_execution_date, ): (rerun_args, mock_figure_out_service_name.return_value, mock_list_clusters.return_value, mock_get_actual_deployments.return_value, mock_get_planned_deployments.return_value, mock_uses_time_variables.return_value, expected_output) = test_case mock_load_chronos_job_config.return_value = {} default_date = datetime.datetime(2002, 2, 2, 2, 2, 2, 2) mock_get_default_execution_date.return_value = default_date mock_execute_rerun_remote.return_value = (0, '') args = MagicMock() args.service = rerun_args[0] args.instance = rerun_args[1] args.clusters = rerun_args[2] if rerun_args[3]: args.execution_date = datetime.datetime.strptime(rerun_args[3], EXECUTION_DATE_FORMAT) else: args.execution_date = None args.verbose = 0 paasta_rerun(args) # No --execution_date argument, but that's ok: the job doesn't use time vars interpolation. # Check if the backend rerun command was called with the default date. if args.execution_date is None and not mock_uses_time_variables.return_value: assert mock_execute_rerun_remote.call_args[1]['execution_date'] \ == default_date.strftime(EXECUTION_DATE_FORMAT) # The job does use time vars interpolation. Make sure the User supplied date was used. if args.execution_date is not None and mock_uses_time_variables.return_value: assert mock_execute_rerun_remote.call_args[1]['execution_date'] == _user_supplied_execution_date output = mock_stdout.getvalue() assert expected_output in output
def test_rerun_validations(test_case, capfd, system_paasta_config): with patch( 'paasta_tools.cli.cmds.rerun.figure_out_service_name', autospec=True, ) as mock_figure_out_service_name, patch( 'paasta_tools.cli.cmds.rerun.list_clusters', autospec=True, ) as mock_list_clusters, patch( 'paasta_tools.cli.cmds.rerun.get_actual_deployments', autospec=True, ) as mock_get_actual_deployments, patch( 'paasta_tools.cli.cmds.rerun.get_planned_deployments', autospec=True, ) as mock_get_planned_deployments, patch( 'paasta_tools.cli.cmds.rerun.execute_chronos_rerun_on_remote_master', autospec=True, ) as mock_execute_rerun_remote, patch( 'paasta_tools.cli.cmds.rerun.chronos_tools.load_chronos_job_config', autospec=True, ) as mock_load_chronos_job_config, patch( 'paasta_tools.cli.cmds.rerun.chronos_tools.uses_time_variables', autospec=True, ) as mock_uses_time_variables, patch( 'paasta_tools.cli.cmds.rerun._get_default_execution_date', autospec=True, ) as mock_get_default_execution_date, patch( 'paasta_tools.cli.cmds.rerun.load_system_paasta_config', autospec=True, ) as mock_load_system_paasta_config, patch( 'paasta_tools.chronos_tools.read_chronos_jobs_for_service', autospec=True, ) as mock_read_chronos_jobs_for_service, patch( 'service_configuration_lib.read_services_configuration', autospec=True, ) as mock_read_services_configuration: ( rerun_args, mock_figure_out_service_name.return_value, mock_list_clusters.return_value, mock_get_actual_deployments.return_value, mock_get_planned_deployments.return_value, mock_uses_time_variables.return_value, expected_output, call_execute_rerun_remote, ) = test_case def fake_load_chronos_jobs_config(service, instance, cluster, *args, **kwargs): mock = MagicMock(spec=ChronosJobConfig) if instance == 'dependent_instance2': mock.get_parents.return_value = ['{}.{}'.format(_service_name, 'dependent_instance1')] else: mock.get_parents.return_value = [] return mock mock_load_chronos_job_config.side_effect = fake_load_chronos_jobs_config default_date = datetime.datetime(2002, 2, 2, 2, 2, 2, 2) mock_get_default_execution_date.return_value = default_date mock_execute_rerun_remote.return_value = (0, '') mock_load_system_paasta_config.return_value = system_paasta_config mock_read_chronos_jobs_for_service.return_value = { 'instance1': {}, 'dependent_instance1': {}, 'dependent_instance2': {'parents': ['{}.{}'.format(_service_name, 'dependent_instance1')]}, } mock_read_services_configuration.return_value = [_service_name] args = MagicMock() args.service = rerun_args[0] args.instance = rerun_args[1] args.clusters = rerun_args[2] if rerun_args[3]: args.execution_date = datetime.datetime.strptime(rerun_args[3], EXECUTION_DATE_FORMAT) else: args.execution_date = None args.rerun_type = rerun_args[4] args.force_disabled = rerun_args[5] args.verbose = 0 paasta_rerun(args) # No --execution_date argument, but that's ok: the job doesn't use time vars interpolation. # Check if the backend rerun command was called with the default date. if args.execution_date is None and not mock_uses_time_variables.return_value: assert mock_execute_rerun_remote.call_args[1]['execution_date'] \ == default_date.strftime(EXECUTION_DATE_FORMAT) if call_execute_rerun_remote: execution_date = args.execution_date if args.execution_date else default_date mock_execute_rerun_remote.assert_called_once_with( service=args.service, instancename=args.instance, cluster=mock.ANY, verbose=args.verbose, execution_date=execution_date.strftime(EXECUTION_DATE_FORMAT), run_all_related_jobs=bool(args.rerun_type and args.rerun_type == 'graph'), force_disabled=args.force_disabled, system_paasta_config=mock_load_system_paasta_config.return_value, ) # The job does use time vars interpolation. Make sure the User supplied date was used. # TODO: this if statement is never true # if args.execution_date is not None and mock_uses_time_variables.return_value: # assert mock_execute_rerun_remote.call_args[1]['execution_date'] == _user_supplied_execution_date output, _ = capfd.readouterr() assert expected_output in output
def test_rerun_validations(test_case): with contextlib.nested( patch('sys.stdout', new_callable=StringIO, autospec=None), patch('paasta_tools.cli.cmds.rerun.figure_out_service_name', autospec=True), patch('paasta_tools.cli.cmds.rerun.list_clusters', autospec=True), patch('paasta_tools.cli.cmds.rerun.get_actual_deployments', autospec=True), patch('paasta_tools.cli.cmds.rerun.get_planned_deployments', autospec=True), patch('paasta_tools.cli.cmds.rerun.execute_chronos_rerun_on_remote_master', autospec=True), patch('paasta_tools.cli.cmds.rerun.chronos_tools.load_chronos_job_config', autospec=True), patch('paasta_tools.cli.cmds.rerun.chronos_tools.uses_time_variables', autospec=True), patch('paasta_tools.cli.cmds.rerun._get_default_execution_date', autospec=True), patch('paasta_tools.cli.cmds.rerun.load_system_paasta_config', autospec=True), ) as ( mock_stdout, mock_figure_out_service_name, mock_list_clusters, mock_get_actual_deployments, mock_get_planned_deployments, mock_execute_rerun_remote, mock_load_chronos_job_config, mock_uses_time_variables, mock_get_default_execution_date, mock_load_system_paasta_config, ): (rerun_args, mock_figure_out_service_name.return_value, mock_list_clusters.return_value, mock_get_actual_deployments.return_value, mock_get_planned_deployments.return_value, mock_uses_time_variables.return_value, expected_output) = test_case mock_load_chronos_job_config.return_value = {} default_date = datetime.datetime(2002, 2, 2, 2, 2, 2, 2) mock_get_default_execution_date.return_value = default_date mock_execute_rerun_remote.return_value = (0, '') mock_load_system_paasta_config.return_value = SystemPaastaConfig({}, '/fake/config') args = MagicMock() args.service = rerun_args[0] args.instance = rerun_args[1] args.clusters = rerun_args[2] if rerun_args[3]: args.execution_date = datetime.datetime.strptime(rerun_args[3], EXECUTION_DATE_FORMAT) else: args.execution_date = None args.verbose = 0 paasta_rerun(args) # No --execution_date argument, but that's ok: the job doesn't use time vars interpolation. # Check if the backend rerun command was called with the default date. if args.execution_date is None and not mock_uses_time_variables.return_value: assert mock_execute_rerun_remote.call_args[1]['execution_date'] \ == default_date.strftime(EXECUTION_DATE_FORMAT) # The job does use time vars interpolation. Make sure the User supplied date was used. if args.execution_date is not None and mock_uses_time_variables.return_value: assert mock_execute_rerun_remote.call_args[1]['execution_date'] == _user_supplied_execution_date output = mock_stdout.getvalue().decode('utf-8') assert expected_output in output