Paul Hobbs | ef4e070 | 2016-06-27 17:01:42 -0700 | [diff] [blame] | 1 | #!/usr/bin/python2 |
| 2 | |
| 3 | # 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 | |
| 9 | from __future__ import print_function |
| 10 | |
Paul Hobbs | 487e381 | 2016-07-22 15:45:33 -0700 | [diff] [blame^] | 11 | import mock |
Paul Hobbs | ef4e070 | 2016-06-27 17:01:42 -0700 | [diff] [blame] | 12 | import unittest |
| 13 | |
| 14 | import apache_log_metrics |
| 15 | |
| 16 | |
| 17 | STATIC_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 | |
| 25 | class 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 Hobbs | 487e381 | 2016-07-22 15:45:33 -0700 | [diff] [blame^] | 40 | class 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 Hobbs | ef4e070 | 2016-06-27 17:01:42 -0700 | [diff] [blame] | 52 | if __name__ == '__main__': |
| 53 | unittest.main() |