Skip to content

Commit a38befc

Browse files
committed
add unit tests
1 parent ed78276 commit a38befc

File tree

14 files changed

+839
-85
lines changed

14 files changed

+839
-85
lines changed

ansible_base/feature_flags/feature_flags.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class AAPFlagNameSchema(TypedDict):
6161
labels=['gateway'],
6262
),
6363
AAPFlagNameSchema(
64-
name="FEATURE_GATEWAY_CREATE_CRC_SERVICE_TYPE",
64+
name="FEATURE_GATEWAY_CREATE_CRC_SERVICE_TYPE_ENABLED",
6565
ui_name="Gateway Create CRC Service Type",
6666
condition="boolean",
6767
value="False",
@@ -71,4 +71,15 @@ class AAPFlagNameSchema(TypedDict):
7171
support_url="https://docs.redhat.com/en/documentation/red_hat_ansible_automation_platform/2.5/",
7272
labels=['gateway'],
7373
),
74+
AAPFlagNameSchema(
75+
name="FEATURE_DISPATCHERD_ENABLED",
76+
ui_name="AAP Dispatcherd",
77+
condition="boolean",
78+
value="False",
79+
visibility="private",
80+
support_level="NOT_FOR_PRODUCTION",
81+
description="TBD",
82+
support_url="https://docs.redhat.com/en/documentation/red_hat_ansible_automation_platform/2.5/",
83+
labels=['eda', 'controller'],
84+
),
7485
]

ansible_base/feature_flags/models/aap_flag.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Meta:
1717
unique_together = ("name", "condition")
1818

1919
def __str__(self):
20-
return "{name} is enabled when {condition} is " "{value}{required}".format(
20+
return "{name} condition {condition} is set to " "{value}{required}".format(
2121
name=self.name,
2222
condition=self.condition,
2323
value=self.value,

ansible_base/feature_flags/utils.py

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,64 +18,64 @@ def create_initial_data(**kwargs): # NOSONAR
1818
"""
1919
Loads in platform feature flags when the server starts
2020
"""
21+
delete_feature_flags()
22+
load_feature_flags()
2123

22-
from ansible_base.feature_flags.models.aap_flag import AAPFlag
2324

24-
def update_feature_flag(existing: AAPFlag, new):
25-
"""
26-
Update only the required fields of the feature flag model.
27-
This is used to ensure that flags can be loaded in when the server starts, with any applicable updates.
28-
"""
29-
existing.support_level = new.get('support_level')
30-
existing.visibility = new.get('visibility')
31-
existing.ui_name = new.get('ui_name')
32-
existing.support_url = new.get('support_url')
33-
existing.required = new.get('required', False)
34-
existing.toggle_type = new.get('toggle_type', 'run-time')
35-
existing.labels = new.get('labels', [])
36-
existing.description = new.get('description', '')
37-
return existing
25+
def update_feature_flag(existing, new):
26+
"""
27+
Update only the required fields of the feature flag model.
28+
This is used to ensure that flags can be loaded in when the server starts, with any applicable updates.
29+
"""
30+
existing.support_level = new.get('support_level')
31+
existing.visibility = new.get('visibility')
32+
existing.ui_name = new.get('ui_name')
33+
existing.support_url = new.get('support_url')
34+
existing.required = new.get('required', False)
35+
existing.toggle_type = new.get('toggle_type', 'run-time')
36+
existing.labels = new.get('labels', [])
37+
existing.description = new.get('description', '')
38+
return existing
3839

39-
def load_feature_flags():
40-
"""
41-
Loads in all feature flags into the database. Updates them if necessary.
42-
"""
43-
feature_flags_model = apps.get_model('dab_feature_flags', 'AAPFlag')
44-
for flag in AAP_FEATURE_FLAGS:
45-
try:
46-
existing_flag = feature_flags_model.objects.filter(name=flag['name'], condition=flag['condition'])
47-
if existing_flag:
48-
feature_flag = update_feature_flag(existing_flag.first(), flag)
49-
else:
50-
if hasattr(settings, flag['name']):
51-
flag['value'] = getattr(settings, flag['name'])
52-
feature_flag = feature_flags_model(**flag)
53-
feature_flag.full_clean()
54-
feature_flag.save()
55-
except ValidationError as e:
56-
# Ignore this error unless better way to bypass this
57-
if e.messages[0] == 'Aap flag with this Name and Condition already exists.':
58-
logger.info(f"Feature flag: {flag['name']} already exists")
59-
else:
60-
error_msg = f"Invalid feature flag: {flag['name']}. Error: {e}"
61-
logger.error(error_msg)
6240

63-
def delete_feature_flags():
64-
"""
65-
If a feature flag has been removed from the platform flags list, delete it from the database.
66-
"""
67-
all_flags = apps.get_model('dab_feature_flags', 'AAPFlag').objects.all()
68-
for flag in all_flags:
69-
found = False
70-
for _flag in AAP_FEATURE_FLAGS:
71-
if flag.name == _flag['name'] and flag.condition == _flag['condition']:
72-
found = True
73-
break
74-
if found:
75-
continue
76-
if not found:
77-
logger.info(f"Deleting feature flag: {flag.name} as it is no longer available as a platform flag")
78-
flag.delete()
41+
def load_feature_flags():
42+
"""
43+
Loads in all feature flags into the database. Updates them if necessary.
44+
"""
45+
feature_flags_model = apps.get_model('dab_feature_flags', 'AAPFlag')
46+
for flag in AAP_FEATURE_FLAGS:
47+
try:
48+
existing_flag = feature_flags_model.objects.filter(name=flag['name'], condition=flag['condition'])
49+
if existing_flag:
50+
feature_flag = update_feature_flag(existing_flag.first(), flag)
51+
else:
52+
if hasattr(settings, flag['name']):
53+
flag['value'] = getattr(settings, flag['name'])
54+
feature_flag = feature_flags_model(**flag)
55+
feature_flag.full_clean()
56+
feature_flag.save()
57+
except ValidationError as e:
58+
# Ignore this error unless better way to bypass this
59+
if e.messages[0] == 'Aap flag with this Name and Condition already exists.':
60+
logger.info(f"Feature flag: {flag['name']} already exists")
61+
else:
62+
error_msg = f"Invalid feature flag: {flag['name']}. Error: {e}"
63+
logger.error(error_msg)
7964

80-
delete_feature_flags()
81-
load_feature_flags()
65+
66+
def delete_feature_flags():
67+
"""
68+
If a feature flag has been removed from the platform flags list, delete it from the database.
69+
"""
70+
all_flags = apps.get_model('dab_feature_flags', 'AAPFlag').objects.all()
71+
for flag in all_flags:
72+
found = False
73+
for _flag in AAP_FEATURE_FLAGS:
74+
if flag.name == _flag['name'] and flag.condition == _flag['condition']:
75+
found = True
76+
break
77+
if found:
78+
continue
79+
if not found:
80+
logger.info(f"Deleting feature flag: {flag.name} as it is no longer available as a platform flag")
81+
flag.delete()

ansible_base/lib/dynamic_config/settings_logic.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,12 @@ def get_mergeable_dab_settings(settings: dict) -> dict: # NOSONAR
306306
if "flags" not in installed_apps:
307307
installed_apps.append('flags')
308308

309-
dab_data['FLAG_SOURCES'] = ('ansible_base.feature_flags.flag_source.AAPFlagSource',)
309+
# After all flags are migrated to database flags, remove settings flag source
310+
# Settings flag source is defined for compatibility until migration is complete
311+
dab_data['FLAG_SOURCES'] = (
312+
'flags.sources.SettingsFlagsSource',
313+
'ansible_base.feature_flags.flag_source.AAPFlagSource',
314+
)
310315

311316
found_template_backend = False
312317
template_context_processor = 'django.template.context_processors.request'

ansible_base/lib/testing/fixtures.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,13 @@ def ldap_authenticator(ldap_configuration):
366366
delete_authenticator(authenticator)
367367

368368

369+
@pytest.fixture
370+
def aap_flags():
371+
from ansible_base.feature_flags.utils import create_initial_data
372+
373+
create_initial_data()
374+
375+
369376
@pytest.fixture
370377
def create_mock_method():
371378
# Creates a function that when called, generates a function that can be used to patch

requirements/requirements_dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pytest-asyncio
1717
pytest-xdist
1818
pytest-cov
1919
pytest-django
20+
pytest-mock
2021
setuptools-scm
2122
sqlparse==0.5.2
2223
psycopg[binary]

test_app/defaults.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -205,29 +205,3 @@
205205
RENAMED_USERNAME_PREFIX = "dab:"
206206

207207
JUST_A_TEST = 41
208-
209-
FLAGS = {
210-
"FEATURE_SOME_PLATFORM_FLAG_ENABLED": [
211-
{
212-
"condition": "boolean",
213-
"value": False,
214-
"required": True,
215-
},
216-
{
217-
"condition": "before date",
218-
"value": '2022-06-01T12:00Z',
219-
},
220-
],
221-
"FEATURE_SOME_PLATFORM_FLAG_FOO_ENABLED": [
222-
{
223-
"condition": "boolean",
224-
"value": False,
225-
},
226-
],
227-
"FEATURE_SOME_PLATFORM_FLAG_BAR_ENABLED": [
228-
{
229-
"condition": "boolean",
230-
"value": True,
231-
},
232-
],
233-
}

test_app/tests/feature_flags/management/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)