<s> import contextlib <EOL> import marathon <EOL> import mock <EOL> from pytest import raises <EOL> from paasta_tools import bounce_lib <EOL> from paasta_tools import marathon_tools <EOL> from paasta_tools import setup_marathon_job <EOL> from paasta_tools import utils <EOL> from paasta_tools . bounce_lib import list_bounce_methods <EOL> from paasta_tools . utils import compose_job_id <EOL> from paasta_tools . utils import decompose_job_id <EOL> from paasta_tools . utils import NoDeploymentsAvailable <EOL> from paasta_tools . utils import NoDockerImageError <EOL> class TestSetupMarathonJob : <EOL> fake_docker_image = '<STR_LIT>' <EOL> fake_cluster = '<STR_LIT>' <EOL> fake_marathon_service_config = marathon_tools . MarathonServiceConfig ( <EOL> service = '<STR_LIT>' , <EOL> cluster = '<STR_LIT>' , <EOL> instance = '<STR_LIT>' , <EOL> config_dict = { <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:100> , <EOL> '<STR_LIT>' : fake_docker_image , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> branch_dict = { } , <EOL> ) <EOL> fake_docker_registry = '<STR_LIT>' <EOL> fake_marathon_config = marathon_tools . MarathonConfig ( { <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT:user>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> } , '<STR_LIT>' ) <EOL> fake_args = mock . MagicMock ( <EOL> service_instance = '<STR_LIT>' , <EOL> soa_dir = '<STR_LIT>' , <EOL> verbose = False , <EOL> ) <EOL> fake_service_namespace_config = marathon_tools . ServiceNamespaceConfig ( { <EOL> '<STR_LIT>' : '<STR_LIT:http>' <EOL> } ) <EOL> def test_main_success ( self ) : <EOL> fake_client = mock . MagicMock ( ) <EOL> with contextlib . nested ( <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = self . fake_args , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = self . fake_marathon_config , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = fake_client , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = self . fake_marathon_service_config , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = ( <NUM_LIT:0> , '<STR_LIT>' ) , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> ) as ( <EOL> parse_args_patch , <EOL> get_main_conf_patch , <EOL> get_client_patch , <EOL> read_service_conf_patch , <EOL> setup_service_patch , <EOL> load_system_paasta_config_patch , <EOL> sensu_patch , <EOL> sys_exit_patch , <EOL> ) : <EOL> load_system_paasta_config_patch . return_value . get_cluster = mock . Mock ( return_value = self . fake_cluster ) <EOL> setup_marathon_job . main ( ) <EOL> parse_args_patch . assert_called_once_with ( ) <EOL> get_main_conf_patch . assert_called_once_with ( ) <EOL> get_client_patch . assert_called_once_with ( <EOL> self . fake_marathon_config . get_url ( ) , <EOL> self . fake_marathon_config . get_username ( ) , <EOL> self . fake_marathon_config . get_password ( ) , <EOL> ) <EOL> read_service_conf_patch . assert_called_once_with ( <EOL> decompose_job_id ( self . fake_args . service_instance ) [ <NUM_LIT:0> ] , <EOL> decompose_job_id ( self . fake_args . service_instance ) [ <NUM_LIT:1> ] , <EOL> self . fake_cluster , <EOL> soa_dir = self . fake_args . soa_dir , <EOL> ) <EOL> setup_service_patch . assert_called_once_with ( <EOL> decompose_job_id ( self . fake_args . service_instance ) [ <NUM_LIT:0> ] , <EOL> decompose_job_id ( self . fake_args . service_instance ) [ <NUM_LIT:1> ] , <EOL> fake_client , <EOL> self . fake_marathon_config , <EOL> self . fake_marathon_service_config , <EOL> '<STR_LIT>' , <EOL> ) <EOL> sys_exit_patch . assert_called_once_with ( <NUM_LIT:0> ) <EOL> def test_main_failure ( self ) : <EOL> fake_client = mock . MagicMock ( ) <EOL> with contextlib . nested ( <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = self . fake_args , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = self . fake_marathon_config , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = fake_client , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = self . fake_marathon_service_config , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = ( <NUM_LIT:1> , '<STR_LIT>' ) , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> ) as ( <EOL> parse_args_patch , <EOL> get_main_conf_patch , <EOL> get_client_patch , <EOL> read_service_conf_patch , <EOL> setup_service_patch , <EOL> load_system_paasta_config_patch , <EOL> sensu_patch , <EOL> sys_exit_patch , <EOL> ) : <EOL> load_system_paasta_config_patch . return_value . get_cluster = mock . Mock ( return_value = self . fake_cluster ) <EOL> setup_marathon_job . main ( ) <EOL> parse_args_patch . assert_called_once_with ( ) <EOL> get_main_conf_patch . assert_called_once_with ( ) <EOL> get_client_patch . assert_called_once_with ( <EOL> self . fake_marathon_config . get_url ( ) , <EOL> self . fake_marathon_config . get_username ( ) , <EOL> self . fake_marathon_config . get_password ( ) ) <EOL> read_service_conf_patch . assert_called_once_with ( <EOL> decompose_job_id ( self . fake_args . service_instance ) [ <NUM_LIT:0> ] , <EOL> decompose_job_id ( self . fake_args . service_instance ) [ <NUM_LIT:1> ] , <EOL> self . fake_cluster , <EOL> soa_dir = self . fake_args . soa_dir ) <EOL> setup_service_patch . assert_called_once_with ( <EOL> decompose_job_id ( self . fake_args . service_instance ) [ <NUM_LIT:0> ] , <EOL> decompose_job_id ( self . fake_args . service_instance ) [ <NUM_LIT:1> ] , <EOL> fake_client , <EOL> self . fake_marathon_config , <EOL> self . fake_marathon_service_config , <EOL> '<STR_LIT>' , <EOL> ) <EOL> sys_exit_patch . assert_called_once_with ( <NUM_LIT:0> ) <EOL> def test_main_exits_if_no_deployments_yet ( self ) : <EOL> fake_client = mock . MagicMock ( ) <EOL> with contextlib . nested ( <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = self . fake_args , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = self . fake_marathon_config , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = fake_client , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> side_effect = NoDeploymentsAvailable ( ) , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = ( <NUM_LIT:1> , '<STR_LIT>' ) , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> ) as ( <EOL> parse_args_patch , <EOL> get_main_conf_patch , <EOL> get_client_patch , <EOL> read_service_conf_patch , <EOL> setup_service_patch , <EOL> load_system_paasta_config_patch , <EOL> ) : <EOL> load_system_paasta_config_patch . return_value . get_cluster = mock . Mock ( return_value = self . fake_cluster ) <EOL> with raises ( SystemExit ) as exc_info : <EOL> setup_marathon_job . main ( ) <EOL> parse_args_patch . assert_called_once_with ( ) <EOL> get_main_conf_patch . assert_called_once_with ( ) <EOL> get_client_patch . assert_called_once_with ( <EOL> self . fake_marathon_config . get_url ( ) , <EOL> self . fake_marathon_config . get_username ( ) , <EOL> self . fake_marathon_config . get_password ( ) ) <EOL> read_service_conf_patch . assert_called_once_with ( <EOL> decompose_job_id ( self . fake_args . service_instance ) [ <NUM_LIT:0> ] , <EOL> decompose_job_id ( self . fake_args . service_instance ) [ <NUM_LIT:1> ] , <EOL> self . fake_cluster , <EOL> soa_dir = self . fake_args . soa_dir ) <EOL> assert exc_info . value . code == <NUM_LIT:0> <EOL> def test_send_event ( self ) : <EOL> fake_service = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_status = '<STR_LIT>' <EOL> fake_output = '<STR_LIT>' <EOL> fake_soa_dir = '<STR_LIT>' <EOL> expected_check_name = '<STR_LIT>' % compose_job_id ( fake_service , fake_instance ) <EOL> with contextlib . nested ( <EOL> mock . patch ( "<STR_LIT>" , autospec = True ) , <EOL> mock . patch ( "<STR_LIT>" , autospec = True ) , <EOL> mock . patch ( "<STR_LIT>" , autospec = True ) , <EOL> ) as ( <EOL> send_event_patch , <EOL> load_marathon_service_config_patch , <EOL> load_system_paasta_config_patch , <EOL> ) : <EOL> load_system_paasta_config_patch . return_value . get_cluster = mock . Mock ( return_value = '<STR_LIT>' ) <EOL> load_marathon_service_config_patch . return_value . get_monitoring . return_value = { } <EOL> setup_marathon_job . send_event ( <EOL> fake_service , <EOL> fake_instance , <EOL> fake_soa_dir , <EOL> fake_status , <EOL> fake_output <EOL> ) <EOL> send_event_patch . assert_called_once_with ( <EOL> fake_service , <EOL> expected_check_name , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> fake_status , <EOL> fake_output , <EOL> fake_soa_dir <EOL> ) <EOL> load_marathon_service_config_patch . assert_called_once_with ( <EOL> fake_service , <EOL> fake_instance , <EOL> load_system_paasta_config_patch . return_value . get_cluster . return_value , <EOL> load_deployments = False , <EOL> soa_dir = fake_soa_dir , <EOL> ) <EOL> def test_send_bounce_keepalive ( self ) : <EOL> fake_service = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_cluster = '<STR_LIT>' <EOL> fake_soa_dir = '<STR_LIT>' <EOL> expected_check_name = '<STR_LIT>' % compose_job_id ( fake_service , fake_instance ) <EOL> with contextlib . nested ( <EOL> mock . patch ( "<STR_LIT>" , autospec = True ) , <EOL> mock . patch ( "<STR_LIT>" , autospec = True ) , <EOL> ) as ( <EOL> send_event_patch , <EOL> load_marathon_service_config_patch , <EOL> ) : <EOL> load_marathon_service_config_patch . return_value . get_monitoring . return_value = { } <EOL> setup_marathon_job . send_sensu_bounce_keepalive ( <EOL> service = fake_service , <EOL> instance = fake_instance , <EOL> cluster = fake_cluster , <EOL> soa_dir = fake_soa_dir , <EOL> ) <EOL> send_event_patch . assert_called_once_with ( <EOL> service = fake_service , <EOL> check_name = expected_check_name , <EOL> overrides = mock . ANY , <EOL> status = <NUM_LIT:0> , <EOL> output = mock . ANY , <EOL> soa_dir = fake_soa_dir , <EOL> ttl = '<STR_LIT>' , <EOL> ) <EOL> load_marathon_service_config_patch . assert_called_once_with ( <EOL> service = fake_service , <EOL> instance = fake_instance , <EOL> cluster = fake_cluster , <EOL> load_deployments = False , <EOL> soa_dir = fake_soa_dir , <EOL> ) <EOL> def test_do_bounce_when_create_app_and_new_app_not_running ( self ) : <EOL> fake_bounce_func_return = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : [ mock . Mock ( app_id = '<STR_LIT>' ) ] , <EOL> } <EOL> fake_bounce_func = mock . create_autospec ( <EOL> bounce_lib . brutal_bounce , <EOL> return_value = fake_bounce_func_return , <EOL> ) <EOL> fake_config = { '<STR_LIT>' : <NUM_LIT:5> } <EOL> fake_new_app_running = False <EOL> fake_happy_new_tasks = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> fake_old_app_live_happy_tasks = { } <EOL> fake_old_app_live_unhappy_tasks = { } <EOL> fake_old_app_draining_tasks = { } <EOL> fake_service = '<STR_LIT>' <EOL> fake_serviceinstance = '<STR_LIT>' <EOL> self . fake_cluster = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_bounce_method = '<STR_LIT>' <EOL> fake_drain_method = mock . Mock ( is_safe_to_kill = lambda t : False ) <EOL> fake_marathon_jobid = '<STR_LIT>' <EOL> fake_client = mock . create_autospec ( <EOL> marathon . MarathonClient <EOL> ) <EOL> expected_new_task_count = fake_config [ "<STR_LIT>" ] - len ( fake_happy_new_tasks ) <EOL> expected_drain_task_count = len ( fake_bounce_func_return [ '<STR_LIT>' ] ) <EOL> with contextlib . nested ( <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> ) as ( mock_log , mock_create_marathon_app , mock_kill_old_ids ) : <EOL> setup_marathon_job . do_bounce ( <EOL> bounce_func = fake_bounce_func , <EOL> drain_method = fake_drain_method , <EOL> config = fake_config , <EOL> new_app_running = fake_new_app_running , <EOL> happy_new_tasks = fake_happy_new_tasks , <EOL> old_app_live_happy_tasks = fake_old_app_live_happy_tasks , <EOL> old_app_live_unhappy_tasks = fake_old_app_live_unhappy_tasks , <EOL> old_app_draining_tasks = fake_old_app_draining_tasks , <EOL> service = fake_service , <EOL> bounce_method = fake_bounce_method , <EOL> serviceinstance = fake_serviceinstance , <EOL> cluster = self . fake_cluster , <EOL> instance = fake_instance , <EOL> marathon_jobid = fake_marathon_jobid , <EOL> client = fake_client , <EOL> soa_dir = '<STR_LIT>' , <EOL> ) <EOL> assert mock_log . call_count == <NUM_LIT:3> <EOL> first_logged_line = mock_log . mock_calls [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] [ "<STR_LIT>" ] <EOL> assert '<STR_LIT>' % expected_new_task_count in first_logged_line <EOL> second_logged_line = mock_log . mock_calls [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] [ "<STR_LIT>" ] <EOL> assert '<STR_LIT>' % fake_marathon_jobid in second_logged_line <EOL> third_logged_line = mock_log . mock_calls [ <NUM_LIT:2> ] [ <NUM_LIT:2> ] [ "<STR_LIT>" ] <EOL> assert '<STR_LIT>' % expected_drain_task_count in third_logged_line <EOL> assert mock_create_marathon_app . call_count == <NUM_LIT:1> <EOL> assert fake_client . kill_task . call_count == <NUM_LIT:0> <EOL> assert fake_drain_method . drain . call_count == len ( fake_bounce_func_return [ "<STR_LIT>" ] ) <EOL> assert mock_kill_old_ids . call_count == <NUM_LIT:0> <EOL> def test_do_bounce_when_create_app_and_new_app_running ( self ) : <EOL> fake_task_to_drain = mock . Mock ( app_id = '<STR_LIT>' ) <EOL> fake_bounce_func_return = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : [ fake_task_to_drain ] , <EOL> } <EOL> fake_bounce_func = mock . create_autospec ( <EOL> bounce_lib . brutal_bounce , <EOL> return_value = fake_bounce_func_return , <EOL> ) <EOL> fake_config = { '<STR_LIT>' : <NUM_LIT:5> } <EOL> fake_new_app_running = True <EOL> fake_happy_new_tasks = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> fake_old_app_live_happy_tasks = { '<STR_LIT>' : set ( [ fake_task_to_drain ] ) } <EOL> fake_old_app_live_unhappy_tasks = { '<STR_LIT>' : set ( ) } <EOL> fake_old_app_draining_tasks = { '<STR_LIT>' : set ( ) } <EOL> fake_service = '<STR_LIT>' <EOL> fake_serviceinstance = '<STR_LIT>' <EOL> self . fake_cluster = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_bounce_method = '<STR_LIT>' <EOL> fake_drain_method = mock . Mock ( is_safe_to_kill = lambda t : False ) <EOL> fake_marathon_jobid = '<STR_LIT>' <EOL> fake_client = mock . create_autospec ( <EOL> marathon . MarathonClient <EOL> ) <EOL> expected_new_task_count = fake_config [ "<STR_LIT>" ] - len ( fake_happy_new_tasks ) <EOL> expected_drain_task_count = len ( fake_bounce_func_return [ '<STR_LIT>' ] ) <EOL> with contextlib . nested ( <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> ) as ( mock_log , mock_create_marathon_app , mock_kill_old_ids ) : <EOL> setup_marathon_job . do_bounce ( <EOL> bounce_func = fake_bounce_func , <EOL> drain_method = fake_drain_method , <EOL> config = fake_config , <EOL> new_app_running = fake_new_app_running , <EOL> happy_new_tasks = fake_happy_new_tasks , <EOL> old_app_live_happy_tasks = fake_old_app_live_happy_tasks , <EOL> old_app_live_unhappy_tasks = fake_old_app_live_unhappy_tasks , <EOL> old_app_draining_tasks = fake_old_app_draining_tasks , <EOL> service = fake_service , <EOL> bounce_method = fake_bounce_method , <EOL> serviceinstance = fake_serviceinstance , <EOL> cluster = self . fake_cluster , <EOL> instance = fake_instance , <EOL> marathon_jobid = fake_marathon_jobid , <EOL> client = fake_client , <EOL> soa_dir = '<STR_LIT>' , <EOL> ) <EOL> first_logged_line = mock_log . mock_calls [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] [ "<STR_LIT>" ] <EOL> assert '<STR_LIT>' % expected_new_task_count in first_logged_line <EOL> second_logged_line = mock_log . mock_calls [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] [ "<STR_LIT>" ] <EOL> assert '<STR_LIT>' % expected_drain_task_count in second_logged_line <EOL> assert mock_log . call_count == <NUM_LIT:2> <EOL> assert mock_create_marathon_app . call_count == <NUM_LIT:0> <EOL> assert fake_client . kill_task . call_count == <NUM_LIT:0> <EOL> assert mock_kill_old_ids . call_count == <NUM_LIT:0> <EOL> assert fake_drain_method . drain . call_count == len ( fake_bounce_func_return [ "<STR_LIT>" ] ) <EOL> def test_do_bounce_when_tasks_to_drain ( self ) : <EOL> fake_task_to_drain = mock . Mock ( app_id = '<STR_LIT>' ) <EOL> fake_bounce_func_return = { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : [ fake_task_to_drain ] , <EOL> } <EOL> fake_bounce_func = mock . create_autospec ( <EOL> bounce_lib . brutal_bounce , <EOL> return_value = fake_bounce_func_return , <EOL> ) <EOL> fake_config = { '<STR_LIT>' : <NUM_LIT:5> } <EOL> fake_new_app_running = True <EOL> fake_happy_new_tasks = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> fake_old_app_live_happy_tasks = { '<STR_LIT>' : set ( [ fake_task_to_drain ] ) } <EOL> fake_old_app_live_unhappy_tasks = { '<STR_LIT>' : set ( [ ] ) } <EOL> fake_old_app_draining_tasks = { '<STR_LIT>' : set ( [ ] ) } <EOL> fake_service = '<STR_LIT>' <EOL> fake_serviceinstance = '<STR_LIT>' <EOL> self . fake_cluster = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_bounce_method = '<STR_LIT>' <EOL> fake_drain_method = mock . Mock ( is_safe_to_kill = lambda t : False ) <EOL> fake_marathon_jobid = '<STR_LIT>' <EOL> fake_client = mock . create_autospec ( <EOL> marathon . MarathonClient <EOL> ) <EOL> expected_new_task_count = fake_config [ "<STR_LIT>" ] - len ( fake_happy_new_tasks ) <EOL> expected_drain_task_count = len ( fake_bounce_func_return [ '<STR_LIT>' ] ) <EOL> with contextlib . nested ( <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> ) as ( mock_log , mock_create_marathon_app , mock_kill_old_ids ) : <EOL> setup_marathon_job . do_bounce ( <EOL> bounce_func = fake_bounce_func , <EOL> drain_method = fake_drain_method , <EOL> config = fake_config , <EOL> new_app_running = fake_new_app_running , <EOL> happy_new_tasks = fake_happy_new_tasks , <EOL> old_app_live_happy_tasks = fake_old_app_live_happy_tasks , <EOL> old_app_live_unhappy_tasks = fake_old_app_live_unhappy_tasks , <EOL> old_app_draining_tasks = fake_old_app_draining_tasks , <EOL> service = fake_service , <EOL> bounce_method = fake_bounce_method , <EOL> serviceinstance = fake_serviceinstance , <EOL> cluster = self . fake_cluster , <EOL> instance = fake_instance , <EOL> marathon_jobid = fake_marathon_jobid , <EOL> client = fake_client , <EOL> soa_dir = '<STR_LIT>' , <EOL> ) <EOL> first_logged_line = mock_log . mock_calls [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] [ "<STR_LIT>" ] <EOL> assert '<STR_LIT>' % expected_new_task_count in first_logged_line <EOL> second_logged_line = mock_log . mock_calls [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] [ "<STR_LIT>" ] <EOL> assert '<STR_LIT>' % ( expected_drain_task_count , '<STR_LIT>' ) in second_logged_line <EOL> assert mock_create_marathon_app . call_count == <NUM_LIT:0> <EOL> assert fake_client . kill_task . call_count == <NUM_LIT:0> <EOL> assert mock_kill_old_ids . call_count == <NUM_LIT:0> <EOL> assert fake_drain_method . drain . call_count == expected_drain_task_count <EOL> def test_do_bounce_when_apps_to_kill ( self ) : <EOL> fake_bounce_func_return = { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : [ ] , <EOL> } <EOL> fake_bounce_func = mock . create_autospec ( <EOL> bounce_lib . brutal_bounce , <EOL> return_value = fake_bounce_func_return , <EOL> ) <EOL> fake_config = { '<STR_LIT>' : <NUM_LIT:5> } <EOL> fake_new_app_running = True <EOL> fake_happy_new_tasks = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> fake_old_app_live_happy_tasks = { '<STR_LIT>' : set ( ) } <EOL> fake_old_app_live_unhappy_tasks = { '<STR_LIT>' : set ( ) } <EOL> fake_old_app_draining_tasks = { '<STR_LIT>' : set ( ) } <EOL> fake_service = '<STR_LIT>' <EOL> fake_serviceinstance = '<STR_LIT>' <EOL> self . fake_cluster = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_bounce_method = '<STR_LIT>' <EOL> fake_drain_method = mock . Mock ( ) <EOL> fake_marathon_jobid = '<STR_LIT>' <EOL> fake_client = mock . create_autospec ( <EOL> marathon . MarathonClient <EOL> ) <EOL> expected_new_task_count = fake_config [ "<STR_LIT>" ] - len ( fake_happy_new_tasks ) <EOL> with contextlib . nested ( <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> ) as ( mock_log , mock_create_marathon_app , mock_kill_old_ids ) : <EOL> setup_marathon_job . do_bounce ( <EOL> bounce_func = fake_bounce_func , <EOL> drain_method = fake_drain_method , <EOL> config = fake_config , <EOL> new_app_running = fake_new_app_running , <EOL> happy_new_tasks = fake_happy_new_tasks , <EOL> old_app_live_happy_tasks = fake_old_app_live_happy_tasks , <EOL> old_app_live_unhappy_tasks = fake_old_app_live_unhappy_tasks , <EOL> old_app_draining_tasks = fake_old_app_draining_tasks , <EOL> service = fake_service , <EOL> bounce_method = fake_bounce_method , <EOL> serviceinstance = fake_serviceinstance , <EOL> cluster = self . fake_cluster , <EOL> instance = fake_instance , <EOL> marathon_jobid = fake_marathon_jobid , <EOL> client = fake_client , <EOL> soa_dir = '<STR_LIT>' , <EOL> ) <EOL> assert mock_log . call_count == <NUM_LIT:3> <EOL> first_logged_line = mock_log . mock_calls [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] [ "<STR_LIT>" ] <EOL> assert '<STR_LIT>' % expected_new_task_count in first_logged_line <EOL> second_logged_line = mock_log . mock_calls [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] [ "<STR_LIT>" ] <EOL> assert '<STR_LIT>' % '<STR_LIT>' in second_logged_line <EOL> assert mock_create_marathon_app . call_count == <NUM_LIT:0> <EOL> assert fake_client . kill_task . call_count == len ( fake_bounce_func_return [ "<STR_LIT>" ] ) <EOL> assert mock_kill_old_ids . call_count == <NUM_LIT:1> <EOL> third_logged_line = mock_log . mock_calls [ <NUM_LIT:2> ] [ <NUM_LIT:2> ] [ "<STR_LIT>" ] <EOL> assert '<STR_LIT>' % ( fake_bounce_method , fake_serviceinstance ) in third_logged_line <EOL> assert '<STR_LIT>' % fake_marathon_jobid in third_logged_line <EOL> def test_do_bounce_when_nothing_to_do ( self ) : <EOL> fake_bounce_func_return = { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : [ ] , <EOL> } <EOL> fake_bounce_func = mock . create_autospec ( <EOL> bounce_lib . brutal_bounce , <EOL> return_value = fake_bounce_func_return , <EOL> ) <EOL> fake_config = { '<STR_LIT>' : <NUM_LIT:3> } <EOL> fake_new_app_running = True <EOL> fake_happy_new_tasks = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> fake_old_app_live_happy_tasks = { } <EOL> fake_old_app_live_unhappy_tasks = { } <EOL> fake_old_app_draining_tasks = { } <EOL> fake_service = '<STR_LIT>' <EOL> fake_serviceinstance = '<STR_LIT>' <EOL> self . fake_cluster = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_bounce_method = '<STR_LIT>' <EOL> fake_drain_method = mock . Mock ( ) <EOL> fake_marathon_jobid = '<STR_LIT>' <EOL> fake_client = mock . create_autospec ( <EOL> marathon . MarathonClient <EOL> ) <EOL> with contextlib . nested ( <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> ) as ( <EOL> mock_log , <EOL> mock_create_marathon_app , <EOL> mock_kill_old_ids , <EOL> mock_send_sensu_bounce_keepalive , <EOL> ) : <EOL> setup_marathon_job . do_bounce ( <EOL> bounce_func = fake_bounce_func , <EOL> drain_method = fake_drain_method , <EOL> config = fake_config , <EOL> new_app_running = fake_new_app_running , <EOL> happy_new_tasks = fake_happy_new_tasks , <EOL> old_app_live_happy_tasks = fake_old_app_live_happy_tasks , <EOL> old_app_live_unhappy_tasks = fake_old_app_live_unhappy_tasks , <EOL> old_app_draining_tasks = fake_old_app_draining_tasks , <EOL> service = fake_service , <EOL> bounce_method = fake_bounce_method , <EOL> serviceinstance = fake_serviceinstance , <EOL> cluster = self . fake_cluster , <EOL> instance = fake_instance , <EOL> marathon_jobid = fake_marathon_jobid , <EOL> client = fake_client , <EOL> soa_dir = '<STR_LIT>' , <EOL> ) <EOL> assert mock_log . call_count == <NUM_LIT:0> <EOL> assert mock_create_marathon_app . call_count == <NUM_LIT:0> <EOL> assert fake_drain_method . drain . call_count == <NUM_LIT:0> <EOL> assert mock_kill_old_ids . call_count == <NUM_LIT:0> <EOL> mock_send_sensu_bounce_keepalive . assert_called_once_with ( <EOL> service = fake_service , <EOL> instance = fake_instance , <EOL> cluster = self . fake_cluster , <EOL> soa_dir = '<STR_LIT>' , <EOL> ) <EOL> def test_do_bounce_when_all_old_tasks_are_unhappy ( self ) : <EOL> old_tasks = [ mock . Mock ( id = id , app_id = '<STR_LIT>' ) for id in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ] <EOL> fake_bounce_func_return = { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : old_tasks , <EOL> } <EOL> fake_bounce_func = mock . create_autospec ( <EOL> bounce_lib . brutal_bounce , <EOL> return_value = fake_bounce_func_return , <EOL> ) <EOL> fake_config = { '<STR_LIT>' : <NUM_LIT:3> } <EOL> fake_new_app_running = True <EOL> fake_happy_new_tasks = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> fake_old_app_live_happy_tasks = { '<STR_LIT>' : set ( [ ] ) } <EOL> fake_old_app_live_unhappy_tasks = { '<STR_LIT>' : set ( old_tasks ) } <EOL> fake_old_app_draining_tasks = { '<STR_LIT>' : set ( [ ] ) } <EOL> fake_service = '<STR_LIT>' <EOL> fake_serviceinstance = '<STR_LIT>' <EOL> self . fake_cluster = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_bounce_method = '<STR_LIT>' <EOL> fake_drain_method = mock . Mock ( ) <EOL> fake_drain_method . is_safe_to_kill . return_value = False <EOL> fake_marathon_jobid = '<STR_LIT>' <EOL> fake_client = mock . create_autospec ( <EOL> marathon . MarathonClient <EOL> ) <EOL> with contextlib . nested ( <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> ) as ( <EOL> mock_log , <EOL> mock_create_marathon_app , <EOL> mock_kill_old_ids , <EOL> mock_send_sensu_bounce_keepalive , <EOL> ) : <EOL> setup_marathon_job . do_bounce ( <EOL> bounce_func = fake_bounce_func , <EOL> drain_method = fake_drain_method , <EOL> config = fake_config , <EOL> new_app_running = fake_new_app_running , <EOL> happy_new_tasks = fake_happy_new_tasks , <EOL> old_app_live_happy_tasks = fake_old_app_live_happy_tasks , <EOL> old_app_live_unhappy_tasks = fake_old_app_live_unhappy_tasks , <EOL> old_app_draining_tasks = fake_old_app_draining_tasks , <EOL> service = fake_service , <EOL> bounce_method = fake_bounce_method , <EOL> serviceinstance = fake_serviceinstance , <EOL> cluster = self . fake_cluster , <EOL> instance = fake_instance , <EOL> marathon_jobid = fake_marathon_jobid , <EOL> client = fake_client , <EOL> soa_dir = '<STR_LIT>' , <EOL> ) <EOL> assert fake_drain_method . drain . call_count == <NUM_LIT:3> <EOL> assert mock_kill_old_ids . call_count == <NUM_LIT:0> <EOL> def test_deploy_service_scale_up ( self ) : <EOL> fake_service = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_jobid = '<STR_LIT>' <EOL> fake_config = { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:5> , <EOL> } <EOL> fake_client = mock . MagicMock ( scale_app = mock . Mock ( ) ) <EOL> fake_bounce_method = '<STR_LIT>' <EOL> fake_drain_method_name = '<STR_LIT>' <EOL> fake_drain_method_params = { } <EOL> fake_nerve_ns = '<STR_LIT>' <EOL> fake_bounce_health_params = { } <EOL> fake_soa_dir = '<STR_LIT>' <EOL> with contextlib . nested ( <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> ) as ( <EOL> mock_log , <EOL> mock_load_system_paasta_config , <EOL> mock_get_matching_apps , <EOL> mock_get_happy_tasks , <EOL> mock_get_drain_method , <EOL> ) : <EOL> mock_load_system_paasta_config . return_value = mock . MagicMock ( <EOL> get_cluster = mock . Mock ( return_value = '<STR_LIT>' ) ) <EOL> mock_get_matching_apps . return_value = [ mock . Mock ( id = '<STR_LIT>' , instances = <NUM_LIT:1> , tasks = [ ] ) ] <EOL> mock_get_happy_tasks . return_value = [ ] <EOL> mock_get_drain_method . return_value = mock . Mock ( is_draining = mock . Mock ( return_value = False ) ) <EOL> setup_marathon_job . deploy_service ( <EOL> service = fake_service , <EOL> instance = fake_instance , <EOL> marathon_jobid = fake_jobid , <EOL> config = fake_config , <EOL> client = fake_client , <EOL> bounce_method = fake_bounce_method , <EOL> drain_method_name = fake_drain_method_name , <EOL> drain_method_params = fake_drain_method_params , <EOL> nerve_ns = fake_nerve_ns , <EOL> bounce_health_params = fake_bounce_health_params , <EOL> soa_dir = fake_soa_dir , <EOL> ) <EOL> fake_client . scale_app . assert_called_once_with ( <EOL> app_id = '<STR_LIT>' , <EOL> instances = <NUM_LIT:5> , <EOL> force = True , <EOL> ) <EOL> def test_deploy_service_scale_down ( self ) : <EOL> fake_service = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_jobid = '<STR_LIT>' <EOL> fake_config = { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> } <EOL> fake_client = mock . MagicMock ( ) <EOL> fake_bounce_method = '<STR_LIT>' <EOL> fake_drain_method_name = '<STR_LIT>' <EOL> fake_drain_method_params = { } <EOL> fake_nerve_ns = '<STR_LIT>' <EOL> fake_bounce_health_params = { } <EOL> fake_soa_dir = '<STR_LIT>' <EOL> with contextlib . nested ( <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> ) as ( <EOL> mock_log , <EOL> mock_load_system_paasta_config , <EOL> mock_get_matching_apps , <EOL> mock_get_happy_tasks , <EOL> mock_get_drain_method , <EOL> mock_get_bounce_method_func , <EOL> mock_bounce_lock_zookeeper , <EOL> mock_do_bounce , <EOL> ) : <EOL> mock_load_system_paasta_config . return_value = mock . MagicMock ( <EOL> get_cluster = mock . Mock ( return_value = '<STR_LIT>' ) ) <EOL> mock_get_matching_apps . return_value = [ mock . Mock ( id = '<STR_LIT>' , instances = <NUM_LIT:5> , tasks = range ( <NUM_LIT:5> ) ) ] <EOL> mock_get_happy_tasks . return_value = range ( <NUM_LIT:5> ) <EOL> mock_get_drain_method . return_value = mock . Mock ( is_draining = mock . Mock ( return_value = False ) ) <EOL> setup_marathon_job . deploy_service ( <EOL> service = fake_service , <EOL> instance = fake_instance , <EOL> marathon_jobid = fake_jobid , <EOL> config = fake_config , <EOL> client = fake_client , <EOL> bounce_method = fake_bounce_method , <EOL> drain_method_name = fake_drain_method_name , <EOL> drain_method_params = fake_drain_method_params , <EOL> nerve_ns = fake_nerve_ns , <EOL> bounce_health_params = fake_bounce_health_params , <EOL> soa_dir = fake_soa_dir , <EOL> ) <EOL> assert mock_do_bounce . call_args [ <NUM_LIT:1> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] < set ( range ( <NUM_LIT:5> ) ) <EOL> assert len ( mock_do_bounce . call_args [ <NUM_LIT:1> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) == <NUM_LIT:4> <EOL> def test_deploy_service_scale_down_doesnt_undrain_scaling_tasks ( self ) : <EOL> fake_service = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_jobid = '<STR_LIT>' <EOL> fake_config = { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> } <EOL> fake_client = mock . MagicMock ( ) <EOL> fake_bounce_method = '<STR_LIT>' <EOL> fake_drain_method_name = '<STR_LIT>' <EOL> fake_drain_method_params = { } <EOL> fake_nerve_ns = '<STR_LIT>' <EOL> fake_bounce_health_params = { } <EOL> fake_soa_dir = '<STR_LIT>' <EOL> with contextlib . nested ( <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> ) as ( <EOL> mock_log , <EOL> mock_load_system_paasta_config , <EOL> mock_get_matching_apps , <EOL> mock_get_happy_tasks , <EOL> mock_get_drain_method , <EOL> mock_get_bounce_method_func , <EOL> mock_bounce_lock_zookeeper , <EOL> mock_do_bounce , <EOL> ) : <EOL> mock_stop_draining = mock . MagicMock ( ) <EOL> mock_load_system_paasta_config . return_value = mock . MagicMock ( <EOL> get_cluster = mock . Mock ( return_value = '<STR_LIT>' ) ) <EOL> mock_get_matching_apps . return_value = [ mock . Mock ( id = '<STR_LIT>' , instances = <NUM_LIT:5> , tasks = range ( <NUM_LIT:5> ) ) ] <EOL> mock_get_happy_tasks . return_value = range ( <NUM_LIT:5> ) <EOL> mock_get_drain_method . return_value = mock . Mock ( is_draining = lambda x : x != <NUM_LIT:0> , <EOL> stop_draining = mock_stop_draining , ) <EOL> setup_marathon_job . deploy_service ( <EOL> service = fake_service , <EOL> instance = fake_instance , <EOL> marathon_jobid = fake_jobid , <EOL> config = fake_config , <EOL> client = fake_client , <EOL> bounce_method = fake_bounce_method , <EOL> drain_method_name = fake_drain_method_name , <EOL> drain_method_params = fake_drain_method_params , <EOL> nerve_ns = fake_nerve_ns , <EOL> bounce_health_params = fake_bounce_health_params , <EOL> soa_dir = fake_soa_dir , <EOL> ) <EOL> assert mock_do_bounce . call_args [ <NUM_LIT:1> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] < set ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> assert len ( mock_do_bounce . call_args [ <NUM_LIT:1> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) == <NUM_LIT:2> <EOL> assert mock_do_bounce . call_args [ <NUM_LIT:1> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] == set ( [ ] ) <EOL> assert mock_stop_draining . call_count == <NUM_LIT:3> <EOL> def test_setup_service_srv_already_exists ( self ) : <EOL> fake_name = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_client = mock . MagicMock ( get_app = mock . Mock ( return_value = True ) ) <EOL> full_id = marathon_tools . format_job_id ( fake_name , fake_instance ) <EOL> fake_complete = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : full_id , <EOL> } <EOL> with contextlib . nested ( <EOL> mock . patch . object ( <EOL> self . fake_marathon_service_config , <EOL> '<STR_LIT>' , <EOL> return_value = fake_complete , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = self . fake_marathon_config , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> autospec = True , <EOL> ) , <EOL> ) as ( <EOL> format_marathon_app_dict_patch , <EOL> get_config_patch , <EOL> deploy_service_patch , <EOL> ) : <EOL> setup_marathon_job . setup_service ( <EOL> service = fake_name , <EOL> instance = fake_instance , <EOL> client = fake_client , <EOL> marathon_config = self . fake_marathon_config , <EOL> service_marathon_config = self . fake_marathon_service_config , <EOL> soa_dir = None , <EOL> ) <EOL> format_marathon_app_dict_patch . assert_called_once_with ( ) <EOL> assert deploy_service_patch . call_count == <NUM_LIT:1> <EOL> def test_setup_service_srv_does_not_exist ( self ) : <EOL> fake_name = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_response = mock . Mock ( <EOL> json = mock . Mock ( return_value = { '<STR_LIT:message>' : '<STR_LIT:test>' } ) ) <EOL> fake_client = mock . MagicMock ( get_app = mock . Mock ( <EOL> side_effect = marathon . exceptions . NotFoundError ( fake_response ) ) ) <EOL> full_id = marathon_tools . format_job_id ( fake_name , fake_instance , '<STR_LIT>' , '<STR_LIT>' ) <EOL> fake_complete = { <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:id>' : full_id , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> fake_bounce = '<STR_LIT>' <EOL> fake_drain_method = '<STR_LIT>' <EOL> fake_drain_method_params = { } <EOL> with contextlib . nested ( <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = ( <NUM_LIT> , '<STR_LIT>' ) , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch . object ( <EOL> self . fake_marathon_service_config , <EOL> '<STR_LIT>' , <EOL> return_value = fake_bounce , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch . object ( <EOL> self . fake_marathon_service_config , <EOL> '<STR_LIT>' , <EOL> return_value = fake_drain_method , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch . object ( <EOL> self . fake_marathon_service_config , <EOL> '<STR_LIT>' , <EOL> return_value = fake_drain_method_params , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch . object ( <EOL> self . fake_marathon_service_config , <EOL> '<STR_LIT>' , <EOL> return_value = fake_complete , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = self . fake_marathon_service_config , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = self . fake_service_namespace_config , <EOL> autospec = True , <EOL> ) , <EOL> ) as ( <EOL> deploy_service_patch , <EOL> get_bounce_patch , <EOL> get_drain_method_patch , <EOL> get_drain_method_params_patch , <EOL> format_marathon_app_dict_patch , <EOL> read_service_conf_patch , <EOL> read_namespace_conf_patch , <EOL> ) : <EOL> status , output = setup_marathon_job . setup_service ( <EOL> service = fake_name , <EOL> instance = fake_instance , <EOL> client = fake_client , <EOL> marathon_config = self . fake_marathon_config , <EOL> service_marathon_config = self . fake_marathon_service_config , <EOL> soa_dir = None , <EOL> ) <EOL> assert status == <NUM_LIT> <EOL> assert output == '<STR_LIT>' <EOL> get_bounce_patch . assert_called_once_with ( ) <EOL> format_marathon_app_dict_patch . assert_called_once_with ( ) <EOL> get_drain_method_patch . assert_called_once_with ( read_namespace_conf_patch . return_value ) <EOL> deploy_service_patch . assert_called_once_with ( <EOL> service = fake_name , <EOL> instance = fake_instance , <EOL> marathon_jobid = full_id , <EOL> config = fake_complete , <EOL> client = fake_client , <EOL> bounce_method = fake_bounce , <EOL> drain_method_name = fake_drain_method , <EOL> drain_method_params = fake_drain_method_params , <EOL> nerve_ns = self . fake_marathon_service_config . get_nerve_namespace ( ) , <EOL> bounce_health_params = self . fake_marathon_service_config . get_bounce_health_params ( <EOL> read_namespace_conf_patch . return_value ) , <EOL> soa_dir = None , <EOL> ) <EOL> def test_setup_service_srv_complete_config_raises ( self ) : <EOL> fake_name = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> with mock . patch . object ( <EOL> self . fake_marathon_service_config , <EOL> '<STR_LIT>' , <EOL> side_effect = NoDockerImageError , <EOL> ) : <EOL> status , output = setup_marathon_job . setup_service ( <EOL> service = fake_name , <EOL> instance = fake_instance , <EOL> client = None , <EOL> marathon_config = None , <EOL> service_marathon_config = self . fake_marathon_service_config , <EOL> soa_dir = None , <EOL> ) <EOL> assert status == <NUM_LIT:1> <EOL> expected = '<STR_LIT>' <EOL> assert expected in output <EOL> def test_deploy_service_unknown_drain_method ( self ) : <EOL> fake_bounce = '<STR_LIT>' <EOL> fake_drain_method = '<STR_LIT>' <EOL> fake_name = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_id = marathon_tools . format_job_id ( fake_name , fake_instance ) <EOL> fake_apps = [ mock . Mock ( id = fake_id , tasks = [ ] ) , mock . Mock ( id = ( '<STR_LIT>' % fake_id ) , tasks = [ ] ) ] <EOL> fake_client = mock . MagicMock ( <EOL> list_apps = mock . Mock ( return_value = fake_apps ) ) <EOL> fake_config = { '<STR_LIT:id>' : fake_id , '<STR_LIT>' : <NUM_LIT:2> } <EOL> errormsg = '<STR_LIT>' <EOL> expected = ( <NUM_LIT:1> , errormsg ) <EOL> with contextlib . nested ( <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> new = { '<STR_LIT>' : mock . Mock ( ) , '<STR_LIT>' : mock . Mock ( ) } , <EOL> ) <EOL> ) as ( mock_log , mock_load_system_paasta_config , mock_drain_methods ) : <EOL> mock_load_system_paasta_config . return_value . get_cluster = mock . Mock ( return_value = '<STR_LIT>' ) <EOL> actual = setup_marathon_job . deploy_service ( <EOL> service = fake_name , <EOL> instance = fake_instance , <EOL> marathon_jobid = fake_id , <EOL> config = fake_config , <EOL> client = fake_client , <EOL> bounce_method = fake_bounce , <EOL> drain_method_name = fake_drain_method , <EOL> drain_method_params = { } , <EOL> nerve_ns = fake_instance , <EOL> bounce_health_params = { } , <EOL> soa_dir = '<STR_LIT>' , <EOL> ) <EOL> assert mock_log . call_count == <NUM_LIT:1> <EOL> assert expected == actual <EOL> def test_deploy_service_unknown_bounce ( self ) : <EOL> fake_bounce = '<STR_LIT>' <EOL> fake_drain_method = '<STR_LIT>' <EOL> fake_name = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_id = marathon_tools . format_job_id ( fake_name , fake_instance ) <EOL> fake_apps = [ mock . Mock ( id = fake_id , tasks = [ ] ) , mock . Mock ( id = ( '<STR_LIT>' % fake_id ) , tasks = [ ] ) ] <EOL> fake_client = mock . MagicMock ( <EOL> list_apps = mock . Mock ( return_value = fake_apps ) ) <EOL> fake_config = { '<STR_LIT:id>' : fake_id , '<STR_LIT>' : <NUM_LIT:2> } <EOL> errormsg = '<STR_LIT>' % ( fake_bounce , '<STR_LIT:U+002CU+0020>' . join ( list_bounce_methods ( ) ) ) <EOL> expected = ( <NUM_LIT:1> , errormsg ) <EOL> with contextlib . nested ( <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> ) as ( mock_log , mock_load_system_paasta_config ) : <EOL> mock_load_system_paasta_config . return_value . get_cluster = mock . Mock ( return_value = '<STR_LIT>' ) <EOL> actual = setup_marathon_job . deploy_service ( <EOL> service = fake_name , <EOL> instance = fake_instance , <EOL> marathon_jobid = fake_id , <EOL> config = fake_config , <EOL> client = fake_client , <EOL> bounce_method = fake_bounce , <EOL> drain_method_name = fake_drain_method , <EOL> drain_method_params = { } , <EOL> nerve_ns = fake_instance , <EOL> bounce_health_params = { } , <EOL> soa_dir = '<STR_LIT>' , <EOL> ) <EOL> assert mock_log . call_count == <NUM_LIT:1> <EOL> assert expected == actual <EOL> fake_client . list_apps . assert_called_once_with ( embed_failures = True ) <EOL> assert fake_client . create_app . call_count == <NUM_LIT:0> <EOL> def test_deploy_service_known_bounce ( self ) : <EOL> fake_bounce = '<STR_LIT>' <EOL> fake_drain_method_name = '<STR_LIT>' <EOL> fake_name = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_id = marathon_tools . format_job_id ( fake_name , fake_instance , '<STR_LIT>' , '<STR_LIT>' ) <EOL> fake_config = { '<STR_LIT:id>' : fake_id , '<STR_LIT>' : <NUM_LIT:2> } <EOL> old_app_id = marathon_tools . format_job_id ( fake_name , fake_instance , '<STR_LIT>' , '<STR_LIT>' ) <EOL> old_task_to_drain = mock . Mock ( id = "<STR_LIT>" , app_id = old_app_id ) <EOL> old_task_is_draining = mock . Mock ( id = "<STR_LIT>" , app_id = old_app_id ) <EOL> old_task_dont_drain = mock . Mock ( id = "<STR_LIT>" , app_id = old_app_id ) <EOL> old_app = mock . Mock ( id = "<STR_LIT>" % old_app_id , tasks = [ old_task_to_drain , old_task_is_draining , old_task_dont_drain ] ) <EOL> fake_client = mock . MagicMock ( <EOL> list_apps = mock . Mock ( return_value = [ old_app ] ) , <EOL> kill_given_tasks = mock . Mock ( spec = lambda task_ids , scale = False : None ) , <EOL> ) <EOL> fake_bounce_func = mock . create_autospec ( <EOL> bounce_lib . brutal_bounce , <EOL> return_value = { <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : [ old_task_to_drain ] , <EOL> } <EOL> ) <EOL> fake_drain_method = mock . Mock ( is_draining = lambda t : t is old_task_is_draining , is_safe_to_kill = lambda t : True ) <EOL> with contextlib . nested ( <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = fake_bounce_func , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> autospec = True <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> autospec = True , <EOL> side_effect = lambda x , _ , __ , ___ , ** kwargs : x . tasks , <EOL> ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , return_value = fake_drain_method ) , <EOL> ) as ( _ , _ , _ , kill_old_ids_patch , create_marathon_app_patch , mock_log , mock_load_system_paasta_config , _ ) : <EOL> mock_load_system_paasta_config . return_value . get_cluster = mock . Mock ( return_value = '<STR_LIT>' ) <EOL> result = setup_marathon_job . deploy_service ( <EOL> service = fake_name , <EOL> instance = fake_instance , <EOL> marathon_jobid = fake_id , <EOL> config = fake_config , <EOL> client = fake_client , <EOL> bounce_method = fake_bounce , <EOL> drain_method_name = fake_drain_method_name , <EOL> drain_method_params = { } , <EOL> nerve_ns = fake_instance , <EOL> bounce_health_params = { } , <EOL> soa_dir = '<STR_LIT>' , <EOL> ) <EOL> assert result [ <NUM_LIT:0> ] == <NUM_LIT:0> , "<STR_LIT>" % result <EOL> fake_client . list_apps . assert_called_once_with ( embed_failures = True ) <EOL> assert fake_client . create_app . call_count == <NUM_LIT:0> <EOL> fake_bounce_func . assert_called_once_with ( <EOL> new_config = fake_config , <EOL> new_app_running = False , <EOL> happy_new_tasks = [ ] , <EOL> old_app_live_happy_tasks = { old_app . id : set ( [ old_task_to_drain , old_task_dont_drain ] ) } , <EOL> old_app_live_unhappy_tasks = { old_app . id : set ( ) } , <EOL> ) <EOL> assert fake_drain_method . drain . call_count == <NUM_LIT:2> <EOL> fake_drain_method . drain . assert_any_call ( old_task_is_draining ) <EOL> fake_drain_method . drain . assert_any_call ( old_task_to_drain ) <EOL> assert fake_client . kill_given_tasks . call_count == <NUM_LIT:1> <EOL> assert set ( [ old_task_to_drain . id , old_task_is_draining . id ] ) == set ( <EOL> fake_client . kill_given_tasks . call_args [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> assert fake_client . kill_given_tasks . call_args [ <NUM_LIT:1> ] [ '<STR_LIT>' ] is True <EOL> create_marathon_app_patch . assert_called_once_with ( fake_config [ '<STR_LIT:id>' ] , fake_config , fake_client ) <EOL> assert kill_old_ids_patch . call_count == <NUM_LIT:0> <EOL> assert mock_log . call_count == <NUM_LIT:5> <EOL> def test_deploy_service_already_bouncing ( self ) : <EOL> fake_bounce = '<STR_LIT>' <EOL> fake_drain_method = '<STR_LIT>' <EOL> fake_name = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_id = marathon_tools . format_job_id ( fake_name , fake_instance , '<STR_LIT>' , '<STR_LIT>' ) <EOL> fake_config = { '<STR_LIT:id>' : fake_id , '<STR_LIT>' : <NUM_LIT:2> } <EOL> old_app_id = ( '<STR_LIT>' % fake_id ) <EOL> old_task = mock . Mock ( id = "<STR_LIT>" , app_id = old_app_id ) <EOL> old_app = mock . Mock ( id = old_app_id , tasks = [ old_task ] ) <EOL> fake_client = mock . MagicMock ( <EOL> list_apps = mock . Mock ( return_value = [ old_app ] ) , <EOL> kill_task = mock . Mock ( spec = lambda app_id , id , scale = False : None ) , <EOL> ) <EOL> fake_bounce_func = mock . create_autospec ( <EOL> bounce_lib . brutal_bounce , <EOL> return_value = { <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : [ old_task ] , <EOL> } <EOL> ) <EOL> fake_short_id = marathon_tools . format_job_id ( fake_name , fake_instance ) <EOL> with contextlib . nested ( <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = fake_bounce_func , <EOL> autospec = True , <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> side_effect = bounce_lib . LockHeldException , <EOL> autospec = True <EOL> ) , <EOL> mock . patch ( <EOL> '<STR_LIT>' , <EOL> autospec = True , <EOL> side_effect = lambda x , _ , __ , ** kwargs : x . tasks , <EOL> ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> ) as ( _ , _ , _ , _ , mock_load_system_paasta_config ) : <EOL> mock_load_system_paasta_config . return_value . get_cluster = mock . Mock ( return_value = '<STR_LIT>' ) <EOL> result = setup_marathon_job . deploy_service ( <EOL> service = fake_name , <EOL> instance = fake_instance , <EOL> marathon_jobid = fake_id , <EOL> config = fake_config , <EOL> client = fake_client , <EOL> bounce_method = fake_bounce , <EOL> drain_method_name = fake_drain_method , <EOL> drain_method_params = { } , <EOL> nerve_ns = fake_instance , <EOL> bounce_health_params = { } , <EOL> soa_dir = '<STR_LIT>' , <EOL> ) <EOL> assert result == ( <NUM_LIT:1> , "<STR_LIT>" % fake_short_id ) <EOL> def test_deploy_service_logs_exceptions ( self ) : <EOL> fake_bounce = '<STR_LIT>' <EOL> fake_drain_method = '<STR_LIT>' <EOL> fake_name = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_id = marathon_tools . format_job_id ( fake_name , fake_instance ) <EOL> fake_apps = [ mock . Mock ( id = fake_id , tasks = [ ] ) , mock . Mock ( id = ( '<STR_LIT>' % fake_id ) , tasks = [ ] ) ] <EOL> fake_client = mock . MagicMock ( <EOL> list_apps = mock . Mock ( return_value = fake_apps ) ) <EOL> fake_config = { '<STR_LIT:id>' : fake_id , '<STR_LIT>' : <NUM_LIT:2> } <EOL> with contextlib . nested ( <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> mock . patch ( '<STR_LIT>' , side_effect = IOError ( '<STR_LIT:foo>' ) ) , <EOL> mock . patch ( '<STR_LIT>' , autospec = True ) , <EOL> ) as ( mock_log , mock_bounce , mock_load_system_paasta_config ) : <EOL> mock_load_system_paasta_config . return_value . get_cluster = mock . Mock ( return_value = '<STR_LIT>' ) <EOL> with raises ( IOError ) : <EOL> setup_marathon_job . deploy_service ( <EOL> service = fake_name , <EOL> instance = fake_instance , <EOL> marathon_jobid = fake_id , <EOL> config = fake_config , <EOL> client = fake_client , <EOL> bounce_method = fake_bounce , <EOL> drain_method_name = fake_drain_method , <EOL> drain_method_params = { } , <EOL> nerve_ns = fake_instance , <EOL> bounce_health_params = { } , <EOL> soa_dir = '<STR_LIT>' , <EOL> ) <EOL> assert fake_name in mock_log . mock_calls [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] [ "<STR_LIT>" ] <EOL> assert '<STR_LIT>' in mock_log . mock_calls [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] [ "<STR_LIT>" ] <EOL> def test_get_marathon_config ( self ) : <EOL> fake_conf = { '<STR_LIT>' : '<STR_LIT>' } <EOL> with mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = fake_conf , <EOL> autospec = True <EOL> ) as get_conf_patch : <EOL> assert setup_marathon_job . get_main_marathon_config ( ) == fake_conf <EOL> get_conf_patch . assert_called_once_with ( ) <EOL> class TestGetOldHappyUnhappyDrainingTasks ( object ) : <EOL> def fake_task ( self , state , happiness ) : <EOL> return mock . Mock ( _drain_state = state , _happiness = happiness ) <EOL> def fake_drain_method ( self ) : <EOL> return mock . Mock ( is_draining = lambda t : t . _drain_state == '<STR_LIT>' ) <EOL> def fake_get_happy_tasks ( self , app , service , nerve_ns , system_paasta_config , ** kwargs ) : <EOL> return [ t for t in app . tasks if t . _happiness == '<STR_LIT>' ] <EOL> def test_get_old_happy_unhappy_draining_tasks_empty ( self ) : <EOL> fake_name = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_id = marathon_tools . format_job_id ( fake_name , fake_instance ) <EOL> fake_apps = [ <EOL> mock . Mock ( id = fake_id , tasks = [ ] ) , <EOL> mock . Mock ( id = ( '<STR_LIT>' % fake_id ) , tasks = [ ] ) <EOL> ] <EOL> fake_system_paasta_config = utils . SystemPaastaConfig ( { } , "<STR_LIT>" ) <EOL> expected_live_happy_tasks = { <EOL> fake_apps [ <NUM_LIT:0> ] . id : set ( ) , <EOL> fake_apps [ <NUM_LIT:1> ] . id : set ( ) , <EOL> } <EOL> expected_live_unhappy_tasks = { <EOL> fake_apps [ <NUM_LIT:0> ] . id : set ( ) , <EOL> fake_apps [ <NUM_LIT:1> ] . id : set ( ) , <EOL> } <EOL> expected_draining_tasks = { <EOL> fake_apps [ <NUM_LIT:0> ] . id : set ( ) , <EOL> fake_apps [ <NUM_LIT:1> ] . id : set ( ) , <EOL> } <EOL> with mock . patch ( '<STR_LIT>' , side_effect = self . fake_get_happy_tasks ) : <EOL> actual = setup_marathon_job . get_old_happy_unhappy_draining_tasks ( <EOL> fake_apps , <EOL> self . fake_drain_method ( ) , <EOL> service = fake_name , <EOL> nerve_ns = fake_instance , <EOL> bounce_health_params = { } , <EOL> system_paasta_config = fake_system_paasta_config , <EOL> ) <EOL> actual_live_happy_tasks , actual_live_unhappy_tasks , actual_draining_tasks = actual <EOL> assert actual_live_happy_tasks == expected_live_happy_tasks <EOL> assert actual_live_unhappy_tasks == expected_live_unhappy_tasks <EOL> assert actual_draining_tasks == expected_draining_tasks <EOL> def test_get_old_happy_unhappy_draining_tasks_not_empty ( self ) : <EOL> fake_name = '<STR_LIT>' <EOL> fake_instance = '<STR_LIT>' <EOL> fake_id = marathon_tools . format_job_id ( fake_name , fake_instance ) <EOL> fake_apps = [ <EOL> mock . Mock ( <EOL> id = fake_id , <EOL> tasks = [ <EOL> self . fake_task ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> self . fake_task ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> self . fake_task ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] , <EOL> ) , <EOL> mock . Mock ( <EOL> id = ( '<STR_LIT>' % fake_id ) , <EOL> tasks = [ <EOL> self . fake_task ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> self . fake_task ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> self . fake_task ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] , <EOL> ) , <EOL> ] <EOL> fake_system_paasta_config = utils . SystemPaastaConfig ( { } , "<STR_LIT>" ) <EOL> expected_live_happy_tasks = { <EOL> fake_apps [ <NUM_LIT:0> ] . id : set ( [ fake_apps [ <NUM_LIT:0> ] . tasks [ <NUM_LIT:0> ] ] ) , <EOL> fake_apps [ <NUM_LIT:1> ] . id : set ( [ fake_apps [ <NUM_LIT:1> ] . tasks [ <NUM_LIT:0> ] ] ) , <EOL> } <EOL> expected_live_unhappy_tasks = { <EOL> fake_apps [ <NUM_LIT:0> ] . id : set ( [ fake_apps [ <NUM_LIT:0> ] . tasks [ <NUM_LIT:1> ] ] ) , <EOL> fake_apps [ <NUM_LIT:1> ] . id : set ( [ fake_apps [ <NUM_LIT:1> ] . tasks [ <NUM_LIT:1> ] ] ) , <EOL> } <EOL> expected_draining_tasks = { <EOL> fake_apps [ <NUM_LIT:0> ] . id : set ( [ fake_apps [ <NUM_LIT:0> ] . tasks [ <NUM_LIT:2> ] ] ) , <EOL> fake_apps [ <NUM_LIT:1> ] . id : set ( [ fake_apps [ <NUM_LIT:1> ] . tasks [ <NUM_LIT:2> ] ] ) , <EOL> } <EOL> with mock . patch ( '<STR_LIT>' , side_effect = self . fake_get_happy_tasks ) : <EOL> actual = setup_marathon_job . get_old_happy_unhappy_draining_tasks ( <EOL> fake_apps , <EOL> self . fake_drain_method ( ) , <EOL> service = fake_name , <EOL> nerve_ns = fake_instance , <EOL> bounce_health_params = { } , <EOL> system_paasta_config = fake_system_paasta_config , <EOL> ) <EOL> actual_live_happy_tasks , actual_live_unhappy_tasks , actual_draining_tasks = actual <EOL> assert actual_live_happy_tasks == expected_live_happy_tasks <EOL> assert actual_live_unhappy_tasks == expected_live_unhappy_tasks <EOL> assert actual_draining_tasks == expected_draining_tasks </s>
