Mike Frysinger | a7f08bc | 2019-08-27 15:16:33 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
Paul Hobbs | ef4e070 | 2016-06-27 17:01:42 -0700 | [diff] [blame] | 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 | |
Congbin Guo | 87c044e | 2019-09-09 16:20:47 -0700 | [diff] [blame^] | 24 | RPC_REQUEST_LINE = ( |
| 25 | '100.115.245.193 - - [08/Sep/2019:07:30:29 -0700] ' |
| 26 | '"GET /list_suite_controls?suite_name=cros_test_platform' |
| 27 | '&build=candy-release/R78-12493.0.0 HTTP/1.1" 200 2724761 "-" "curl/7.35"', |
| 28 | '100.115.196.119 - - [08/Sep/2019:07:14:38 -0700] ' |
| 29 | '"POST /update/nyan_big-release/R77-12371.46.0 HTTP/1.1" 200 416 "-" "-"', |
| 30 | ) |
| 31 | |
Paul Hobbs | ef4e070 | 2016-06-27 17:01:42 -0700 | [diff] [blame] | 32 | |
| 33 | class TestParsers(unittest.TestCase): |
| 34 | """Tests the parsing functions in apache_log_metrics.""" |
| 35 | |
| 36 | def testParseStaticResponse(self): |
| 37 | match = apache_log_metrics.STATIC_GET_MATCHER.match( |
| 38 | STATIC_REQUEST_LINE) |
| 39 | self.assertTrue(match) |
| 40 | |
| 41 | ip = match.group('ip_addr') |
| 42 | self.assertEqual(ip, '172.24.26.30') |
| 43 | self.assertFalse(apache_log_metrics.InLab(ip)) |
| 44 | |
| 45 | self.assertEqual(match.group('size'), '13805917') |
| 46 | |
| 47 | |
Paul Hobbs | 487e381 | 2016-07-22 15:45:33 -0700 | [diff] [blame] | 48 | class TestEmitters(unittest.TestCase): |
| 49 | """Tests the emitter functions in apache_log_metrics.""" |
| 50 | |
| 51 | def testEmitStaticResponse(self): |
| 52 | match = apache_log_metrics.STATIC_GET_MATCHER.match( |
| 53 | STATIC_REQUEST_LINE) |
| 54 | # Calling the emitter should not raise any exceptions (for example, by |
| 55 | # referencing regex match groups that don't exist. |
| 56 | with mock.patch.object(apache_log_metrics, 'metrics'): |
| 57 | apache_log_metrics.EmitStaticRequestMetric(match) |
| 58 | |
Congbin Guo | 87c044e | 2019-09-09 16:20:47 -0700 | [diff] [blame^] | 59 | def testEmitRpcUsageMetric(self): |
| 60 | for line in RPC_REQUEST_LINE: |
| 61 | match = apache_log_metrics.RPC_USAGE_MATCHER.match(line) |
| 62 | with mock.patch.object(apache_log_metrics, 'metrics'): |
| 63 | apache_log_metrics.EmitRpcUsageMetric(match) |
| 64 | |
Paul Hobbs | 487e381 | 2016-07-22 15:45:33 -0700 | [diff] [blame] | 65 | |
Paul Hobbs | ef4e070 | 2016-06-27 17:01:42 -0700 | [diff] [blame] | 66 | if __name__ == '__main__': |
| 67 | unittest.main() |