diff options
author | Alan Rominger <arominge@redhat.com> | 2022-11-08 22:06:13 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-08 22:06:13 +0100 |
commit | d6004fd2d3105284f579f75bfed235851a2131f2 (patch) | |
tree | 67a37237563b4cec86f003ed0cc1e1534b45c833 | |
parent | Merge pull request #13148 from kialam/fix-host-event-modal-type-error (diff) | |
parent | Refactor test_get_cleanup_task_kwargs_active_jobs and add new test (diff) | |
download | awx-d6004fd2d3105284f579f75bfed235851a2131f2.tar.xz awx-d6004fd2d3105284f579f75bfed235851a2131f2.zip |
Merge pull request #13153 from AlanCoding/cleanup_args
Refactor test_get_cleanup_task_kwargs_active_jobs and add new test
-rw-r--r-- | awx/main/models/ha.py | 11 | ||||
-rw-r--r-- | awx/main/tests/functional/test_instances.py | 20 |
2 files changed, 25 insertions, 6 deletions
diff --git a/awx/main/models/ha.py b/awx/main/models/ha.py index dbeb81dcac..f101a94d7a 100644 --- a/awx/main/models/ha.py +++ b/awx/main/models/ha.py @@ -233,11 +233,12 @@ class Instance(HasPolicyEditsMixin, BaseModel): if not isinstance(vargs.get('grace_period'), int): vargs['grace_period'] = 60 # grace period of 60 minutes, need to set because CLI default will not take effect if 'exclude_strings' not in vargs and vargs.get('file_pattern'): - active_pks = list( - UnifiedJob.objects.filter( - (models.Q(execution_node=self.hostname) | models.Q(controller_node=self.hostname)) & models.Q(status__in=('running', 'waiting')) - ).values_list('pk', flat=True) - ) + active_job_qs = UnifiedJob.objects.filter(status__in=('running', 'waiting')) + if self.node_type == 'execution': + active_job_qs = active_job_qs.filter(execution_node=self.hostname) + else: + active_job_qs = active_job_qs.filter(controller_node=self.hostname) + active_pks = list(active_job_qs.values_list('pk', flat=True)) if active_pks: vargs['exclude_strings'] = [JOB_FOLDER_PREFIX % job_id for job_id in active_pks] if 'remove_images' in vargs or 'image_prune' in vargs: diff --git a/awx/main/tests/functional/test_instances.py b/awx/main/tests/functional/test_instances.py index 8ce6524d38..df6d177868 100644 --- a/awx/main/tests/functional/test_instances.py +++ b/awx/main/tests/functional/test_instances.py @@ -1,7 +1,7 @@ import pytest from unittest import mock -from awx.main.models import AdHocCommand, InventoryUpdate, JobTemplate +from awx.main.models import AdHocCommand, InventoryUpdate, JobTemplate, Job from awx.main.models.activity_stream import ActivityStream from awx.main.models.ha import Instance, InstanceGroup from awx.main.tasks.system import apply_cluster_membership_policies @@ -16,6 +16,24 @@ def test_default_tower_instance_group(default_instance_group, job_factory): @pytest.mark.django_db +@pytest.mark.parametrize('node_type', ('execution', 'control')) +@pytest.mark.parametrize('active', (True, False)) +def test_get_cleanup_task_kwargs_active_jobs(node_type, active): + instance = Instance.objects.create(hostname='foobar', node_type=node_type) + job_kwargs = dict() + job_kwargs['controller_node' if node_type == 'control' else 'execution_node'] = instance.hostname + job_kwargs['status'] = 'running' if active else 'successful' + + job = Job.objects.create(**job_kwargs) + kwargs = instance.get_cleanup_task_kwargs() + + if active: + assert kwargs['exclude_strings'] == [f'awx_{job.pk}_'] + else: + assert 'exclude_strings' not in kwargs + + +@pytest.mark.django_db class TestPolicyTaskScheduling: """Tests make assertions about when the policy task gets scheduled""" |