15
15
"""Unit tests for feature flags functionality."""
16
16
17
17
import pytest
18
+ from ansible_base .feature_flags .models import AAPFlag
19
+ from ansible_base .feature_flags .utils import (
20
+ create_initial_data as seed_feature_flags ,
21
+ )
18
22
19
23
from aap_eda .settings import features
20
24
from aap_eda .settings .features import _get_feature
@@ -29,14 +33,10 @@ def clear_feature_cache():
29
33
@pytest .mark .django_db
30
34
def test_get_feature_flag (settings ):
31
35
"""Test getting feature flag values."""
32
- settings .FLAGS = {
33
- settings .DISPATCHERD_FEATURE_FLAG_NAME : [
34
- {"condition" : "boolean" , "value" : True }
35
- ],
36
- settings .ANALYTICS_FEATURE_FLAG_NAME : [
37
- {"condition" : "boolean" , "value" : False }
38
- ],
39
- }
36
+ AAPFlag .objects .all ().delete ()
37
+ setattr (settings , settings .DISPATCHERD_FEATURE_FLAG_NAME , True )
38
+ setattr (settings , settings .ANALYTICS_FEATURE_FLAG_NAME , False )
39
+ seed_feature_flags ()
40
40
41
41
assert features .DISPATCHERD is True
42
42
assert features .ANALYTICS is False
@@ -45,40 +45,38 @@ def test_get_feature_flag(settings):
45
45
@pytest .mark .django_db
46
46
def test_feature_flag_caching (settings ):
47
47
"""Test that feature flag values are properly cached."""
48
- settings .FLAGS = {
49
- settings .DISPATCHERD_FEATURE_FLAG_NAME : [
50
- {"condition" : "boolean" , "value" : True }
51
- ]
52
- }
53
-
48
+ AAPFlag .objects .all ().delete ()
49
+ setattr (settings , settings .DISPATCHERD_FEATURE_FLAG_NAME , True )
50
+ seed_feature_flags ()
54
51
# First access - should cache the value
55
52
assert features .DISPATCHERD is True
56
53
57
54
# Change the underlying flag value
58
- settings . FLAGS [ settings .DISPATCHERD_FEATURE_FLAG_NAME ][ 0 ][ "value" ] = False
59
-
55
+ setattr ( settings , settings .DISPATCHERD_FEATURE_FLAG_NAME , False )
56
+ seed_feature_flags ()
60
57
# Should still get the cached value
61
58
assert features .DISPATCHERD is True
62
59
63
60
64
61
@pytest .mark .django_db
65
62
def test_cache_invalidation (settings ):
66
63
"""Test that cache invalidation works as expected."""
67
- settings .FLAGS = {
68
- settings .DISPATCHERD_FEATURE_FLAG_NAME : [
69
- {"condition" : "boolean" , "value" : True }
70
- ]
71
- }
64
+ AAPFlag .objects .all ().delete ()
65
+ setattr (settings , settings .DISPATCHERD_FEATURE_FLAG_NAME , True )
66
+ seed_feature_flags ()
72
67
73
68
# Populate cache
74
69
assert features .DISPATCHERD is True
75
70
76
71
# Change the flag value and clear cache
77
- settings .FLAGS [settings .DISPATCHERD_FEATURE_FLAG_NAME ][0 ]["value" ] = False
72
+ setattr (settings , settings .DISPATCHERD_FEATURE_FLAG_NAME , False )
73
+ seed_feature_flags ()
78
74
_get_feature .cache_clear ()
79
75
80
- # Should get the new value after cache clear
81
- assert features .DISPATCHERD is False
76
+ # Feature should remain true.
77
+ # If runtime toggle, we should only be able to
78
+ # update the value after toggling it via the platform gateway
79
+ assert features .DISPATCHERD is True
82
80
83
81
84
82
@pytest .mark .django_db
0 commit comments