Merge pull request #1617 from anoek/unit-test-cleanup

Test cleanup and slow test separation
This commit is contained in:
Akita Noek
2016-04-20 13:18:07 -04:00
11 changed files with 38 additions and 13 deletions

View File

@@ -7,6 +7,8 @@ import os
import subprocess
import tempfile
import mock
import unittest2 as unittest
# Django
from django.conf import settings
@@ -58,6 +60,7 @@ class BaseAdHocCommandTest(BaseJobExecutionTest):
return self.credential
@unittest.skipIf(os.environ.get('SKIP_SLOW_TESTS', False), 'Skipping slow test')
class RunAdHocCommandTest(BaseAdHocCommandTest):
'''
Test cases for RunAdHocCommand celery task.
@@ -375,6 +378,7 @@ class RunAdHocCommandTest(BaseAdHocCommandTest):
def run_pexpect_mock(self, *args, **kwargs):
return 'successful', 0
@unittest.skipIf(os.environ.get('SKIP_SLOW_TESTS', False), 'Skipping slow test')
class AdHocCommandApiTest(BaseAdHocCommandTest):
'''
Test API list/detail views for ad hoc commands.

View File

@@ -0,0 +1,75 @@
# Copyright (c) 2015 Ansible, Inc.
# All Rights Reserved.
import json
from django.test import TestCase
from rest_framework.permissions import AllowAny
from rest_framework.test import APIRequestFactory
from rest_framework.views import APIView
from awx.api.utils.decorators import paginated
class PaginatedDecoratorTests(TestCase):
"""A set of tests for ensuring that the "paginated" decorator works
in the way we expect.
"""
def setUp(self):
self.rf = APIRequestFactory()
# Define an uninteresting view that we can use to test
# that the paginator wraps in the way we expect.
class View(APIView):
permission_classes = (AllowAny,)
@paginated
def get(self, request, limit, ordering, offset):
return ['a', 'b', 'c', 'd', 'e'], 26, None
self.view = View.as_view()
def test_implicit_first_page(self):
"""Establish that if we get an implicit request for the first page
(e.g. no page provided), that it is returned appropriately.
"""
# Create a request, and run the paginated function.
request = self.rf.get('/dummy/', {'page_size': 5})
response = self.view(request)
# Ensure the response looks like what it should.
r = json.loads(response.rendered_content)
self.assertEqual(r['count'], 26)
self.assertEqual(r['next'], '/dummy/?page=2&page_size=5')
self.assertEqual(r['previous'], None)
self.assertEqual(r['results'], ['a', 'b', 'c', 'd', 'e'])
def test_mid_page(self):
"""Establish that if we get a request for a page in the middle, that
the paginator causes next and prev to be set appropriately.
"""
# Create a request, and run the paginated function.
request = self.rf.get('/dummy/', {'page': 3, 'page_size': 5})
response = self.view(request)
# Ensure the response looks like what it should.
r = json.loads(response.rendered_content)
self.assertEqual(r['count'], 26)
self.assertEqual(r['next'], '/dummy/?page=4&page_size=5')
self.assertEqual(r['previous'], '/dummy/?page=2&page_size=5')
self.assertEqual(r['results'], ['a', 'b', 'c', 'd', 'e'])
def test_last_page(self):
"""Establish that if we get a request for the last page, that the
paginator picks up on it and sets `next` to None.
"""
# Create a request, and run the paginated function.
request = self.rf.get('/dummy/', {'page': 6, 'page_size': 5})
response = self.view(request)
# Ensure the response looks like what it should.
r = json.loads(response.rendered_content)
self.assertEqual(r['count'], 26)
self.assertEqual(r['next'], None)
self.assertEqual(r['previous'], '/dummy/?page=5&page_size=5')
self.assertEqual(r['results'], ['a', 'b', 'c', 'd', 'e'])

View File

@@ -0,0 +1,44 @@
# Copyright (c) 2015 Ansible, Inc.
# All Rights Reserved.
from django.conf import settings
from django.test import LiveServerTestCase
from django.test.utils import override_settings
from awx.main.tests.job_base import BaseJobTestMixin
@override_settings(CELERY_ALWAYS_EAGER=True,
CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
ANSIBLE_TRANSPORT='local')
class JobTasksTests(BaseJobTestMixin, LiveServerTestCase):
"""A set of tests to ensure that the job_tasks endpoint, available at
`/api/v1/jobs/{id}/job_tasks/`, works as expected.
"""
def setUp(self):
super(JobTasksTests, self).setUp()
settings.INTERNAL_API_URL = self.live_server_url
def test_tasks_endpoint(self):
"""Establish that the `job_tasks` endpoint shows what we expect,
which is a rollup of information about each of the corresponding
job events.
"""
# Create a job
job = self.make_job(self.jt_ops_east_run, self.user_sue, 'new')
job.signal_start()
# Get the initial job event.
event = job.job_events.get(event='playbook_on_play_start')
# Actually make the request for the job tasks.
with self.current_user(self.user_sue):
url = '/api/v1/jobs/%d/job_tasks/?event_id=%d' % (job.id, event.id)
response = self.get(url)
# Test to make sure we got back what we expected.
result = response['results'][0]
self.assertEqual(result['host_count'], 7)
self.assertEqual(result['changed_count'], 7)
self.assertFalse(result['failed'])
self.assertTrue(result['changed'])

View File

@@ -108,6 +108,7 @@ grandchild
parent
'''
class BaseCommandMixin(object):
'''
Base class for tests that run management commands.
@@ -381,6 +382,7 @@ class CleanupJobsTest(BaseCommandMixin, BaseLiveServerTest):
self.assertFalse(ad_hoc_commands_after)
@unittest.skipIf(os.environ.get('SKIP_SLOW_TESTS', False), 'Skipping slow test')
class CleanupActivityStreamTest(BaseCommandMixin, BaseTest):
'''
Test cases for cleanup_activitystream management command.
@@ -447,6 +449,7 @@ class CleanupActivityStreamTest(BaseCommandMixin, BaseTest):
'create took %0.3fs, cleanup took %0.3fs, expected < %0.3fs' % (create_elapsed, cleanup_elapsed, create_elapsed / 4))
@unittest.skipIf(os.environ.get('SKIP_SLOW_TESTS', False), 'Skipping slow test')
class InventoryImportTest(BaseCommandMixin, BaseLiveServerTest):
'''
Test cases for inventory_import management command.
@@ -935,6 +938,11 @@ class InventoryImportTest(BaseCommandMixin, BaseLiveServerTest):
self.assertNotEqual(new_inv.total_groups, 0)
self.assertElapsedLessThan(60)
@unittest.skipIf(True,
'This test is deprecated and being removed from '
'integration and unit tests in favor of writing '
'an explicit unit test around what the original '
'problem was')
def test_splunk_inventory(self):
new_inv = self.organizations[0].inventories.create(name='splunk')
self.assertEqual(new_inv.hosts.count(), 0)

View File

@@ -8,6 +8,8 @@ import os
import re
import tempfile
import time
import unittest2 as unittest
# Django
from django.conf import settings
@@ -37,6 +39,7 @@ inventory['group-\u037c\u03b4\u0138\u0137\u03cd\u03a1\u0121\u0137\u0138\u01a1'].
print json.dumps(inventory)
"""
@unittest.skipIf(os.environ.get('SKIP_SLOW_TESTS', False), 'Skipping slow test')
class InventoryTest(BaseTest):
def setUp(self):
@@ -1097,6 +1100,7 @@ class InventoryTest(BaseTest):
self.assertEqual(response['hosts']['failed'], 8)
@unittest.skipIf(os.environ.get('SKIP_SLOW_TESTS', False), 'Skipping slow test')
@override_settings(CELERY_ALWAYS_EAGER=True,
CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
IGNORE_CELERY_INSPECTOR=True,

View File

@@ -3,6 +3,8 @@
# Python
from __future__ import absolute_import
import os
import unittest2 as unittest
# Django
import django
@@ -15,6 +17,7 @@ import yaml
__all__ = ['JobTemplateLaunchTest', 'JobTemplateLaunchPasswordsTest']
@unittest.skipIf(os.environ.get('SKIP_SLOW_TESTS', False), 'Skipping slow test')
class JobTemplateLaunchTest(BaseJobTestMixin, django.test.TransactionTestCase):
def setUp(self):
super(JobTemplateLaunchTest, self).setUp()
@@ -183,6 +186,7 @@ class JobTemplateLaunchTest(BaseJobTestMixin, django.test.TransactionTestCase):
with self.current_user(self.user_sue):
self.post(self.launch_url, {}, expect=400)
@unittest.skipIf(os.environ.get('SKIP_SLOW_TESTS', False), 'Skipping slow test')
class JobTemplateLaunchPasswordsTest(BaseJobTestMixin, django.test.TransactionTestCase):
def setUp(self):
super(JobTemplateLaunchPasswordsTest, self).setUp()

View File

@@ -9,6 +9,9 @@ import struct
import threading
import time
import urlparse
import os
import unittest2 as unittest
# Django
import django.test
@@ -183,6 +186,7 @@ TEST_SURVEY_REQUIREMENTS = '''
}
'''
@unittest.skipIf(os.environ.get('SKIP_SLOW_TESTS', False), 'Skipping slow test')
class JobTemplateTest(BaseJobTestMixin, django.test.TransactionTestCase):
JOB_TEMPLATE_FIELDS = ('id', 'type', 'url', 'related', 'summary_fields',
@@ -501,6 +505,7 @@ class JobTemplateTest(BaseJobTestMixin, django.test.TransactionTestCase):
with self.current_user(self.user_doug):
self.get(detail_url, expect=403)
@unittest.skipIf(os.environ.get('SKIP_SLOW_TESTS', False), 'Skipping slow test')
class JobTest(BaseJobTestMixin, django.test.TransactionTestCase):
def test_get_job_list(self):
@@ -653,6 +658,7 @@ class JobTest(BaseJobTestMixin, django.test.TransactionTestCase):
# and that jobs come back nicely serialized with related resources and so on ...
# that we can drill all the way down and can get at host failure lists, etc ...
@unittest.skipIf(os.environ.get('SKIP_SLOW_TESTS', False), 'Skipping slow test')
@override_settings(CELERY_ALWAYS_EAGER=True,
CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
ANSIBLE_TRANSPORT='local')
@@ -1028,6 +1034,7 @@ class JobTemplateCallbackTest(BaseJobTestMixin, django.test.LiveServerTestCase):
self.post(url, data, expect=400, remote_addr=host_ip)
@unittest.skipIf(os.environ.get('SKIP_SLOW_TESTS', False), 'Skipping slow test')
@override_settings(CELERY_ALWAYS_EAGER=True,
CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
ANSIBLE_TRANSPORT='local')
@@ -1092,6 +1099,7 @@ class JobTransactionTest(BaseJobTestMixin, django.test.LiveServerTestCase):
self.assertEqual(job.status, 'successful', job.result_stdout)
self.assertFalse(errors)
@unittest.skipIf(os.environ.get('SKIP_SLOW_TESTS', False), 'Skipping slow test')
class JobTemplateSurveyTest(BaseJobTestMixin, django.test.TransactionTestCase):
def setUp(self):
super(JobTemplateSurveyTest, self).setUp()

View File

@@ -3,6 +3,8 @@
# Python
from __future__ import absolute_import
import os
import unittest2 as unittest
# Django
from django.core.urlresolvers import reverse
@@ -15,6 +17,7 @@ from awx.main.tests.job_base import BaseJobTestMixin
__all__ = ['JobStartCancelTest',]
@unittest.skipIf(os.environ.get('SKIP_SLOW_TESTS', False), 'Skipping slow test')
class JobStartCancelTest(BaseJobTestMixin, BaseLiveServerTest):
def test_job_start(self):

View File

@@ -237,6 +237,7 @@ TEST_VAULT_PLAYBOOK = '''$ANSIBLE_VAULT;1.1;AES256
TEST_VAULT_PASSWORD = '1234'
@unittest.skipIf(os.environ.get('SKIP_SLOW_TESTS', False), 'Skipping slow test')
class RunJobTest(BaseJobExecutionTest):
'''
Test cases for RunJob celery task.
@@ -337,10 +338,9 @@ class RunJobTest(BaseJobExecutionTest):
print
qs = self.super_django_user.get_queryset(JobEvent)
for je in qs.filter(job=job):
print je.get_event_display2()
print je.event, je, je.failed
print je.event_data
print
print(je.get_event_display2())
print(je.event, je, je.failed)
print(je.event_data)
for job_event in job_events:
unicode(job_event) # For test coverage.
job_event.save()