Skip to content

Commit 9d63d7d

Browse files
author
Frederick Ross
committed
Reworked test_service.py.
service.parse will need to be rewritten. It's useless right now.
1 parent 8b08d08 commit 9d63d7d

File tree

2 files changed

+96
-102
lines changed

2 files changed

+96
-102
lines changed

splunklib/client.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@
107107

108108
MATCH_ENTRY_CONTENT = "%s/%s/*" % (XNAME_ENTRY, XNAME_CONTENT)
109109

110+
capabilities = record({k:k for k in [
111+
"admin_all_objects", "change_authentication",
112+
"change_own_password", "delete_by_keyword",
113+
"edit_deployment_client", "edit_deployment_server",
114+
"edit_dist_peer", "edit_forwarders", "edit_httpauths",
115+
"edit_input_defaults", "edit_monitor", "edit_roles",
116+
"edit_scripted", "edit_search_server", "edit_server",
117+
"edit_splunktcp", "edit_splunktcp_ssl", "edit_tcp",
118+
"edit_udp", "edit_user", "edit_web_settings", "get_metadata",
119+
"get_typeahead", "indexes_edit", "license_edit", "license_tab",
120+
"list_deployment_client", "list_forwarders", "list_httpauths",
121+
"list_inputs", "request_remote_tok", "rest_apps_management",
122+
"rest_apps_view", "rest_properties_get", "rest_properties_set",
123+
"restart_splunkd", "rtsearch", "schedule_search", "search",
124+
"use_file_operator"]})
125+
126+
127+
class NoSuchUserException(Exception):
128+
pass
129+
130+
class NoSuchApplicationException(Exception):
131+
pass
132+
110133
class IllegalOperationException(Exception):
111134
pass
112135

tests/test_service.py

Lines changed: 73 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -15,159 +15,111 @@
1515
# under the License.
1616

1717
import testlib
18+
import logging
1819

20+
import unittest
21+
22+
import splunklib.data as data
1923

2024
import splunklib.client as client
2125
from splunklib.binding import HTTPError
2226

2327
class TestCase(testlib.TestCase):
2428
def test_capabilities(self):
25-
service = client.connect(**self.opts.kwargs)
26-
27-
expected = [
28-
"admin_all_objects", "change_authentication",
29-
"change_own_password", "delete_by_keyword",
30-
"edit_deployment_client", "edit_deployment_server",
31-
"edit_dist_peer", "edit_forwarders", "edit_httpauths",
32-
"edit_input_defaults", "edit_monitor", "edit_roles",
33-
"edit_scripted", "edit_search_server", "edit_server",
34-
"edit_splunktcp", "edit_splunktcp_ssl", "edit_tcp",
35-
"edit_udp", "edit_user", "edit_web_settings", "get_metadata",
36-
"get_typeahead", "indexes_edit", "license_edit", "license_tab",
37-
"list_deployment_client", "list_forwarders", "list_httpauths",
38-
"list_inputs", "request_remote_tok", "rest_apps_management",
39-
"rest_apps_view", "rest_properties_get", "rest_properties_set",
40-
"restart_splunkd", "rtsearch", "schedule_search", "search",
41-
"use_file_operator" ]
42-
43-
capabilities = service.capabilities
44-
for item in expected: self.assertTrue(item in capabilities)
29+
capabilities = self.service.capabilities
30+
for item in client.capabilities:
31+
self.assertTrue(item in capabilities)
4532

4633
def test_info(self):
47-
service = client.connect(**self.opts.kwargs)
48-
49-
info = service.info
50-
keys = [
51-
"build", "cpu_arch", "guid", "isFree", "isTrial", "licenseKeys",
34+
info = self.service.info
35+
keys = ["build", "cpu_arch", "guid", "isFree", "isTrial", "licenseKeys",
5236
"licenseSignature", "licenseState", "master_guid", "mode",
53-
"os_build", "os_name", "os_version", "serverName", "version" ]
54-
for key in keys: self.assertTrue(key in info.keys())
55-
56-
def test_namespaces(self):
57-
kwargs = self.opts.kwargs.copy()
37+
"os_build", "os_name", "os_version", "serverName", "version"]
38+
for key in keys:
39+
self.assertTrue(key in info.keys())
5840

59-
# Verify connect with no namespace
60-
service = client.connect(**kwargs)
41+
def test_without_namespace(self):
42+
service = client.connect(**self.opts.kwargs)
6143
service.apps.list()
6244

63-
# Verify namespace permutations using standard app & owner args
64-
kwargs.update({ 'app': "search", 'owner': None })
45+
def test_app_namespace(self):
46+
kwargs = self.opts.kwargs.copy()
47+
kwargs.update({'app': "search", 'owner': None})
6548
service_ns = client.connect(**kwargs)
6649
service_ns.apps.list()
6750

51+
def test_owner_wildcard(self):
52+
kwargs = self.opts.kwargs.copy()
6853
kwargs.update({ 'app': "search", 'owner': "-" })
6954
service_ns = client.connect(**kwargs)
7055
service_ns.apps.list()
7156

57+
def test_default_app(self):
58+
kwargs = self.opts.kwargs.copy()
7259
kwargs.update({ 'app': None, 'owner': "admin" })
7360
service_ns = client.connect(**kwargs)
7461
service_ns.apps.list()
7562

63+
def test_app_wildcard(self):
64+
kwargs = self.opts.kwargs.copy()
7665
kwargs.update({ 'app': "-", 'owner': "admin" })
7766
service_ns = client.connect(**kwargs)
7867
service_ns.apps.list()
7968

69+
def test_user_namespace(self):
70+
kwargs = self.opts.kwargs.copy()
8071
kwargs.update({ 'app': "search", 'owner': "admin" })
8172
service_ns = client.connect(**kwargs)
8273
service_ns.apps.list()
8374

84-
appname = "sdk-test-app"
85-
username = "sdk-test-user"
86-
testlib.delete_app(service, appname)
87-
if username in service.users: service.users.delete(username)
88-
self.assertFalse(service.apps.contains(appname))
89-
self.assertFalse(service.users.contains(username))
90-
91-
# App & owner dont exist, verify that the following errors
92-
kwargs.update({ 'app': appname, 'owner': username })
93-
with self.assertRaises(HTTPError):
75+
def test_nonexistant_user(self):
76+
username = testlib.tmpname()
77+
self.assertTrue(username not in self.service.users)
78+
with self.assertRaises(client.NoSuchUserException):
79+
kwargs = self.opts.kwargs.copy()
80+
kwargs.update({'owner': username})
9481
service_ns = client.connect(**kwargs)
9582
service_ns.apps.list()
9683

97-
# Validate namespace permutations with new app & user
98-
service.apps.create(appname)
99-
service.users.create(username, password="changeme", roles="power")
100-
101-
kwargs.update({ 'app': appname, 'owner': None })
102-
service_ns = client.connect(**kwargs)
103-
service_ns.apps.list()
104-
105-
kwargs.update({ 'app': appname, 'owner': "-" })
106-
service_ns = client.connect(**kwargs)
107-
service_ns.apps.list()
108-
109-
kwargs.update({ 'app': None, 'owner': username })
110-
service_ns = client.connect(**kwargs)
111-
service_ns.apps.list()
112-
113-
kwargs.update({ 'app': "-", 'owner': username })
114-
service_ns = client.connect(**kwargs)
115-
service_ns.apps.list()
116-
117-
kwargs.update({ 'app': appname, 'owner': username })
118-
service_ns = client.connect(**kwargs)
119-
service_ns.apps.list()
120-
121-
# Cleanup
122-
testlib.delete_app(service, appname)
123-
service.users.delete(username)
124-
125-
self.assertFalse(service.apps.contains(appname))
126-
self.assertFalse(service.users.contains(username))
127-
128-
def test_trailing(self):
129-
self.assertRaises(ValueError, client.trailing, 'this is a test', 'boris')
130-
self.assertRaises(ValueError, client.trailing, 'this is a test', 's is', 'boris')
131-
template = '/servicesNS/boris/search/another/path/segment/that runs on'
132-
self.assertEqual(template, client.trailing(template))
133-
self.assertEqual('boris/search/another/path/segment/that runs on', client.trailing(template, 'ervicesNS/'))
134-
self.assertEqual('another/path/segment/that runs on', client.trailing(template, 'servicesNS/', '/', '/'))
135-
84+
def test_nonexistant_app(self):
85+
app_name = testlib.tmpname()
86+
self.assertTrue(app_name not in self.service.apps)
87+
with self.assertRaises(client.NoSuchApplicationException):
88+
kwargs = self.opts.kwargs.copy()
89+
kwargs.update({'app': app_name})
90+
service_ns = client.connect(**kwargs)
91+
service_ns.apps.list()
13692

13793
def test_parse(self):
138-
service = client.connect(**self.opts.kwargs)
139-
140-
response = service.parse("search *")
94+
# Awaiting new parse method.
95+
response = self.service.parse('search * abc="def" | dedup abc')
14196
self.assertEqual(response.status, 200)
142-
143-
response = service.parse("search index=twitter status_count=* | stats count(status_source) as count by status_source | sort -count | head 20")
144-
self.assertEqual(response.status, 200)
145-
146-
try:
147-
service.parse("xyzzy")
148-
self.fail()
149-
except HTTPError, e:
150-
self.assertEqual(e.status, 400)
151-
except:
152-
self.fail()
97+
# try:
98+
# service.parse("xyzzy")
99+
# self.fail()
100+
# except HTTPError, e:
101+
# self.assertEqual(e.status, 400)
102+
# except:
103+
# self.fail()
153104

154105
def test_restart(self):
155106
service = client.connect(**self.opts.kwargs)
156107
testlib.restart(service)
157108
service.login() # Make sure we are awake
158109

159-
def test_settings(self):
160-
service = client.connect(**self.opts.kwargs)
161-
settings = service.settings
162-
110+
class TestSettings(testlib.TestCase):
111+
def test_read_settings(self):
112+
settings = self.service.settings
163113
# Verify that settings contains the keys we expect
164114
keys = [
165115
"SPLUNK_DB", "SPLUNK_HOME", "enableSplunkWebSSL", "host",
166116
"httpport", "mgmtHostPort", "minFreeSpace", "pass4SymmKey",
167117
"serverName", "sessionTimeout", "startwebserver", "trustedIP"
168118
]
169-
for key in keys: self.assertTrue(key in settings.content)
119+
for key in keys:
120+
self.assertTrue(key in settings)
170121

122+
def test_update_settings(self):
171123
# Verify that we can update the settings
172124
original = settings['sessionTimeout']
173125
self.assertTrue(original != "42h")
@@ -182,5 +134,24 @@ def test_settings(self):
182134
updated = settings['sessionTimeout']
183135
self.assertEqual(updated, original)
184136

137+
class TestTrailing(unittest.TestCase):
138+
template = '/servicesNS/boris/search/another/path/segment/that runs on'
139+
140+
def test_raises_when_not_found_first(self):
141+
self.assertRaises(ValueError, client.trailing, 'this is a test', 'boris')
142+
143+
def test_raises_when_not_found_second(self):
144+
self.assertRaises(ValueError, client.trailing, 'this is a test', 's is', 'boris')
145+
146+
def test_no_args_is_identity(self):
147+
self.assertEqual(self.template, client.trailing(self.template))
148+
149+
def test_trailing_with_one_arg_works(self):
150+
self.assertEqual('boris/search/another/path/segment/that runs on', client.trailing(self.template, 'ervicesNS/'))
151+
152+
def test_trailing_with_n_args_works(self):
153+
self.assertEqual('another/path/segment/that runs on', client.trailing(self.template, 'servicesNS/', '/', '/'))
154+
155+
185156
if __name__ == "__main__":
186157
testlib.main()

0 commit comments

Comments
 (0)