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

[docker] be resilient to dockerpy buggy api calls #2608

Merged
merged 1 commit into from
Jun 16, 2016
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
26 changes: 26 additions & 0 deletions tests/checks/mock/test_docker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# (C) Datadog, Inc. 2010-2016
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
# stdlib
import mock
import unittest

from utils.dockerutil import DockerUtil


class TestDockerutil(unittest.TestCase):
def setUp(self):
self.dockerutil = DockerUtil()

@mock.patch('utils.dockerutil.DockerUtil.client')
def test_get_events(self, mocked_client):
mocked_client.events.return_value = [
{'status': 'stop', 'id': '1234567890', 'from': '1234567890', 'time': 1423247867}
]
events_generator, _ = self.dockerutil.get_events()
self.assertEqual(len(events_generator), 1)

# bug in dockerpy, we should be resilient
mocked_client.events.return_value = [u'an error from Docker API here']
events_generator, _ = self.dockerutil.get_events()
self.assertEqual(len(list(events_generator)), 0)
14 changes: 9 additions & 5 deletions utils/dockerutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ class MountException(Exception):
log = logging.getLogger(__name__)


class DockerUtil():
class DockerUtil:
__metaclass__ = Singleton

DEFAULT_SETTINGS = {"version": DEFAULT_VERSION}

def __init__(self, **kwargs):
self._docker_root = None
self.events = []

if 'init_config' in kwargs and 'instance' in kwargs:
init_config = kwargs.get('init_config')
Expand Down Expand Up @@ -96,11 +97,14 @@ def get_events(self):
until=now, decode=True)
self._latest_event_collection_ts = now
for event in event_generator:
if event != '':
try:
if event.get('status') in CONFIG_RELOAD_STATUS:
should_reload_conf = True
self.events.append(event)
if event.get('status') in CONFIG_RELOAD_STATUS:
should_reload_conf = True
return (self.events, should_reload_conf)
except AttributeError:
log.debug('Unable to parse Docker event: %s', event)

return self.events, should_reload_conf

def get_hostname(self):
"""Return the `Name` param from `docker info` to use as the hostname"""
Expand Down