OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import base64 | 5 import base64 |
| 6 import httplib2 |
| 7 import json |
6 import os | 8 import os |
7 import tempfile | 9 import tempfile |
8 import unittest | 10 import unittest |
9 | 11 |
10 from googleapiclient import errors | 12 from googleapiclient import errors |
11 import mock | 13 import mock |
12 | 14 |
13 from infra_libs.ts_mon.common import interface | 15 from infra_libs.ts_mon.common import interface |
14 from infra_libs.ts_mon.common import monitors | 16 from infra_libs.ts_mon.common import monitors |
| 17 from infra_libs.ts_mon.common import pb_to_popo |
15 from infra_libs.ts_mon.common import targets | 18 from infra_libs.ts_mon.common import targets |
16 from infra_libs.ts_mon.protos import metrics_pb2 | 19 from infra_libs.ts_mon.protos import metrics_pb2 |
17 import infra_libs | 20 import infra_libs |
18 | 21 |
19 | 22 |
20 class MonitorTest(unittest.TestCase): | 23 class MonitorTest(unittest.TestCase): |
21 | 24 |
22 def test_send(self): | 25 def test_send(self): |
23 m = monitors.Monitor() | 26 m = monitors.Monitor() |
24 metric1 = metrics_pb2.MetricsData(name='m1') | 27 metric1 = metrics_pb2.MetricsData(name='m1') |
25 with self.assertRaises(NotImplementedError): | 28 with self.assertRaises(NotImplementedError): |
26 m.send(metric1) | 29 m.send(metric1) |
27 | 30 |
| 31 class HttpsMonitorTest(unittest.TestCase): |
| 32 |
| 33 def setUp(self): |
| 34 super(HttpsMonitorTest, self).setUp() |
| 35 |
| 36 def message(self, pb): |
| 37 pb = monitors.Monitor._wrap_proto(pb) |
| 38 return json.dumps({'resource': pb_to_popo.convert(pb) }) |
| 39 |
| 40 def _test_send(self, http): |
| 41 mon = monitors.HttpsMonitor('endpoint', '/path/to/creds.p8.json', http=http) |
| 42 resp = mock.MagicMock(spec=httplib2.Response, status=200) |
| 43 mon._http.request = mock.MagicMock(return_value=[resp, ""]) |
| 44 |
| 45 metric1 = metrics_pb2.MetricsData(name='m1') |
| 46 mon.send(metric1) |
| 47 metric2 = metrics_pb2.MetricsData(name='m2') |
| 48 mon.send([metric1, metric2]) |
| 49 collection = metrics_pb2.MetricsCollection(data=[metric1, metric2]) |
| 50 mon.send(collection) |
| 51 |
| 52 mon._http.request.assert_has_calls([ |
| 53 mock.call('endpoint', method='POST', body=self.message(metric1)), |
| 54 mock.call('endpoint', method='POST', |
| 55 body=self.message([metric1, metric2])), |
| 56 mock.call('endpoint', method='POST', body=self.message(collection)), |
| 57 ]) |
| 58 |
| 59 @mock.patch('infra_libs.ts_mon.common.monitors.HttpsMonitor.' |
| 60 '_load_credentials', autospec=True) |
| 61 def test_default_send(self, _load_creds): |
| 62 self._test_send(None) |
| 63 |
| 64 @mock.patch('infra_libs.ts_mon.common.monitors.HttpsMonitor.' |
| 65 '_load_credentials', autospec=True) |
| 66 def test_nondefault_send(self, _load_creds): |
| 67 self._test_send(httplib2.Http()) |
| 68 |
| 69 @mock.patch('infra_libs.ts_mon.common.monitors.HttpsMonitor.' |
| 70 '_load_credentials', autospec=True) |
| 71 def test_send_resp_failure(self, _load_creds): |
| 72 mon = monitors.HttpsMonitor('endpoint', '/path/to/creds.p8.json') |
| 73 resp = mock.MagicMock(spec=httplib2.Response, status=400) |
| 74 mon._http.request = mock.MagicMock(return_value=[resp, ""]) |
| 75 |
| 76 metric1 = metrics_pb2.MetricsData(name='m1') |
| 77 mon.send(metric1) |
| 78 |
| 79 mon._http.request.assert_called_once_with('endpoint', method='POST', |
| 80 body=self.message(metric1)) |
| 81 |
| 82 @mock.patch('infra_libs.ts_mon.common.monitors.HttpsMonitor.' |
| 83 '_load_credentials', autospec=True) |
| 84 def test_send_http_failure(self, _load_creds): |
| 85 mon = monitors.HttpsMonitor('endpoint', '/path/to/creds.p8.json') |
| 86 mon._http.request = mock.MagicMock(side_effect=ValueError()) |
| 87 |
| 88 metric1 = metrics_pb2.MetricsData(name='m1') |
| 89 mon.send(metric1) |
| 90 |
| 91 mon._http.request.assert_called_once_with('endpoint', method='POST', |
| 92 body=self.message(metric1)) |
| 93 |
28 | 94 |
29 class PubSubMonitorTest(unittest.TestCase): | 95 class PubSubMonitorTest(unittest.TestCase): |
30 | 96 |
31 def setUp(self): | 97 def setUp(self): |
32 super(PubSubMonitorTest, self).setUp() | 98 super(PubSubMonitorTest, self).setUp() |
33 interface.state.target = targets.TaskTarget( | 99 interface.state.target = targets.TaskTarget( |
34 'test_service', 'test_job', 'test_region', 'test_host') | 100 'test_service', 'test_job', 'test_region', 'test_host') |
35 | 101 |
36 @mock.patch('infra_libs.httplib2_utils.InstrumentedHttp', autospec=True) | 102 @mock.patch('infra_libs.httplib2_utils.InstrumentedHttp', autospec=True) |
37 @mock.patch('infra_libs.ts_mon.common.monitors.discovery', autospec=True) | 103 @mock.patch('infra_libs.ts_mon.common.monitors.discovery', autospec=True) |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 collection = metrics_pb2.MetricsCollection(data=[metric1, metric2]) | 288 collection = metrics_pb2.MetricsCollection(data=[metric1, metric2]) |
223 m.send(collection) | 289 m.send(collection) |
224 | 290 |
225 | 291 |
226 class NullMonitorTest(unittest.TestCase): | 292 class NullMonitorTest(unittest.TestCase): |
227 | 293 |
228 def test_send(self): | 294 def test_send(self): |
229 m = monitors.NullMonitor() | 295 m = monitors.NullMonitor() |
230 metric1 = metrics_pb2.MetricsData(name='m1') | 296 metric1 = metrics_pb2.MetricsData(name='m1') |
231 m.send(metric1) | 297 m.send(metric1) |
OLD | NEW |