|
1 | 1 | import sys |
2 | 2 |
|
3 | 3 | import io |
| 4 | +import re |
4 | 5 | import xml.etree.ElementTree as ET |
5 | 6 | from splunklib.client import Service |
6 | 7 | from splunklib.modularinput import Script, EventWriter, Scheme, Argument, Event |
@@ -228,3 +229,37 @@ def stream_events(self, inputs, ew): |
228 | 229 | assert output.err == "" |
229 | 230 | assert isinstance(script.service, Service) |
230 | 231 | assert script.service.authority == script.authority_uri |
| 232 | + |
| 233 | + |
| 234 | +def test_log_script_exception(monkeypatch): |
| 235 | + out, err = io.StringIO(), io.StringIO() |
| 236 | + |
| 237 | + # Override abstract methods |
| 238 | + class NewScript(Script): |
| 239 | + def get_scheme(self): |
| 240 | + return None |
| 241 | + |
| 242 | + def stream_events(self, inputs, ew): |
| 243 | + raise RuntimeError("Some error") |
| 244 | + |
| 245 | + script = NewScript() |
| 246 | + input_configuration = data_open("data/conf_with_2_inputs.xml") |
| 247 | + |
| 248 | + ew = EventWriter(out, err) |
| 249 | + |
| 250 | + assert script.run_script([TEST_SCRIPT_PATH], ew, input_configuration) == 1 |
| 251 | + |
| 252 | + # Remove paths and line numbers |
| 253 | + err = re.sub(r'File "[^"]+', 'File "...', err.getvalue()) |
| 254 | + err = re.sub(r'line \d+', 'line 123', err) |
| 255 | + |
| 256 | + assert out.getvalue() == "" |
| 257 | + assert err == ( |
| 258 | + 'ERROR Some error - ' |
| 259 | + 'Traceback (most recent call last): ' |
| 260 | + ' File "...", line 123, in run_script ' |
| 261 | + ' self.stream_events(self._input_definition, event_writer) ' |
| 262 | + ' File "...", line 123, in stream_events ' |
| 263 | + ' raise RuntimeError("Some error") ' |
| 264 | + 'RuntimeError: Some error ' |
| 265 | + ) |
0 commit comments