Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(360)

Unified Diff: infra_libs/ts_mon/common/test/monitors_test.py

Issue 2114073002: Add HttpsMonitor (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Fix coverage Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « infra_libs/ts_mon/common/pb_to_popo.py ('k') | infra_libs/ts_mon/common/test/pb_to_popo_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: infra_libs/ts_mon/common/test/monitors_test.py
diff --git a/infra_libs/ts_mon/common/test/monitors_test.py b/infra_libs/ts_mon/common/test/monitors_test.py
index 289f0b3fe1ef54b91f61a4a0d1a973c2aa56ef2a..4f9ef6a7254c7450e40f0ba449c356e57292bcb5 100644
--- a/infra_libs/ts_mon/common/test/monitors_test.py
+++ b/infra_libs/ts_mon/common/test/monitors_test.py
@@ -3,6 +3,8 @@
# found in the LICENSE file.
import base64
+import httplib2
+import json
import os
import tempfile
import unittest
@@ -12,6 +14,7 @@ import mock
from infra_libs.ts_mon.common import interface
from infra_libs.ts_mon.common import monitors
+from infra_libs.ts_mon.common import pb_to_popo
from infra_libs.ts_mon.common import targets
from infra_libs.ts_mon.protos import metrics_pb2
import infra_libs
@@ -25,6 +28,69 @@ class MonitorTest(unittest.TestCase):
with self.assertRaises(NotImplementedError):
m.send(metric1)
+class HttpsMonitorTest(unittest.TestCase):
+
+ def setUp(self):
+ super(HttpsMonitorTest, self).setUp()
+
+ def message(self, pb):
+ pb = monitors.Monitor._wrap_proto(pb)
+ return json.dumps({'resource': pb_to_popo.convert(pb) })
+
+ def _test_send(self, http):
+ mon = monitors.HttpsMonitor('endpoint', '/path/to/creds.p8.json', http=http)
+ resp = mock.MagicMock(spec=httplib2.Response, status=200)
+ mon._http.request = mock.MagicMock(return_value=[resp, ""])
+
+ metric1 = metrics_pb2.MetricsData(name='m1')
+ mon.send(metric1)
+ metric2 = metrics_pb2.MetricsData(name='m2')
+ mon.send([metric1, metric2])
+ collection = metrics_pb2.MetricsCollection(data=[metric1, metric2])
+ mon.send(collection)
+
+ mon._http.request.assert_has_calls([
+ mock.call('endpoint', method='POST', body=self.message(metric1)),
+ mock.call('endpoint', method='POST',
+ body=self.message([metric1, metric2])),
+ mock.call('endpoint', method='POST', body=self.message(collection)),
+ ])
+
+ @mock.patch('infra_libs.ts_mon.common.monitors.HttpsMonitor.'
+ '_load_credentials', autospec=True)
+ def test_default_send(self, _load_creds):
+ self._test_send(None)
+
+ @mock.patch('infra_libs.ts_mon.common.monitors.HttpsMonitor.'
+ '_load_credentials', autospec=True)
+ def test_nondefault_send(self, _load_creds):
+ self._test_send(httplib2.Http())
+
+ @mock.patch('infra_libs.ts_mon.common.monitors.HttpsMonitor.'
+ '_load_credentials', autospec=True)
+ def test_send_resp_failure(self, _load_creds):
+ mon = monitors.HttpsMonitor('endpoint', '/path/to/creds.p8.json')
+ resp = mock.MagicMock(spec=httplib2.Response, status=400)
+ mon._http.request = mock.MagicMock(return_value=[resp, ""])
+
+ metric1 = metrics_pb2.MetricsData(name='m1')
+ mon.send(metric1)
+
+ mon._http.request.assert_called_once_with('endpoint', method='POST',
+ body=self.message(metric1))
+
+ @mock.patch('infra_libs.ts_mon.common.monitors.HttpsMonitor.'
+ '_load_credentials', autospec=True)
+ def test_send_http_failure(self, _load_creds):
+ mon = monitors.HttpsMonitor('endpoint', '/path/to/creds.p8.json')
+ mon._http.request = mock.MagicMock(side_effect=ValueError())
+
+ metric1 = metrics_pb2.MetricsData(name='m1')
+ mon.send(metric1)
+
+ mon._http.request.assert_called_once_with('endpoint', method='POST',
+ body=self.message(metric1))
+
class PubSubMonitorTest(unittest.TestCase):
« no previous file with comments | « infra_libs/ts_mon/common/pb_to_popo.py ('k') | infra_libs/ts_mon/common/test/pb_to_popo_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698