|
| 1 | +import mock |
1 | 2 | from unittest import TestCase |
2 | 3 |
|
3 | 4 | from nose.tools import eq_, ok_, assert_raises |
4 | 5 |
|
5 | 6 | from ddtrace import config as global_config |
6 | 7 | from ddtrace.settings import Config, ConfigException |
7 | 8 |
|
| 9 | +from .test_tracer import get_dummy_tracer |
| 10 | + |
8 | 11 |
|
9 | 12 | class GlobalConfigTestCase(TestCase): |
10 | 13 | """Test the `Configuration` class that stores integration settings""" |
11 | 14 | def setUp(self): |
12 | 15 | self.config = Config() |
| 16 | + self.tracer = get_dummy_tracer() |
13 | 17 |
|
14 | 18 | def test_registration(self): |
15 | 19 | # ensure an integration can register a new list of settings |
@@ -90,3 +94,151 @@ def test_settings_merge_deep(self): |
90 | 94 | )) |
91 | 95 | eq_(self.config.requests['a']['b']['c'], True) |
92 | 96 | eq_(self.config.requests['a']['b']['d'], True) |
| 97 | + |
| 98 | + def test_settings_hook(self): |
| 99 | + """ |
| 100 | + When calling `Hooks._emit()` |
| 101 | + When there is a hook registered |
| 102 | + we call the hook as expected |
| 103 | + """ |
| 104 | + # Setup our hook |
| 105 | + @self.config.web.hooks.on('request') |
| 106 | + def on_web_request(span): |
| 107 | + span.set_tag('web.request', '/') |
| 108 | + |
| 109 | + # Create our span |
| 110 | + span = self.tracer.start_span('web.request') |
| 111 | + ok_('web.request' not in span.meta) |
| 112 | + |
| 113 | + # Emit the span |
| 114 | + self.config.web.hooks._emit('request', span) |
| 115 | + |
| 116 | + # Assert we updated the span as expected |
| 117 | + eq_(span.get_tag('web.request'), '/') |
| 118 | + |
| 119 | + def test_settings_hook_args(self): |
| 120 | + """ |
| 121 | + When calling `Hooks._emit()` with arguments |
| 122 | + When there is a hook registered |
| 123 | + we call the hook as expected |
| 124 | + """ |
| 125 | + # Setup our hook |
| 126 | + @self.config.web.hooks.on('request') |
| 127 | + def on_web_request(span, request, response): |
| 128 | + span.set_tag('web.request', request) |
| 129 | + span.set_tag('web.response', response) |
| 130 | + |
| 131 | + # Create our span |
| 132 | + span = self.tracer.start_span('web.request') |
| 133 | + ok_('web.request' not in span.meta) |
| 134 | + |
| 135 | + # Emit the span |
| 136 | + # DEV: The actual values don't matter, we just want to test args + kwargs usage |
| 137 | + self.config.web.hooks._emit('request', span, 'request', response='response') |
| 138 | + |
| 139 | + # Assert we updated the span as expected |
| 140 | + eq_(span.get_tag('web.request'), 'request') |
| 141 | + eq_(span.get_tag('web.response'), 'response') |
| 142 | + |
| 143 | + def test_settings_hook_args_failure(self): |
| 144 | + """ |
| 145 | + When calling `Hooks._emit()` with arguments |
| 146 | + When there is a hook registered that is missing parameters |
| 147 | + we do not raise an exception |
| 148 | + """ |
| 149 | + # Setup our hook |
| 150 | + # DEV: We are missing the required "response" argument |
| 151 | + @self.config.web.hooks.on('request') |
| 152 | + def on_web_request(span, request): |
| 153 | + span.set_tag('web.request', request) |
| 154 | + |
| 155 | + # Create our span |
| 156 | + span = self.tracer.start_span('web.request') |
| 157 | + ok_('web.request' not in span.meta) |
| 158 | + |
| 159 | + # Emit the span |
| 160 | + # DEV: This also asserts that no exception was raised |
| 161 | + self.config.web.hooks._emit('request', span, 'request', response='response') |
| 162 | + |
| 163 | + # Assert we did not update the span |
| 164 | + ok_('web.request' not in span.meta) |
| 165 | + |
| 166 | + def test_settings_multiple_hooks(self): |
| 167 | + """ |
| 168 | + When calling `Hooks._emit()` |
| 169 | + When there are multiple hooks registered |
| 170 | + we do not raise an exception |
| 171 | + """ |
| 172 | + # Setup our hooks |
| 173 | + @self.config.web.hooks.on('request') |
| 174 | + def on_web_request(span): |
| 175 | + span.set_tag('web.request', '/') |
| 176 | + |
| 177 | + @self.config.web.hooks.on('request') |
| 178 | + def on_web_request2(span): |
| 179 | + span.set_tag('web.status', 200) |
| 180 | + |
| 181 | + @self.config.web.hooks.on('request') |
| 182 | + def on_web_request3(span): |
| 183 | + span.set_tag('web.method', 'GET') |
| 184 | + |
| 185 | + # Create our span |
| 186 | + span = self.tracer.start_span('web.request') |
| 187 | + ok_('web.request' not in span.meta) |
| 188 | + ok_('web.status' not in span.meta) |
| 189 | + ok_('web.method' not in span.meta) |
| 190 | + |
| 191 | + # Emit the span |
| 192 | + self.config.web.hooks._emit('request', span) |
| 193 | + |
| 194 | + # Assert we updated the span as expected |
| 195 | + eq_(span.get_tag('web.request'), '/') |
| 196 | + eq_(span.get_tag('web.status'), '200') |
| 197 | + eq_(span.get_tag('web.method'), 'GET') |
| 198 | + |
| 199 | + def test_settings_hook_failure(self): |
| 200 | + """ |
| 201 | + When calling `Hooks._emit()` |
| 202 | + When the hook raises an exception |
| 203 | + we do not raise an exception |
| 204 | + """ |
| 205 | + # Setup our failing hook |
| 206 | + on_web_request = mock.Mock(side_effect=Exception) |
| 207 | + self.config.web.hooks.register('request')(on_web_request) |
| 208 | + |
| 209 | + # Create our span |
| 210 | + span = self.tracer.start_span('web.request') |
| 211 | + |
| 212 | + # Emit the span |
| 213 | + # DEV: This is the test, to ensure no exceptions are raised |
| 214 | + self.config.web.hooks._emit('request', span) |
| 215 | + on_web_request.assert_called() |
| 216 | + |
| 217 | + def test_settings_no_hook(self): |
| 218 | + """ |
| 219 | + When calling `Hooks._emit()` |
| 220 | + When no hook is registered |
| 221 | + we do not raise an exception |
| 222 | + """ |
| 223 | + # Create our span |
| 224 | + span = self.tracer.start_span('web.request') |
| 225 | + |
| 226 | + # Emit the span |
| 227 | + # DEV: This is the test, to ensure no exceptions are raised |
| 228 | + self.config.web.hooks._emit('request', span) |
| 229 | + on_web_request.assert_called() |
| 230 | + |
| 231 | + def test_settings_no_hook(self): |
| 232 | + """ |
| 233 | + When calling `Hooks._emit()` |
| 234 | + When no span is provided |
| 235 | + we do not raise an exception |
| 236 | + """ |
| 237 | + # Setup our hooks |
| 238 | + @self.config.web.hooks.on('request') |
| 239 | + def on_web_request(span): |
| 240 | + span.set_tag('web.request', '/') |
| 241 | + |
| 242 | + # Emit the span |
| 243 | + # DEV: This is the test, to ensure no exceptions are raised |
| 244 | + self.config.web.hooks._emit('request', None) |
0 commit comments