diff --git a/awx/main/tests/functional/test_api_generics.py b/awx/main/tests/functional/api/test_api_generics.py similarity index 100% rename from awx/main/tests/functional/test_api_generics.py rename to awx/main/tests/functional/api/test_api_generics.py diff --git a/awx/main/tests/functional/test_db_credential.py b/awx/main/tests/functional/models/test_db_credential.py similarity index 100% rename from awx/main/tests/functional/test_db_credential.py rename to awx/main/tests/functional/models/test_db_credential.py diff --git a/awx/main/tests/functional/test_ha.py b/awx/main/tests/functional/models/test_ha.py similarity index 100% rename from awx/main/tests/functional/test_ha.py rename to awx/main/tests/functional/models/test_ha.py diff --git a/awx/main/tests/functional/test_labels.py b/awx/main/tests/functional/rbac/test_rbac_labels.py similarity index 100% rename from awx/main/tests/functional/test_labels.py rename to awx/main/tests/functional/rbac/test_rbac_labels.py diff --git a/awx/main/tests/functional/tasks/test_tasks_jobs.py b/awx/main/tests/functional/tasks/test_tasks_jobs.py new file mode 100644 index 000000000000..a627731bbb25 --- /dev/null +++ b/awx/main/tests/functional/tasks/test_tasks_jobs.py @@ -0,0 +1,27 @@ +import pytest +import os + +from awx.main.tasks.jobs import RunJob +from awx.main.models import Job + + +@pytest.fixture +def scm_revision_file(tmpdir_factory): + # Returns path to temporary testing revision file + revision_file = tmpdir_factory.mktemp('revisions').join('revision.txt') + with open(str(revision_file), 'w') as f: + f.write('1234567890123456789012345678901234567890') + return os.path.join(revision_file.dirname, 'revision.txt') + + +@pytest.mark.django_db +def test_does_not_run_reaped_job(mocker, mock_me): + job = Job.objects.create(status='failed', job_explanation='This job has been reaped.') + mock_run = mocker.patch('awx.main.tasks.jobs.ansible_runner.interface.run') + try: + RunJob().run(job.id) + except Exception: + pass + job.refresh_from_db() + assert job.status == 'failed' + mock_run.assert_not_called() diff --git a/awx/main/tests/functional/test_tasks.py b/awx/main/tests/functional/tasks/test_tasks_system.py similarity index 62% rename from awx/main/tests/functional/test_tasks.py rename to awx/main/tests/functional/tasks/test_tasks_system.py index 8e5305807f0f..6fb3acbfbab7 100644 --- a/awx/main/tests/functional/test_tasks.py +++ b/awx/main/tests/functional/tasks/test_tasks_system.py @@ -1,28 +1,37 @@ -import pytest import os import tempfile import shutil -from awx.main.tasks.jobs import RunJob -from awx.main.tasks.system import CleanupImagesAndFiles, execution_node_health_check -from awx.main.models import Instance, Job +import pytest - -@pytest.fixture -def scm_revision_file(tmpdir_factory): - # Returns path to temporary testing revision file - revision_file = tmpdir_factory.mktemp('revisions').join('revision.txt') - with open(str(revision_file), 'w') as f: - f.write('1234567890123456789012345678901234567890') - return os.path.join(revision_file.dirname, 'revision.txt') +from awx.main.tasks.system import CleanupImagesAndFiles, execution_node_health_check, inspect_established_receptor_connections +from awx.main.models import Instance, Job, ReceptorAddress, InstanceLink @pytest.mark.django_db -@pytest.mark.parametrize('node_type', ('control. hybrid')) -def test_no_worker_info_on_AWX_nodes(node_type): - hostname = 'us-south-3-compute.invalid' - Instance.objects.create(hostname=hostname, node_type=node_type) - assert execution_node_health_check(hostname) is None +class TestLinkState: + @pytest.fixture(autouse=True) + def configure_settings(self, settings): + settings.IS_K8S = True + + def test_inspect_established_receptor_connections(self): + ''' + Change link state from ADDING to ESTABLISHED + if the receptor status KnownConnectionCosts field + has an entry for the source and target node. + ''' + hop1 = Instance.objects.create(hostname='hop1') + hop2 = Instance.objects.create(hostname='hop2') + hop2addr = ReceptorAddress.objects.create(instance=hop2, address='hop2', port=5678) + InstanceLink.objects.create(source=hop1, target=hop2addr, link_state=InstanceLink.States.ADDING) + + # calling with empty KnownConnectionCosts should not change the link state + inspect_established_receptor_connections({"KnownConnectionCosts": {}}) + assert InstanceLink.objects.get(source=hop1, target=hop2addr).link_state == InstanceLink.States.ADDING + + mesh_state = {"KnownConnectionCosts": {"hop1": {"hop2": 1}}} + inspect_established_receptor_connections(mesh_state) + assert InstanceLink.objects.get(source=hop1, target=hop2addr).link_state == InstanceLink.States.ESTABLISHED @pytest.fixture @@ -46,6 +55,14 @@ def mock_job_folder(job_folder_factory): return job_folder_factory() +@pytest.mark.django_db +@pytest.mark.parametrize('node_type', ('control. hybrid')) +def test_no_worker_info_on_AWX_nodes(node_type): + hostname = 'us-south-3-compute.invalid' + Instance.objects.create(hostname=hostname, node_type=node_type) + assert execution_node_health_check(hostname) is None + + @pytest.mark.django_db def test_folder_cleanup_stale_file(mock_job_folder, mock_me): CleanupImagesAndFiles.run() @@ -81,16 +98,3 @@ def test_folder_cleanup_multiple_running_jobs(job_folder_factory, me_inst): CleanupImagesAndFiles.run(grace_period=0) assert [os.path.exists(d) for d in dirs] == [True for i in range(num_jobs)] - - -@pytest.mark.django_db -def test_does_not_run_reaped_job(mocker, mock_me): - job = Job.objects.create(status='failed', job_explanation='This job has been reaped.') - mock_run = mocker.patch('awx.main.tasks.jobs.ansible_runner.interface.run') - try: - RunJob().run(job.id) - except Exception: - pass - job.refresh_from_db() - assert job.status == 'failed' - mock_run.assert_not_called() diff --git a/awx/main/tests/functional/test_linkstate.py b/awx/main/tests/functional/test_linkstate.py deleted file mode 100644 index 478883870a67..000000000000 --- a/awx/main/tests/functional/test_linkstate.py +++ /dev/null @@ -1,30 +0,0 @@ -import pytest - -from awx.main.models import Instance, ReceptorAddress, InstanceLink -from awx.main.tasks.system import inspect_established_receptor_connections - - -@pytest.mark.django_db -class TestLinkState: - @pytest.fixture(autouse=True) - def configure_settings(self, settings): - settings.IS_K8S = True - - def test_inspect_established_receptor_connections(self): - ''' - Change link state from ADDING to ESTABLISHED - if the receptor status KnownConnectionCosts field - has an entry for the source and target node. - ''' - hop1 = Instance.objects.create(hostname='hop1') - hop2 = Instance.objects.create(hostname='hop2') - hop2addr = ReceptorAddress.objects.create(instance=hop2, address='hop2', port=5678) - InstanceLink.objects.create(source=hop1, target=hop2addr, link_state=InstanceLink.States.ADDING) - - # calling with empty KnownConnectionCosts should not change the link state - inspect_established_receptor_connections({"KnownConnectionCosts": {}}) - assert InstanceLink.objects.get(source=hop1, target=hop2addr).link_state == InstanceLink.States.ADDING - - mesh_state = {"KnownConnectionCosts": {"hop1": {"hop2": 1}}} - inspect_established_receptor_connections(mesh_state) - assert InstanceLink.objects.get(source=hop1, target=hop2addr).link_state == InstanceLink.States.ESTABLISHED