コード例 #1
0
ファイル: tests.py プロジェクト: Connexions/cnx-logging
 def setUp(self):
     self.statist = MockStatist(self)
     logger = logging.getLogger(self.__class__.__name__)
     logger.setLevel(logging.INFO)
     self.log_handler = LogCapturingHandler(self)
     logger.addHandler(self.log_handler)
     from cnxlogging import Application
     self.app = Application(self.statist, logger)
コード例 #2
0
ファイル: tests.py プロジェクト: Connexions/cnx-logging
class ApplicationTestCase(unittest.TestCase):

    def setUp(self):
        self.statist = MockStatist(self)
        logger = logging.getLogger(self.__class__.__name__)
        logger.setLevel(logging.INFO)
        self.log_handler = LogCapturingHandler(self)
        logger.addHandler(self.log_handler)
        from cnxlogging import Application
        self.app = Application(self.statist, logger)

    def start_response(self, *args, **kwargs):
        self.resp_args = args
        self.resp_kwargs = kwargs

    def test_metric_acceptance(self):
        metric_data = {
            'type': 'incr',
            'label': 'i.haz.clikd.cheezburgr',
            ##'value': None,  # Tests that missing value or `None' becomes 1.
            }
        environ = {
            'PATH_INFO': '/metric',
            'REQUEST_METHOD': 'POST',
            }
        setup_testing_defaults(environ)
        # Assign the posted message.
        environ['wsgi.input'].write(json.dumps(metric_data))
        environ['wsgi.input'].seek(0)

        resp_body = self.app(environ, self.start_response)

        # Check response, smoke test.
        self.assertEqual(resp_body, [])
        self.assertEqual(self.resp_args[0].upper(), '200 OK')
        self.assertEqual(self.resp_args[1], [])

        # Check the metric was accepted.
        self.assertEqual(self.stats, [u'i.haz.clikd.cheezburgr:1|c'])

    def test_log_acceptance(self):
        message = 'Smoo clikd on a cheezburgr'
        metric_data = {
            'message': message,
            }
        environ = {
            'PATH_INFO': '/log',
            'REQUEST_METHOD': 'POST',
            }
        setup_testing_defaults(environ)
        # Assign the posted message.
        environ['wsgi.input'].write(json.dumps(metric_data))
        environ['wsgi.input'].seek(0)

        resp_body = self.app(environ, self.start_response)

        # Check response, smoke test.
        self.assertEqual(resp_body, [])
        self.assertEqual(self.resp_args[0], '200 OK')
        self.assertEqual(self.resp_args[1], [])

        # Check the metric was accepted.
        self.assertEqual([x.msg for x in self.logged], [message])

    def test_only_accepts_post(self):
        environ = {'REQUEST_METHOD': 'GET'}
        setup_testing_defaults(environ)

        resp_body = self.app(environ, self.start_response)

        # Check response, smoke test.
        self.assertEqual(resp_body, [])
        self.assertEqual(self.resp_args[0].upper(), '404 NOT FOUND')
        self.assertEqual(self.resp_args[1], [])

    def test_invalid_metric_type(self):
        metric_data = {
            'type': 'smudge',
            'label': 'doo.be.doo.be.do',
            'value': None,
            }
        environ = {
            'PATH_INFO': '/metric',
            'REQUEST_METHOD': 'POST',
            }
        setup_testing_defaults(environ)
        # Assign the posted message.
        environ['wsgi.input'].write(json.dumps(metric_data))
        environ['wsgi.input'].seek(0)

        from cnxlogging import InvalidMetricType
        with self.assertRaises(InvalidMetricType):
            resp_body = self.app.handle_metric(metric_data)

        resp_body = self.app(environ, self.start_response)
        self.assertEqual(self.resp_args[0], '500 Internal Server Error')
        self.assertEqual(self.resp_args[1], [('Content-type', 'text/plain')])
        self.assertEqual(resp_body,
                         ['InvalidMetricType: Invalid metric type: smudge'])