blob: 0592336fd92ef2e20538006832ccc1757ceea03c [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# Copyright 2020 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Unit tests for the monitoring module."""
6
7import unittest
8from framework import monitoring
9
10COMMON_TEST_FIELDS = monitoring.GetCommonFields(200, 'monorail.v3.MethodName')
11
12
13class MonitoringTest(unittest.TestCase):
14
15 def testIncrementAPIRequestsCount(self):
16 # Non-service account email gets hidden.
17 monitoring.IncrementAPIRequestsCount(
18 'v3', 'monorail-prod', client_email='client-email@chicken.com')
19 self.assertEqual(
20 1,
21 monitoring.API_REQUESTS_COUNT.get(
22 fields={
23 'client_id': 'monorail-prod',
24 'client_email': 'user@email.com',
25 'version': 'v3'
26 }))
27
28 # None email address gets replaced by 'anonymous'.
29 monitoring.IncrementAPIRequestsCount('v3', 'monorail-prod')
30 self.assertEqual(
31 1,
32 monitoring.API_REQUESTS_COUNT.get(
33 fields={
34 'client_id': 'monorail-prod',
35 'client_email': 'anonymous',
36 'version': 'v3'
37 }))
38
39 # Service account email is not hidden
40 monitoring.IncrementAPIRequestsCount(
41 'endpoints',
42 'monorail-prod',
43 client_email='123456789@developer.gserviceaccount.com')
44 self.assertEqual(
45 1,
46 monitoring.API_REQUESTS_COUNT.get(
47 fields={
48 'client_id': 'monorail-prod',
49 'client_email': '123456789@developer.gserviceaccount.com',
50 'version': 'endpoints'
51 }))
52
53 def testGetCommonFields(self):
54 fields = monitoring.GetCommonFields(200, 'monorail.v3.TestName')
55 self.assertEqual(
56 {
57 'status': 200,
58 'name': 'monorail.v3.TestName',
59 'is_robot': False
60 }, fields)
61
62 def testAddServerDurations(self):
63 self.assertIsNone(
64 monitoring.SERVER_DURATIONS.get(fields=COMMON_TEST_FIELDS))
65 monitoring.AddServerDurations(500, COMMON_TEST_FIELDS)
66 self.assertIsNotNone(
67 monitoring.SERVER_DURATIONS.get(fields=COMMON_TEST_FIELDS))
68
69 def testIncrementServerResponseStatusCount(self):
70 monitoring.IncrementServerResponseStatusCount(COMMON_TEST_FIELDS)
71 self.assertEqual(
72 1, monitoring.SERVER_RESPONSE_STATUS.get(fields=COMMON_TEST_FIELDS))
73
74 def testAddServerRequesteBytes(self):
75 self.assertIsNone(
76 monitoring.SERVER_REQUEST_BYTES.get(fields=COMMON_TEST_FIELDS))
77 monitoring.AddServerRequesteBytes(1234, COMMON_TEST_FIELDS)
78 self.assertIsNotNone(
79 monitoring.SERVER_REQUEST_BYTES.get(fields=COMMON_TEST_FIELDS))
80
81 def testAddServerResponseBytes(self):
82 self.assertIsNone(
83 monitoring.SERVER_RESPONSE_BYTES.get(fields=COMMON_TEST_FIELDS))
84 monitoring.AddServerResponseBytes(9876, COMMON_TEST_FIELDS)
85 self.assertIsNotNone(
86 monitoring.SERVER_RESPONSE_BYTES.get(fields=COMMON_TEST_FIELDS))