|
| 1 | +import pytest |
| 2 | +from unittest.mock import patch |
| 3 | +from pytest_splunk_addon.standard_lib.addon_parser.eventtype_parser import EventTypeParser |
| 4 | + |
| 5 | + |
| 6 | +def test_eventtypes_can_be_parsed_and_extracted(parser): |
| 7 | + assert parser.eventtypes, "eventypes can not be called or does not return any value" |
| 8 | + |
| 9 | + |
| 10 | +def test_eventtypes_can_be_parsed_and_returned(parser): |
| 11 | + expected_outputs = [{"stanza": x} for x in ["fake_splunkd", "fake_for_tags_positive"]] |
| 12 | + for i, event in enumerate(parser.get_eventtypes()): |
| 13 | + assert event == expected_outputs[i], "expeceted event {} not found".format(expected_outputs[i]) |
| 14 | + |
| 15 | + |
| 16 | +def test_get_eventtypes_calls_app_get_config(parser): |
| 17 | + with patch.object(parser.app, "eventtypes_conf") as mock: |
| 18 | + for _ in parser.get_eventtypes(): |
| 19 | + pass |
| 20 | + mock.assert_called_once() |
| 21 | + |
| 22 | + |
| 23 | +def test_no_config_file(parser): |
| 24 | + parser.app.eventtypes_conf = os_error |
| 25 | + assert not parser.eventtypes, "eventtypes created when no config file exists" |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture() |
| 29 | +def parser(): |
| 30 | + class FakeApp: |
| 31 | + def __init__(self): |
| 32 | + self.splunk_app_path = "fake_path" |
| 33 | + self.eventtypes = FakeConfigurationFile() |
| 34 | + |
| 35 | + def eventtypes_conf(self): |
| 36 | + return self.eventtypes |
| 37 | + |
| 38 | + class FakeConfigurationFile: |
| 39 | + def __init__(self): |
| 40 | + self.headers = [] |
| 41 | + self.sects = { |
| 42 | + "fake_splunkd": { |
| 43 | + "name": "fake_splunkd", |
| 44 | + "options": "index=_internal sourcetype=splunkd" |
| 45 | + }, |
| 46 | + "fake_for_tags_positive": { |
| 47 | + "name": "fake_for_tags_positive", |
| 48 | + "options": "sourcetype=splunkd" |
| 49 | + } |
| 50 | + } |
| 51 | + self.errors = [] |
| 52 | + |
| 53 | + return EventTypeParser("fake_path", FakeApp()) |
| 54 | + |
| 55 | + |
| 56 | +def os_error(): |
| 57 | + raise OSError |
0 commit comments