-
Notifications
You must be signed in to change notification settings - Fork 812
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[supervisord] full coverage integration tests
Upgrade integration tests to use AgentCheckTest. Assess full coverage.
- Loading branch information
Showing
1 changed file
with
72 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |