|
| 1 | +import io |
| 2 | +import json |
1 | 3 | import sys |
2 | 4 |
|
| 5 | +from mock import Mock, patch |
| 6 | + |
| 7 | +from splunklib import six |
3 | 8 | from splunklib.client import Service |
4 | 9 | from splunklib.modularinput import Script, EventWriter, Scheme, Argument, Event |
5 | | -import io |
6 | 10 |
|
7 | 11 | from splunklib.modularinput.utils import xml_compare |
8 | 12 | from tests.modularinput.modularinput_testlib import data_open |
|
14 | 18 |
|
15 | 19 | TEST_SCRIPT_PATH = "__IGNORED_SCRIPT_PATH__" |
16 | 20 |
|
| 21 | +PATCHED_TELEMETRY_RESPONSE = { |
| 22 | + 'status': 201, |
| 23 | + 'reason': 'Created', |
| 24 | + 'body.read.return_value': six.ensure_binary(json.dumps({ |
| 25 | + 'message': 'Data submitted successfully', |
| 26 | + 'metricValueID': '26844DB9-7806-40E0-96C0-1BD554930BA8' |
| 27 | + })), |
| 28 | + 'headers': [ |
| 29 | + ('content-type', 'application/json; charset=UTF-8') |
| 30 | + ] |
| 31 | +} |
17 | 32 |
|
18 | 33 | def test_error_on_script_with_null_scheme(capsys): |
19 | 34 | """A script that returns a null scheme should generate no output on |
@@ -184,9 +199,12 @@ def stream_events(self, inputs, ew): |
184 | 199 | script = NewScript() |
185 | 200 | input_configuration = data_open("data/conf_with_2_inputs.xml") |
186 | 201 |
|
187 | | - ew = EventWriter(sys.stdout, sys.stderr) |
| 202 | + event_writer = EventWriter(sys.stdout, sys.stderr) |
188 | 203 |
|
189 | | - return_value = script.run_script([TEST_SCRIPT_PATH], ew, input_configuration) |
| 204 | + with patch.object(Service, 'post') as patched_telemetry_post: |
| 205 | + patched_telemetry_post.return_value = Mock(**PATCHED_TELEMETRY_RESPONSE) |
| 206 | + |
| 207 | + return_value = script.run_script([TEST_SCRIPT_PATH], event_writer, input_configuration) |
190 | 208 |
|
191 | 209 | output = capsys.readouterr() |
192 | 210 | assert output.err == "" |
@@ -218,7 +236,12 @@ def stream_events(self, inputs, ew): |
218 | 236 | self.authority_uri = inputs.metadata['server_uri'] |
219 | 237 |
|
220 | 238 | script = NewScript() |
221 | | - with data_open("data/conf_with_2_inputs.xml") as input_configuration: |
| 239 | + |
| 240 | + with data_open("data/conf_with_2_inputs.xml") as input_configuration, \ |
| 241 | + patch.object(Service, 'post') as patched_telemetry_post: |
| 242 | + |
| 243 | + patched_telemetry_post.return_value = Mock(**PATCHED_TELEMETRY_RESPONSE) |
| 244 | + |
222 | 245 | ew = EventWriter(sys.stdout, sys.stderr) |
223 | 246 |
|
224 | 247 | assert script.service is None |
|
0 commit comments