Skip to content

Commit 64a883e

Browse files
author
Frederick Ross
committed
Made modular inputs and oneshot input testing use the app collection.
1 parent d2333c1 commit 64a883e

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

tests/test_input.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,23 @@ def test_inputs_list_on_one_kind_with_search(self):
5959
self.assertEqual(expected, found)
6060

6161
def test_oneshot(self):
62+
self.installAppFromCollection('file_to_upload')
63+
6264
index_name = testlib.tmpname()
6365
index = self.service.indexes.create(index_name)
64-
self.restartSplunk(timeout=120)
65-
index = self.service.indexes[index_name]
66+
self.assertEventuallyTrue(lambda: index.refresh() and index['disabled'] == '0')
67+
6668
eventCount = int(index['totalEventCount'])
67-
from os import path
68-
testpath = path.dirname(path.abspath(__file__))
69-
self.service.inputs.oneshot(path.join(testpath, 'testfile.txt'), index=index_name)
69+
70+
path = self.pathInApp("file_to_upload", ["log.txt"])
71+
self.service.inputs.oneshot(path, index=index_name)
7072
self.assertEventuallyEqual(
71-
str(eventCount+1),
73+
str(eventCount+4),
7274
lambda: index.refresh()['totalEventCount']
7375
)
7476

7577
def test_oneshot_on_nonexistant_file(self):
7678
name = testlib.tmpname()
77-
from os import path
78-
self.assertFalse(path.exists(name))
7979
self.assertRaises(client.OperationFailedException,
8080
self.service.inputs.oneshot, name)
8181

@@ -121,9 +121,15 @@ def test_lists_modular_inputs(self):
121121
if self.service.splunk_version[0] < 5:
122122
return # Modular inputs don't exist prior to 5.0
123123
else:
124+
# Install modular inputs to list, and restart
125+
# so they'll show up.
126+
self.installAppFromCollection("modular-inputs")
127+
self.uncheckedRestartSplunk()
128+
124129
inputs = self.service.inputs
125130
if ('test2','abcd') not in inputs:
126131
inputs.create('test2', 'abcd', field1='boris')
132+
127133
input = inputs['test2', 'abcd']
128134
self.assertEqual(input.field1, 'boris')
129135

tests/test_modular_input_kinds.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ModularInputKindTestCase(testlib.SDKTestCase):
2121
def setUp(self):
2222
super(ModularInputKindTestCase, self).setUp()
2323
self.installAppFromCollection("modular-inputs")
24+
self.uncheckedRestartSplunk()
2425

2526
def test_list_arguments(self):
2627
if self.service.splunk_version[0] < 5:

tests/testlib.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,25 @@ def installAppFromCollection(self, name):
147147
collectionName = 'sdk-app-collection'
148148
if collectionName not in self.service.apps:
149149
raise ValueError("sdk-test-application not installed in splunkd")
150+
appPath = self.pathInApp(collectionName, ["build", name+".tar"])
151+
kwargs = {"update": 1, "name": appPath}
152+
try:
153+
self.service.post("apps/appinstall", **kwargs)
154+
except client.HTTPError as he:
155+
if he.status == 400:
156+
raise IOError("App %s not found in app collection" % name)
157+
self.installedApps.append(name)
158+
159+
def pathInApp(self, appName, pathComponents):
160+
"""Return a path to *pathComponents* in *appName*.
161+
162+
*pathComponents* shold be a list of strings giving the components.
163+
This function will try to figure out the correct separator (/ or \)
164+
for the platform that splunkd is running on and construct the path
165+
as needed.
166+
167+
:return: A string giving the path.
168+
"""
150169
splunkHome = self.service.settings['SPLUNK_HOME']
151170
if "/" in splunkHome and "\\" in splunkHome:
152171
raise ValueError("There are both forward and back slashes in $SPLUNK_HOME. What system are you on?!?")
@@ -156,15 +175,11 @@ def installAppFromCollection(self, name):
156175
separator = "\\"
157176
else:
158177
raise ValueError("No separators in $SPLUNK_HOME. Can't determine what file separator to use.")
159-
appPath = separator.join([splunkHome, "etc", "apps", collectionName,
160-
"build", name + ".tar"])
161-
kwargs = {"update": 1, "name": appPath}
162-
try:
163-
self.service.post("apps/appinstall", **kwargs)
164-
except client.HTTPError as he:
165-
if he.status == 400:
166-
raise IOError("App %s not found in app collection" % name)
167-
self.installedApps.append(name)
178+
appPath = separator.join([splunkHome, "etc", "apps", appName] + pathComponents)
179+
return appPath
180+
181+
def uncheckedRestartSplunk(self, timeout=120):
182+
self.service.restart(timeout)
168183

169184
def restartSplunk(self, timeout=120):
170185
if self.service.restart_required:
@@ -178,19 +193,20 @@ def setUpClass(cls):
178193

179194
def setUp(self):
180195
unittest.TestCase.setUp(self)
181-
self.caused_restart_required = False
182196
self.service = client.connect(**self.opts.kwargs)
183197
if self.service.restart_required:
184-
self.restart_already_required = True
198+
self.restartSplunk()
185199
logging.debug("Connected to splunkd version %s", '.'.join(str(x) for x in self.service.splunk_version))
186200

187201
def tearDown(self):
188-
if self.service.restart_required and not self.restart_already_required:
202+
if self.service.restart_required:
189203
self.fail("Test left Splunk in a state requiring a restart.")
190204
for appName in self.installedApps:
191205
if appName in self.service.apps:
192206
self.service.apps.delete(appName)
193207
wait(lambda: appName not in self.service.apps)
208+
if self.service.restart_required:
209+
self.clearRestartMessage()
194210

195211
def main():
196212
unittest.main()

0 commit comments

Comments
 (0)