Skip to content

Commit b4ba2bf

Browse files
author
Fred Ross
committed
Merge pull request #58 from splunk/feature/2.6.1
Test suite now runs and passes on Python 2.6
2 parents 680fe04 + 484edf8 commit b4ba2bf

25 files changed

+150
-38
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,18 @@ away some of the lower level details of the _binding_ layer.
141141
### Unit tests
142142

143143
The SDK contains a small but growing collection of unit tests. Running the
144-
tests is simple and rewarding:
144+
tests is simple and rewarding. If you are using Python 2.7, the test suite
145+
will run using only Python's standard library. If you are using Python 2.6,
146+
you need to install the `unittest2` library, since the test suite depends on
147+
features added to `unittest` in Python 2.7. `unittest2` is only needed for
148+
the running test suite, not for using the SDK.
145149

146-
> cd tests<br>
147-
> python runtests.py
150+
To run the test suite, run the following command in the top level directory
151+
of the SDK repository:
152+
153+
> python setup.py test
154+
155+
You can also run the test_all.py script in the tests/ directory.
148156

149157
Alternatively, you can read more about our testing "framework"
150158
[here](https://github.com/splunk/splunk-sdk-python/tree/master/tests).

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import splunklib
2020

2121
def run_test_suite():
22-
import unittest
22+
try:
23+
import unittest2 as unittest
24+
except ImportError:
25+
import unittest
2326
original_cwd = os.path.abspath(os.getcwd())
2427
os.chdir('tests')
2528
suite = unittest.defaultTestLoader.discover('.')

splunklib/client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,9 +1688,15 @@ def _entity_path(self, state):
16881688

16891689
class Stanza(Entity):
16901690
"""This class contains a single configuration stanza."""
1691+
16911692
def submit(self, stanza):
16921693
"""Sets the keys in *stanza* this Stanza.
16931694
1695+
*stanza* will usually be a dictionary of key/value pairs, but can also
1696+
by a raw string to send as the POST body of the request (e.g.,
1697+
`"key=some+value&other+key=another+value"`). Sending raw strings should
1698+
be considered deprecated.
1699+
16941700
:param stanza: A dictionary of key/value pairs to set in this stanza.
16951701
:type stanza: ``dict``
16961702
:return: The :class:`Stanza` object this method is called on.
@@ -2472,7 +2478,7 @@ def oneshot(self, path, **kwargs):
24722478
self.post('oneshot', name=path, **kwargs)
24732479
except HTTPError as he:
24742480
if he.status == 400:
2475-
raise OperationFailedException(he.message)
2481+
raise OperationFailedException(str(he))
24762482
else:
24772483
raise
24782484

@@ -2848,7 +2854,7 @@ def create(self, query, **kwargs):
28482854
response = self.post(search=query, **kwargs)
28492855
except HTTPError as he:
28502856
if he.status == 400: # Bad request. Raise a TypeError with the reason.
2851-
raise TypeError(he.message)
2857+
raise TypeError(str(he))
28522858
sid = _load_sid(response)
28532859
return Job(self.service, sid)
28542860

tests/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ fetch it from `https://github.com/splunk/sdk-app-collection`. Put the
2222
whole repository in `$SPLUNK_HOME/etc/apps`, so the git root would be
2323
`$SPLUNK_HOME/etc/apps/sdk-app-collection`.
2424

25+
If you are running Python 2.6, you must install the `unittest2` library,
26+
since the test suite depends on features added to `unittest` in Python 2.7.
27+
Python 2.7 should run the test suite using only Python's standard library.
28+
2529
The test suite depends on nothing but Python's standard library. You can
2630
simply execute:
2731

tests/test_all.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
"""Runs all the Splunk Python SDK unit tests."""
1818

1919
import os
20-
import unittest
20+
try:
21+
import unittest2 as unittest
22+
except ImportError:
23+
import unittest
2124
import testlib
2225

2326
suite = unittest.defaultTestLoader.discover('.')

tests/test_app.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,8 @@ def test_updateInfo(self):
9999
self.assertTrue(p is not None)
100100

101101
if __name__ == "__main__":
102-
import unittest
102+
try:
103+
import unittest2 as unittest
104+
except ImportError:
105+
import unittest
103106
unittest.main()

tests/test_binding.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,9 @@ def test_connect_with_preexisting_token_sans_user_and_pass(self):
581581
socket.write("\r\n")
582582
socket.close()
583583

584-
585584
if __name__ == "__main__":
586-
import unittest
585+
try:
586+
import unittest2 as unittest
587+
except ImportError:
588+
import unittest
587589
unittest.main()

tests/test_collection.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ def test_getitem_with_nonsense(self):
240240
self.assertRaises(KeyError, coll.__getitem__, name)
241241

242242
if __name__ == "__main__":
243-
import unittest
243+
try:
244+
import unittest2 as unittest
245+
except ImportError:
246+
import unittest
244247
unittest.main()
245248

tests/test_conf.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ def test_confs(self):
9292
self.assertTrue(key in stanza)
9393
self.assertEqual(value, stanza[key])
9494

95+
key2 = testlib.tmpname()
96+
val2 = testlib.tmpname()
97+
stanza.submit("%s=%s&%s=%s" % (key2, val2, key2, val2))
98+
stanza.refresh()
99+
self.assertTrue(key2 in stanza)
100+
self.assertEqual(val2, stanza[key2])
101+
95102
count = len(conf)
96103
conf.delete(stanza_name)
97104
self.assertFalse(stanza_name in conf)
@@ -103,5 +110,8 @@ def test_confs(self):
103110
self.assertTrue(conf_name in confs)
104111

105112
if __name__ == "__main__":
106-
import unittest
113+
try:
114+
import unittest2 as unittest
115+
except ImportError:
116+
import unittest
107117
unittest.main()

tests/test_data.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# License for the specific language governing permissions and limitations
1515
# under the License.
1616

17+
import sys
1718
from os import path
1819
import xml.etree.ElementTree as et
1920

@@ -114,7 +115,12 @@ def test_real(self):
114115
self.assertEqual(result.feed.entry.content.os_version, '10.8.0')
115116

116117
def test_invalid(self):
117-
self.assertRaises(et.ParseError, data.load, "<dict</dict>")
118+
if sys.version_info[1] >= 7:
119+
self.assertRaises(et.ParseError, data.load, "<dict</dict>")
120+
else:
121+
from xml.parsers.expat import ExpatError
122+
self.assertRaises(ExpatError, data.load, "<dict</dict>")
123+
118124
self.assertRaises(KeyError, data.load, "<dict><key>a</key></dict>")
119125

120126
def test_dict(self):
@@ -240,6 +246,9 @@ def test_record(self):
240246

241247

242248
if __name__ == "__main__":
243-
import unittest
249+
try:
250+
import unittest2 as unittest
251+
except ImportError:
252+
import unittest
244253
unittest.main()
245254

0 commit comments

Comments
 (0)