From adeff1a87779f534ffabee2e5dc67ec1292e9ae0 Mon Sep 17 00:00:00 2001 From: Yann Mahe Date: Tue, 7 Jul 2015 18:35:49 -0400 Subject: [PATCH] [supervisord] full coverage integration tests Upgrade integration tests to use AgentCheckTest. Assess full coverage. --- tests/checks/integration/test_supervisord.py | 105 +++++++++++++------ 1 file changed, 72 insertions(+), 33 deletions(-) diff --git a/tests/checks/integration/test_supervisord.py b/tests/checks/integration/test_supervisord.py index a6d09b9337..00a045c154 100644 --- a/tests/checks/integration/test_supervisord.py +++ b/tests/checks/integration/test_supervisord.py @@ -1,48 +1,87 @@ # stdlib -import os from time import sleep -import unittest # 3p from nose.plugins.attrib import attr # project -from tests.checks.common import get_check +from checks import AgentCheck +from tests.checks.common import AgentCheckTest + +PROCESSES = ["program_0", "program_1", "program_2"] +STATUSES = ["down", "up", "unknown"] @attr(requires='supervisord') -class TestSupervisordCheck(unittest.TestCase): +class TestSupervisordCheck(AgentCheckTest): + CHECK_NAME = 'supervisord' + + SUPERVISORD_CONFIG = [{ + 'name': "travis", + 'socket': "unix:///home/vagrant/workspace/supervisor_test/supervisord/supervisor.sock", + }] - def test_travis_supervisord(self): - """Integration test for supervisord check. Using a supervisord on Travis.""" + BAD_SUPERVISORD_CONFIG = [{ + 'name': "travis", + 'socket': "unix:///wrong/path/supervisor.sock", + }] - # Load yaml config - config_str = open(os.environ['VOLATILE_DIR'] + '/supervisor/supervisord.yaml', 'r').read() - self.assertTrue(config_str is not None and len(config_str) > 0, msg=config_str) + # Supervisord should run 3 programs for 10, 20 and 30 seconds + # respectively. + # The following dictionnary shows the processes by state for each iteration. + PROCESSES_BY_STATE_BY_ITERATION = map( + lambda x: dict(up=PROCESSES[x:], down=PROCESSES[:x], unknown=[]), + range(4) + ) - # init the check and get the instances - check, instances = get_check('supervisord', config_str) - self.assertTrue(check is not None, msg=check) - self.assertEquals(len(instances), 1) + def test_check(self): + """ + Run Supervisord check and assess coverage + """ + config = {'instances': self.SUPERVISORD_CONFIG} + instance_tags = ["supervisord_server:travis"] - # Supervisord should run 3 programs for 30, 60 and 90 seconds - # respectively. The tests below will ensure that the process count - # metric is reported correctly after (roughly) 10, 40, 70 and 100 seconds for i in range(4): - try: - # Run the check - check.check(instances[0]) - except Exception, e: - # Make sure that it ran successfully - self.assertTrue(False, msg=str(e)) - else: - up, down = 0, 0 - for name, timestamp, value, meta in check.get_metrics(): - if name == 'supervisord.process.count': - if 'status:up' in meta['tags']: - up = value - elif 'status:down' in meta['tags']: - down = value - self.assertEquals(up, 3 - i) - self.assertEquals(down, i) - sleep(10) + # Run the check + self.run_check(config) + + # Check metrics and service checks scoped by process + for proc in PROCESSES: + process_tags = instance_tags + ["supervisord_process:{0}".format(proc)] + process_status = AgentCheck.OK if proc in \ + self.PROCESSES_BY_STATE_BY_ITERATION[i]['up'] else AgentCheck.CRITICAL + + self.assertMetric("supervisord.process.uptime", tags=process_tags, count=1) + self.assertServiceCheck("supervisord.process.status", status=process_status, + tags=process_tags, count=1) + # Check instance metrics + for status in STATUSES: + status_tags = instance_tags + ["status:{0}".format(status)] + count_processes = len(self.PROCESSES_BY_STATE_BY_ITERATION[i][status]) + self.assertMetric("supervisord.process.count", value=count_processes, + tags=status_tags, count=1) + + # Check service checks + self.assertServiceCheck("supervisord.can_connect", status=AgentCheck.OK, + tags=instance_tags, count=1) + + # Raises when COVERAGE=true and coverage < 100% + self.coverage_report() + + # Sleep 10s to give enough time to processes to terminate + sleep(10) + + def test_connection_falure(self): + """ + Service check reports connection failure + """ + config = {'instances': self.BAD_SUPERVISORD_CONFIG} + instance_tags = ["supervisord_server:travis"] + + self.assertRaises( + Exception, + lambda: self.run_check(config) + ) + self.assertServiceCheck("supervisord.can_connect", status=AgentCheck.CRITICAL, + tags=instance_tags, count=1) + self.coverage_report()