Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
hub-client / tests / test_logger.py
Size: Mime:
import json
import logging
from StringIO import StringIO

from django.conf import settings
from django.test import TestCase
from django.test.utils import override_settings
from mock import patch

from dockerhub.logger import (
    DockerJsonFormatter, BugsnagHandler, patch_logging_logger)


patch_logging_logger()


class DockerJsonLoggerTestCase(TestCase):
    service_name = 'Test-Service'
    version = '0.0.1'
    environment = 'messy'

    def setUp(self):
        self.logger = logging.getLogger('test')
        self.logger.setLevel(logging.DEBUG)
        self.buffer = StringIO()

        self.logHandler = logging.StreamHandler(self.buffer)
        self.logger.addHandler(self.logHandler)
        fmt = ('%(levelname)s %(asctime)s %(name)s %(pathname)s %(filename)s'
               '%(module)s %(lineno)d %(message)s %(exc_info)s')
        fr = DockerJsonFormatter(fmt)
        self.logHandler.setFormatter(fr)

    @override_settings(SERVICE_NAME=service_name)
    @override_settings(VERSION_NUMBER=version)
    @override_settings(RELEASE_STAGE=environment)
    def test_logging_format(self):
        msg = "Log some great message in json"
        self.logger.info(msg)
        json_log = json.loads(self.buffer.getvalue())
        self.assertEqual(json_log["message"], msg)
        self.assertEqual(json_log["service"], settings.SERVICE_NAME)
        self.assertEqual(json_log["version"], settings.VERSION_NUMBER)
        self.assertEqual(json_log["environment"], settings.RELEASE_STAGE)

    @override_settings(SERVICE_NAME=service_name)
    @override_settings(VERSION_NUMBER=version)
    @override_settings(RELEASE_STAGE=environment)
    def test_exception_format(self):
        try:
            exc_msg = 'this is the error message'
            raise RuntimeError(exc_msg)
        except Exception:
            msg = "Exception message"
            self.logger.exception(msg)

        json_log = json.loads(self.buffer.getvalue())
        self.assertEqual(json_log["message"], msg)
        self.assertIn(exc_msg, json_log["exc_info"][1])
        self.assertEqual(json_log["service"], settings.SERVICE_NAME)
        self.assertEqual(json_log["version"], settings.VERSION_NUMBER)
        self.assertEqual(json_log["environment"], settings.RELEASE_STAGE)

    def test_missing_settings(self):
        msg = "Log some great message in json"
        self.logger.info(msg)
        json_log = json.loads(self.buffer.getvalue())
        self.assertEqual(json_log["message"], msg)
        self.assertEqual(json_log["service"], 'Unknown Service')
        self.assertEqual(json_log["version"], 'Unknown Version')
        self.assertEqual(json_log["environment"], 'Unknown Environment')


class BugsnagHandlerTestCase(TestCase):

    def setUp(self):
        self.logger = logging.getLogger('test')
        self.logger.setLevel(logging.DEBUG)

        self.logHandler = BugsnagHandler()
        self.logger.addHandler(self.logHandler)

    def mock_notify(self, *args, **kwargs):
        return

    def test_warning(self):
        with patch('dockerhub.logger.bugsnag.notify') as patch_bugsnag_notify:
            self.logger.warning("Godzilla attack!",
                                extra={'city': 'SF',
                                       'damage': 'GG Bridge destroyed',
                                       'context': 'Disaster!',
                                       'year': '2015'})
        patch_bugsnag_notify.assert_called()
        patch_bugsnag_notify.reset()

    def test_error(self):
        with patch('dockerhub.logger.bugsnag.notify') as patch_bugsnag_notify:
            self.logger.error("Godzilla attack!",
                              extra={'city': 'SF',
                                     'damage': 'GG Bridge destroyed',
                                     'context': 'Disaster!',
                                     'year': '2015'})
        patch_bugsnag_notify.assert_called()

    def test_exception(self):
        with patch('dockerhub.logger.bugsnag.notify') as patch_bugsnag_notify:
            self.logger.exception("Godzilla attack!",
                                  extra={
                                      'city': 'SF',
                                      'damage': 'GG Bridge destroyed',
                                      'context': 'Disaster!',
                                      'year': '2015'})
        patch_bugsnag_notify.assert_called()