-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathtest_feature_flags.py
589 lines (522 loc) · 21.6 KB
/
test_feature_flags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
from typing import Dict, List, Optional
import pytest
from botocore.config import Config
from aws_lambda_powertools.utilities.feature_flags import ConfigurationStoreError, schema
from aws_lambda_powertools.utilities.feature_flags.appconfig import AppConfigStore
from aws_lambda_powertools.utilities.feature_flags.exceptions import StoreClientError
from aws_lambda_powertools.utilities.feature_flags.feature_flags import FeatureFlags
from aws_lambda_powertools.utilities.feature_flags.schema import RuleAction
from aws_lambda_powertools.utilities.parameters import GetParameterError
@pytest.fixture(scope="module")
def config():
return Config(region_name="us-east-1")
def init_feature_flags(
mocker, mock_schema: Dict, config: Config, envelope: str = "", jmespath_options: Optional[Dict] = None
) -> FeatureFlags:
mocked_get_conf = mocker.patch("aws_lambda_powertools.utilities.parameters.AppConfigProvider.get")
mocked_get_conf.return_value = mock_schema
app_conf_fetcher = AppConfigStore(
environment="test_env",
application="test_app",
name="test_conf_name",
max_age=600,
sdk_config=config,
envelope=envelope,
jmespath_options=jmespath_options,
)
feature_flags: FeatureFlags = FeatureFlags(store=app_conf_fetcher)
return feature_flags
def init_fetcher_side_effect(mocker, config: Config, side_effect) -> AppConfigStore:
mocked_get_conf = mocker.patch("aws_lambda_powertools.utilities.parameters.AppConfigProvider.get")
mocked_get_conf.side_effect = side_effect
return AppConfigStore(
environment="env",
application="application",
name="conf",
max_age=1,
sdk_config=config,
)
# this test checks that we get correct value of feature that exists in the schema.
# we also don't send an empty context dict in this case
def test_flags_rule_does_not_match(mocker, config):
expected_value = True
mocked_app_config_schema = {
"my_feature": {
"default": expected_value,
"rules": {
"tenant id equals 345345435": {
"when_match": False,
"conditions": [
{
"action": RuleAction.EQUALS.value,
"key": "tenant_id",
"value": "345345435",
}
],
}
},
}
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
toggle = feature_flags.evaluate(name="my_feature", context={}, default=False)
assert toggle == expected_value
# this test checks that if you try to get a feature that doesn't exist in the schema,
# you get the default value of False that was sent to the evaluate API
def test_flags_no_conditions_feature_does_not_exist(mocker, config):
expected_value = False
mocked_app_config_schema = {"my_fake_feature": {"default": True}}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
toggle = feature_flags.evaluate(name="my_feature", context={}, default=expected_value)
assert toggle == expected_value
# check that feature match works when they are no rules and we send context.
# default value is False but the feature has a True default_value.
def test_flags_no_rules(mocker, config):
expected_value = True
mocked_app_config_schema = {"my_feature": {"default": expected_value}}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
toggle = feature_flags.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
assert toggle == expected_value
# check a case where the feature exists but the rule doesn't match so we revert to the default value of the feature
def test_flags_conditions_no_match(mocker, config):
expected_value = True
mocked_app_config_schema = {
"my_feature": {
"default": expected_value,
"rules": {
"tenant id equals 345345435": {
"when_match": False,
"conditions": [
{
"action": RuleAction.EQUALS.value,
"key": "tenant_id",
"value": "345345435",
}
],
}
},
}
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
toggle = feature_flags.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
assert toggle == expected_value
# check that a rule can match when it has multiple conditions, see rule name for further explanation
def test_flags_conditions_rule_not_match_multiple_conditions_match_only_one_condition(mocker, config):
expected_value = False
tenant_id_val = "6"
username_val = "a"
mocked_app_config_schema = {
"my_feature": {
"default": expected_value,
"rules": {
"tenant id equals 6 and username is a": {
"when_match": True,
"conditions": [
{
"action": RuleAction.EQUALS.value, # this condition matches
"key": "tenant_id",
"value": tenant_id_val,
},
{
"action": RuleAction.EQUALS.value, # this condition does not
"key": "username",
"value": "bbb",
},
],
}
},
}
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
toggle = feature_flags.evaluate(
name="my_feature",
context={
"tenant_id": tenant_id_val,
"username": username_val,
},
default=True,
)
assert toggle == expected_value
def test_flags_conditions_rule_match_equal_multiple_conditions(mocker, config):
expected_value = False
tenant_id_val = "6"
username_val = "a"
mocked_app_config_schema = {
"my_feature": {
"default": True,
"rules": {
"tenant id equals 6 and username is a": {
"when_match": expected_value,
"conditions": [
{
"action": RuleAction.EQUALS.value, # this rule will match, it has multiple conditions
"key": "tenant_id",
"value": tenant_id_val,
},
{
"action": RuleAction.EQUALS.value,
"key": "username",
"value": username_val,
},
],
}
},
}
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
toggle = feature_flags.evaluate(
name="my_feature",
context={
"tenant_id": tenant_id_val,
"username": username_val,
},
default=True,
)
assert toggle == expected_value
# check a case when rule doesn't match and it has multiple conditions,
# different tenant id causes the rule to not match.
# default value of the feature in this case is True
def test_flags_conditions_no_rule_match_equal_multiple_conditions(mocker, config):
expected_val = True
mocked_app_config_schema = {
"my_feature": {
"default": expected_val,
"rules": {
# rule will not match
"tenant id equals 645654 and username is a": {
"when_match": False,
"conditions": [
{
"action": RuleAction.EQUALS.value,
"key": "tenant_id",
"value": "645654",
},
{
"action": RuleAction.EQUALS.value,
"key": "username",
"value": "a",
},
],
}
},
}
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
toggle = feature_flags.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
assert toggle == expected_val
# check rule match for multiple of action types
def test_flags_conditions_rule_match_multiple_actions_multiple_rules_multiple_conditions(mocker, config):
expected_value_first_check = True
expected_value_second_check = False
expected_value_third_check = False
expected_value_fourth_case = False
mocked_app_config_schema = {
"my_feature": {
"default": expected_value_third_check,
"rules": {
"tenant id equals 6 and username startswith a": {
"when_match": expected_value_first_check,
"conditions": [
{
"action": RuleAction.EQUALS.value,
"key": "tenant_id",
"value": "6",
},
{
"action": RuleAction.STARTSWITH.value,
"key": "username",
"value": "a",
},
],
},
"tenant id equals 4446 and username startswith a and endswith z": {
"when_match": expected_value_second_check,
"conditions": [
{
"action": RuleAction.EQUALS.value,
"key": "tenant_id",
"value": "4446",
},
{
"action": RuleAction.STARTSWITH.value,
"key": "username",
"value": "a",
},
{
"action": RuleAction.ENDSWITH.value,
"key": "username",
"value": "z",
},
],
},
},
}
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
# match first rule
toggle = feature_flags.evaluate(name="my_feature", context={"tenant_id": "6", "username": "abcd"}, default=False)
assert toggle == expected_value_first_check
# match second rule
toggle = feature_flags.evaluate(name="my_feature", context={"tenant_id": "4446", "username": "az"}, default=False)
assert toggle == expected_value_second_check
# match no rule
toggle = feature_flags.evaluate(
name="my_feature", context={"tenant_id": "11114446", "username": "ab"}, default=False
)
assert toggle == expected_value_third_check
# feature doesn't exist
toggle = feature_flags.evaluate(
name="my_fake_feature",
context={"tenant_id": "11114446", "username": "ab"},
default=expected_value_fourth_case,
)
assert toggle == expected_value_fourth_case
# check a case where the feature exists but the rule doesn't match so we revert to the default value of the feature
def test_flags_match_rule_with_in_action(mocker, config):
expected_value = True
mocked_app_config_schema = {
"my_feature": {
"default": False,
"rules": {
"tenant id is contained in [6, 2]": {
"when_match": expected_value,
"conditions": [
{
"action": RuleAction.IN.value,
"key": "tenant_id",
"value": ["6", "2"],
}
],
}
},
}
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
toggle = feature_flags.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
assert toggle == expected_value
def test_flags_no_match_rule_with_in_action(mocker, config):
expected_value = False
mocked_app_config_schema = {
"my_feature": {
"default": expected_value,
"rules": {
"tenant id is contained in [8, 2]": {
"when_match": True,
"conditions": [
{
"action": RuleAction.IN.value,
"key": "tenant_id",
"value": ["8", "2"],
}
],
}
},
}
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
toggle = feature_flags.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
assert toggle == expected_value
def test_flags_match_rule_with_not_in_action(mocker, config):
expected_value = True
mocked_app_config_schema = {
"my_feature": {
"default": False,
"rules": {
"tenant id is contained in [8, 2]": {
"when_match": expected_value,
"conditions": [
{
"action": RuleAction.NOT_IN.value,
"key": "tenant_id",
"value": ["10", "4"],
}
],
}
},
}
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
toggle = feature_flags.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
assert toggle == expected_value
def test_flags_no_match_rule_with_not_in_action(mocker, config):
expected_value = False
mocked_app_config_schema = {
"my_feature": {
"default": expected_value,
"rules": {
"tenant id is contained in [8, 2]": {
"when_match": True,
"conditions": [
{
"action": RuleAction.NOT_IN.value,
"key": "tenant_id",
"value": ["6", "4"],
}
],
}
},
}
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
toggle = feature_flags.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
assert toggle == expected_value
def test_multiple_features_enabled(mocker, config):
expected_value = ["my_feature", "my_feature2"]
mocked_app_config_schema = {
"my_feature": {
"default": False,
"rules": {
"tenant id is contained in [6, 2]": {
"when_match": True,
"conditions": [
{
"action": RuleAction.IN.value,
"key": "tenant_id",
"value": ["6", "2"],
}
],
}
},
},
"my_feature2": {
"default": True,
},
"my_feature3": {
"default": False,
},
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
enabled_list: List[str] = feature_flags.get_enabled_features(context={"tenant_id": "6", "username": "a"})
assert enabled_list == expected_value
def test_multiple_features_only_some_enabled(mocker, config):
expected_value = ["my_feature", "my_feature2", "my_feature4"]
mocked_app_config_schema = {
"my_feature": { # rule will match here, feature is enabled due to rule match
"default": False,
"rules": {
"tenant id is contained in [6, 2]": {
"when_match": True,
"conditions": [
{
"action": RuleAction.IN.value,
"key": "tenant_id",
"value": ["6", "2"],
}
],
}
},
},
"my_feature2": {
"default": True,
},
"my_feature3": {
"default": False,
},
# rule will not match here, feature is enabled by default
"my_feature4": {
"default": True,
"rules": {
"tenant id equals 7": {
"when_match": False,
"conditions": [
{
"action": RuleAction.EQUALS.value,
"key": "tenant_id",
"value": "7",
}
],
}
},
},
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
enabled_list: List[str] = feature_flags.get_enabled_features(context={"tenant_id": "6", "username": "a"})
assert enabled_list == expected_value
def test_get_feature_toggle_handles_error(mocker, config):
# GIVEN a schema fetch that raises a ConfigurationStoreError
schema_fetcher = init_fetcher_side_effect(mocker, config, GetParameterError())
feature_flags = FeatureFlags(schema_fetcher)
# WHEN calling evaluate
toggle = feature_flags.evaluate(name="Foo", default=False)
# THEN handle the error and return the default
assert toggle is False
def test_get_all_enabled_feature_flags_handles_error(mocker, config):
# GIVEN a schema fetch that raises a ConfigurationStoreError
schema_fetcher = init_fetcher_side_effect(mocker, config, GetParameterError())
feature_flags = FeatureFlags(schema_fetcher)
# WHEN calling get_enabled_features
flags = feature_flags.get_enabled_features(context=None)
# THEN handle the error and return an empty list
assert flags == []
def test_app_config_get_parameter_err(mocker, config):
# GIVEN an appconfig with a missing config
app_conf_fetcher = init_fetcher_side_effect(mocker, config, GetParameterError())
# WHEN calling get_configuration
with pytest.raises(ConfigurationStoreError) as err:
app_conf_fetcher.get_configuration()
# THEN raise ConfigurationStoreError error
assert "AWS AppConfig configuration" in str(err.value)
def test_match_by_action_no_matching_action(mocker, config):
# GIVEN an unsupported action
feature_flags = init_feature_flags(mocker, {}, config)
# WHEN calling _match_by_action
result = feature_flags._match_by_action("Foo", None, "foo")
# THEN default to False
assert result is False
def test_match_by_action_attribute_error(mocker, config):
# GIVEN a startswith action and 2 integer
feature_flags = init_feature_flags(mocker, {}, config)
# WHEN calling _match_by_action
result = feature_flags._match_by_action(RuleAction.STARTSWITH.value, 1, 100)
# THEN swallow the AttributeError and return False
assert result is False
def test_is_rule_matched_no_matches(mocker, config):
# GIVEN an empty list of conditions
rule = {schema.CONDITIONS_KEY: []}
rules_context = {}
feature_flags = init_feature_flags(mocker, {}, config)
# WHEN calling _evaluate_conditions
result = feature_flags._evaluate_conditions(
rule_name="dummy", feature_name="dummy", rule=rule, context=rules_context
)
# THEN return False
assert result is False
def test_features_jmespath_envelope(mocker, config):
expected_value = True
mocked_app_config_schema = {"features": {"my_feature": {"default": expected_value}}}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config, envelope="features")
toggle = feature_flags.evaluate(name="my_feature", context={}, default=False)
assert toggle == expected_value
# test_match_rule_with_equals_action
def test_match_condition_with_dict_value(mocker, config):
expected_value = True
mocked_app_config_schema = {
"my_feature": {
"default": False,
"rules": {
"tenant id is 6 and username is lessa": {
"when_match": expected_value,
"conditions": [
{
"action": RuleAction.EQUALS.value,
"key": "tenant",
"value": {"tenant_id": "6", "username": "lessa"},
}
],
}
},
}
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
ctx = {"tenant": {"tenant_id": "6", "username": "lessa"}}
toggle = feature_flags.evaluate(name="my_feature", context=ctx, default=False)
assert toggle == expected_value
def test_get_feature_toggle_propagates_access_denied_error(mocker, config):
# GIVEN a schema fetch that raises a StoreClientError
# due to client invalid permissions to fetch from the store
err = "An error occurred (AccessDeniedException) when calling the GetConfiguration operation"
schema_fetcher = init_fetcher_side_effect(mocker, config, GetParameterError(err))
feature_flags = FeatureFlags(schema_fetcher)
# WHEN calling evaluate
# THEN raise StoreClientError error
with pytest.raises(StoreClientError, match="AccessDeniedException") as err:
feature_flags.evaluate(name="Foo", default=False)