Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/integration/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def setUp(self):
else:
logging.debug(f"App {self.app_name} already exists. Skipping creation.")
if self.service.restart_required:
self.service.restart(120)
self.restart_splunk()

def tearDown(self):
super().tearDown()
Expand Down
13 changes: 7 additions & 6 deletions tests/integration/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class ServiceTestCase(testlib.SDKTestCase):
def test_autologin(self):
service = client.connect(autologin=True, **self.opts.kwargs)
self.service.restart(timeout=120)
self.restart_splunk()
reader = service.jobs.oneshot("search index=internal | head 1")
self.assertIsNotNone(reader)

Expand Down Expand Up @@ -128,7 +128,7 @@ def test_parse_fail(self):

def test_restart(self):
service = client.connect(**self.opts.kwargs)
self.service.restart(timeout=300)
self.restart_splunk()
service.login() # Make sure we are awake

def test_read_outputs_with_type(self):
Expand All @@ -139,11 +139,11 @@ def test_read_outputs_with_type(self):
self.assertTrue("tcp", entity.content.type)

if service.restart_required:
self.restartSplunk()
self.restart_splunk()
service = client.connect(**self.opts.kwargs)
client.Entity(service, "data/outputs/tcp/syslog/" + name).delete()
if service.restart_required:
self.restartSplunk()
self.restart_splunk()

def test_splunk_version(self):
service = client.connect(**self.opts.kwargs)
Expand Down Expand Up @@ -259,7 +259,7 @@ def test_autologin_with_cookie(self):
**self.opts.kwargs,
)
self.assertTrue(service.has_cookies())
self.service.restart(timeout=120)
testlib.restart_splunk(self.service)
reader = service.jobs.oneshot("search index=internal | head 1")
self.assertIsNotNone(reader)

Expand Down Expand Up @@ -351,7 +351,8 @@ def test_update_settings(self):
settings.refresh()
updated = settings["sessionTimeout"]
self.assertEqual(updated, original)
self.restartSplunk()
if self.service.restart_required:
self.restart_splunk()


class TestTrailing(unittest.TestCase):
Expand Down
23 changes: 14 additions & 9 deletions tests/testlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ def wait(predicate, timeout=60, pause_time=0.5):
logging.debug("wait finished after %s seconds", datetime.now() - start)


def restart_splunk(service: client.Service):
service.restart(timeout=120)
# Give Splunk some additional time. In our test suite, a subsequent
# restart shortly after the initial restart can cause Splunk to crash
# and fail to start.
sleep(15)


class SDKTestCase(unittest.TestCase):
restart_already_required = False
installedApps = []
Expand Down Expand Up @@ -138,6 +146,9 @@ def check_entity(self, entity):
continue
raise

def restart_splunk(self):
restart_splunk(self.service)

def clear_restart_message(self):
"""Tell Splunk to forget that it needs to be restarted.

Expand Down Expand Up @@ -175,7 +186,7 @@ def install_app_from_collection(self, name):
if he.status == 400:
raise IOError(f"App {name} not found in app collection")
if self.service.restart_required:
self.service.restart(120)
self.restart_splunk()
self.installedApps.append(name)

def app_collection_installed(self):
Expand Down Expand Up @@ -221,20 +232,14 @@ def pathInApp(self, appName, pathComponents):
appPath = separator.join([splunkHome, "etc", "apps", appName] + pathComponents)
return appPath

def restartSplunk(self, timeout=240):
if self.service.restart_required:
self.service.restart(timeout)
else:
raise NoRestartRequiredError()

@classmethod
def setUpClass(cls):
cls.opts = parse([], {}, ".env")
cls.opts.kwargs.update({"retries": 3})
# Before we start, make sure splunk doesn't need a restart.
service = client.connect(**cls.opts.kwargs)
if service.restart_required:
service.restart(timeout=120)
self.restart_splunk()

def setUp(self):
unittest.TestCase.setUp(self)
Expand All @@ -244,7 +249,7 @@ def setUp(self):
# and restart. That way we'll be sane for the rest of
# the test.
if self.service.restart_required:
self.restartSplunk()
self.restart_splunk()
logging.debug(
"Connected to splunkd version %s",
".".join(str(x) for x in self.service.splunk_version),
Expand Down