|
| 1 | +import pytest |
| 2 | +from unittest.mock import patch |
| 3 | +from collections import namedtuple |
| 4 | +from pytest_splunk_addon.standard_lib.app_test_generator import AppTestGenerator |
| 5 | + |
| 6 | +module = "pytest_splunk_addon.standard_lib.app_test_generator" |
| 7 | +config = { |
| 8 | + "splunk_app": "fake_app", |
| 9 | + "field_bank": "fake_field_bank", |
| 10 | + "splunk_dm_path": "fake_path", |
| 11 | + "store_events": True, |
| 12 | + "splunk_data_generator": "psa.conf", |
| 13 | +} |
| 14 | +pytest_config = namedtuple("Config", ["getoption"]) |
| 15 | +test_config = pytest_config(getoption=lambda x, *y: config[x]) |
| 16 | +test_config_without_dm_path = pytest_config( |
| 17 | + getoption=lambda x, *y: config[x] if x != "splunk_dm_path" else None |
| 18 | +) |
| 19 | +params = namedtuple("ParameterSet", ["values", "id"]) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture() |
| 23 | +def app_test_generator(mock_object): |
| 24 | + fieldtest_generator = mock_object(f"{module}.FieldTestGenerator") |
| 25 | + cim_test_generator = mock_object(f"{module}.CIMTestGenerator") |
| 26 | + indextime_test_generator = mock_object(f"{module}.IndexTimeTestGenerator") |
| 27 | + requirement_test_generator = mock_object(f"{module}.ReqsTestGenerator") |
| 28 | + for mock_element in [ |
| 29 | + fieldtest_generator, |
| 30 | + cim_test_generator, |
| 31 | + indextime_test_generator, |
| 32 | + requirement_test_generator, |
| 33 | + ]: |
| 34 | + setattr(mock_element, "return_value", mock_element) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize( |
| 38 | + "simple_config, path", |
| 39 | + [ |
| 40 | + (test_config, "fake_path"), |
| 41 | + (test_config_without_dm_path, "/fake_dir/data_models"), |
| 42 | + ], |
| 43 | +) |
| 44 | +def test_app_test_generator_instantiation( |
| 45 | + mock_object, os_path_join_file_mock, app_test_generator, simple_config, path |
| 46 | +): |
| 47 | + os_path_dirname_mock = mock_object("os.path.dirname") |
| 48 | + os_path_dirname_mock.return_value = "/fake_dir" |
| 49 | + atg = AppTestGenerator(simple_config) |
| 50 | + atg.fieldtest_generator.assert_called_once_with( |
| 51 | + config["splunk_app"], field_bank=config["field_bank"] |
| 52 | + ) |
| 53 | + atg.cim_test_generator.assert_called_once_with(config["splunk_app"], path) |
| 54 | + atg.requirement_test_generator.assert_called_once_with(config["splunk_app"]) |
| 55 | + atg.indextime_test_generator.assert_called_once_with() |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize( |
| 59 | + "fixture, called_function, test_generator, generator_args, generator_kwargs, expected_tests, dedup_call_count", |
| 60 | + [ |
| 61 | + ( |
| 62 | + "splunk_searchtime_fields", |
| 63 | + "fieldtest_generator", |
| 64 | + lambda fixture: (f"{fixture}_test_{i + 1}" for i in range(3)), |
| 65 | + ["splunk_searchtime_fields"], |
| 66 | + {}, |
| 67 | + [ |
| 68 | + "splunk_searchtime_fields_test_1", |
| 69 | + "splunk_searchtime_fields_test_2", |
| 70 | + "splunk_searchtime_fields_test_3", |
| 71 | + ], |
| 72 | + 1, |
| 73 | + ), |
| 74 | + ( |
| 75 | + "splunk_searchtime_cim", |
| 76 | + "cim_test_generator", |
| 77 | + lambda fixture: (f"{fixture}_test_{i + 1}" for i in range(3)), |
| 78 | + ["splunk_searchtime_cim"], |
| 79 | + {}, |
| 80 | + [ |
| 81 | + "splunk_searchtime_cim_test_1", |
| 82 | + "splunk_searchtime_cim_test_2", |
| 83 | + "splunk_searchtime_cim_test_3", |
| 84 | + ], |
| 85 | + 1, |
| 86 | + ), |
| 87 | + ( |
| 88 | + "splunk_searchtime_requirement", |
| 89 | + "requirement_test_generator", |
| 90 | + lambda fixture: (f"{fixture}_test_{i + 1}" for i in range(3)), |
| 91 | + ["splunk_searchtime_requirement"], |
| 92 | + {}, |
| 93 | + [ |
| 94 | + "splunk_searchtime_requirement_test_1", |
| 95 | + "splunk_searchtime_requirement_test_2", |
| 96 | + "splunk_searchtime_requirement_test_3", |
| 97 | + ], |
| 98 | + 1, |
| 99 | + ), |
| 100 | + ( |
| 101 | + "splunk_indextime_key_fields", |
| 102 | + "indextime_test_generator", |
| 103 | + lambda x, app_path, config_path, test_type: ( |
| 104 | + params(values=f"splunk_indextime_{test_type}_test_{3 - i}", id=3 - i) |
| 105 | + for i in range(3) |
| 106 | + ), |
| 107 | + [True], |
| 108 | + { |
| 109 | + "app_path": "fake_app", |
| 110 | + "config_path": "psa.conf", |
| 111 | + "test_type": "key_fields", |
| 112 | + }, |
| 113 | + [ |
| 114 | + params(values=f"splunk_indextime_key_fields_test_1", id=1), |
| 115 | + params(values=f"splunk_indextime_key_fields_test_2", id=2), |
| 116 | + params(values=f"splunk_indextime_key_fields_test_3", id=3), |
| 117 | + ], |
| 118 | + 0, |
| 119 | + ), |
| 120 | + ( |
| 121 | + "splunk_indextime_time", |
| 122 | + "indextime_test_generator", |
| 123 | + lambda x, app_path, config_path, test_type: ( |
| 124 | + params(values=f"splunk_indextime_{test_type}_test_{3 - i}", id=3 - i) |
| 125 | + for i in range(3) |
| 126 | + ), |
| 127 | + [True], |
| 128 | + {"app_path": "fake_app", "config_path": "psa.conf", "test_type": "_time"}, |
| 129 | + [ |
| 130 | + params(values=f"splunk_indextime__time_test_1", id=1), |
| 131 | + params(values=f"splunk_indextime__time_test_2", id=2), |
| 132 | + params(values=f"splunk_indextime__time_test_3", id=3), |
| 133 | + ], |
| 134 | + 0, |
| 135 | + ), |
| 136 | + ( |
| 137 | + "splunk_indextime_line_breaker", |
| 138 | + "indextime_test_generator", |
| 139 | + lambda x, app_path, config_path, test_type: ( |
| 140 | + params(values=f"splunk_indextime_{test_type}_test_{3 - i}", id=3 - i) |
| 141 | + for i in range(3) |
| 142 | + ), |
| 143 | + [True], |
| 144 | + { |
| 145 | + "app_path": "fake_app", |
| 146 | + "config_path": "psa.conf", |
| 147 | + "test_type": "line_breaker", |
| 148 | + }, |
| 149 | + [ |
| 150 | + params(values=f"splunk_indextime_line_breaker_test_1", id=1), |
| 151 | + params(values=f"splunk_indextime_line_breaker_test_2", id=2), |
| 152 | + params(values=f"splunk_indextime_line_breaker_test_3", id=3), |
| 153 | + ], |
| 154 | + 0, |
| 155 | + ), |
| 156 | + ], |
| 157 | +) |
| 158 | +def test_generate_tests( |
| 159 | + app_test_generator, |
| 160 | + fixture, |
| 161 | + called_function, |
| 162 | + test_generator, |
| 163 | + generator_args, |
| 164 | + generator_kwargs, |
| 165 | + expected_tests, |
| 166 | + dedup_call_count, |
| 167 | +): |
| 168 | + atg = AppTestGenerator(test_config) |
| 169 | + setattr(getattr(atg, called_function).generate_tests, "side_effect", test_generator) |
| 170 | + with patch.object( |
| 171 | + AppTestGenerator, "dedup_tests", side_effect=lambda x, y: x |
| 172 | + ) as dedup_mock: |
| 173 | + out = list(atg.generate_tests(fixture)) |
| 174 | + assert out == expected_tests |
| 175 | + getattr(atg, called_function).generate_tests.assert_called_once_with( |
| 176 | + *generator_args, **generator_kwargs |
| 177 | + ) |
| 178 | + assert dedup_mock.call_count == dedup_call_count |
| 179 | + |
| 180 | + |
| 181 | +def test_dedup_tests(app_test_generator): |
| 182 | + parameter_list = [params(values=f"val{x}", id=x) for x in range(7)] |
| 183 | + atg = AppTestGenerator(test_config) |
| 184 | + out = [] |
| 185 | + for parameters in [parameter_list[:3], parameter_list[2:5]]: |
| 186 | + out.extend(atg.dedup_tests(parameters, "splunk_searchtime")) |
| 187 | + assert out == parameter_list[:5] |
| 188 | + assert atg.seen_tests == {("splunk_searchtime", x) for x in range(5)} |
0 commit comments