1515"""Unit tests for feature flags functionality."""
1616
1717import 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+ )
1822
1923from aap_eda .settings import features
2024from aap_eda .settings .features import _get_feature
@@ -29,14 +33,10 @@ def clear_feature_cache():
2933@pytest .mark .django_db
3034def test_get_feature_flag (settings ):
3135 """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 ()
4040
4141 assert features .DISPATCHERD is True
4242 assert features .ANALYTICS is False
@@ -45,40 +45,38 @@ def test_get_feature_flag(settings):
4545@pytest .mark .django_db
4646def test_feature_flag_caching (settings ):
4747 """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 ()
5451 # First access - should cache the value
5552 assert features .DISPATCHERD is True
5653
5754 # 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 ()
6057 # Should still get the cached value
6158 assert features .DISPATCHERD is True
6259
6360
6461@pytest .mark .django_db
6562def test_cache_invalidation (settings ):
6663 """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 ()
7267
7368 # Populate cache
7469 assert features .DISPATCHERD is True
7570
7671 # 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 ()
7874 _get_feature .cache_clear ()
7975
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
8280
8381
8482@pytest .mark .django_db
0 commit comments