From dfc4a0c0e0be757c01dc89a16cc2279b1b35d1cc Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Mon, 9 Dec 2019 10:51:25 -0500 Subject: [PATCH] Compute fields in smart task_impact tests --- awx/main/tests/functional/conftest.py | 16 ++++++++++++++++ .../tests/functional/models/test_unified_job.py | 10 +++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/awx/main/tests/functional/conftest.py b/awx/main/tests/functional/conftest.py index 404f92fdcf..5b545d8e91 100644 --- a/awx/main/tests/functional/conftest.py +++ b/awx/main/tests/functional/conftest.py @@ -122,6 +122,22 @@ def project_playbooks(): mocked.start() +@pytest.fixture +def run_computed_fields_right_away(request): + + def run_me(inventory_id, should_update_hosts=True): + i = Inventory.objects.get(id=inventory_id) + i.update_computed_fields(update_hosts=should_update_hosts) + + mocked = mock.patch( + 'awx.main.signals.update_inventory_computed_fields.delay', + new=run_me + ) + mocked.start() + + request.addfinalizer(mocked.stop) + + @pytest.fixture @mock.patch.object(Project, "update", lambda self, **kwargs: None) def project(instance, organization): diff --git a/awx/main/tests/functional/models/test_unified_job.py b/awx/main/tests/functional/models/test_unified_job.py index 4d0418c498..c1d0967583 100644 --- a/awx/main/tests/functional/models/test_unified_job.py +++ b/awx/main/tests/functional/models/test_unified_job.py @@ -281,15 +281,18 @@ class TestTaskImpact: return job return r - def test_limit_task_impact(self, job_host_limit): + def test_limit_task_impact(self, job_host_limit, run_computed_fields_right_away): job = job_host_limit(5, 2) + job.inventory.refresh_from_db() # FIXME: computed fields operates on reloaded inventory + assert job.inventory.total_hosts == 5 assert job.task_impact == 2 + 1 # forks becomes constraint - def test_host_task_impact(self, job_host_limit): + def test_host_task_impact(self, job_host_limit, run_computed_fields_right_away): job = job_host_limit(3, 5) + job.inventory.refresh_from_db() # FIXME: computed fields operates on reloaded inventory assert job.task_impact == 3 + 1 # hosts becomes constraint - def test_shard_task_impact(self, slice_job_factory): + def test_shard_task_impact(self, slice_job_factory, run_computed_fields_right_away): # factory creates on host per slice workflow_job = slice_job_factory(3, jt_kwargs={'forks': 50}, spawn=True) # arrange the jobs by their number @@ -308,4 +311,5 @@ class TestTaskImpact: len(jobs[0].inventory.get_script_data(slice_number=i + 1, slice_count=3)['all']['hosts']) for i in range(3) ] == [2, 1, 1] + jobs[0].inventory.refresh_from_db() # FIXME: computed fields operates on reloaded inventory assert [job.task_impact for job in jobs] == [3, 2, 2]