From 479119f3ce923c4a7585367e8facfd6c48484fb6 Mon Sep 17 00:00:00 2001 From: Wayne Witzel III Date: Tue, 14 Jun 2016 13:39:10 -0400 Subject: [PATCH 1/3] add more task unit tests --- awx/main/tasks.py | 1 + awx/main/tests/unit/test_tasks.py | 35 ++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/awx/main/tasks.py b/awx/main/tasks.py index f174ffe37f..2814848700 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -46,6 +46,7 @@ from django.contrib.auth.models import User # AWX from awx.main.constants import CLOUD_PROVIDERS from awx.main.models import * # noqa +from awx.main.models import UnifiedJob from awx.main.models.label import Label from awx.main.queue import FifoQueue from awx.main.conf import tower_settings diff --git a/awx/main/tests/unit/test_tasks.py b/awx/main/tests/unit/test_tasks.py index 1cbd9fddf8..58f05eaa0a 100644 --- a/awx/main/tests/unit/test_tasks.py +++ b/awx/main/tests/unit/test_tasks.py @@ -1,4 +1,14 @@ -from awx.main.tasks import run_label_cleanup +import pytest + +from awx.main.models import ( + UnifiedJob, + Notification, +) + +from awx.main.tasks import ( + run_label_cleanup, + send_notifications, +) def test_run_label_cleanup(mocker): qs = mocker.Mock(**{'count.return_value': 3, 'delete.return_value': None}) @@ -10,3 +20,26 @@ def test_run_label_cleanup(mocker): qs.delete.assert_called_with() assert 3 == ret +def test_send_notifications_not_list(): + with pytest.raises(TypeError): + send_notifications(None) + +def test_send_notifications_job_id(mocker): + with mocker.patch('awx.main.models.UnifiedJob.objects.get'): + send_notifications([], job_id=1) + assert UnifiedJob.objects.get.called + assert UnifiedJob.objects.get.called_with(id=1) + +def test_send_notifications_list(mocker): + mock_job = mocker.MagicMock(spec=UnifiedJob) + with mocker.patch('awx.main.models.UnifiedJob.objects.get', return_value=mock_job): + mock_notification = mocker.MagicMock(spec=Notification, subject="test") + with mocker.patch('awx.main.models.Notification.objects.get', return_value=mock_notification): + send_notifications([1,2], job_id=1) + assert Notification.objects.get.call_count == 2 + assert mock_notification.status == "successful" + assert mock_notification.save.called + + assert mock_job.notifications.add.called + assert mock_job.notifications.add.called_with(mock_notification) + From 32d781175dc9d15f7ab4864af0b9341ac50cbe5d Mon Sep 17 00:00:00 2001 From: Wayne Witzel III Date: Wed, 29 Jun 2016 12:08:42 -0400 Subject: [PATCH 2/3] add and update task unit tests --- awx/main/tests/unit/test_tasks.py | 39 ++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/awx/main/tests/unit/test_tasks.py b/awx/main/tests/unit/test_tasks.py index 58f05eaa0a..338a49e3e3 100644 --- a/awx/main/tests/unit/test_tasks.py +++ b/awx/main/tests/unit/test_tasks.py @@ -1,4 +1,5 @@ import pytest +from contextlib import contextmanager from awx.main.models import ( UnifiedJob, @@ -8,8 +9,18 @@ from awx.main.models import ( from awx.main.tasks import ( run_label_cleanup, send_notifications, + run_administrative_checks, ) +from awx.main.task_engine import TaskSerializer + + +@contextmanager +def apply_patches(_patches): + [p.start() for p in _patches] + yield + [p.stop() for p in _patches] + def test_run_label_cleanup(mocker): qs = mocker.Mock(**{'count.return_value': 3, 'delete.return_value': None}) mock_label = mocker.patch('awx.main.models.label.Label.get_orphaned_labels',return_value=qs) @@ -31,10 +42,15 @@ def test_send_notifications_job_id(mocker): assert UnifiedJob.objects.get.called_with(id=1) def test_send_notifications_list(mocker): + patches = list() + mock_job = mocker.MagicMock(spec=UnifiedJob) - with mocker.patch('awx.main.models.UnifiedJob.objects.get', return_value=mock_job): - mock_notification = mocker.MagicMock(spec=Notification, subject="test") - with mocker.patch('awx.main.models.Notification.objects.get', return_value=mock_notification): + patches.append(mocker.patch('awx.main.models.UnifiedJob.objects.get', return_value=mock_job)) + + mock_notification = mocker.MagicMock(spec=Notification, subject="test") + patches.append(mocker.patch('awx.main.models.Notification.objects.get', return_value=mock_notification)) + + with apply_patches(patches): send_notifications([1,2], job_id=1) assert Notification.objects.get.call_count == 2 assert mock_notification.status == "successful" @@ -43,3 +59,20 @@ def test_send_notifications_list(mocker): assert mock_job.notifications.add.called assert mock_job.notifications.add.called_with(mock_notification) +def test_run_admin_checks_usage(mocker): + patches = list() + patches.append(mocker.patch('awx.main.tasks.tower_settings')) + patches.append(mocker.patch('awx.main.tasks.User')) + + mock_ts = mocker.Mock(spec=TaskSerializer) + mock_ts.from_database.return_value = {'instance_count': 100, 'current_instances': 91} + patches.append(mocker.patch('awx.main.tasks.TaskSerializer', return_value=mock_ts)) + + mock_sm = mocker.Mock() + patches.append(mocker.patch('awx.main.tasks.send_mail', wraps=mock_sm)) + + with apply_patches(patches): + run_administrative_checks() + assert mock_sm.called + assert '90%' in mock_sm.call_args_list[0][0][0] + assert 'expire' in mock_sm.call_args_list[1][0][0] From 5ef385db2037e3686015f9c1a530877215430e60 Mon Sep 17 00:00:00 2001 From: Wayne Witzel III Date: Wed, 29 Jun 2016 20:17:59 -0400 Subject: [PATCH 3/3] add extra parametrize test and fix indent error --- awx/main/tests/unit/test_tasks.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/awx/main/tests/unit/test_tasks.py b/awx/main/tests/unit/test_tasks.py index 338a49e3e3..ce88f019ce 100644 --- a/awx/main/tests/unit/test_tasks.py +++ b/awx/main/tests/unit/test_tasks.py @@ -51,21 +51,22 @@ def test_send_notifications_list(mocker): patches.append(mocker.patch('awx.main.models.Notification.objects.get', return_value=mock_notification)) with apply_patches(patches): - send_notifications([1,2], job_id=1) - assert Notification.objects.get.call_count == 2 - assert mock_notification.status == "successful" - assert mock_notification.save.called + send_notifications([1,2], job_id=1) + assert Notification.objects.get.call_count == 2 + assert mock_notification.status == "successful" + assert mock_notification.save.called - assert mock_job.notifications.add.called - assert mock_job.notifications.add.called_with(mock_notification) + assert mock_job.notifications.add.called + assert mock_job.notifications.add.called_with(mock_notification) -def test_run_admin_checks_usage(mocker): +@pytest.mark.parametrize("current_instances,call_count", [(91, 2), (89,1)]) +def test_run_admin_checks_usage(mocker, current_instances, call_count): patches = list() patches.append(mocker.patch('awx.main.tasks.tower_settings')) patches.append(mocker.patch('awx.main.tasks.User')) mock_ts = mocker.Mock(spec=TaskSerializer) - mock_ts.from_database.return_value = {'instance_count': 100, 'current_instances': 91} + mock_ts.from_database.return_value = {'instance_count': 100, 'current_instances': current_instances} patches.append(mocker.patch('awx.main.tasks.TaskSerializer', return_value=mock_ts)) mock_sm = mocker.Mock() @@ -74,5 +75,7 @@ def test_run_admin_checks_usage(mocker): with apply_patches(patches): run_administrative_checks() assert mock_sm.called - assert '90%' in mock_sm.call_args_list[0][0][0] - assert 'expire' in mock_sm.call_args_list[1][0][0] + if call_count == 2: + assert '90%' in mock_sm.call_args_list[0][0][0] + else: + assert 'expire' in mock_sm.call_args_list[0][0][0]