|
| 1 | +# Copyright 2017 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | + |
| 17 | +import mock |
| 18 | + |
| 19 | +from opencensus.trace.reporters import google_cloud_reporter |
| 20 | + |
| 21 | + |
| 22 | +class _Client(object): |
| 23 | + def __init__(self, project=None): |
| 24 | + if project is None: |
| 25 | + project = 'PROJECT' |
| 26 | + |
| 27 | + self.project = project |
| 28 | + |
| 29 | + |
| 30 | +class TestGoogleCloudReporter(unittest.TestCase): |
| 31 | + |
| 32 | + def test_constructor_default(self): |
| 33 | + patch = mock.patch( |
| 34 | + 'opencensus.trace.reporters.google_cloud_reporter.Client', |
| 35 | + new=_Client) |
| 36 | + |
| 37 | + with patch: |
| 38 | + reporter = google_cloud_reporter.GoogleCloudReporter() |
| 39 | + |
| 40 | + project_id = 'PROJECT' |
| 41 | + self.assertEqual(reporter.project_id, project_id) |
| 42 | + |
| 43 | + def test_constructor_explicit(self): |
| 44 | + client = mock.Mock() |
| 45 | + project_id = 'PROJECT' |
| 46 | + client.project = project_id |
| 47 | + |
| 48 | + reporter = google_cloud_reporter.GoogleCloudReporter( |
| 49 | + client=client, |
| 50 | + project_id=project_id) |
| 51 | + |
| 52 | + self.assertIs(reporter.client, client) |
| 53 | + self.assertEqual(reporter.project_id, project_id) |
| 54 | + |
| 55 | + def test_report(self): |
| 56 | + trace = {'spans': [], 'traceId': '6e0c63257de34c92bf9efcd03927272e'} |
| 57 | + |
| 58 | + client = mock.Mock() |
| 59 | + project_id = 'PROJECT' |
| 60 | + client.project = project_id |
| 61 | + |
| 62 | + reporter = google_cloud_reporter.GoogleCloudReporter( |
| 63 | + client=client, |
| 64 | + project_id=project_id) |
| 65 | + |
| 66 | + reporter.report(trace) |
| 67 | + |
| 68 | + trace['projectId'] = project_id |
| 69 | + traces = {'traces': [trace]} |
| 70 | + |
| 71 | + client.patch_traces.assert_called_with(traces) |
| 72 | + self.assertTrue(client.patch_traces.called) |
| 73 | + |
| 74 | + def test_translate_to_stackdriver(self): |
| 75 | + project_id = 'PROJECT' |
| 76 | + trace = {'spans': [], 'traceId': '6e0c63257de34c92bf9efcd03927272e'} |
| 77 | + |
| 78 | + client = mock.Mock() |
| 79 | + client.project = project_id |
| 80 | + reporter = google_cloud_reporter.GoogleCloudReporter( |
| 81 | + client=client, |
| 82 | + project_id=project_id) |
| 83 | + |
| 84 | + |
| 85 | + traces = reporter.translate_to_stackdriver(trace) |
| 86 | + |
| 87 | + trace['projectId'] = project_id |
| 88 | + expected_traces = {'traces': [trace]} |
| 89 | + |
| 90 | + self.assertEqual(traces, expected_traces) |
0 commit comments