| 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 os | 6 import os |
| 7 import tempfile | 7 import tempfile |
| 8 import unittest | 8 import unittest |
| 9 | 9 |
| 10 import mock | 10 import mock |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 'mytopic') | 75 'mytopic') |
| 76 | 76 |
| 77 m_open.assert_called_once_with('/path/to/creds.p8.json', 'r') | 77 m_open.assert_called_once_with('/path/to/creds.p8.json', 'r') |
| 78 storage_inst.get.assert_called_once_with() | 78 storage_inst.get.assert_called_once_with() |
| 79 creds.authorize.assert_called_once_with(http_mock) | 79 creds.authorize.assert_called_once_with(http_mock) |
| 80 discovery.build.assert_called_once_with('pubsub', 'v1', http=http_mock) | 80 discovery.build.assert_called_once_with('pubsub', 'v1', http=http_mock) |
| 81 self.assertEquals(mon._topic, 'projects/myproject/topics/mytopic') | 81 self.assertEquals(mon._topic, 'projects/myproject/topics/mytopic') |
| 82 | 82 |
| 83 @mock.patch('infra_libs.ts_mon.common.monitors.PubSubMonitor.' | 83 @mock.patch('infra_libs.ts_mon.common.monitors.PubSubMonitor.' |
| 84 '_load_credentials', autospec=True) | 84 '_load_credentials', autospec=True) |
| 85 @mock.patch('googleapiclient.discovery.build', autospec=True) | 85 @mock.patch('apiclient.discovery.build', autospec=True) |
| 86 def test_send(self, _discovery, _load_creds): | 86 def test_send(self, _discovery, _load_creds): |
| 87 mon = monitors.PubSubMonitor('/path/to/creds.p8.json', 'myproject', | 87 mon = monitors.PubSubMonitor('/path/to/creds.p8.json', 'myproject', |
| 88 'mytopic') | 88 'mytopic') |
| 89 mon._api = mock.MagicMock() | 89 mon._api = mock.MagicMock() |
| 90 topic = 'projects/myproject/topics/mytopic' | 90 topic = 'projects/myproject/topics/mytopic' |
| 91 | 91 |
| 92 metric1 = metrics_pb2.MetricsData(name='m1') | 92 metric1 = metrics_pb2.MetricsData(name='m1') |
| 93 mon.send(metric1) | 93 mon.send(metric1) |
| 94 metric2 = metrics_pb2.MetricsData(name='m2') | 94 metric2 = metrics_pb2.MetricsData(name='m2') |
| 95 mon.send([metric1, metric2]) | 95 mon.send([metric1, metric2]) |
| 96 collection = metrics_pb2.MetricsCollection(data=[metric1, metric2]) | 96 collection = metrics_pb2.MetricsCollection(data=[metric1, metric2]) |
| 97 mon.send(collection) | 97 mon.send(collection) |
| 98 | 98 |
| 99 def message(pb): | 99 def message(pb): |
| 100 pb = monitors.Monitor._wrap_proto(pb) | 100 pb = monitors.Monitor._wrap_proto(pb) |
| 101 return {'messages': [{'data': base64.b64encode(pb.SerializeToString())}]} | 101 return {'messages': [{'data': base64.b64encode(pb.SerializeToString())}]} |
| 102 publish = mon._api.projects.return_value.topics.return_value.publish | 102 publish = mon._api.projects.return_value.topics.return_value.publish |
| 103 publish.assert_has_calls([ | 103 publish.assert_has_calls([ |
| 104 mock.call(topic=topic, body=message(metric1)), | 104 mock.call(topic=topic, body=message(metric1)), |
| 105 mock.call().execute(num_retries=5), | 105 mock.call().execute(num_retries=5), |
| 106 mock.call(topic=topic, body=message([metric1, metric2])), | 106 mock.call(topic=topic, body=message([metric1, metric2])), |
| 107 mock.call().execute(num_retries=5), | 107 mock.call().execute(num_retries=5), |
| 108 mock.call(topic=topic, body=message(collection)), | 108 mock.call(topic=topic, body=message(collection)), |
| 109 mock.call().execute(num_retries=5), | 109 mock.call().execute(num_retries=5), |
| 110 ]) | 110 ]) |
| 111 | 111 |
| 112 @mock.patch('infra_libs.ts_mon.common.monitors.PubSubMonitor.' | 112 @mock.patch('infra_libs.ts_mon.common.monitors.PubSubMonitor.' |
| 113 '_load_credentials', autospec=True) | 113 '_load_credentials', autospec=True) |
| 114 @mock.patch('googleapiclient.discovery.build', autospec=True) | 114 @mock.patch('apiclient.discovery.build', autospec=True) |
| 115 def test_send_uninitialized(self, discovery, _load_creds): | 115 def test_send_uninitialized(self, discovery, _load_creds): |
| 116 """Test initialization retry logic, and also un-instrumented http path.""" | 116 """Test initialization retry logic, and also un-instrumented http path.""" |
| 117 discovery.side_effect = EnvironmentError() # Fail initialization. | 117 discovery.side_effect = EnvironmentError() # Fail initialization. |
| 118 mon = monitors.PubSubMonitor('/path/to/creds.p8.json', 'myproject', | 118 mon = monitors.PubSubMonitor('/path/to/creds.p8.json', 'myproject', |
| 119 'mytopic', use_instrumented_http=False) | 119 'mytopic', use_instrumented_http=False) |
| 120 | 120 |
| 121 metric1 = metrics_pb2.MetricsData(name='m1') | 121 metric1 = metrics_pb2.MetricsData(name='m1') |
| 122 mon.send(metric1) | 122 mon.send(metric1) |
| 123 self.assertIsNone(mon._api) | 123 self.assertIsNone(mon._api) |
| 124 | 124 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 collection = metrics_pb2.MetricsCollection(data=[metric1, metric2]) | 165 collection = metrics_pb2.MetricsCollection(data=[metric1, metric2]) |
| 166 m.send(collection) | 166 m.send(collection) |
| 167 | 167 |
| 168 | 168 |
| 169 class NullMonitorTest(unittest.TestCase): | 169 class NullMonitorTest(unittest.TestCase): |
| 170 | 170 |
| 171 def test_send(self): | 171 def test_send(self): |
| 172 m = monitors.NullMonitor() | 172 m = monitors.NullMonitor() |
| 173 metric1 = metrics_pb2.MetricsData(name='m1') | 173 metric1 = metrics_pb2.MetricsData(name='m1') |
| 174 m.send(metric1) | 174 m.send(metric1) |
| OLD | NEW |