|
| 1 | +"""Redis storage end to end tests.""" |
| 2 | +#pylint: disable=no-self-use,protected-access,line-too-long,too-few-public-methods |
| 3 | + |
| 4 | +import json |
| 5 | +import os |
| 6 | + |
| 7 | +from splitio.client.util import get_metadata |
| 8 | +from splitio.models import splits, impressions, events |
| 9 | +from splitio.storage.pluggable import PluggableEventsStorage, PluggableImpressionsStorage, PluggableSegmentStorage, \ |
| 10 | + PluggableSplitStorage, PluggableTelemetryStorage |
| 11 | +from splitio.client.config import DEFAULT_CONFIG |
| 12 | +from tests.storage.test_pluggable import StorageMockAdapter |
| 13 | + |
| 14 | +class PluggableSplitStorageIntegrationTests(object): |
| 15 | + """Pluggable Split storage e2e tests.""" |
| 16 | + |
| 17 | + def test_put_fetch(self): |
| 18 | + """Test storing and retrieving splits in redis.""" |
| 19 | + adapter = StorageMockAdapter() |
| 20 | + try: |
| 21 | + storage = PluggableSplitStorage(adapter) |
| 22 | + split_fn = os.path.join(os.path.dirname(__file__), 'files', 'split_Changes.json') |
| 23 | + with open(split_fn, 'r') as flo: |
| 24 | + data = json.loads(flo.read()) |
| 25 | + for split in data['splits']: |
| 26 | + adapter.set(storage._prefix.format(split_name=split['name']), split) |
| 27 | + adapter.increment(storage._traffic_type_prefix.format(traffic_type_name=split['trafficTypeName']), 1) |
| 28 | + adapter.set(storage._split_till_prefix, data['till']) |
| 29 | + |
| 30 | + split_objects = [splits.from_raw(raw) for raw in data['splits']] |
| 31 | + for split_object in split_objects: |
| 32 | + raw = split_object.to_json() |
| 33 | + |
| 34 | + original_splits = {split.name: split for split in split_objects} |
| 35 | + fetched_splits = {name: storage.get(name) for name in original_splits.keys()} |
| 36 | + |
| 37 | + assert set(original_splits.keys()) == set(fetched_splits.keys()) |
| 38 | + |
| 39 | + for original_split in original_splits.values(): |
| 40 | + fetched_split = fetched_splits[original_split.name] |
| 41 | + assert original_split.traffic_type_name == fetched_split.traffic_type_name |
| 42 | + assert original_split.seed == fetched_split.seed |
| 43 | + assert original_split.algo == fetched_split.algo |
| 44 | + assert original_split.status == fetched_split.status |
| 45 | + assert original_split.change_number == fetched_split.change_number |
| 46 | + assert original_split.killed == fetched_split.killed |
| 47 | + assert original_split.default_treatment == fetched_split.default_treatment |
| 48 | + for index, original_condition in enumerate(original_split.conditions): |
| 49 | + fetched_condition = fetched_split.conditions[index] |
| 50 | + assert original_condition.label == fetched_condition.label |
| 51 | + assert original_condition.condition_type == fetched_condition.condition_type |
| 52 | + assert len(original_condition.matchers) == len(fetched_condition.matchers) |
| 53 | + assert len(original_condition.partitions) == len(fetched_condition.partitions) |
| 54 | + |
| 55 | + adapter.set(storage._split_till_prefix, data['till']) |
| 56 | + assert storage.get_change_number() == data['till'] |
| 57 | + |
| 58 | + assert storage.is_valid_traffic_type('user') is True |
| 59 | + assert storage.is_valid_traffic_type('account') is True |
| 60 | + assert storage.is_valid_traffic_type('anything-else') is False |
| 61 | + |
| 62 | + finally: |
| 63 | + to_delete = [ |
| 64 | + "SPLITIO.split.sample_feature", |
| 65 | + "SPLITIO.splits.till", |
| 66 | + "SPLITIO.split.all_feature", |
| 67 | + "SPLITIO.split.killed_feature", |
| 68 | + "SPLITIO.split.Risk_Max_Deductible", |
| 69 | + "SPLITIO.split.whitelist_feature", |
| 70 | + "SPLITIO.split.regex_test", |
| 71 | + "SPLITIO.split.boolean_test", |
| 72 | + "SPLITIO.split.dependency_test", |
| 73 | + "SPLITIO.trafficType.user", |
| 74 | + "SPLITIO.trafficType.account" |
| 75 | + ] |
| 76 | + for item in to_delete: |
| 77 | + adapter.delete(item) |
| 78 | + |
| 79 | + storage = PluggableSplitStorage(adapter) |
| 80 | + assert storage.is_valid_traffic_type('user') is False |
| 81 | + assert storage.is_valid_traffic_type('account') is False |
| 82 | + |
| 83 | + def test_get_all(self): |
| 84 | + """Test get all names & splits.""" |
| 85 | + adapter = StorageMockAdapter() |
| 86 | + try: |
| 87 | + storage = PluggableSplitStorage(adapter) |
| 88 | + split_fn = os.path.join(os.path.dirname(__file__), 'files', 'split_Changes.json') |
| 89 | + with open(split_fn, 'r') as flo: |
| 90 | + data = json.loads(flo.read()) |
| 91 | + for split in data['splits']: |
| 92 | + adapter.set(storage._prefix.format(split_name=split['name']), split) |
| 93 | + adapter.increment(storage._traffic_type_prefix.format(traffic_type_name=split['trafficTypeName']), 1) |
| 94 | + adapter.set(storage._split_till_prefix, data['till']) |
| 95 | + |
| 96 | + split_objects = [splits.from_raw(raw) for raw in data['splits']] |
| 97 | + original_splits = {split.name: split for split in split_objects} |
| 98 | + fetched_names = storage.get_split_names() |
| 99 | + fetched_splits = {split.name: split for split in storage.get_all_splits()} |
| 100 | + assert set(fetched_names) == set(fetched_splits.keys()) |
| 101 | + |
| 102 | + for original_split in original_splits.values(): |
| 103 | + fetched_split = fetched_splits[original_split.name] |
| 104 | + assert original_split.traffic_type_name == fetched_split.traffic_type_name |
| 105 | + assert original_split.seed == fetched_split.seed |
| 106 | + assert original_split.algo == fetched_split.algo |
| 107 | + assert original_split.status == fetched_split.status |
| 108 | + assert original_split.change_number == fetched_split.change_number |
| 109 | + assert original_split.killed == fetched_split.killed |
| 110 | + assert original_split.default_treatment == fetched_split.default_treatment |
| 111 | + for index, original_condition in enumerate(original_split.conditions): |
| 112 | + fetched_condition = fetched_split.conditions[index] |
| 113 | + assert original_condition.label == fetched_condition.label |
| 114 | + assert original_condition.condition_type == fetched_condition.condition_type |
| 115 | + assert len(original_condition.matchers) == len(fetched_condition.matchers) |
| 116 | + assert len(original_condition.partitions) == len(fetched_condition.partitions) |
| 117 | + finally: |
| 118 | + [adapter.delete(key) for key in ['SPLITIO.split.sample_feature', |
| 119 | + 'SPLITIO.splits.till', |
| 120 | + 'SPLITIO.split.all_feature', |
| 121 | + 'SPLITIO.split.killed_feature', |
| 122 | + 'SPLITIO.split.Risk_Max_Deductible', |
| 123 | + 'SPLITIO.split.whitelist_feature', |
| 124 | + 'SPLITIO.split.regex_test', |
| 125 | + 'SPLITIO.split.boolean_test', |
| 126 | + 'SPLITIO.split.dependency_test']] |
| 127 | + |
| 128 | + |
| 129 | +class PluggableSegmentStorageIntegrationTests(object): |
| 130 | + """Redis Segment storage e2e tests.""" |
| 131 | + |
| 132 | + def test_put_fetch_contains(self): |
| 133 | + """Test storing and retrieving splits in redis.""" |
| 134 | + adapter = StorageMockAdapter() |
| 135 | + try: |
| 136 | + storage = PluggableSegmentStorage(adapter) |
| 137 | + adapter.set(storage._prefix.format(segment_name='some_segment'), {'key1', 'key2', 'key3', 'key4'}) |
| 138 | + adapter.set(storage._segment_till_prefix.format(segment_name='some_segment'), 123) |
| 139 | + assert storage.segment_contains('some_segment', 'key0') is False |
| 140 | + assert storage.segment_contains('some_segment', 'key1') is True |
| 141 | + assert storage.segment_contains('some_segment', 'key2') is True |
| 142 | + assert storage.segment_contains('some_segment', 'key3') is True |
| 143 | + assert storage.segment_contains('some_segment', 'key4') is True |
| 144 | + assert storage.segment_contains('some_segment', 'key5') is False |
| 145 | + |
| 146 | + fetched = storage.get('some_segment') |
| 147 | + assert fetched.keys == set(['key1', 'key2', 'key3', 'key4']) |
| 148 | + assert fetched.change_number == 123 |
| 149 | + finally: |
| 150 | + adapter.delete('SPLITIO.segment.some_segment') |
| 151 | + adapter.delete('SPLITIO.segment.some_segment.till') |
| 152 | + |
| 153 | + |
| 154 | +class PluggableImpressionsStorageIntegrationTests(object): |
| 155 | + """Pluggable Impressions storage e2e tests.""" |
| 156 | + |
| 157 | + def _put_impressions(self, adapter, metadata): |
| 158 | + storage = PluggableImpressionsStorage(adapter, metadata) |
| 159 | + storage.put([ |
| 160 | + impressions.Impression('key1', 'feature1', 'on', 'l1', 123456, 'b1', 321654), |
| 161 | + impressions.Impression('key2', 'feature1', 'on', 'l1', 123456, 'b1', 321654), |
| 162 | + impressions.Impression('key3', 'feature1', 'on', 'l1', 123456, 'b1', 321654) |
| 163 | + ]) |
| 164 | + |
| 165 | + |
| 166 | + def test_put_fetch_contains(self): |
| 167 | + """Test storing and retrieving splits in redis.""" |
| 168 | + adapter = StorageMockAdapter() |
| 169 | + try: |
| 170 | + self._put_impressions(adapter, get_metadata({})) |
| 171 | + |
| 172 | + imps = adapter.get('SPLITIO.impressions') |
| 173 | + assert len(imps) == 3 |
| 174 | + for rawImpression in imps: |
| 175 | + impression = json.loads(rawImpression) |
| 176 | + assert impression['m']['i'] != 'NA' |
| 177 | + assert impression['m']['n'] != 'NA' |
| 178 | + finally: |
| 179 | + adapter.delete('SPLITIO.impressions') |
| 180 | + |
| 181 | + def test_put_fetch_contains_ip_address_disabled(self): |
| 182 | + """Test storing and retrieving splits in redis.""" |
| 183 | + adapter = StorageMockAdapter() |
| 184 | + try: |
| 185 | + cfg = DEFAULT_CONFIG.copy() |
| 186 | + cfg.update({'IPAddressesEnabled': False}) |
| 187 | + self._put_impressions(adapter, get_metadata(cfg)) |
| 188 | + |
| 189 | + imps = adapter.get('SPLITIO.impressions') |
| 190 | + assert len(imps) == 3 |
| 191 | + for rawImpression in imps: |
| 192 | + impression = json.loads(rawImpression) |
| 193 | + assert impression['m']['i'] == 'NA' |
| 194 | + assert impression['m']['n'] == 'NA' |
| 195 | + finally: |
| 196 | + adapter.delete('SPLITIO.impressions') |
| 197 | + |
| 198 | + |
| 199 | +class PluggableEventsStorageIntegrationTests(object): |
| 200 | + """Redis Events storage e2e tests.""" |
| 201 | + def _put_events(self, adapter, metadata): |
| 202 | + storage = PluggableEventsStorage(adapter, metadata) |
| 203 | + storage.put([ |
| 204 | + events.EventWrapper( |
| 205 | + event=events.Event('key1', 'user', 'purchase', 3.5, 123456, None), |
| 206 | + size=1024, |
| 207 | + ), |
| 208 | + events.EventWrapper( |
| 209 | + event=events.Event('key2', 'user', 'purchase', 3.5, 123456, None), |
| 210 | + size=1024, |
| 211 | + ), |
| 212 | + events.EventWrapper( |
| 213 | + event=events.Event('key3', 'user', 'purchase', 3.5, 123456, None), |
| 214 | + size=1024, |
| 215 | + ), |
| 216 | + ]) |
| 217 | + |
| 218 | + def test_put_fetch_contains(self): |
| 219 | + """Test storing and retrieving splits in redis.""" |
| 220 | + adapter = StorageMockAdapter() |
| 221 | + try: |
| 222 | + self._put_events(adapter, get_metadata({})) |
| 223 | + evts = adapter.get('SPLITIO.events') |
| 224 | + assert len(evts) == 3 |
| 225 | + for rawEvent in evts: |
| 226 | + event = json.loads(rawEvent) |
| 227 | + assert event['m']['i'] != 'NA' |
| 228 | + assert event['m']['n'] != 'NA' |
| 229 | + finally: |
| 230 | + adapter.delete('SPLITIO.events') |
| 231 | + |
| 232 | + def test_put_fetch_contains_ip_address_disabled(self): |
| 233 | + """Test storing and retrieving splits in redis.""" |
| 234 | + adapter = StorageMockAdapter() |
| 235 | + try: |
| 236 | + cfg = DEFAULT_CONFIG.copy() |
| 237 | + cfg.update({'IPAddressesEnabled': False}) |
| 238 | + self._put_events(adapter, get_metadata(cfg)) |
| 239 | + |
| 240 | + evts = adapter.get('SPLITIO.events') |
| 241 | + assert len(evts) == 3 |
| 242 | + for rawEvent in evts: |
| 243 | + event = json.loads(rawEvent) |
| 244 | + assert event['m']['i'] == 'NA' |
| 245 | + assert event['m']['n'] == 'NA' |
| 246 | + finally: |
| 247 | + adapter.delete('SPLITIO.events') |
0 commit comments