Skip to content

Commit

Permalink
[docker] add unit test for excl by tag feature
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoCavaille committed Feb 4, 2015
1 parent 0e862f5 commit 3eaed62
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
8 changes: 5 additions & 3 deletions checks.d/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def unix_open(self, req):
class Docker(AgentCheck):
"""Collect metrics and events from Docker API and cgroups"""

def __init__(self, name, init_config, agentConfig):
AgentCheck.__init__(self, name, init_config, agentConfig)
def __init__(self, name, init_config, agentConfig, instances=None):
AgentCheck.__init__(self, name, init_config, agentConfig, instances)

# Initialize a HTTP opener with Unix socket support
socket_timeout = int(init_config.get('socket_timeout', 0)) or DEFAULT_SOCKET_TIMEOUT
Expand Down Expand Up @@ -415,11 +415,13 @@ def _get_cgroup_file(self, cgroup, container_id, filename):
def _find_cgroup(self, hierarchy, docker_root):
"""Finds the mount point for a specified cgroup hierarchy. Works with
old style and new style mounts."""
fp = None
try:
fp = open(os.path.join(docker_root, "/proc/mounts"))
mounts = map(lambda x: x.split(), fp.read().splitlines())
finally:
fp.close()
if fp is not None:
fp.close()
cgroup_mounts = filter(lambda x: x[2] == "cgroup", mounts)
if len(cgroup_mounts) == 0:
raise Exception("Can't find mounted cgroups. If you run the Agent inside a container,"
Expand Down
21 changes: 21 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@
from config import get_checksd_path
from util import get_os, get_hostname

def get_check_class(name):
checksd_path = get_checksd_path(get_os())
if checksd_path not in sys.path:
sys.path.append(checksd_path)

check_module = __import__(name)
check_class = None
classes = inspect.getmembers(check_module, inspect.isclass)
for _, clsmember in classes:
if clsmember == AgentCheck:
continue
if issubclass(clsmember, AgentCheck):
check_class = clsmember
if AgentCheck in clsmember.__bases__:
continue
else:
break

return check_class

def load_check(name, config, agentConfig):
checksd_path = get_checksd_path(get_os())
if checksd_path not in sys.path:
Expand Down Expand Up @@ -39,6 +59,7 @@ def load_check(name, config, agentConfig):
try:
return check_class(name, init_config=init_config, agentConfig=agentConfig, instances=instances)
except Exception as e:
raise
raise Exception("Check is using old API, {0}".format(e))

def kill_subprocess(process_obj):
Expand Down
77 changes: 77 additions & 0 deletions tests/test_docker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import unittest

from mock import patch

from tests.common import get_check_class

def _mocked_find_cgroup(*args, **kwargs):
return

class DockerCheckTest(unittest.TestCase):
def test_tag_exclude_all(self):
""" exclude all, except ubuntu and debian. """
instance = {
'include': [
'docker_image:ubuntu',
'docker_image:debian',
],
'exclude': ['.*'],
}

klass = get_check_class('docker')
# NO-OP but loads the check
with patch.object(klass, '_find_cgroup', _mocked_find_cgroup):
check = klass('docker', {}, {})

check._prepare_filters(instance)
self.assertEquals(len(instance['exclude_patterns']), 1)
self.assertEquals(len(instance['include_patterns']), 2)

truth_table_exclusion = {
'some_tag': True,
'debian:ubuntu': True,
'docker_image:centos': True,
'docker_image:ubuntu': False,
'docker_image:debian': False,
}

for tag, val in truth_table_exclusion.iteritems():
self.assertEquals(
check._is_container_excluded(instance, [tag]),
val,
"{0} expected {1} but is not".format(tag, val)
)

def test_tag_include_all(self):
""" exclude all, except ubuntu and debian. """
instance = {
'include': [],
'exclude': [
'docker_image:ubuntu',
'docker_image:debian',
],
}

klass = get_check_class('docker')
# NO-OP but loads the check
with patch.object(klass, '_find_cgroup', _mocked_find_cgroup):
check = klass('docker', {}, {})

check._prepare_filters(instance)
self.assertEquals(len(instance['exclude_patterns']), 2)
self.assertEquals(len(instance['include_patterns']), 0)

truth_table_exclusion = {
'some_tag': False,
'debian:ubuntu': False,
'docker_image:centos': False,
'docker_image:ubuntu': True,
'docker_image:debian': True,
}

for tag, val in truth_table_exclusion.iteritems():
self.assertEquals(
check._is_container_excluded(instance, [tag]),
val,
"{0} expected {1} but is not".format(tag, val)
)

0 comments on commit 3eaed62

Please sign in to comment.