Skip to content

Commit 89c3e71

Browse files
committed
Factor out several tests into separete test scripts.
1 parent d8a23d5 commit 89c3e71

File tree

4 files changed

+172
-99
lines changed

4 files changed

+172
-99
lines changed

tests/runtests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
"test_binding.py",
2929
"test_collection.py",
3030
"test_client.py",
31+
"test_app.py",
3132
"test_event_type.py",
3233
"test_fired_alert.py",
34+
"test_user.py",
3335
"test_saved_search.py",
3436
"test_examples.py",
3537
]

tests/test_app.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2011-2012 Splunk, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
import splunklib.client as client
18+
19+
import testlib
20+
21+
def delete_app(service, name):
22+
if (service.apps.contains(name)):
23+
service.apps.delete(name)
24+
testlib.restart(service)
25+
26+
class TestCase(testlib.TestCase):
27+
def test_read(self):
28+
service = client.connect(**self.opts.kwargs)
29+
30+
for app in service.apps:
31+
app.name
32+
app.path
33+
app.content
34+
app.metadata
35+
36+
for app in service.apps:
37+
app.refresh()
38+
39+
def test_crud(self):
40+
service = client.connect(**self.opts.kwargs)
41+
42+
appname = "sdk-test-app"
43+
44+
delete_app(service, appname)
45+
self.assertFalse(appname in service.apps)
46+
47+
kwargs = {
48+
'author': "Me",
49+
'description': "Test app description",
50+
'label': "SDK Test",
51+
'manageable': False,
52+
'template': "barebones",
53+
'visible': True,
54+
}
55+
service.apps.create(appname, **kwargs)
56+
self.assertTrue(appname in service.apps)
57+
app = service.apps[appname]
58+
self.assertEqual(app['author'], "Me")
59+
self.assertEqual(app['label'], "SDK Test")
60+
self.assertEqual(app['manageable'], "0")
61+
self.assertEqual(app['visible'], "1")
62+
63+
kwargs = {
64+
'author': "SDK",
65+
'visible': False,
66+
}
67+
app = service.apps[appname]
68+
app.update(**kwargs)
69+
app.refresh()
70+
self.assertEqual(app['author'], "SDK")
71+
self.assertEqual(app['label'], "SDK Test")
72+
self.assertEqual(app['manageable'], "0")
73+
self.assertEqual(app['visible'], "0")
74+
75+
delete_app(service, appname)
76+
self.assertFalse(appname in service.apps)
77+
78+
if __name__ == "__main__":
79+
testlib.main()

tests/test_client.py

Lines changed: 19 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,11 @@
2323

2424
import testlib
2525

26-
def create_app(service, name):
27-
service.apps.create(name)
28-
testlib.restart(service)
29-
30-
def create_user(service, name, password="changeme", roles="power"):
31-
service.users.create(name, password=password, roles=roles)
32-
3326
def delete_app(service, name):
3427
if (service.apps.contains(name)):
3528
service.apps.delete(name)
3629
testlib.restart(service)
3730

38-
def delete_user(service, name):
39-
if (service.users.contains(name)):
40-
service.users.delete(name)
41-
4231
# Verify that we can instantiate and connect to a service, test basic
4332
# interaction with the service and make sure we can connect and interact
4433
# with a variety of namespace configurations.
@@ -71,71 +60,51 @@ def test(self):
7160
service_ns = client.connect(**kwargs)
7261
service_ns.apps()
7362

74-
app = "sdk-tests"
75-
user = "sdk-user"
76-
delete_app(service, app)
77-
delete_user(service, user)
78-
self.assertFalse(service.apps.contains(app))
79-
self.assertFalse(service.users.contains(user))
63+
appname = "sdk-test-app"
64+
username = "sdk-test-user"
65+
delete_app(service, appname)
66+
if username in service.users: service.users.delete(username)
67+
self.assertFalse(service.apps.contains(appname))
68+
self.assertFalse(service.users.contains(username))
8069

8170
# App & owner dont exist, verify that the following errors
82-
kwargs.update({ 'app': app, 'owner': user })
71+
kwargs.update({ 'app': appname, 'owner': username })
8372
with self.assertRaises(HTTPError):
8473
service_ns = client.connect(**kwargs)
8574
service_ns.apps()
8675

8776
# Validate namespace permutations with new app & user
88-
create_app(service, app)
89-
create_user(service, user)
77+
service.apps.create(appname)
78+
service.users.create(username, password="changeme", roles="power")
9079

91-
kwargs.update({ 'app': app, 'owner': None })
80+
kwargs.update({ 'app': appname, 'owner': None })
9281
service_ns = client.connect(**kwargs)
9382
service_ns.apps()
9483

95-
kwargs.update({ 'app': app, 'owner': "-" })
84+
kwargs.update({ 'app': appname, 'owner': "-" })
9685
service_ns = client.connect(**kwargs)
9786
service_ns.apps()
9887

99-
kwargs.update({ 'app': None, 'owner': user })
88+
kwargs.update({ 'app': None, 'owner': username })
10089
service_ns = client.connect(**kwargs)
10190
service_ns.apps()
10291

103-
kwargs.update({ 'app': "-", 'owner': user })
92+
kwargs.update({ 'app': "-", 'owner': username })
10493
service_ns = client.connect(**kwargs)
10594
service_ns.apps()
10695

107-
kwargs.update({ 'app': app, 'owner': user })
96+
kwargs.update({ 'app': appname, 'owner': username })
10897
service_ns = client.connect(**kwargs)
10998
service_ns.apps()
11099

111100
# Cleanup
112-
delete_app(service, app)
113-
delete_user(service, user)
101+
delete_app(service, appname)
102+
service.users.delete(username)
114103

115-
self.assertFalse(service.apps.contains(app))
116-
self.assertFalse(service.users.contains(user))
104+
self.assertFalse(service.apps.contains(appname))
105+
self.assertFalse(service.users.contains(username))
117106

118107
class ClientTestCase(testlib.TestCase):
119-
def test_apps(self):
120-
service = client.connect(**self.opts.kwargs)
121-
122-
for app in service.apps: app.refresh()
123-
124-
delete_app(service, 'sdk-tests')
125-
self.assertFalse(service.apps.contains('sdk-tests'))
126-
127-
create_app(service, 'sdk-tests')
128-
self.assertTrue(service.apps.contains('sdk-tests'))
129-
130-
testapp = service.apps['sdk-tests']
131-
self.assertTrue(testapp['author'] != "Splunk")
132-
testapp.update(author="Splunk")
133-
testapp.refresh()
134-
self.assertTrue(testapp['author'] == "Splunk")
135-
136-
delete_app(service, 'sdk-tests')
137-
self.assertFalse(service.apps.contains('sdk-tests'))
138-
139108
def test_capabilities(self):
140109
service = client.connect(**self.opts.kwargs)
141110

@@ -163,8 +132,7 @@ def test_confs(self):
163132

164133
for conf in service.confs:
165134
for stanza in conf: stanza.refresh()
166-
# no need to refresh every conf file for the test
167-
break
135+
break # no need to refresh every conf file for the test
168136

169137
self.assertTrue(service.confs.contains('props'))
170138
props = service.confs['props']
@@ -553,53 +521,5 @@ def test_settings(self):
553521
updated = settings['sessionTimeout']
554522
self.assertEqual(updated, original)
555523

556-
def test_users(self):
557-
service = client.connect(**self.opts.kwargs)
558-
users = service.users
559-
roles = service.roles
560-
561-
# Verify that we can read the users collection
562-
for user in users:
563-
for role in user.content.roles:
564-
self.assertTrue(roles.contains(role))
565-
566-
if users.contains("sdk-user"): users.delete("sdk-user")
567-
self.assertFalse(users.contains("sdk-user"))
568-
569-
user = users.create("sdk-user", password="changeme", roles="power")
570-
self.assertTrue(users.contains("sdk-user"))
571-
572-
# Verify the new user has the expected attributes
573-
self.assertTrue('email' in user.content)
574-
self.assertTrue('password' in user.content)
575-
self.assertTrue('realname' in user.content)
576-
self.assertTrue('roles' in user.content)
577-
578-
# Verify that we can update the user
579-
self.assertTrue(user['email'] is None)
580-
user.update(email="foo@bar.com")
581-
user.refresh()
582-
self.assertTrue(user['email'] == "foo@bar.com")
583-
584-
# Verify that we can delete the user
585-
users.delete("sdk-user")
586-
self.assertFalse(users.contains("sdk-user"))
587-
588-
# Splunk lowercases user names, verify the casing works as expected
589-
self.assertFalse(users.contains("sdk-user"))
590-
self.assertFalse(users.contains("SDK-User"))
591-
592-
user = users.create("SDK-User", password="changeme", roles="power")
593-
self.assertTrue(user.name == "sdk-user")
594-
self.assertTrue(users.contains("SDK-User"))
595-
self.assertTrue(users.contains("sdk-user"))
596-
597-
user = users['SDK-User']
598-
self.assertTrue(user.name == "sdk-user")
599-
600-
users.delete("SDK-User")
601-
self.assertFalse(users.contains("SDK-User"))
602-
self.assertFalse(users.contains("sdk-user"))
603-
604524
if __name__ == "__main__":
605525
testlib.main()

tests/test_user.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2011-2012 Splunk, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
import splunklib.client as client
18+
19+
import testlib
20+
21+
class TestCase(testlib.TestCase):
22+
def test(self):
23+
service = client.connect(**self.opts.kwargs)
24+
25+
users = service.users
26+
roles = service.roles
27+
28+
# Verify that we can read the users collection
29+
for user in users:
30+
for role in user.content.roles:
31+
self.assertTrue(roles.contains(role))
32+
33+
if users.contains("sdk-user"): users.delete("sdk-user")
34+
self.assertFalse(users.contains("sdk-user"))
35+
36+
user = users.create("sdk-user", password="changeme", roles="power")
37+
self.assertTrue(users.contains("sdk-user"))
38+
39+
# Verify the new user has the expected attributes
40+
self.assertTrue('email' in user.content)
41+
self.assertTrue('password' in user.content)
42+
self.assertTrue('realname' in user.content)
43+
self.assertTrue('roles' in user.content)
44+
45+
# Verify that we can update the user
46+
self.assertTrue(user['email'] is None)
47+
user.update(email="foo@bar.com")
48+
user.refresh()
49+
self.assertTrue(user['email'] == "foo@bar.com")
50+
51+
# Verify that we can delete the user
52+
users.delete("sdk-user")
53+
self.assertFalse(users.contains("sdk-user"))
54+
55+
# Splunk lowercases user names, verify the casing works as expected
56+
self.assertFalse(users.contains("sdk-user"))
57+
self.assertFalse(users.contains("SDK-User"))
58+
59+
user = users.create("SDK-User", password="changeme", roles="power")
60+
self.assertTrue(user.name == "sdk-user")
61+
self.assertTrue(users.contains("SDK-User"))
62+
self.assertTrue(users.contains("sdk-user"))
63+
64+
user = users['SDK-User']
65+
self.assertTrue(user.name == "sdk-user")
66+
67+
users.delete("SDK-User")
68+
self.assertFalse(users.contains("SDK-User"))
69+
self.assertFalse(users.contains("sdk-user"))
70+
71+
if __name__ == "__main__":
72+
testlib.main()

0 commit comments

Comments
 (0)