diff --git a/awx/main/tests/functional/utils/test_execution_environments.py b/awx/main/tests/functional/utils/test_execution_environments.py new file mode 100644 index 0000000000..15bd67b202 --- /dev/null +++ b/awx/main/tests/functional/utils/test_execution_environments.py @@ -0,0 +1,46 @@ +import pytest + +from django.conf import settings +from django.test.utils import override_settings + +from awx.main.models.execution_environments import ExecutionEnvironment +from awx.main.utils.execution_environments import get_default_execution_environment +from awx.main.management.commands.register_default_execution_environments import Command + + +@pytest.fixture +def set_up_defaults(): + Command().handle() + + +@pytest.mark.django_db +def test_default_to_jobs_default(set_up_defaults, organization): + """Under normal operation, the default EE should be from the list of global job EEs + which are populated by the installer + """ + # Fill in some other unrelated EEs + ExecutionEnvironment.objects.create(name='Steves environment', image='quay.io/ansible/awx-ee') + ExecutionEnvironment(name=settings.GLOBAL_JOB_EXECUTION_ENVIRONMENTS[0]['name'], image='quay.io/ansible/awx-ee', organization=organization) + default_ee = get_default_execution_environment() + assert default_ee.image == settings.GLOBAL_JOB_EXECUTION_ENVIRONMENTS[0]['image'] + assert default_ee.name == settings.GLOBAL_JOB_EXECUTION_ENVIRONMENTS[0]['name'] + + +@pytest.mark.django_db +def test_default_to_control_plane(set_up_defaults): + """If all of the job execution environments are job execution environments have gone missing + then it will refuse to use the control plane execution environment as the default + """ + for ee in ExecutionEnvironment.objects.all(): + if ee.name == 'Control Plane Execution Environment': + continue + ee.delete() + assert get_default_execution_environment() is None + + +@pytest.mark.django_db +def test_user_default(set_up_defaults): + """If superuser has configured a default, then their preference should come first, of course""" + ee = ExecutionEnvironment.objects.create(name='Steves environment', image='quay.io/ansible/awx-ee') + with override_settings(DEFAULT_EXECUTION_ENVIRONMENT=ee): + assert get_default_execution_environment() == ee diff --git a/awx/main/utils/execution_environments.py b/awx/main/utils/execution_environments.py index 5bc01d879a..c218c72802 100644 --- a/awx/main/utils/execution_environments.py +++ b/awx/main/utils/execution_environments.py @@ -13,6 +13,11 @@ def get_control_plane_execution_environment(): def get_default_execution_environment(): if settings.DEFAULT_EXECUTION_ENVIRONMENT is not None: return settings.DEFAULT_EXECUTION_ENVIRONMENT + installed_default = ExecutionEnvironment.objects.filter( + image__in=[ee['image'] for ee in settings.GLOBAL_JOB_EXECUTION_ENVIRONMENTS], organization=None, managed_by_tower=False + ).first() + if installed_default: + return installed_default return ExecutionEnvironment.objects.filter(organization=None, managed_by_tower=False).first()