Skip to content

Commit

Permalink
[etcd] update test suite for full coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoCavaille committed May 13, 2015
1 parent 3f24461 commit 2beb2d8
Showing 1 changed file with 69 additions and 38 deletions.
107 changes: 69 additions & 38 deletions tests/checks/integration/test_etcd.py
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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()

0 comments on commit 2beb2d8

Please sign in to comment.