blob: b854a194670994fae58577acd740eb4681809eac [file] [log] [blame]
# -*- coding: utf-8 -*-
# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Unit test script for nginx_access_log_metrics."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import unittest
import mock
import nginx_access_log_metrics
# pylint: disable=protected-access
class TestMetric(unittest.TestCase):
"""Test class for nginx_access_log_metrics."""
def test_match_the_log_line(self):
"""Test the regex to match a target log line."""
match = nginx_access_log_metrics._SUCCESS_RESPONSE_MATCHER.match(
' 2018-08-01T09:11:27-07:00 "GET a_url HTTP/1.1" '
'200 12345 "agent/1.2.3" HIT')
self.assertEqual('ip_addr'), '')
self.assertEqual('url_path'), 'a_url')
self.assertEqual('size'), '12345')
self.assertEqual('cache_status'), 'HIT')
def test_match_URL_path(self):
"""Test the regex to match a URL path."""
url = '/extract/a_bucket/a-release/R1-2.3/archive'
# The match works for URL has or hasn't parameter.
for u in [url, url + '?key=value']:
match = nginx_access_log_metrics._URL_PATH_MATCHER.match(u)
self.assertEqual('action'), 'extract')
self.assertEqual('bucket'), 'a_bucket')
self.assertEqual('build'), 'a-release')
self.assertEqual('milestone'), 'R1')
self.assertEqual('version'), '2.3')
self.assertEqual('filename'), 'archive')
def test_emitter(self):
"""Test the emitter."""
match = nginx_access_log_metrics._SUCCESS_RESPONSE_MATCHER.match(
' 2018-08-01T09:11:27-07:00 "GET '
'/extract/a_bucket/build/R1-2.3/archive HTTP/1.1" '
'200 12345 "agent/1.2.3" HIT')
# Calling the emitter should not raise any exceptions (for example, by
# referencing regex match groups that don't exist.
with mock.patch.object(nginx_access_log_metrics, 'metrics') as m: