Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tests] use AgentCheckTest for couch.py #1462

Merged
merged 1 commit into from
Mar 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions checks.d/couch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
from urlparse import urljoin

# 3rd party
import simplejson as json
import requests

# project
from checks import AgentCheck
from util import headers



class CouchDb(AgentCheck):
"""Extracts stats from CouchDB via its REST API
http://wiki.apache.org/couchdb/Runtime_Statistics
Expand Down
7 changes: 7 additions & 0 deletions ci/couchdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,18 @@ def couchdb_rootdir
&& ./configure --prefix=#{couchdb_rootdir} --with-js-lib=#{couchdb_rootdir}/lib --with-js-include=#{couchdb_rootdir}/include/js\
&& make -j $CONCURRENCY\
&& make install)
else
# Still needed to start
ENV['PATH'] = "#{couchdb_rootdir}/bin:#{ENV['PATH']}"
ENV['LD_LIBRARY_PATH'] = "#{couchdb_rootdir}/lib:#{ENV['LD_LIBRARY_PATH']}"
ENV['DYLD_LIBRARY_PATH'] = "#{couchdb_rootdir}/lib:#{ENV['DYLD_LIBRARY_PATH']}"
end
end

task :before_script => ['ci:common:before_script'] do
sh %(#{couchdb_rootdir}/bin/couchdb -b)
# Couch takes some time to start
sleep_for 2
end

task :script => ['ci:common:script'] do
Expand Down
89 changes: 39 additions & 50 deletions tests/test_couch.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,43 @@
import unittest
from tests.common import load_check
from tests.common import AgentCheckTest
from nose.plugins.attrib import attr
from checks import AgentCheck

@attr(requires='couchdb')
class CouchDBTestCase(unittest.TestCase):

def test_metrics(self):
config = {
'instances': [{
'server': 'http://localhost:5984',
}]
}
agentConfig = {
'version': '0.1',
'api_key': 'toto'
}

self.check = load_check('couch', config, agentConfig)

self.check.check(config['instances'][0])

metrics = self.check.get_metrics()
self.assertTrue(type(metrics) == type([]), metrics)
self.assertTrue(len(metrics) > 3)
self.assertTrue(len([k for k in metrics if "instance:http://localhost:5984" in k[3]['tags']]) > 3)

def test_service_checks(self):
config = {
'instances': [
{'server': 'http://localhost:5984'},
{'server': 'http://localhost:5985'}]
}
agentConfig = {
'version': '0.1',
'api_key': 'toto'
}

self.check = load_check('couch', config, agentConfig)
self.check.check(config['instances'][0])
self.assertRaises(Exception, self.check.check, config['instances'][1])

service_checks = self.check.get_service_checks()
self.assertEqual(len(service_checks), 2)

ok_svc_check = service_checks[0]
self.assertEqual(ok_svc_check['check'], self.check.SERVICE_CHECK_NAME)
self.assertEqual(ok_svc_check['status'], AgentCheck.OK)

cr_svc_check = service_checks[1]
self.assertEqual(cr_svc_check['check'], self.check.SERVICE_CHECK_NAME)
self.assertEqual(cr_svc_check['status'], AgentCheck.CRITICAL)

@attr(requires='couchdb')
class CouchTestCase(AgentCheckTest):

CHECK_NAME = 'couch'
DB_NAMES = ['_users', '_replicator']
CHECK_GAUGES = [
'couchdb.by_db.disk_size',
'couchdb.by_db.doc_count',
]

def __init__(self, *args, **kwargs):
AgentCheckTest.__init__(self, *args, **kwargs)
self.config = {"instances": [{"server": "http://localhost:5984"}]}

def test_couch(self):
self.run_check(self.config)
for db_name in self.DB_NAMES:
tags = ['instance:http://localhost:5984', 'db:{0}'.format(db_name)]
for gauge in self.CHECK_GAUGES:
self.assertMetric(gauge, tags=tags, count=1)

self.assertServiceCheck(self.check.SERVICE_CHECK_NAME,
status=AgentCheck.OK,
tags=['instance:http://localhost:5984'],
count=1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Can you throw a self.coverage_report() in the mix? It fails the test if you run the CI with COVERAGE=true and you didn't test all the metrics / service checks, I plan on making it a bit nicer but run it in the CI at some point.


self.coverage_report()

def test_bad_config(self):
self.assertRaises(
Exception,
lambda: self.run_check({"instances": [{"server": "http://localhost:5985"}]})
)

self.assertServiceCheck(self.check.SERVICE_CHECK_NAME,
status=AgentCheck.CRITICAL,
tags=['instance:http://localhost:5985'],
count=1)