From 25905497a07dfd69ad0338d90b1fb6a2eb26b846 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. --- ci/resources/supervisord/supervisord.yaml | 6 -- ci/supervisord.rb | 10 +- tests/checks/integration/test_supervisord.py | 105 +++++++++++++------ 3 files changed, 77 insertions(+), 44 deletions(-) delete mode 100755 ci/resources/supervisord/supervisord.yaml diff --git a/ci/resources/supervisord/supervisord.yaml b/ci/resources/supervisord/supervisord.yaml deleted file mode 100755 index 5bc09be5db..0000000000 --- a/ci/resources/supervisord/supervisord.yaml +++ /dev/null @@ -1,6 +0,0 @@ -init_config: - -instances: - - name: travis - socket: unix://VOLATILE_DIR//supervisor.sock - host: http://127.0.0.1 diff --git a/ci/supervisord.rb b/ci/supervisord.rb index d804a5e53b..2598d2eef4 100644 --- a/ci/supervisord.rb +++ b/ci/supervisord.rb @@ -21,12 +21,10 @@ def supervisor_rootdir task before_script: ['ci:common:before_script'] do sh %(mkdir -p $VOLATILE_DIR/supervisor) - %w(supervisord.conf supervisord.yaml).each do |conf| - sh %(cp $TRAVIS_BUILD_DIR/ci/resources/supervisord/#{conf}\ - $VOLATILE_DIR/supervisor/) - sh %(sed -i -- 's/VOLATILE_DIR/#{ENV['VOLATILE_DIR'].gsub '/', '\/'}/g'\ - $VOLATILE_DIR/supervisor/#{conf}) - end + sh %(cp $TRAVIS_BUILD_DIR/ci/resources/supervisord/supervisord.conf\ + $VOLATILE_DIR/supervisor/) + sh %(sed -i -- 's/VOLATILE_DIR/#{ENV['VOLATILE_DIR'].gsub '/', '\/'}/g'\ + $VOLATILE_DIR/supervisor/supervisord.conf) 3.times do |i| sh %(cp $TRAVIS_BUILD_DIR/ci/resources/supervisord/program_#{i}.sh\ diff --git a/tests/checks/integration/test_supervisord.py b/tests/checks/integration/test_supervisord.py index a6d09b9337..053bc5c686 100644 --- a/tests/checks/integration/test_supervisord.py +++ b/tests/checks/integration/test_supervisord.py @@ -1,48 +1,89 @@ # 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://{0}//supervisor.sock".format(os.environ['VOLATILE_DIR']), + }] - 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", + 'host': "http://127.0.0.1", + }] - # 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()