blob: 17c582af539de7bd98aa41a24162677b9487b7d4 [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
Congbin Guo87c044e2019-09-09 16:20:47 -070024RPC_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 Hobbsef4e0702016-06-27 17:01:42 -070032
33class 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 Hobbs487e3812016-07-22 15:45:33 -070048class 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 Guo87c044e2019-09-09 16:20:47 -070059 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 Hobbs487e3812016-07-22 15:45:33 -070065
Paul Hobbsef4e0702016-06-27 17:01:42 -070066if __name__ == '__main__':
67 unittest.main()