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 @mock.patch('infra_libs.ts_mon.common.monitors.HttpsMonitor.' |
| 41 '_load_credentials', autospec=True) |
| 42 def test_send(self, _load_creds): |
| 43 mon = monitors.HttpsMonitor('endpoint', '/path/to/creds.p8.json') |
| 44 resp = mock.MagicMock(spec=httplib2.Response, status=200) |
| 45 mon._http.request = mock.MagicMock(return_value=[resp, ""]) |
| 46 |
| 47 metric1 = metrics_pb2.MetricsData(name='m1') |
| 48 mon.send(metric1) |
| 49 metric2 = metrics_pb2.MetricsData(name='m2') |
| 50 mon.send([metric1, metric2]) |
| 51 collection = metrics_pb2.MetricsCollection(data=[metric1, metric2]) |
| 52 mon.send(collection) |
| 53 |
| 54 mon._http.request.assert_has_calls([ |
| 55 mock.call('endpoint', method='POST', body=self.message(metric1)), |
| 56 mock.call('endpoint', method='POST', |
| 57 body=self.message([metric1, metric2])), |
| 58 mock.call('endpoint', method='POST', body=self.message(collection)), |
| 59 ]) |
| 60 |
| 61 @mock.patch('infra_libs.ts_mon.common.monitors.HttpsMonitor.' |
| 62 '_load_credentials', autospec=True) |
| 63 def test_send_resp_failure(self, _load_creds): |
| 64 mon = monitors.HttpsMonitor('endpoint', '/path/to/creds.p8.json') |
| 65 resp = mock.MagicMock(spec=httplib2.Response, status=400) |
| 66 mon._http.request = mock.MagicMock(return_value=[resp, ""]) |
| 67 |
| 68 metric1 = metrics_pb2.MetricsData(name='m1') |
| 69 mon.send(metric1) |
| 70 |
| 71 mon._http.request.assert_called_once_with('endpoint', method='POST', |
| 72 body=self.message(metric1)) |
| 73 |
| 74 @mock.patch('infra_libs.ts_mon.common.monitors.HttpsMonitor.' |
| 75 '_load_credentials', autospec=True) |
| 76 def test_send_http_failure(self, _load_creds): |
| 77 mon = monitors.HttpsMonitor('endpoint', '/path/to/creds.p8.json') |
| 78 mon._http.request = mock.MagicMock(side_effect=ValueError()) |
| 79 |
| 80 metric1 = metrics_pb2.MetricsData(name='m1') |
| 81 mon.send(metric1) |
| 82 |
| 83 mon._http.request.assert_called_once_with('endpoint', method='POST', |
| 84 body=self.message(metric1)) |
| 85 |
28 | 86 |
29 class PubSubMonitorTest(unittest.TestCase): | 87 class PubSubMonitorTest(unittest.TestCase): |
30 | 88 |
31 def setUp(self): | 89 def setUp(self): |
32 super(PubSubMonitorTest, self).setUp() | 90 super(PubSubMonitorTest, self).setUp() |
33 interface.state.target = targets.TaskTarget( | 91 interface.state.target = targets.TaskTarget( |
34 'test_service', 'test_job', 'test_region', 'test_host') | 92 'test_service', 'test_job', 'test_region', 'test_host') |
35 | 93 |
36 @mock.patch('infra_libs.httplib2_utils.InstrumentedHttp', autospec=True) | 94 @mock.patch('infra_libs.httplib2_utils.InstrumentedHttp', autospec=True) |
37 @mock.patch('infra_libs.ts_mon.common.monitors.discovery', autospec=True) | 95 @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]) | 280 collection = metrics_pb2.MetricsCollection(data=[metric1, metric2]) |
223 m.send(collection) | 281 m.send(collection) |
224 | 282 |
225 | 283 |
226 class NullMonitorTest(unittest.TestCase): | 284 class NullMonitorTest(unittest.TestCase): |
227 | 285 |
228 def test_send(self): | 286 def test_send(self): |
229 m = monitors.NullMonitor() | 287 m = monitors.NullMonitor() |
230 metric1 = metrics_pb2.MetricsData(name='m1') | 288 metric1 = metrics_pb2.MetricsData(name='m1') |
231 m.send(metric1) | 289 m.send(metric1) |
OLD | NEW |