Merge pull request #1558 from matburt/demo_data

Creating pre-loaded demo data
This commit is contained in:
Matthew Jones 2016-04-18 13:54:40 -04:00
commit 29875a428b
3 changed files with 50 additions and 39 deletions

View File

@ -1,27 +0,0 @@
# Copyright (c) 2015 Ansible, Inc.
# All Rights Reserved
from django.core.management.base import BaseCommand
from crum import impersonate
from awx.main.models import User, Organization
class Command(BaseCommand):
"""Creates the default organization if and only if no organizations
exist in the system.
"""
help = 'Creates a default organization iff there are none.'
def handle(self, *args, **kwargs):
# Sanity check: Is there already an organization in the system?
if Organization.objects.count():
return
# Create a default organization as the first superuser found.
try:
superuser = User.objects.filter(is_superuser=True).order_by('pk')[0]
except IndexError:
superuser = None
with impersonate(superuser):
Organization.objects.create(name='Default')
print('Default organization added.')

View File

@ -0,0 +1,47 @@
# Copyright (c) 2015 Ansible, Inc.
# All Rights Reserved
from django.core.management.base import BaseCommand
from crum import impersonate
from awx.main.models import User, Organization, Project, Inventory, Credential, Host, JobTemplate
class Command(BaseCommand):
"""Create preloaded data, intended for new installs
"""
help = 'Creates a preload tower data iff there is none.'
def handle(self, *args, **kwargs):
# Sanity check: Is there already an organization in the system?
if Organization.objects.count():
return
# Create a default organization as the first superuser found.
try:
superuser = User.objects.filter(is_superuser=True).order_by('pk')[0]
except IndexError:
superuser = None
with impersonate(superuser):
o = Organization.objects.create(name='Default')
p = Project.objects.create(name='Demo Project',
scm_type='git',
scm_url='https://github.com/ansible/ansible-tower-samples',
scm_update_on_launch=True,
scm_update_cache_timeout=0,
organization=o)
c = Credential.objects.create(name='Demo Credential',
username=superuser.username,
created_by=superuser)
c.owner_role.members.add(superuser)
i = Inventory.objects.create(name='Demo Inventory',
organization=o,
created_by=superuser)
Host.objects.create(name='localhost',
inventory=i,
variables="ansible_connection: local",
created_by=superuser)
JobTemplate.objects.create(name='Demo Job Template',
project=p,
inventory=i,
credential=c)
print('Default organization added.')

View File

@ -191,32 +191,23 @@ class CreateDefaultOrgTest(BaseCommandMixin, BaseTest):
def setUp(self):
super(CreateDefaultOrgTest, self).setUp()
self.setup_instances()
def test_create_default_org(self):
self.setup_users()
self.assertEqual(Organization.objects.count(), 0)
result, stdout, stderr = self.run_command('create_default_org')
result, stdout, stderr = self.run_command('create_preload_data')
self.assertEqual(result, None)
self.assertTrue('Default organization added' in stdout)
self.assertEqual(Organization.objects.count(), 1)
org = Organization.objects.all()[0]
self.assertEqual(org.created_by, self.super_django_user)
self.assertEqual(org.modified_by, self.super_django_user)
result, stdout, stderr = self.run_command('create_default_org')
result, stdout, stderr = self.run_command('create_preload_data')
self.assertEqual(result, None)
self.assertFalse('Default organization added' in stdout)
self.assertEqual(Organization.objects.count(), 1)
def test_create_default_org_when_no_superuser_exists(self):
self.assertEqual(Organization.objects.count(), 0)
result, stdout, stderr = self.run_command('create_default_org')
self.assertEqual(result, None)
self.assertTrue('Default organization added' in stdout)
self.assertEqual(Organization.objects.count(), 1)
org = Organization.objects.all()[0]
self.assertEqual(org.created_by, None)
self.assertEqual(org.modified_by, None)
class DumpDataTest(BaseCommandMixin, BaseTest):
'''
Test cases for dumpdata management command.