• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_access_log_metrics.py"""
8
9from __future__ import print_function
10
11import os
12import mock
13import re
14import subprocess
15import tempfile
16import unittest
17
18import apache_access_log_metrics
19
20
21SCRIPT_PATH = os.path.abspath(
22    os.path.join(os.path.dirname(__file__),
23                 'apache_access_log_metrics.py'))
24
25
26EXAMPLE_REQUEST_LINE = (
27    r'chromeos-server2.mtv.corp.google.com:80 100.108.96.5 - - '
28    r'[19/May/2017:11:47:03 -0700] '
29    r'"POST /afe/server/noauth/rpc/ HTTP/1.1\"" '
30    r'200 354 "-" "Python-urllib/2.7" 5'
31)
32
33
34class TestParsers(unittest.TestCase):
35    """Tests the parsing functions in apache_access_log_metrics."""
36
37    def testParseResponse(self):
38        """Tests that the regex matches the example log line."""
39        match = apache_access_log_metrics.ACCESS_MATCHER.match(
40            EXAMPLE_REQUEST_LINE)
41        self.assertTrue(match)
42
43        self.assertEqual(match.group('bytes_sent'), '354')
44        self.assertEqual(match.group('response_seconds'), '5')
45
46
47class TestEmitters(unittest.TestCase):
48    """Tests the emitter functions in apache_access_log_metrics."""
49
50    def testEmitResponse(self):
51        """Tests that the matcher function doesn't throw an Exception."""
52        match = apache_access_log_metrics.ACCESS_MATCHER.match(
53            EXAMPLE_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_access_log_metrics, 'metrics'):
57            apache_access_log_metrics.EmitRequestMetrics(match)
58
59
60class TestScript(unittest.TestCase):
61    """Tests the script end-to-end."""
62    def testApachAccessLogScriptWithMatchingLine(self):
63        """Try shelling out the the script with --debug-file.
64
65        Sending it a line which matches the first-line regex should result in
66        output from ACCESS_TIME_METRIC.
67        """
68        with tempfile.NamedTemporaryFile() as temp_file:
69            p = subprocess.Popen([SCRIPT_PATH,
70                                  '--debug-metrics-file', temp_file.name],
71                                 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
72            p.communicate(EXAMPLE_REQUEST_LINE)
73
74            with open(temp_file.name) as fh:
75                contents = fh.read()
76
77            self.assertTrue(re.search(
78                apache_access_log_metrics.ACCESS_TIME_METRIC[1:] + r'\b',
79                contents))
80            self.assertTrue(re.search(
81                apache_access_log_metrics.ACCESS_BYTES_METRIC[1:] + r'\b',
82
83                contents))
84
85
86
87if __name__ == '__main__':
88  unittest.main()
89