|
| 1 | +# Copyright 2017, OpenCensus Authors |
| 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.ext.pymongo import trace |
| 20 | + |
| 21 | + |
| 22 | +class Test_pymongo_trace(unittest.TestCase): |
| 23 | + |
| 24 | + def test_trace_integration(self): |
| 25 | + mock_register = mock.Mock() |
| 26 | + |
| 27 | + patch = mock.patch( |
| 28 | + 'pymongo.monitoring.register', |
| 29 | + side_effect=mock_register) |
| 30 | + |
| 31 | + with patch: |
| 32 | + trace.trace_integration() |
| 33 | + |
| 34 | + self.assertTrue(mock_register.called) |
| 35 | + |
| 36 | + def test_started(self): |
| 37 | + mock_tracer = MockTracer() |
| 38 | + |
| 39 | + patch = mock.patch( |
| 40 | + 'opencensus.trace.execution_context.get_opencensus_tracer', |
| 41 | + return_value=mock_tracer) |
| 42 | + |
| 43 | + command_attrs = { |
| 44 | + 'filter': 'filter', |
| 45 | + 'sort': 'sort', |
| 46 | + 'limit': 'limit', |
| 47 | + 'command_name': 'find' |
| 48 | + } |
| 49 | + |
| 50 | + expected_attrs = { |
| 51 | + 'filter': 'filter', |
| 52 | + 'sort': 'sort', |
| 53 | + 'limit': 'limit', |
| 54 | + 'request_id': 'request_id', |
| 55 | + 'connection_id': 'connection_id' |
| 56 | + } |
| 57 | + |
| 58 | + expected_name = 'pymongo.database_name.find.command_name' |
| 59 | + |
| 60 | + with patch: |
| 61 | + trace.MongoCommandListener().started( |
| 62 | + event=MockEvent(command_attrs)) |
| 63 | + |
| 64 | + self.assertEqual(mock_tracer.current_span.attributes, expected_attrs) |
| 65 | + self.assertEqual(mock_tracer.current_span.name, expected_name) |
| 66 | + |
| 67 | + def test_succeed(self): |
| 68 | + mock_tracer = MockTracer() |
| 69 | + mock_tracer.start_span() |
| 70 | + |
| 71 | + patch = mock.patch( |
| 72 | + 'opencensus.trace.execution_context.get_opencensus_tracer', |
| 73 | + return_value=mock_tracer) |
| 74 | + |
| 75 | + expected_attrs = {'status': 'succeeded'} |
| 76 | + |
| 77 | + with patch: |
| 78 | + trace.MongoCommandListener().succeeded(event=MockEvent(None)) |
| 79 | + |
| 80 | + self.assertEqual(mock_tracer.current_span.attributes, expected_attrs) |
| 81 | + mock_tracer.end_span.assert_called_with() |
| 82 | + |
| 83 | + def test_failed(self): |
| 84 | + mock_tracer = MockTracer() |
| 85 | + mock_tracer.start_span() |
| 86 | + |
| 87 | + patch = mock.patch( |
| 88 | + 'opencensus.trace.execution_context.get_opencensus_tracer', |
| 89 | + return_value=mock_tracer) |
| 90 | + |
| 91 | + expected_attrs = {'status': 'failed'} |
| 92 | + |
| 93 | + with patch: |
| 94 | + trace.MongoCommandListener().failed(event=MockEvent(None)) |
| 95 | + |
| 96 | + self.assertEqual(mock_tracer.current_span.attributes, expected_attrs) |
| 97 | + mock_tracer.end_span.assert_called_with() |
| 98 | + |
| 99 | + |
| 100 | +class MockCommand(object): |
| 101 | + def __init__(self, command_attrs): |
| 102 | + self.command_attrs = command_attrs |
| 103 | + |
| 104 | + def get(self, key, default=None): |
| 105 | + return self.command_attrs[key] \ |
| 106 | + if key in self.command_attrs else default |
| 107 | + |
| 108 | + |
| 109 | +class MockEvent(object): |
| 110 | + def __init__(self, command_attrs): |
| 111 | + self.command = MockCommand(command_attrs) |
| 112 | + |
| 113 | + def __getattr__(self, item): |
| 114 | + return item |
| 115 | + |
| 116 | + |
| 117 | +class MockTracer(object): |
| 118 | + def __init__(self): |
| 119 | + self.current_span = None |
| 120 | + self.end_span = mock.Mock() |
| 121 | + |
| 122 | + def start_span(self, name=None): |
| 123 | + span = mock.Mock() |
| 124 | + span.name = name |
| 125 | + span.attributes = {} |
| 126 | + self.current_span = span |
| 127 | + return span |
| 128 | + |
| 129 | + def add_attribute_to_current_span(self, key, value): |
| 130 | + self.current_span.attributes[key] = value |
0 commit comments