|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright © 2011-2025 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 | +from splunklib import results |
| 18 | +from tests import testlib |
| 19 | +from splunklib.binding import HTTPError |
| 20 | + |
| 21 | + |
| 22 | +class ModularInput(testlib.SDKTestCase): |
| 23 | + def setUp(self): |
| 24 | + super().setUp() |
| 25 | + self.assertIn( |
| 26 | + "modularinput", [kind.name for kind in self.service.modular_input_kinds] |
| 27 | + ) |
| 28 | + if ("test", "modularinput") in [ |
| 29 | + (input.name, input.kind) for input in self.service.inputs |
| 30 | + ]: |
| 31 | + self.service.inputs.delete("test", "modularinput") |
| 32 | + if "test_modular_input" in [index.name for index in self.service.indexes]: |
| 33 | + self.service.indexes.delete("test_modular_input") |
| 34 | + |
| 35 | + def test_modular_input(self): |
| 36 | + self.service.indexes.create("test_modular_input") |
| 37 | + |
| 38 | + input = self.service.inputs.create( |
| 39 | + "test", |
| 40 | + "modularinput", |
| 41 | + endpoint="https://example.com/api/endpoint", |
| 42 | + index="test_modular_input", |
| 43 | + ) |
| 44 | + |
| 45 | + def query(): |
| 46 | + stream = self.service.jobs.oneshot( |
| 47 | + 'search index="test_modular_input"', output_mode="json" |
| 48 | + ) |
| 49 | + reader = results.JSONResultsReader(stream) |
| 50 | + return list(reader) |
| 51 | + |
| 52 | + # Wait until the modular input is executed by splunk. |
| 53 | + self.assertEventuallyTrue(lambda: len(query()) != 0, timeout=10) |
| 54 | + |
| 55 | + items = query() |
| 56 | + self.assertTrue(len(items) == 1) |
| 57 | + self.assertIn("_raw", items[0]) |
| 58 | + self.assertEqual(items[0]["_raw"], "example message") |
| 59 | + |
| 60 | + input.delete() |
| 61 | + self.service.indexes.delete("test_modular_input") |
| 62 | + |
| 63 | + def test_external_validator(self): |
| 64 | + def create(): |
| 65 | + self.service.inputs.create( |
| 66 | + "test", |
| 67 | + "modularinput", |
| 68 | + endpoint="http://example.com/api/endpoint", |
| 69 | + index="test_modular_input", |
| 70 | + ) |
| 71 | + |
| 72 | + self.assertRaisesRegex(HTTPError, "non-supported scheme http", create) |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + import unittest |
| 77 | + |
| 78 | + unittest.main() |
0 commit comments