blob: 531142bd3e8858c4cb8a31a57a6da60cbcd16aea [file] [log] [blame]
Mike Frysingera7f08bc2019-08-27 15:16:33 -04001#!/usr/bin/env python2
2# -*- coding: utf-8 -*-
Paul Hobbsef4e0702016-06-27 17:01:42 -07003# Copyright 2016 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Unit tests for apache_log_metrics.py"""
8
9from __future__ import print_function
10
Paul Hobbs487e3812016-07-22 15:45:33 -070011import mock
Paul Hobbsef4e0702016-06-27 17:01:42 -070012import unittest
13
14import apache_log_metrics
15
16
17STATIC_REQUEST_LINE = (
18 '172.24.26.30 - - [30/Jun/2016:15:34:40 -0700] '
19 '"GET /static/veyron_minnie-release/R52-8350.46.0/'
20 'autotest_server_package.tar.bz2'
21 ' HTTP/1.1" 200 13805917 "-" "Wget/1.15 (linux-gnu)'
22)
23
24
25class TestParsers(unittest.TestCase):
26 """Tests the parsing functions in apache_log_metrics."""
27
28 def testParseStaticResponse(self):
29 match = apache_log_metrics.STATIC_GET_MATCHER.match(
30 STATIC_REQUEST_LINE)
31 self.assertTrue(match)
32
33 ip = match.group('ip_addr')
34 self.assertEqual(ip, '172.24.26.30')
35 self.assertFalse(apache_log_metrics.InLab(ip))
36
37 self.assertEqual(match.group('size'), '13805917')
38
39
Paul Hobbs487e3812016-07-22 15:45:33 -070040class TestEmitters(unittest.TestCase):
41 """Tests the emitter functions in apache_log_metrics."""
42
43 def testEmitStaticResponse(self):
44 match = apache_log_metrics.STATIC_GET_MATCHER.match(
45 STATIC_REQUEST_LINE)
46 # Calling the emitter should not raise any exceptions (for example, by
47 # referencing regex match groups that don't exist.
48 with mock.patch.object(apache_log_metrics, 'metrics'):
49 apache_log_metrics.EmitStaticRequestMetric(match)
50
51
Paul Hobbsef4e0702016-06-27 17:01:42 -070052if __name__ == '__main__':
53 unittest.main()