|
| 1 | +""" |
| 2 | +This module tests NemesisRegistry method on custom Subclass tree |
| 3 | +Should not be dependent on the implementation of Nemesis class |
| 4 | +""" |
| 5 | + |
| 6 | +import pytest |
| 7 | +from sdcm.nemesis_registry import NemesisRegistry |
| 8 | + |
| 9 | + |
| 10 | +class TestNemesis: |
| 11 | + COMMON_STRING = "called test function " |
| 12 | + flag_a = False |
| 13 | + flag_b = False |
| 14 | + flag_c = False |
| 15 | + flag_d = False |
| 16 | + flag_common = False |
| 17 | + |
| 18 | + def disrupt_method_a(self): |
| 19 | + print(self.COMMON_STRING + "a") |
| 20 | + |
| 21 | + def disrupt_method_b(self): |
| 22 | + print(self.COMMON_STRING + "b") |
| 23 | + |
| 24 | + def disrupt_method_c(self): |
| 25 | + print(self.COMMON_STRING + "c") |
| 26 | + |
| 27 | + def disrupt_method_d(self): |
| 28 | + print(self.COMMON_STRING + "d") |
| 29 | + |
| 30 | + |
| 31 | +class CustomNemesisA(TestNemesis): |
| 32 | + flag_a = True |
| 33 | + flag_common = True |
| 34 | + |
| 35 | + def disrupt(self): |
| 36 | + self.disrupt_method_a() |
| 37 | + |
| 38 | + |
| 39 | +class CustomNemesisB(TestNemesis): |
| 40 | + flag_b = True |
| 41 | + flag_common = True |
| 42 | + |
| 43 | + def disrupt(self): |
| 44 | + self.disrupt_method_b() |
| 45 | + |
| 46 | + |
| 47 | +class CustomNemesisC(CustomNemesisA): |
| 48 | + flag_c = True |
| 49 | + |
| 50 | + def disrupt(self): |
| 51 | + self.disrupt_method_c() |
| 52 | + |
| 53 | + |
| 54 | +class CustomNemesisD(CustomNemesisB): |
| 55 | + flag_d = True |
| 56 | + flag_a = True |
| 57 | + flag_common = True |
| 58 | + |
| 59 | + def disrupt(self): |
| 60 | + self.disrupt_method_d() |
| 61 | + |
| 62 | + |
| 63 | +@pytest.fixture |
| 64 | +def registry(): |
| 65 | + return NemesisRegistry(base_class=TestNemesis) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize( |
| 69 | + "logical_phrase, expected_classes", |
| 70 | + [ |
| 71 | + ("flag_a", {CustomNemesisA, CustomNemesisD}), |
| 72 | + ("flag_b", {CustomNemesisB}), |
| 73 | + ("flag_c", {CustomNemesisC}), |
| 74 | + ("flag_d", {CustomNemesisD}), |
| 75 | + ("flag_common", {CustomNemesisA, CustomNemesisB, CustomNemesisD}), |
| 76 | + ], |
| 77 | +) |
| 78 | +def test_filter_subclasses_by_single_flag(registry, logical_phrase, expected_classes): |
| 79 | + filtered = registry.filter_subclasses(registry.get_subclasses(), logical_phrase) |
| 80 | + assert set(filtered) == expected_classes |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.parametrize( |
| 84 | + "logical_phrase, expected_classes", |
| 85 | + [ |
| 86 | + ("flag_a and flag_d", {CustomNemesisD}), |
| 87 | + ("flag_common and not flag_c", {CustomNemesisA, CustomNemesisB, CustomNemesisD}), |
| 88 | + ("CustomNemesisA or flag_d", {CustomNemesisA, CustomNemesisD}), |
| 89 | + ], |
| 90 | +) |
| 91 | +def test_filter_subclasses_by_combined_flags(registry, logical_phrase, expected_classes): |
| 92 | + filtered = registry.filter_subclasses(registry.get_subclasses(), logical_phrase) |
| 93 | + assert set(filtered) == expected_classes |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.parametrize( |
| 97 | + "logical_phrase, expected_classes", |
| 98 | + [ |
| 99 | + ("(CustomNemesisA or CustomNemesisD) and not flag_d", {CustomNemesisA}), |
| 100 | + ("flag_common and not flag_a", {CustomNemesisB}), |
| 101 | + ], |
| 102 | +) |
| 103 | +def test_filter_subclasses_with_complex_expression(registry, logical_phrase, expected_classes): |
| 104 | + filtered = registry.filter_subclasses(registry.get_subclasses(), logical_phrase) |
| 105 | + assert set(filtered) == expected_classes |
| 106 | + |
| 107 | + |
| 108 | +def test_get_subclasses(registry): |
| 109 | + subclasses = registry.get_subclasses() |
| 110 | + assert set(subclasses) == {CustomNemesisA, CustomNemesisB, CustomNemesisC, CustomNemesisD} |
| 111 | + |
| 112 | + |
| 113 | +def test_gather_properties(registry): |
| 114 | + class_properties, method_properties = registry.gather_properties() |
| 115 | + |
| 116 | + expected_class_properties = { |
| 117 | + "CustomNemesisA": {"flag_a": True, "flag_common": True}, |
| 118 | + "CustomNemesisB": {"flag_b": True, "flag_common": True}, |
| 119 | + "CustomNemesisC": {"flag_c": True}, |
| 120 | + "CustomNemesisD": {"flag_a": True, "flag_d": True, "flag_common": True}, |
| 121 | + } |
| 122 | + |
| 123 | + expected_method_properties = { |
| 124 | + "disrupt_method_a": {"flag_a": True, "flag_common": True}, |
| 125 | + "disrupt_method_b": {"flag_b": True, "flag_common": True}, |
| 126 | + "disrupt_method_c": {"flag_c": True}, |
| 127 | + "disrupt_method_d": {"flag_a": True, "flag_d": True, "flag_common": True}, |
| 128 | + } |
| 129 | + |
| 130 | + assert class_properties == expected_class_properties |
| 131 | + assert method_properties == expected_method_properties |
| 132 | + |
| 133 | + |
| 134 | +@pytest.mark.parametrize( |
| 135 | + "logical_phrase, expected_methods", |
| 136 | + [ |
| 137 | + ("flag_a", {CustomNemesisA.disrupt_method_a, CustomNemesisD.disrupt_method_d}), |
| 138 | + ("flag_b", {CustomNemesisB.disrupt_method_b}), |
| 139 | + ("flag_common and not flag_b", {CustomNemesisA.disrupt_method_a, CustomNemesisD.disrupt_method_d}), |
| 140 | + ("flag_c", {CustomNemesisC.disrupt_method_c}), |
| 141 | + ("flag_d", {CustomNemesisD.disrupt_method_d}), |
| 142 | + ], |
| 143 | +) |
| 144 | +def test_get_disrupt_methods(registry, logical_phrase, expected_methods): |
| 145 | + disrupt_methods = registry.get_disrupt_methods(logical_phrase) |
| 146 | + assert set(disrupt_methods) == expected_methods |
| 147 | + |
| 148 | + |
| 149 | +def test_get_disrupt_method_execution(registry, capsys): |
| 150 | + """Tests how you can use get_disrupt_methods to actually call returned methods""" |
| 151 | + disrupt_methods = registry.get_disrupt_methods("flag_common and not flag_b") |
| 152 | + for disrupt_method in disrupt_methods: |
| 153 | + disrupt_method(TestNemesis()) |
| 154 | + |
| 155 | + captured = capsys.readouterr() |
| 156 | + assert "called test function a" in captured.out |
| 157 | + assert "called test function d" in captured.out |
0 commit comments