Skip to content

Commit 816eed1

Browse files
author
Frederick Ross
committed
Merge branch 'feature/fross-working' of github.com:splunk/splunk-sdk-python into feature/fross-working
2 parents 4d052dc + 9448df9 commit 816eed1

4 files changed

Lines changed: 53 additions & 5 deletions

File tree

‎splunklib/client.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
import logging
6868
from time import sleep
6969
from datetime import datetime, timedelta
70+
import socket
71+
import contextlib
7072

7173
from binding import Context, HTTPError, AuthenticationError, namespace, UrlEncoded
7274
from data import record
@@ -1481,6 +1483,32 @@ def attach(self, host=None, source=None, sourcetype=None):
14811483
sock.write(h)
14821484
return sock
14831485

1486+
@contextlib.contextmanager
1487+
def attached_socket(self, *args, **kwargs):
1488+
"""Open a raw socket in a with block to write data to Splunk.
1489+
1490+
The arguments are identical to those for ``attach``. The socket is automatically
1491+
closed at the end of the with block, even if an exception is raised in the block.
1492+
1493+
:param `host`: The host value for events written to the stream.
1494+
:param `source`: The source value for events written to the stream.
1495+
:param `sourcetype`: The sourcetype value for events written to the
1496+
1497+
**Example**::
1498+
1499+
import splunklib.client as client
1500+
s = client.connect(...)
1501+
index = s.indexes['some_index']
1502+
with index.attached_socket(sourcetype='test') as sock:
1503+
sock.send('Test event\r\n')
1504+
"""
1505+
try:
1506+
sock = self.attach(*args, **kwargs)
1507+
yield sock
1508+
finally:
1509+
sock.shutdown(socket.SHUT_RDWR)
1510+
sock.close()
1511+
14841512
def clean(self, timeout=60):
14851513
"""Deletes the contents of the index.
14861514

‎splunklib/data.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,16 @@ def load_elem(element, nametable=None):
120120
if isinstance(value, str):
121121
attrs["$text"] = value
122122
return name, attrs
123-
# Both attrs & value are complex, so merge the two dicts
123+
# Both attrs & value are complex, so merge the two dicts, resolving collisions.
124+
collision_keys = []
124125
for key, val in attrs.iteritems():
125-
#assert not value.has_key(k) # Assume no collisions
126-
value[key] = val
126+
if key in value and key in collision_keys:
127+
value[key].append(val)
128+
elif key in value and key not in collision_keys:
129+
value[key] = [value[key], val]
130+
collision_keys.append(key)
131+
else:
132+
value[key] = val
127133
return name, value
128134

129135
# Parse a <list> element and return a Python list

‎tests/test_data.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ def test_elems(self):
5555
result = data.load("<a><b><c>1</c></b><b>2</b></a>")
5656
self.assertEqual(result, {'a': {'b': [{'c': '1'}, '2']}})
5757

58+
result = data.load('<e><a1>alpha</a1><a1>beta</a1></e>')
59+
self.assertEqual(result, {'e': {'a1': ['alpha', 'beta']}})
60+
61+
result = data.load("<e a1='v1'><a1>v2</a1></e>")
62+
self.assertEqual(result, {'e': {'a1': ['v2', 'v1']}})
63+
5864
def test_attrs(self):
5965
result = data.load("<e a1='v1'/>")
6066
self.assertEqual(result, {'e': {'a1': 'v1'}})
@@ -72,7 +78,7 @@ def test_attrs(self):
7278
self.assertEqual(result, {'e': {'a1': 'v1', 'b': 'bv2'}})
7379

7480
result = data.load("<e a1='v1'><a1>v2</a1></e>")
75-
self.assertEqual(result, {'e': {'a1': 'v1'}})
81+
self.assertEqual(result, {'e': {'a1': ['v2', 'v1']}})
7682

7783
result = data.load("<e1 a1='v1'><e2 a1='v1'>v2</e2></e1>")
7884
self.assertEqual(result,

‎tests/test_index.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def test_disable_enable(self):
6060
class IndexWithRestartTest(IndexTest):
6161
def setUp(self):
6262
super(IndexWithRestartTest, self).setUp()
63-
self.service.restart(timeout=120)
63+
self.service.restart(timeout=300)
6464
self.index = self.service.indexes[self.index_name]
6565

6666
def test_cannot_clean_enabled(self):
@@ -101,6 +101,14 @@ def test_submit_via_attach(self):
101101
self.index.refresh()
102102
self.assertEqual(self.index['totalEventCount'], str(eventCount+1))
103103

104+
def test_submit_via_attached_socket(self):
105+
eventCount = int(self.index['totalEventCount'])
106+
f = self.index.attached_socket
107+
with f() as sock:
108+
sock.send('Hello world!\r\n')
109+
testlib.retry(self.index, 'totalEventCount', str(eventCount+1), step=1)
110+
self.assertEqual(self.index['totalEventCount'], str(eventCount+1))
111+
104112
def test_upload(self):
105113
# The following test must run on machine where splunkd runs,
106114
# otherwise a failure is expected

0 commit comments

Comments
 (0)