def test_get_happy_tasks_check_haproxy_multiple_locations(self): """If we specify that a task should be in haproxy, don't call it happy unless it's in haproxy.""" tasks = [mock.Mock(health_check_results=[mock.Mock(alive=True)]) for i in xrange(5)] fake_app = mock.Mock(tasks=tasks, health_checks=[]) with contextlib.nested( mock.patch('bounce_lib.get_registered_marathon_tasks', side_effect=[tasks[2:3], tasks[3:]], autospec=True), mock.patch('mesos_tools.get_mesos_slaves_grouped_by_attribute', autospec=True), ) as ( get_registered_marathon_tasks_patch, get_mesos_slaves_grouped_by_attribute_patch, ): get_mesos_slaves_grouped_by_attribute_patch.return_value = { 'fake_region': ['fake_host1'], 'fake_other_region': ['fake_host2'], } assert bounce_lib.get_happy_tasks(fake_app, 'service', 'namespace', check_haproxy=True) == tasks[2:] get_registered_marathon_tasks_patch.assert_any_call( 'fake_host1', DEFAULT_SYNAPSE_PORT, 'service.namespace', tasks, ) get_registered_marathon_tasks_patch.assert_any_call( 'fake_host2', DEFAULT_SYNAPSE_PORT, 'service.namespace', tasks, )
def test_get_happy_tasks_when_some_unhealthy(self): """Only tasks with a passing healthcheck should be happy""" fake_failing_healthcheck_results = [mock.Mock(alive=False)] fake_successful_healthcheck_results = [mock.Mock(alive=True)] tasks = [mock.Mock(health_check_results=fake_failing_healthcheck_results), mock.Mock(health_check_results=fake_failing_healthcheck_results), mock.Mock(health_check_results=fake_successful_healthcheck_results)] fake_app = mock.Mock(tasks=tasks, health_checks=[]) assert bounce_lib.get_happy_tasks(fake_app, 'service', 'namespace') == tasks[-1:]
def test_get_happy_tasks_min_task_uptime_when_unhealthy(self): """If we specify a minimum task age, tasks newer than that should not be considered happy.""" now = datetime.datetime(2000, 1, 1, 0, 0, 0) tasks = [mock.Mock(health_check_results=[mock.Mock(alive=False)], started_at=(now - datetime.timedelta(minutes=i))) for i in xrange(5)] fake_app = mock.Mock(tasks=tasks, health_checks=[]) with mock.patch('paasta_tools.bounce_lib.datetime.datetime', utcnow=lambda: now, autospec=True): assert bounce_lib.get_happy_tasks(fake_app, 'service', 'namespace', min_task_uptime=121) == []
def test_get_happy_tasks_min_task_uptime(self): """If we specify a minimum task age, tasks newer than that should not be considered happy.""" now = datetime.datetime(2000, 1, 1, 0, 0, 0) tasks = [mock.Mock(health_check_results=[], started_at=(now - datetime.timedelta(minutes=i))) for i in xrange(5)] fake_app = mock.Mock(tasks=tasks, health_checks=[]) # I would have just mocked datetime.datetime.utcnow, but that's apparently difficult; I have to mock # datetime.datetime instead, and give it a utcnow attribute. with mock.patch('paasta_tools.bounce_lib.datetime.datetime', utcnow=lambda: now, autospec=True): assert bounce_lib.get_happy_tasks(fake_app, 'service', 'namespace', min_task_uptime=121) == tasks[3:]
def test_get_happy_tasks_check_haproxy_when_unhealthy(self): """If we specify that a task should be in haproxy, don't call it happy unless it's in haproxy.""" tasks = [mock.Mock(health_check_results=[mock.Mock(alive=False)]) for i in xrange(5)] fake_app = mock.Mock(tasks=tasks, health_checks=[]) with contextlib.nested( mock.patch('bounce_lib.get_registered_marathon_tasks', return_value=tasks[2:], autospec=True), mock.patch('mesos_tools.get_mesos_slaves_grouped_by_attribute', return_value={'fake_region': ['fake_host']}, autospec=True), ) as ( _, get_mesos_slaves_grouped_by_attribute_patch, ): assert bounce_lib.get_happy_tasks(fake_app, 'service', 'namespace', check_haproxy=True) == []
def test_get_happy_tasks_with_multiple_healthchecks_fail(self): """Only tasks with at least one passing healthcheck should be happy""" fake_successful_healthcheck_results = [mock.Mock(alive=False), mock.Mock(alive=False)] tasks = [mock.Mock(health_check_results=fake_successful_healthcheck_results)] fake_app = mock.Mock(tasks=tasks, health_checks=[]) assert bounce_lib.get_happy_tasks(fake_app, 'service', 'namespace') == []
def test_get_happy_tasks_when_all_healthy(self): """All tasks with only passing healthchecks should be happy""" tasks = [mock.Mock(health_check_results=[mock.Mock(alive=True)]) for _ in xrange(5)] fake_app = mock.Mock(tasks=tasks, health_checks=[]) assert bounce_lib.get_happy_tasks(fake_app, 'service', 'namespace') == tasks
def test_get_happy_tasks_when_running_with_healthchecks_defined(self): """All running tasks with no health check results are unhealthy if the app defines healthchecks""" tasks = [mock.Mock(health_check_results=[]) for _ in xrange(5)] fake_app = mock.Mock(tasks=tasks, health_checks=["fake_healthcheck_definition"]) assert bounce_lib.get_happy_tasks(fake_app, 'service', 'namespace') == []