def test_do_bounce_when_create_app_and_new_app_running(self): fake_task_to_drain = mock.Mock(app_id='fake_app_to_kill_1') fake_bounce_func_return = { 'create_app': True, 'tasks_to_drain': [fake_task_to_drain], } fake_bounce_func = mock.create_autospec( bounce_lib.brutal_bounce, return_value=fake_bounce_func_return, ) fake_config = {'instances': 5} fake_new_app_running = True fake_happy_new_tasks = ['fake_one', 'fake_two', 'fake_three'] fake_old_app_live_tasks = {'fake_app_to_kill_1': set([fake_task_to_drain])} fake_old_app_draining_tasks = {'fake_app_to_kill_1': set()} fake_service = 'fake_service' fake_serviceinstance = 'fake_service.fake_instance' self.fake_cluster = 'fake_cluster' fake_instance = 'fake_instance' fake_bounce_method = 'fake_bounce_method' fake_drain_method = mock.Mock(is_safe_to_kill=lambda t: False) fake_marathon_jobid = 'fake.marathon.jobid' fake_client = mock.create_autospec( marathon.MarathonClient ) expected_new_task_count = fake_config["instances"] - len(fake_happy_new_tasks) expected_drain_task_count = len(fake_bounce_func_return['tasks_to_drain']) with contextlib.nested( mock.patch('setup_marathon_job._log', autospec=True), mock.patch('setup_marathon_job.bounce_lib.create_marathon_app', autospec=True), mock.patch('setup_marathon_job.bounce_lib.kill_old_ids', autospec=True), ) as (mock_log, mock_create_marathon_app, mock_kill_old_ids): setup_marathon_job.do_bounce( bounce_func=fake_bounce_func, drain_method=fake_drain_method, config=fake_config, new_app_running=fake_new_app_running, happy_new_tasks=fake_happy_new_tasks, old_app_live_tasks=fake_old_app_live_tasks, old_app_draining_tasks=fake_old_app_draining_tasks, service=fake_service, bounce_method=fake_bounce_method, serviceinstance=fake_serviceinstance, cluster=self.fake_cluster, instance=fake_instance, marathon_jobid=fake_marathon_jobid, client=fake_client, soa_dir='fake_soa_dir', ) first_logged_line = mock_log.mock_calls[0][2]["line"] assert '%s new tasks' % expected_new_task_count in first_logged_line second_logged_line = mock_log.mock_calls[1][2]["line"] assert 'draining %s old tasks' % expected_drain_task_count in second_logged_line assert mock_log.call_count == 2 assert mock_create_marathon_app.call_count == 0 assert fake_client.kill_task.call_count == 0 assert mock_kill_old_ids.call_count == 0 assert fake_drain_method.drain.call_count == len(fake_bounce_func_return["tasks_to_drain"])
def test_do_bounce_when_nothing_to_do(self): fake_bounce_func_return = { 'create_app': False, 'tasks_to_drain': [], } fake_bounce_func = mock.create_autospec( bounce_lib.brutal_bounce, return_value=fake_bounce_func_return, ) fake_config = {'instances': 3} fake_new_app_running = True fake_happy_new_tasks = ['fake_one', 'fake_two', 'fake_three'] fake_old_app_live_tasks = {} fake_old_app_draining_tasks = {} fake_service = 'fake_service' fake_serviceinstance = 'fake_service.fake_instance' self.fake_cluster = 'fake_cluster' fake_instance = 'fake_instance' fake_bounce_method = 'fake_bounce_method' fake_drain_method = mock.Mock() fake_marathon_jobid = 'fake.marathon.jobid' fake_client = mock.create_autospec( marathon.MarathonClient ) with contextlib.nested( mock.patch('setup_marathon_job._log', autospec=True), mock.patch('setup_marathon_job.bounce_lib.create_marathon_app', autospec=True), mock.patch('setup_marathon_job.bounce_lib.kill_old_ids', autospec=True), mock.patch('setup_marathon_job.send_sensu_bounce_keepalive', autospec=True), ) as ( mock_log, mock_create_marathon_app, mock_kill_old_ids, mock_send_sensu_bounce_keepalive, ): setup_marathon_job.do_bounce( bounce_func=fake_bounce_func, drain_method=fake_drain_method, config=fake_config, new_app_running=fake_new_app_running, happy_new_tasks=fake_happy_new_tasks, old_app_live_tasks=fake_old_app_live_tasks, old_app_draining_tasks=fake_old_app_draining_tasks, service=fake_service, bounce_method=fake_bounce_method, serviceinstance=fake_serviceinstance, cluster=self.fake_cluster, instance=fake_instance, marathon_jobid=fake_marathon_jobid, client=fake_client, soa_dir='fake_soa_dir', ) assert mock_log.call_count == 0 assert mock_create_marathon_app.call_count == 0 assert fake_drain_method.drain.call_count == 0 assert mock_kill_old_ids.call_count == 0 # When doing nothing, we need to send the keepalive heartbeat to Sensu mock_send_sensu_bounce_keepalive.assert_called_once_with( service=fake_service, instance=fake_instance, cluster=self.fake_cluster, soa_dir='fake_soa_dir', )
def test_do_bounce_when_apps_to_kill(self): fake_bounce_func_return = { 'create_app': False, 'tasks_to_drain': [], } fake_bounce_func = mock.create_autospec( bounce_lib.brutal_bounce, return_value=fake_bounce_func_return, ) fake_config = {'instances': 5} fake_new_app_running = True fake_happy_new_tasks = ['fake_one', 'fake_two', 'fake_three'] fake_old_app_live_tasks = {'fake_app_to_kill_1': set()} fake_old_app_draining_tasks = {'fake_app_to_kill_1': set()} fake_service = 'fake_service' fake_serviceinstance = 'fake_service.fake_instance' self.fake_cluster = 'fake_cluster' fake_instance = 'fake_instance' fake_bounce_method = 'fake_bounce_method' fake_drain_method = mock.Mock() fake_marathon_jobid = 'fake.marathon.jobid' fake_client = mock.create_autospec( marathon.MarathonClient ) expected_new_task_count = fake_config["instances"] - len(fake_happy_new_tasks) with contextlib.nested( mock.patch('setup_marathon_job._log', autospec=True), mock.patch('setup_marathon_job.bounce_lib.create_marathon_app', autospec=True), mock.patch('setup_marathon_job.bounce_lib.kill_old_ids', autospec=True), ) as (mock_log, mock_create_marathon_app, mock_kill_old_ids): setup_marathon_job.do_bounce( bounce_func=fake_bounce_func, drain_method=fake_drain_method, config=fake_config, new_app_running=fake_new_app_running, happy_new_tasks=fake_happy_new_tasks, old_app_live_tasks=fake_old_app_live_tasks, old_app_draining_tasks=fake_old_app_draining_tasks, service=fake_service, bounce_method=fake_bounce_method, serviceinstance=fake_serviceinstance, cluster=self.fake_cluster, instance=fake_instance, marathon_jobid=fake_marathon_jobid, client=fake_client, soa_dir='fake_soa_dir', ) assert mock_log.call_count == 3 first_logged_line = mock_log.mock_calls[0][2]["line"] assert '%s new tasks' % expected_new_task_count in first_logged_line second_logged_line = mock_log.mock_calls[1][2]["line"] assert 'removing old unused apps with app_ids: %s' % 'fake_app_to_kill_1' in second_logged_line assert mock_create_marathon_app.call_count == 0 assert fake_client.kill_task.call_count == len(fake_bounce_func_return["tasks_to_drain"]) assert mock_kill_old_ids.call_count == 1 third_logged_line = mock_log.mock_calls[2][2]["line"] assert '%s bounce on %s finish' % (fake_bounce_method, fake_serviceinstance) in third_logged_line assert 'Now running %s' % fake_marathon_jobid in third_logged_line