| 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 | 6 import httplib2 |
| 7 import json | 7 import json |
| 8 import os | 8 import os |
| 9 import tempfile | 9 import tempfile |
| 10 import unittest | 10 import unittest |
| 11 | 11 |
| 12 from googleapiclient import errors | 12 from googleapiclient import errors |
| 13 import mock | 13 import mock |
| 14 | 14 |
| 15 from infra_libs import httplib2_utils |
| 15 from infra_libs.ts_mon.common import interface | 16 from infra_libs.ts_mon.common import interface |
| 16 from infra_libs.ts_mon.common import monitors | 17 from infra_libs.ts_mon.common import monitors |
| 17 from infra_libs.ts_mon.common import pb_to_popo | 18 from infra_libs.ts_mon.common import pb_to_popo |
| 18 from infra_libs.ts_mon.common import targets | 19 from infra_libs.ts_mon.common import targets |
| 19 from infra_libs.ts_mon.protos import metrics_pb2 | 20 from infra_libs.ts_mon.protos import metrics_pb2 |
| 20 import infra_libs | 21 import infra_libs |
| 21 | 22 |
| 22 | 23 |
| 23 class MonitorTest(unittest.TestCase): | 24 class MonitorTest(unittest.TestCase): |
| 24 | 25 |
| 25 def test_send(self): | 26 def test_send(self): |
| 26 m = monitors.Monitor() | 27 m = monitors.Monitor() |
| 27 metric1 = metrics_pb2.MetricsData(name='m1') | 28 metric1 = metrics_pb2.MetricsData(name='m1') |
| 28 with self.assertRaises(NotImplementedError): | 29 with self.assertRaises(NotImplementedError): |
| 29 m.send(metric1) | 30 m.send(metric1) |
| 30 | 31 |
| 31 class HttpsMonitorTest(unittest.TestCase): | 32 class HttpsMonitorTest(unittest.TestCase): |
| 32 | 33 |
| 33 def setUp(self): | 34 def setUp(self): |
| 34 super(HttpsMonitorTest, self).setUp() | 35 super(HttpsMonitorTest, self).setUp() |
| 35 | 36 |
| 36 def message(self, pb): | 37 def message(self, pb): |
| 37 pb = monitors.Monitor._wrap_proto(pb) | 38 pb = monitors.Monitor._wrap_proto(pb) |
| 38 return json.dumps({'resource': pb_to_popo.convert(pb) }) | 39 return json.dumps({'resource': pb_to_popo.convert(pb) }) |
| 39 | 40 |
| 40 def _test_send(self, http): | 41 def _test_send(self, http): |
| 41 mon = monitors.HttpsMonitor('endpoint', '/path/to/creds.p8.json', http=http) | 42 mon = monitors.HttpsMonitor('endpoint', ':gce', http=http) |
| 42 resp = mock.MagicMock(spec=httplib2.Response, status=200) | 43 resp = mock.MagicMock(spec=httplib2.Response, status=200) |
| 43 mon._http.request = mock.MagicMock(return_value=[resp, ""]) | 44 mon._http.request = mock.MagicMock(return_value=[resp, ""]) |
| 44 | 45 |
| 45 metric1 = metrics_pb2.MetricsData(name='m1') | 46 metric1 = metrics_pb2.MetricsData(name='m1') |
| 46 mon.send(metric1) | 47 mon.send(metric1) |
| 47 metric2 = metrics_pb2.MetricsData(name='m2') | 48 metric2 = metrics_pb2.MetricsData(name='m2') |
| 48 mon.send([metric1, metric2]) | 49 mon.send([metric1, metric2]) |
| 49 collection = metrics_pb2.MetricsCollection(data=[metric1, metric2]) | 50 collection = metrics_pb2.MetricsCollection(data=[metric1, metric2]) |
| 50 mon.send(collection) | 51 mon.send(collection) |
| 51 | 52 |
| 52 mon._http.request.assert_has_calls([ | 53 mon._http.request.assert_has_calls([ |
| 53 mock.call('endpoint', method='POST', body=self.message(metric1)), | 54 mock.call('endpoint', method='POST', body=self.message(metric1)), |
| 54 mock.call('endpoint', method='POST', | 55 mock.call('endpoint', method='POST', |
| 55 body=self.message([metric1, metric2])), | 56 body=self.message([metric1, metric2])), |
| 56 mock.call('endpoint', method='POST', body=self.message(collection)), | 57 mock.call('endpoint', method='POST', body=self.message(collection)), |
| 57 ]) | 58 ]) |
| 58 | 59 |
| 59 @mock.patch('infra_libs.ts_mon.common.monitors.HttpsMonitor.' | 60 def test_default_send(self): |
| 60 '_load_credentials', autospec=True) | |
| 61 def test_default_send(self, _load_creds): | |
| 62 self._test_send(None) | 61 self._test_send(None) |
| 63 | 62 |
| 64 @mock.patch('infra_libs.ts_mon.common.monitors.HttpsMonitor.' | 63 def test_http_send(self): |
| 65 '_load_credentials', autospec=True) | |
| 66 def test_nondefault_send(self, _load_creds): | |
| 67 self._test_send(httplib2.Http()) | 64 self._test_send(httplib2.Http()) |
| 68 | 65 |
| 66 def test_instrumented_http_send(self): |
| 67 self._test_send(httplib2_utils.InstrumentedHttp('test')) |
| 68 |
| 69 @mock.patch('infra_libs.ts_mon.common.monitors.HttpsMonitor.' | 69 @mock.patch('infra_libs.ts_mon.common.monitors.HttpsMonitor.' |
| 70 '_load_credentials', autospec=True) | 70 '_load_credentials', autospec=True) |
| 71 def test_send_resp_failure(self, _load_creds): | 71 def test_send_resp_failure(self, _load_creds): |
| 72 mon = monitors.HttpsMonitor('endpoint', '/path/to/creds.p8.json') | 72 mon = monitors.HttpsMonitor('endpoint', '/path/to/creds.p8.json') |
| 73 resp = mock.MagicMock(spec=httplib2.Response, status=400) | 73 resp = mock.MagicMock(spec=httplib2.Response, status=400) |
| 74 mon._http.request = mock.MagicMock(return_value=[resp, ""]) | 74 mon._http.request = mock.MagicMock(return_value=[resp, ""]) |
| 75 | 75 |
| 76 metric1 = metrics_pb2.MetricsData(name='m1') | 76 metric1 = metrics_pb2.MetricsData(name='m1') |
| 77 mon.send(metric1) | 77 mon.send(metric1) |
| 78 | 78 |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 collection = metrics_pb2.MetricsCollection(data=[metric1, metric2]) | 288 collection = metrics_pb2.MetricsCollection(data=[metric1, metric2]) |
| 289 m.send(collection) | 289 m.send(collection) |
| 290 | 290 |
| 291 | 291 |
| 292 class NullMonitorTest(unittest.TestCase): | 292 class NullMonitorTest(unittest.TestCase): |
| 293 | 293 |
| 294 def test_send(self): | 294 def test_send(self): |
| 295 m = monitors.NullMonitor() | 295 m = monitors.NullMonitor() |
| 296 metric1 = metrics_pb2.MetricsData(name='m1') | 296 metric1 = metrics_pb2.MetricsData(name='m1') |
| 297 m.send(metric1) | 297 m.send(metric1) |
| OLD | NEW |