Merge pull request #4027 from AlanCoding/ryan_hosts_36

[alan] use a computed inventory field for task impact math
This commit is contained in:
Alan Rominger
2019-12-09 14:51:02 -05:00
committed by GitHub
3 changed files with 24 additions and 4 deletions

View File

@@ -634,7 +634,7 @@ class Job(UnifiedJob, JobOptions, SurveyJobMixin, JobNotificationMixin, TaskMana
else: else:
# If for some reason we can't count the hosts then lets assume the impact as forks # If for some reason we can't count the hosts then lets assume the impact as forks
if self.inventory is not None: if self.inventory is not None:
count_hosts = self.inventory.hosts.count() count_hosts = self.inventory.total_hosts
if self.job_slice_count > 1: if self.job_slice_count > 1:
# Integer division intentional # Integer division intentional
count_hosts = (count_hosts + self.job_slice_count - self.job_slice_number) // self.job_slice_count count_hosts = (count_hosts + self.job_slice_count - self.job_slice_number) // self.job_slice_count

View File

@@ -122,6 +122,22 @@ def project_playbooks():
mocked.start() 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 @pytest.fixture
@mock.patch.object(Project, "update", lambda self, **kwargs: None) @mock.patch.object(Project, "update", lambda self, **kwargs: None)
def project(instance, organization): def project(instance, organization):

View File

@@ -281,15 +281,18 @@ class TestTaskImpact:
return job return job
return r 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 = 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 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 = 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 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 # factory creates on host per slice
workflow_job = slice_job_factory(3, jt_kwargs={'forks': 50}, spawn=True) workflow_job = slice_job_factory(3, jt_kwargs={'forks': 50}, spawn=True)
# arrange the jobs by their number # 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']) len(jobs[0].inventory.get_script_data(slice_number=i + 1, slice_count=3)['all']['hosts'])
for i in range(3) for i in range(3)
] == [2, 1, 1] ] == [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] assert [job.task_impact for job in jobs] == [3, 2, 2]