From fd5f3a82d28177ef51369ca11cd27aa4527f1ad1 Mon Sep 17 00:00:00 2001 From: beeankha Date: Wed, 12 May 2021 11:52:12 -0400 Subject: [PATCH 1/3] Show error if no Execution Environment is found on project sync/job run --- awx/main/tasks.py | 7 +++++++ awx/main/tests/unit/test_tasks.py | 34 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/awx/main/tasks.py b/awx/main/tasks.py index 6f785b6312..994209aaf2 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -994,6 +994,9 @@ class BaseTask(object): env['AWX_PRIVATE_DATA_DIR'] = private_data_dir + if self.instance.execution_environment is None: + raise RuntimeError('The project could not sync because there is no Execution Environment.') + ee_cred = self.instance.execution_environment.credential if ee_cred: verify_ssl = ee_cred.get_input('verify_ssl') @@ -1709,6 +1712,10 @@ class RunJob(BaseTask): error = _('Job could not start because it does not have a valid project.') self.update_model(job.pk, status='failed', job_explanation=error) raise RuntimeError(error) + elif job.execution_environment is None: + error = _('Job could not start because no Execution Environment could be found.') + self.update_model(job.pk, status='error', job_explanation=error) + raise RuntimeError(error) elif job.project.status in ('error', 'failed'): msg = _('The project revision for this job template is unknown due to a failed update.') job = self.update_model(job.pk, status='failed', job_explanation=msg) diff --git a/awx/main/tests/unit/test_tasks.py b/awx/main/tests/unit/test_tasks.py index 1cd7dd9069..9c3028df58 100644 --- a/awx/main/tests/unit/test_tasks.py +++ b/awx/main/tests/unit/test_tasks.py @@ -1927,3 +1927,37 @@ def test_notification_job_finished(mocker): with mocker.patch('awx.main.models.UnifiedJob.objects.get', mocker.MagicMock(return_value=uj)): tasks.handle_success_and_failure_notifications(1) uj.send_notification_templates.assert_called() + + +@pytest.mark.django_db +class TestNullExecutionEnvironment(TestJobExecution): + def test_job_run_no_ee(self, execution_environment, private_data_dir, job): + org = Organization(pk=1) + proj = Project(pk=1, organization=org) + job = Job(project=proj, organization=org, inventory=Inventory(pk=1)) + job.execution_environment = None + task = tasks.RunJob() + task.instance = job + task.update_model = mock.Mock(return_value=job) + task.model.objects.get = mock.Mock(return_value=job) + + with mock.patch('awx.main.tasks.copy_tree'): + with pytest.raises(RuntimeError) as e: + task.pre_run_hook(job, private_data_dir) + + update_model_call = task.update_model.call_args[1] + assert update_model_call['status'] == 'error' + assert 'Job could not start because no Execution Environment could be found' in str(e.value) + + def test_project_update_no_ee(self, execution_environment, job): + org = Organization(pk=1) + proj = Project(pk=1, organization=org) + project_update = ProjectUpdate(pk=1, project=proj, scm_type='git') + project_update.execution_environment = None + task = tasks.RunProjectUpdate() + task.instance = project_update + + with pytest.raises(RuntimeError) as e: + task.build_env(job, {}) + + assert 'The project could not sync because there is no Execution Environment' in str(e.value) From 19d7f3e34607b2e58dcbf43b84f97ef261e0ddc6 Mon Sep 17 00:00:00 2001 From: beeankha Date: Wed, 12 May 2021 13:30:10 -0400 Subject: [PATCH 2/3] Update unit tests --- awx/main/tests/unit/test_tasks.py | 57 +++++++++++++++---------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/awx/main/tests/unit/test_tasks.py b/awx/main/tests/unit/test_tasks.py index 9c3028df58..4b438effda 100644 --- a/awx/main/tests/unit/test_tasks.py +++ b/awx/main/tests/unit/test_tasks.py @@ -1929,35 +1929,34 @@ def test_notification_job_finished(mocker): uj.send_notification_templates.assert_called() -@pytest.mark.django_db -class TestNullExecutionEnvironment(TestJobExecution): - def test_job_run_no_ee(self, execution_environment, private_data_dir, job): - org = Organization(pk=1) - proj = Project(pk=1, organization=org) - job = Job(project=proj, organization=org, inventory=Inventory(pk=1)) - job.execution_environment = None - task = tasks.RunJob() - task.instance = job - task.update_model = mock.Mock(return_value=job) - task.model.objects.get = mock.Mock(return_value=job) - - with mock.patch('awx.main.tasks.copy_tree'): - with pytest.raises(RuntimeError) as e: - task.pre_run_hook(job, private_data_dir) - - update_model_call = task.update_model.call_args[1] - assert update_model_call['status'] == 'error' - assert 'Job could not start because no Execution Environment could be found' in str(e.value) - - def test_project_update_no_ee(self, execution_environment, job): - org = Organization(pk=1) - proj = Project(pk=1, organization=org) - project_update = ProjectUpdate(pk=1, project=proj, scm_type='git') - project_update.execution_environment = None - task = tasks.RunProjectUpdate() - task.instance = project_update +def test_job_run_no_ee(execution_environment, private_data_dir, job): + org = Organization(pk=1) + proj = Project(pk=1, organization=org) + job = Job(project=proj, organization=org, inventory=Inventory(pk=1)) + job.execution_environment = None + task = tasks.RunJob() + task.instance = job + task.update_model = mock.Mock(return_value=job) + task.model.objects.get = mock.Mock(return_value=job) + with mock.patch('awx.main.tasks.copy_tree'): with pytest.raises(RuntimeError) as e: - task.build_env(job, {}) + task.pre_run_hook(job, private_data_dir) - assert 'The project could not sync because there is no Execution Environment' in str(e.value) + update_model_call = task.update_model.call_args[1] + assert update_model_call['status'] == 'error' + assert 'Job could not start because no Execution Environment could be found' in str(e.value) + + +def test_project_update_no_ee(execution_environment, job): + org = Organization(pk=1) + proj = Project(pk=1, organization=org) + project_update = ProjectUpdate(pk=1, project=proj, scm_type='git') + project_update.execution_environment = None + task = tasks.RunProjectUpdate() + task.instance = project_update + + with pytest.raises(RuntimeError) as e: + task.build_env(job, {}) + + assert 'The project could not sync because there is no Execution Environment' in str(e.value) From 3520a6e066cf69a4ee3b3b79cbfbe855724bcda8 Mon Sep 17 00:00:00 2001 From: beeankha Date: Wed, 12 May 2021 14:00:04 -0400 Subject: [PATCH 3/3] Update fixtures on unit tests --- awx/main/tests/unit/test_tasks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/awx/main/tests/unit/test_tasks.py b/awx/main/tests/unit/test_tasks.py index 4b438effda..82a8e1809f 100644 --- a/awx/main/tests/unit/test_tasks.py +++ b/awx/main/tests/unit/test_tasks.py @@ -1929,7 +1929,7 @@ def test_notification_job_finished(mocker): uj.send_notification_templates.assert_called() -def test_job_run_no_ee(execution_environment, private_data_dir, job): +def test_job_run_no_ee(): org = Organization(pk=1) proj = Project(pk=1, organization=org) job = Job(project=proj, organization=org, inventory=Inventory(pk=1)) @@ -1948,7 +1948,7 @@ def test_job_run_no_ee(execution_environment, private_data_dir, job): assert 'Job could not start because no Execution Environment could be found' in str(e.value) -def test_project_update_no_ee(execution_environment, job): +def test_project_update_no_ee(): org = Organization(pk=1) proj = Project(pk=1, organization=org) project_update = ProjectUpdate(pk=1, project=proj, scm_type='git')