|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright © 2011-2024 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 | +# integration test - tests SDK usage as communication with splunk over http api to create and manage apps |
| 18 | + |
| 19 | +import logging |
| 20 | +from tests import testlib |
| 21 | +from splunklib import client |
| 22 | + |
| 23 | + |
| 24 | +class TestApp(testlib.SDKTestCase): |
| 25 | + app = None |
| 26 | + app_name = None |
| 27 | + |
| 28 | + def setUp(self): |
| 29 | + super().setUp() |
| 30 | + if self.app is None: |
| 31 | + for app in self.service.apps: |
| 32 | + if app.name.startswith('delete-me'): |
| 33 | + self.service.apps.delete(app.name) |
| 34 | + # Creating apps takes 0.8s, which is too long to wait for |
| 35 | + # each test in this test suite. Therefore we create one |
| 36 | + # app and reuse it. Since apps are rather less fraught |
| 37 | + # than entities like indexes, this is okay. |
| 38 | + self.app_name = testlib.tmpname() |
| 39 | + self.app = self.service.apps.create(self.app_name) |
| 40 | + logging.debug(f"Creating app {self.app_name}") |
| 41 | + else: |
| 42 | + logging.debug(f"App {self.app_name} already exists. Skipping creation.") |
| 43 | + if self.service.restart_required: |
| 44 | + self.service.restart(120) |
| 45 | + |
| 46 | + def tearDown(self): |
| 47 | + super().tearDown() |
| 48 | + # The rest of this will leave Splunk in a state requiring a restart. |
| 49 | + # It doesn't actually matter, though. |
| 50 | + self.service = client.connect(**self.opts.kwargs) |
| 51 | + app_name = '' |
| 52 | + for app in self.service.apps: |
| 53 | + app_name = app.name |
| 54 | + if app_name.startswith('delete-me'): |
| 55 | + self.service.apps.delete(app_name) |
| 56 | + self.assertEventuallyTrue(lambda: app_name not in self.service.apps) |
| 57 | + self.clear_restart_message() |
| 58 | + |
| 59 | + def test_app_integrity(self): |
| 60 | + self.check_entity(self.app) |
| 61 | + self.app.setupInfo |
| 62 | + self.app['setupInfo'] |
| 63 | + |
| 64 | + def test_disable_enable(self): |
| 65 | + self.app.disable() |
| 66 | + self.app.refresh() |
| 67 | + self.assertEqual(self.app['disabled'], '1') |
| 68 | + self.app.enable() |
| 69 | + self.app.refresh() |
| 70 | + self.assertEqual(self.app['disabled'], '0') |
| 71 | + |
| 72 | + def test_update(self): |
| 73 | + kwargs = { |
| 74 | + 'author': "Me", |
| 75 | + 'description': "Test app description", |
| 76 | + 'label': "SDK Test", |
| 77 | + 'version': "1.2", |
| 78 | + 'visible': True, |
| 79 | + } |
| 80 | + self.app.update(**kwargs) |
| 81 | + self.app.refresh() |
| 82 | + self.assertEqual(self.app['author'], "Me") |
| 83 | + self.assertEqual(self.app['label'], "SDK Test") |
| 84 | + self.assertEqual(self.app['version'], "1.2") |
| 85 | + self.assertEqual(self.app['visible'], "1") |
| 86 | + |
| 87 | + def test_delete(self): |
| 88 | + name = testlib.tmpname() |
| 89 | + self.service.apps.create(name) |
| 90 | + self.assertTrue(name in self.service.apps) |
| 91 | + self.service.apps.delete(name) |
| 92 | + self.assertFalse(name in self.service.apps) |
| 93 | + self.clear_restart_message() # We don't actually have to restart here. |
| 94 | + |
| 95 | + def test_package(self): |
| 96 | + p = self.app.package() |
| 97 | + self.assertEqual(p.name, self.app_name) |
| 98 | + self.assertTrue(p.path.endswith(self.app_name + '.spl')) |
| 99 | + # Assert string due to deprecation of this property in new Splunk versions |
| 100 | + self.assertIsInstance(p.url, str) |
| 101 | + |
| 102 | + def test_updateInfo(self): |
| 103 | + p = self.app.updateInfo() |
| 104 | + self.assertTrue(p is not None) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + import unittest |
| 109 | + unittest.main() |
0 commit comments