Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style |
| 3 | # license that can be found in the LICENSE file or at |
| 4 | # https://developers.google.com/open-source/licenses/bsd |
| 5 | |
| 6 | """Tests for MonorailTSMonJSHandler.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import json |
| 12 | import unittest |
| 13 | from mock import patch |
| 14 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 15 | import flask |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 16 | import webapp2 |
| 17 | from google.appengine.ext import testbed |
| 18 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 19 | from framework.ts_mon_js import FlaskMonorailTSMonJSHandler |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 20 | from framework.ts_mon_js import MonorailTSMonJSHandler |
| 21 | from services import service_manager |
| 22 | |
| 23 | |
| 24 | class MonorailTSMonJSHandlerTest(unittest.TestCase): |
| 25 | |
| 26 | def setUp(self): |
| 27 | self.testbed = testbed.Testbed() |
| 28 | self.testbed.activate() |
| 29 | self.testbed.init_user_stub() |
| 30 | |
| 31 | def tearDown(self): |
| 32 | self.testbed.deactivate() |
| 33 | |
| 34 | @patch('framework.xsrf.ValidateToken') |
| 35 | @patch('time.time') |
| 36 | def testSubmitMetrics(self, _mockTime, _mockValidateToken): |
| 37 | """Test normal case POSTing metrics.""" |
| 38 | _mockTime.return_value = 1537821859 |
| 39 | req = webapp2.Request.blank('/_/ts_mon_js') |
| 40 | req.body = json.dumps({ |
| 41 | 'metrics': [{ |
| 42 | 'MetricInfo': { |
| 43 | 'Name': 'monorail/frontend/issue_update_latency', |
| 44 | 'ValueType': 2, |
| 45 | }, |
| 46 | 'Cells': [{ |
| 47 | 'value': { |
| 48 | 'sum': 1234, |
| 49 | 'count': 4321, |
| 50 | 'buckets': { |
| 51 | 0: 123, |
| 52 | 1: 321, |
| 53 | 2: 213, |
| 54 | }, |
| 55 | }, |
| 56 | 'fields': { |
| 57 | 'client_id': '789', |
| 58 | 'host_name': 'rutabaga', |
| 59 | 'document_visible': True, |
| 60 | }, |
| 61 | 'start_time': 1537821859 - 60, |
| 62 | }], |
| 63 | }], |
| 64 | }) |
| 65 | res = webapp2.Response() |
| 66 | ts_mon_handler = MonorailTSMonJSHandler(request=req, response=res) |
| 67 | class MockApp(object): |
| 68 | def __init__(self): |
| 69 | self.config = {'services': service_manager.Services()} |
| 70 | ts_mon_handler.app = MockApp() |
| 71 | |
| 72 | ts_mon_handler.post() |
| 73 | |
| 74 | self.assertEqual(res.status_int, 201) |
| 75 | self.assertEqual(res.body, 'Ok.') |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 76 | |
| 77 | |
| 78 | class FlaskMonorailTSMonJSHandlerTest(unittest.TestCase): |
| 79 | |
| 80 | def setUp(self): |
| 81 | self.testbed = testbed.Testbed() |
| 82 | self.testbed.activate() |
| 83 | self.testbed.init_user_stub() |
| 84 | self.services = service_manager.Services() |
| 85 | self.app = flask.Flask('test_app') |
| 86 | self.app.config['TESTING'] = True |
| 87 | self.app.add_url_rule( |
| 88 | '/_/ts_mon_js.do', |
| 89 | view_func=FlaskMonorailTSMonJSHandler( |
| 90 | services=self.services).PostMonorailTSMonJSHandler, |
| 91 | methods=['POST']) |
| 92 | |
| 93 | def tearDown(self): |
| 94 | self.testbed.deactivate() |
| 95 | |
| 96 | @patch('framework.xsrf.ValidateToken') |
| 97 | @patch('time.time') |
| 98 | def testFlaskSubmitMetrics(self, _mockTime, _mockValidateToken): |
| 99 | """Test normal case POSTing metrics.""" |
| 100 | _mockTime.return_value = 1537821859 |
| 101 | res = self.app.test_client().post( |
| 102 | '/_/ts_mon_js.do', |
| 103 | data = json.dumps( |
| 104 | { |
| 105 | 'metrics': |
| 106 | [ |
| 107 | { |
| 108 | 'MetricInfo': |
| 109 | { |
| 110 | 'Name': |
| 111 | 'monorail/frontend/issue_update_latency', |
| 112 | 'ValueType': |
| 113 | 2, |
| 114 | }, |
| 115 | 'Cells': |
| 116 | [ |
| 117 | { |
| 118 | 'value': |
| 119 | { |
| 120 | 'sum': 1234, |
| 121 | 'count': 4321, |
| 122 | 'buckets': |
| 123 | { |
| 124 | 0: 123, |
| 125 | 1: 321, |
| 126 | 2: 213, |
| 127 | }, |
| 128 | }, |
| 129 | 'fields': |
| 130 | { |
| 131 | 'client_id': '789', |
| 132 | 'host_name': 'rutabaga', |
| 133 | 'document_visible': True, |
| 134 | }, |
| 135 | 'start_time': 1537821799, |
| 136 | } |
| 137 | ], |
| 138 | } |
| 139 | ], |
| 140 | })) |
| 141 | self.assertEqual(res.status_code, 201) |