Skip to content

Commit 37b01d1

Browse files
committed
Mocked telemetry in modularinput tests
1 parent a8ba77e commit 37b01d1

File tree

5 files changed

+52
-21
lines changed

5 files changed

+52
-21
lines changed

splunklib/modularinput/script.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@
1313
# under the License.
1414

1515
from __future__ import absolute_import
16+
17+
import sys
18+
1619
from abc import ABCMeta, abstractmethod
20+
import splunklib
21+
from splunklib import six
1722
from splunklib.six.moves.urllib.parse import urlsplit
18-
import sys
1923

2024
from ..client import Service
2125
from .event_writer import EventWriter
2226
from .input_definition import InputDefinition
2327
from .validation_definition import ValidationDefinition
24-
from splunklib import six
28+
from ..wire._internal import Telemetry, TelemetryMetric
2529

2630
try:
2731
import xml.etree.cElementTree as ET
@@ -70,6 +74,20 @@ def run_script(self, args, event_writer, input_stream):
7074
# passed on stdin as XML, and the script will write events on
7175
# stdout and log entries on stderr.
7276
self._input_definition = InputDefinition.parse(input_stream)
77+
78+
# create a telemetry metric
79+
metric = TelemetryMetric(**{
80+
'metric_type': 'event',
81+
'component': 'splunk-sdk-python',
82+
'data': {
83+
'version': splunklib.__version__
84+
}
85+
})
86+
87+
# call out to telemetry
88+
telemetry = Telemetry(self.service)
89+
telemetry.submit(metric.to_wire())
90+
7391
self.stream_events(self._input_definition, event_writer)
7492
event_writer.close()
7593
return 0

splunklib/wire/_internal/json_sink.py

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def submit(self, data):
4444
"""
4545

4646
response = self._post('', headers=self.__class__.JSON_HEADER, body=json.dumps(data))
47+
4748
body = json.loads(response.body.read().decode('utf-8'))
4849

4950
return response, body

tests/modularinput/test_script.py

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import io
2+
import json
13
import sys
24

5+
from mock import Mock, patch
6+
7+
from splunklib import six
38
from splunklib.client import Service
49
from splunklib.modularinput import Script, EventWriter, Scheme, Argument, Event
5-
import io
610

711
from splunklib.modularinput.utils import xml_compare
812
from tests.modularinput.modularinput_testlib import data_open
@@ -14,6 +18,17 @@
1418

1519
TEST_SCRIPT_PATH = "__IGNORED_SCRIPT_PATH__"
1620

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+
}
1732

1833
def test_error_on_script_with_null_scheme(capsys):
1934
"""A script that returns a null scheme should generate no output on
@@ -184,9 +199,12 @@ def stream_events(self, inputs, ew):
184199
script = NewScript()
185200
input_configuration = data_open("data/conf_with_2_inputs.xml")
186201

187-
ew = EventWriter(sys.stdout, sys.stderr)
202+
event_writer = EventWriter(sys.stdout, sys.stderr)
188203

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)
190208

191209
output = capsys.readouterr()
192210
assert output.err == ""
@@ -218,7 +236,12 @@ def stream_events(self, inputs, ew):
218236
self.authority_uri = inputs.metadata['server_uri']
219237

220238
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+
222245
ew = EventWriter(sys.stdout, sys.stderr)
223246

224247
assert script.service is None

tests/test_telemetry.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,10 @@
2121
from splunklib.wire._internal.telemetry import Telemetry
2222
from splunklib.wire._internal.telemetry_metric import TelemetryMetric
2323

24-
try:
25-
import unittest
26-
except ImportError:
27-
import unittest2 as unittest
28-
2924
@pytest.mark.app
30-
class TelemetryTestCase(testlib.SDKTestCase):
25+
class TestTelemetry(testlib.SDKTestCase):
3126
def setUp(self):
32-
super(TelemetryTestCase, self).setUp()
27+
super(TestTelemetry, self).setUp()
3328

3429
self.service.namespace['owner'] = 'nobody'
3530
self.service.namespace['app'] = 'sdk-app-collection'
@@ -51,10 +46,3 @@ def test_submit(self):
5146

5247
# it should return a 201
5348
self.assertEqual(response.status, 201)
54-
55-
if __name__ == "__main__":
56-
try:
57-
import unittest2 as unittest
58-
except ImportError:
59-
import unittest
60-
unittest.main()

tox.ini

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ passenv = LANG
2727
setenv = SPLUNK_HOME=/opt/splunk
2828
INPUT_EXAMPLE_UPLOAD=/opt/splunk/var/log/splunk/splunkd_ui_access.log
2929
whitelist_externals = true
30-
deps = pytest
30+
deps = mock
31+
pytest
3132
pytest-cov
3233
xmlrunner
3334
unittest2

0 commit comments

Comments
 (0)