From 2beb2d8f944e776b398f18cab8259d9a3de4c38a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Cavaill=C3=A9?= Date: Wed, 13 May 2015 14:35:08 -0400 Subject: [PATCH] [etcd] update test suite for full coverage --- tests/checks/integration/test_etcd.py | 107 +++++++++++++++++--------- 1 file changed, 69 insertions(+), 38 deletions(-) diff --git a/tests/checks/integration/test_etcd.py b/tests/checks/integration/test_etcd.py index 9926743775..f3d48b4e1e 100644 --- a/tests/checks/integration/test_etcd.py +++ b/tests/checks/integration/test_etcd.py @@ -1,28 +1,60 @@ +# stdlib import unittest -from tests.common import AgentCheckTest + +# 3p from nose.plugins.attrib import attr -from time import sleep -from checks import AgentCheck from requests.exceptions import Timeout -@attr(requires='etcd') -class EtcdTest(AgentCheckTest): +# project +from tests.checks.common import AgentCheckTest + +@attr(requires='etcd') +class CheckEtcdTest(AgentCheckTest): CHECK_NAME = "etcd" + + STORE_METRICS = [ + 'compareanddelete.fail', + 'compareanddelete.success', + 'compareandswap.fail', + 'compareandswap.success', + 'create.fail', + 'create.success', + 'delete.fail', + 'delete.success', + 'expire.count', + 'gets.fail', + 'gets.success', + 'sets.fail', + 'sets.success', + 'update.fail', + 'update.success', + 'watchers', + ] + def __init__(self, *args, **kwargs): AgentCheckTest.__init__(self, *args, **kwargs) self.config = {"instances": [{"url": "http://localhost:4001"}]} def test_metrics(self): - self.run_check(self.config) - sleep(1) - self.run_check(self.config) + self.run_check_twice(self.config) + tags = ['url:http://localhost:4001', 'etcd_state:leader'] - self.assertMetric('etcd.store.gets.success', metric_value=0.0, tags=tags) - self.assertMetric('etcd.store.gets.fail', metric_value=0.0, tags=tags) - self.assertMetric('etcd.self.send.appendrequest.count', metric_value=0.0, tags=tags) + for mname in self.STORE_METRICS: + self.assertMetric('etcd.store.%s' % mname, tags=tags, count=1) + + self.assertMetric('etcd.self.send.appendrequest.count', tags=tags, count=1) + self.assertMetric('etcd.self.recv.appendrequest.count', tags=tags, count=1) + self.assertServiceCheckOK(self.check.SERVICE_CHECK_NAME, + count=1, + tags=['url:http://localhost:4001']) + self.coverage_report() + + + # FIXME: not really an integration test, should be pretty easy + # to spin up a cluster to test that. def test_followers(self): mock = { "followers": { @@ -55,35 +87,34 @@ def test_followers(self): }, "leader": "etcd-node2" } - self.run_check(self.config) - sleep(1) - # Monkey-patch json to avoid having to stand up a full cluster - self.check._get_leader_metrics = lambda u, t: mock - self.run_check(self.config) - sleep(1) - self.run_check(self.config) - - self.assertMetric('etcd.leader.counts.fail') - self.assertMetric('etcd.leader.counts.success') - self.assertMetric('etcd.leader.latency.avg') - self.assertMetric('etcd.leader.latency.min') - self.assertMetric('etcd.leader.latency.max') - self.assertMetric('etcd.leader.latency.stddev') - self.assertMetric('etcd.leader.latency.current') - - def test_service_checks(self): - self.run_check(self.config) - - self.assertEqual(len(self.service_checks), 1, self.service_checks) - self.assertServiceCheck(self.check.SERVICE_CHECK_NAME, - status=AgentCheck.OK, - tags=['url:http://localhost:4001', 'etcd_state:leader']) + + mocks = { + '_get_leader_metrics': lambda u, t: mock + } + + self.run_check_twice(self.config, mocks=mocks) + + common_leader_tags = ['url:http://localhost:4001', 'etcd_state:leader'] + follower_tags = [ + common_leader_tags[:] + ['follower:etcd-node1'], + common_leader_tags[:] + ['follower:etcd-node3'], + ] + + for fol_tags in follower_tags: + self.assertMetric('etcd.leader.counts.fail', count=1, tags=fol_tags) + self.assertMetric('etcd.leader.counts.success', count=1, tags=fol_tags) + self.assertMetric('etcd.leader.latency.avg', count=1, tags=fol_tags) + self.assertMetric('etcd.leader.latency.min', count=1, tags=fol_tags) + self.assertMetric('etcd.leader.latency.max', count=1, tags=fol_tags) + self.assertMetric('etcd.leader.latency.stddev', count=1, tags=fol_tags) + self.assertMetric('etcd.leader.latency.current', count=1, tags=fol_tags) def test_bad_config(self): self.assertRaises(Exception, lambda: self.run_check({"instances": [{"url": "http://localhost:4001/test"}]})) - self.assertEqual(len(self.service_checks), 1, self.service_checks) - self.assertServiceCheck(self.check.SERVICE_CHECK_NAME, - status=AgentCheck.CRITICAL, - tags=['url:http://localhost:4001/test/v2/stats/self']) + self.assertServiceCheckCritical(self.check.SERVICE_CHECK_NAME, + count=1, + tags=['url:http://localhost:4001/test/v2/stats/self']) + + self.coverage_report()