mirror of
https://github.com/ansible/awx.git
synced 2026-02-02 01:58:09 -03:30
Changed how Project local_path is configured, added option to specify playbook on JobTemplate and Job, updated admin and tests.
This commit is contained in:
@@ -16,7 +16,11 @@
|
||||
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User as DjangoUser
|
||||
import django.test
|
||||
from django.test.client import Client
|
||||
@@ -31,6 +35,13 @@ class BaseTestMixin(object):
|
||||
def setUp(self):
|
||||
super(BaseTestMixin, self).setUp()
|
||||
self.object_ctr = 0
|
||||
self._temp_project_dirs = []
|
||||
|
||||
def tearDown(self):
|
||||
super(BaseTestMixin, self).tearDown()
|
||||
for project_dir in self._temp_project_dirs:
|
||||
if os.path.exists(project_dir):
|
||||
shutil.rmtree(project_dir, True)
|
||||
|
||||
def make_user(self, username, password, super_user=False):
|
||||
django_user = None
|
||||
@@ -50,13 +61,24 @@ class BaseTestMixin(object):
|
||||
))
|
||||
return results
|
||||
|
||||
def make_projects(self, created_by, count=1):
|
||||
def make_projects(self, created_by, count=1, playbook_content=''):
|
||||
results = []
|
||||
for x in range(0, count):
|
||||
self.object_ctr = self.object_ctr + 1
|
||||
# Create temp project directory.
|
||||
project_dir = tempfile.mkdtemp(dir=settings.PROJECTS_ROOT)
|
||||
self._temp_project_dirs.append(project_dir)
|
||||
# Create temp playbook in project (if playbook content is given).
|
||||
if playbook_content:
|
||||
handle, playbook_path = tempfile.mkstemp(suffix='.yml',
|
||||
dir=project_dir)
|
||||
test_playbook_file = os.fdopen(handle, 'w')
|
||||
test_playbook_file.write(playbook_content)
|
||||
test_playbook_file.close()
|
||||
results.append(Project.objects.create(
|
||||
name="proj%s-%s" % (x, self.object_ctr), description="proj%s" % x, scm_type='git',
|
||||
default_playbook='foo.yml', local_repository='/checkout', created_by=created_by
|
||||
name="proj%s-%s" % (x, self.object_ctr), description="proj%s" % x,
|
||||
#scm_type='git', default_playbook='foo.yml',
|
||||
local_path=project_dir, created_by=created_by
|
||||
))
|
||||
return results
|
||||
|
||||
|
||||
@@ -71,13 +71,9 @@ class JobsTest(BaseTest):
|
||||
self.team.users.add(self.other_django_user)
|
||||
self.team.users.add(self.other2_django_user)
|
||||
|
||||
self.project = Project.objects.create(
|
||||
name = 'testProject',
|
||||
created_by = self.normal_django_user,
|
||||
local_repository = '/tmp/',
|
||||
scm_type = 'git',
|
||||
default_playbook = 'site.yml',
|
||||
)
|
||||
self.project = self.make_projects(self.normal_django_user, 1,
|
||||
playbook_content='')[0]
|
||||
self.organization.projects.add(self.project)
|
||||
|
||||
# other django user is on the project team and can deploy
|
||||
self.permission1 = Permission.objects.create(
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
from django.conf import settings
|
||||
from django.test.utils import override_settings
|
||||
@@ -53,10 +54,9 @@ class RunJobTest(BaseCeleryTest):
|
||||
|
||||
def setUp(self):
|
||||
super(RunJobTest, self).setUp()
|
||||
self.test_project_path = None
|
||||
self.setup_users()
|
||||
self.organization = self.make_organizations(self.super_django_user, 1)[0]
|
||||
self.project = self.make_projects(self.normal_django_user, 1)[0]
|
||||
self.organization.projects.add(self.project)
|
||||
self.inventory = Inventory.objects.create(name='test-inventory',
|
||||
description='description for test-inventory',
|
||||
organization=self.organization)
|
||||
@@ -71,27 +71,33 @@ class RunJobTest(BaseCeleryTest):
|
||||
def tearDown(self):
|
||||
super(RunJobTest, self).tearDown()
|
||||
os.environ.pop('ACOM_TEST_DATABASE_NAME', None)
|
||||
os.remove(self.test_playbook)
|
||||
if self.test_project_path:
|
||||
shutil.rmtree(self.test_project_path, True)
|
||||
|
||||
def create_test_playbook(self, s):
|
||||
handle, self.test_playbook = tempfile.mkstemp(suffix='.yml', prefix='playbook-')
|
||||
test_playbook_file = os.fdopen(handle, 'w')
|
||||
test_playbook_file.write(s)
|
||||
test_playbook_file.close()
|
||||
self.project.default_playbook = self.test_playbook
|
||||
self.project.save()
|
||||
def create_test_project(self, playbook_content):
|
||||
self.project = self.make_projects(self.normal_django_user, 1, playbook_content)[0]
|
||||
self.organization.projects.add(self.project)
|
||||
|
||||
def create_test_job(self, **kwargs):
|
||||
opts = {
|
||||
'name': 'test-job',
|
||||
'inventory': self.inventory,
|
||||
'project': self.project,
|
||||
'playbook': self.project.available_playbooks[0],
|
||||
}
|
||||
opts.update(kwargs)
|
||||
return Job.objects.create(**opts)
|
||||
|
||||
def test_run_job(self):
|
||||
self.create_test_playbook(TEST_PLAYBOOK)
|
||||
job = Job.objects.create(name='test-job', inventory=self.inventory,
|
||||
project=self.project)
|
||||
self.create_test_project(TEST_PLAYBOOK)
|
||||
job = self.create_test_job()
|
||||
self.assertEqual(job.status, 'pending')
|
||||
self.assertEqual(set(job.hosts.values_list('pk', flat=True)),
|
||||
set([self.host.pk]))
|
||||
job = Job.objects.get(pk=job.pk)
|
||||
#print 'stdout:', launch_job_status.result_stdout
|
||||
#print 'stderr:', launch_job_status.result_stderr
|
||||
#print launch_job_status.status
|
||||
#print 'stdout:', job.result_stdout
|
||||
#print 'stderr:', job.result_stderr
|
||||
#print job.status
|
||||
#print settings.DATABASES
|
||||
self.assertEqual(job.status, 'successful')
|
||||
self.assertTrue(job.result_stdout)
|
||||
@@ -114,9 +120,8 @@ class RunJobTest(BaseCeleryTest):
|
||||
self.assertEqual(job.processed_hosts.count(), 1)
|
||||
|
||||
def test_check_job(self):
|
||||
self.create_test_playbook(TEST_PLAYBOOK)
|
||||
job = Job.objects.create(name='test-job', inventory=self.inventory,
|
||||
project=self.project, job_type='check')
|
||||
self.create_test_project(TEST_PLAYBOOK)
|
||||
job = self.create_test_job(job_type='check')
|
||||
self.assertEqual(job.status, 'pending')
|
||||
self.assertEqual(set(job.hosts.values_list('pk', flat=True)),
|
||||
set([self.host.pk]))
|
||||
@@ -139,9 +144,8 @@ class RunJobTest(BaseCeleryTest):
|
||||
self.assertEqual(job.processed_hosts.count(), 1)
|
||||
|
||||
def test_run_job_that_fails(self):
|
||||
self.create_test_playbook(TEST_PLAYBOOK2)
|
||||
job = Job.objects.create(name='test-job', inventory=self.inventory,
|
||||
project=self.project)
|
||||
self.create_test_project(TEST_PLAYBOOK2)
|
||||
job = self.create_test_job()
|
||||
self.assertEqual(job.status, 'pending')
|
||||
self.assertEqual(set(job.hosts.values_list('pk', flat=True)),
|
||||
set([self.host.pk]))
|
||||
@@ -163,9 +167,8 @@ class RunJobTest(BaseCeleryTest):
|
||||
self.assertEqual(job.processed_hosts.count(), 1)
|
||||
|
||||
def test_check_job_where_task_would_fail(self):
|
||||
self.create_test_playbook(TEST_PLAYBOOK2)
|
||||
job = Job.objects.create(name='test-job', inventory=self.inventory,
|
||||
project=self.project, job_type='check')
|
||||
self.create_test_project(TEST_PLAYBOOK2)
|
||||
job = self.create_test_job(job_type='check')
|
||||
self.assertEqual(job.status, 'pending')
|
||||
self.assertEqual(set(job.hosts.values_list('pk', flat=True)),
|
||||
set([self.host.pk]))
|
||||
@@ -187,5 +190,3 @@ class RunJobTest(BaseCeleryTest):
|
||||
self.assertEqual(job.unreachable_hosts.count(), 0)
|
||||
self.assertEqual(job.skipped_hosts.count(), 1)
|
||||
self.assertEqual(job.processed_hosts.count(), 1)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user