def testConsolidateServiceStatesHealthy(self): """Test consolidating state for a healthy service.""" cfm = manager.CheckFileManager(checkdir=CHECKDIR) # Setup some test check results hcname = TestHealthCheck.__name__ hcname2 = '%s_2' % hcname statuses = [ manager.HEALTHCHECK_STATUS(hcname, True, '', []), manager.HEALTHCHECK_STATUS(hcname2, True, '', [])] cfm.service_check_results.setdefault(TEST_SERVICE_NAME, {}) cfm.service_check_results[TEST_SERVICE_NAME][hcname] = ( manager.HCEXECUTION_COMPLETED, TEST_EXEC_TIME, statuses[0]) cfm.service_check_results[TEST_SERVICE_NAME][hcname2] = ( manager.HCEXECUTION_IN_PROGRESS, TEST_EXEC_TIME, statuses[1]) # Run and check. cfm.ConsolidateServiceStates() self.assertTrue(TEST_SERVICE_NAME in cfm.service_states) _, health, healthchecks = cfm.service_states.get(TEST_SERVICE_NAME) self.assertTrue(health) self.assertEquals(0, len(healthchecks))
def testIsServiceHealthy(self): """Test checking whether service statuses are healthy.""" # Define some health check statuses. hch = manager.HEALTHCHECK_STATUS('healthy', True, manager.NULL_DESCRIPTION, manager.EMPTY_ACTIONS) hcq = manager.HEALTHCHECK_STATUS('quasi-healthy', True, 'Quasi-Healthy', ['QuasiAction']) hcu = manager.HEALTHCHECK_STATUS('unhealthy', False, 'Unhealthy', ['UnhealthyAction']) # Test a healthy service. s = manager.SERVICE_STATUS('healthy', True, []) self.assertTrue(manager.isServiceHealthy(s)) # Test a quasi-healthy service. s = manager.SERVICE_STATUS('quasi-healthy', True, [hch, hcq]) self.assertFalse(manager.isServiceHealthy(s)) # Test an unhealthy service. s = manager.SERVICE_STATUS('unhealthy', False, [hcu]) self.assertFalse(manager.isServiceHealthy(s)) # Test an object that is not a service status. self.assertFalse(manager.isServiceHealthy(hch))
def testConsolidateServiceStatesQuasiHealthy(self): """Test consolidating state for a service with quasi-healthy checks.""" cfm = manager.CheckFileManager(checkdir=CHECKDIR) # Setup some test check results hcname = TestHealthCheck.__name__ statuses = [ manager.HEALTHCHECK_STATUS(hcname, True, 'Quasi', ['RepairQuasi']), manager.HEALTHCHECK_STATUS(hcname, True, '', [])] cfm.service_check_results.setdefault(TEST_SERVICE_NAME, {}) for i, status in enumerate(statuses): name = '%s_%s' % (hcname, i) cfm.service_check_results[TEST_SERVICE_NAME][name] = ( manager.HCEXECUTION_COMPLETED, TEST_EXEC_TIME, status) # Run and check the results. cfm.ConsolidateServiceStates() self.assertTrue(TEST_SERVICE_NAME in cfm.service_states) _, health, healthchecks = cfm.service_states[TEST_SERVICE_NAME] self.assertTrue(health) self.assertEquals(1, len(healthchecks)) self.assertTrue(statuses[0] in healthchecks)
def testDetermineHealthcheckStatusQuasihealth(self): """Test DetermineHealthcheckStatus on a quasi-healthy check.""" hcname = TestHealthCheckQuasihealthy.__name__ testhc = TestHealthCheckQuasihealthy() desc, actions = testhc.Diagnose(testhc.Check()) expected = manager.HEALTHCHECK_STATUS(hcname, True, desc, actions) self.assertEquals(expected, manager.DetermineHealthcheckStatus(hcname, testhc))
def __init__(self): failed_check = manager.HEALTHCHECK_STATUS('hc1', False, 'Failed', []) self.service_statuses = [ manager.SERVICE_STATUS('service1', True, []), manager.SERVICE_STATUS('service2', False, [failed_check]) ] self.action_info = manager.ACTION_INFO('DummyAction', '', ['x'], {})
def testDetermineHealthcheckStatusHealthy(self): """Test DetermineHealthCheckStatus on a healthy check.""" hcname = TestHealthCheck.__name__ testhc = TestHealthCheck() expected = manager.HEALTHCHECK_STATUS(hcname, True, manager.NULL_DESCRIPTION, manager.EMPTY_ACTIONS) self.assertEquals(expected, manager.DetermineHealthcheckStatus(hcname, testhc))
def testMapHealthcheckStatusToDict(self): """Test mapping a manager.HEALTHCHECK_STATUS to a dict.""" def _func(): pass status = manager.HEALTHCHECK_STATUS('test', False, 'desc', [_func]) expect = {'name': 'test', 'health': False, 'description': 'desc', 'actions': ['_func']} self.assertEquals(expect, manager.MapHealthcheckStatusToDict(status))
def testIsHealthcheckHealthy(self): """Test checking whether health check statuses are healthy.""" # Test a healthy health check. hch = manager.HEALTHCHECK_STATUS('healthy', True, manager.NULL_DESCRIPTION, manager.EMPTY_ACTIONS) self.assertTrue(manager.isHealthcheckHealthy(hch)) # Test a quasi-healthy health check. hcq = manager.HEALTHCHECK_STATUS('quasi-healthy', True, 'Quasi-Healthy', ['QuasiAction']) self.assertFalse(manager.isHealthcheckHealthy(hcq)) # Test an unhealthy health check. hcu = manager.HEALTHCHECK_STATUS('unhealthy', False, 'Unhealthy', ['UnhealthyAction']) self.assertFalse(manager.isHealthcheckHealthy(hcu)) # Test an object that is not a health check status. s = manager.SERVICE_STATUS('service_status', True, []) self.assertFalse(manager.isHealthcheckHealthy(s))
def testMapServiceStatusToDict(self): """Test mapping a manager.SERVICE_STATUS to a dict.""" def _func(): pass hcstatus = manager.HEALTHCHECK_STATUS('test', False, 'desc', [_func]) hcexpect = {'name': 'test', 'health': False, 'description': 'desc', 'actions': ['_func']} status = manager.SERVICE_STATUS('test-service', False, [hcstatus]) expect = {'service': 'test-service', 'health': False, 'healthchecks': [hcexpect]} self.assertEquals(expect, manager.MapServiceStatusToDict(status))
def testActionInfo(self): """Test the ActionInfo RPC to collect information on a repair action.""" cfm = manager.CheckFileManager(checkdir=CHECKDIR) hcobj = TestHealthCheckMultipleActions() hcname = hcobj.__class__.__name__ actions = [ hcobj.NoParams, hcobj.PositionalParams, hcobj.DefaultParams, hcobj.MixedParams ] cfm.service_checks[TEST_SERVICE_NAME] = {hcname: (TEST_MTIME, hcobj)} unhealthy_status = manager.SERVICE_STATUS(TEST_SERVICE_NAME, False, [ manager.HEALTHCHECK_STATUS(hcname, False, 'Always fails', actions) ]) cfm.service_states[TEST_SERVICE_NAME] = unhealthy_status # Test ActionInfo when the action has no parameters. expect = manager.ACTION_INFO('NoParams', 'NoParams Action.', [], {}) self.assertEquals( expect, cfm.ActionInfo(TEST_SERVICE_NAME, hcname, 'NoParams')) # Test ActionInfo when the action has only positional parameters. expect = manager.ACTION_INFO('PositionalParams', 'PositionalParams Action.', ['x', 'y', 'z'], {}) self.assertEquals( expect, cfm.ActionInfo(TEST_SERVICE_NAME, hcname, 'PositionalParams')) # Test ActionInfo when the action has only default parameters. expect = manager.ACTION_INFO('DefaultParams', 'DefaultParams Action.', [], { 'x': 1, 'y': 2, 'z': 3 }) self.assertEquals( expect, cfm.ActionInfo(TEST_SERVICE_NAME, hcname, 'DefaultParams')) # Test ActionInfo when the action has positional and default parameters. expect = manager.ACTION_INFO('MixedParams', 'MixedParams Action.', ['x', 'y'], {'z': 1}) self.assertEquals( expect, cfm.ActionInfo(TEST_SERVICE_NAME, hcname, 'MixedParams'))
def testActionInfoActionNonExistent(self): """Test the ActionInfo RPC when the action does not exist.""" cfm = manager.CheckFileManager(checkdir=CHECKDIR) hcobj = TestHealthCheckUnhealthy() cfm.service_checks[TEST_SERVICE_NAME] = { hcobj.__class__.__name__: (TEST_MTIME, hcobj)} unhealthy_status = manager.SERVICE_STATUS( TEST_SERVICE_NAME, False, [manager.HEALTHCHECK_STATUS(hcobj.__class__.__name__, False, 'Always fails', [hcobj.Repair])]) cfm.service_states[TEST_SERVICE_NAME] = unhealthy_status expect = manager.ACTION_INFO('test', 'Action not recognized.', [], {}) result = cfm.ActionInfo(TEST_SERVICE_NAME, hcobj.__class__.__name__, 'test') self.assertEquals(expect, result)
def testRepairServiceInvalidActionArguments(self): """Test the RepairService RPC when the action arguments are invalid.""" cfm = manager.CheckFileManager(checkdir=CHECKDIR) hcobj = TestHealthCheckUnhealthy() cfm.service_checks[TEST_SERVICE_NAME] = { hcobj.__class__.__name__: (TEST_MTIME, hcobj)} unhealthy_status = manager.SERVICE_STATUS( TEST_SERVICE_NAME, False, [manager.HEALTHCHECK_STATUS(hcobj.__class__.__name__, False, 'Always fails', [hcobj.Repair])]) cfm.service_states[TEST_SERVICE_NAME] = unhealthy_status status = cfm.GetStatus(TEST_SERVICE_NAME) self.assertFalse(status.health) self.assertEquals(1, len(status.healthchecks)) status = cfm.RepairService(TEST_SERVICE_NAME, hcobj.__class__.__name__, 'Repair', [1, 2, 3], {}) self.assertFalse(status.health) self.assertEquals(1, len(status.healthchecks))
def testRepairService(self): """Test the RepairService RPC to repair an unhealthy service.""" cfm = manager.CheckFileManager(checkdir=CHECKDIR) hcobj = TestHealthCheckUnhealthy() cfm.service_checks[TEST_SERVICE_NAME] = { hcobj.__class__.__name__: (TEST_MTIME, hcobj) } unhealthy_status = manager.SERVICE_STATUS(TEST_SERVICE_NAME, False, [ manager.HEALTHCHECK_STATUS(hcobj.__class__.__name__, False, 'Always fails', [hcobj.Repair]) ]) cfm.service_states[TEST_SERVICE_NAME] = unhealthy_status status = cfm.GetStatus(TEST_SERVICE_NAME) self.assertFalse(status.health) self.assertEquals(1, len(status.healthchecks)) status = cfm.RepairService(TEST_SERVICE_NAME, hcobj.__class__.__name__, hcobj.Repair.__name__, [], {}) self.assertTrue(status.health) self.assertEquals(0, len(status.healthchecks))