mirror of
https://github.com/ansible/awx.git
synced 2026-01-24 16:01:20 -03:30
87 lines
3.3 KiB
Python
87 lines
3.3 KiB
Python
# Copyright (c) 2015 Ansible, Inc.
|
|
# All Rights Reserved
|
|
|
|
# Python
|
|
import StringIO
|
|
import sys
|
|
import json
|
|
|
|
# Django
|
|
from django.core.management import call_command
|
|
|
|
# AWX
|
|
from awx.main.models import * # noqa
|
|
from awx.main.tests.base import BaseTestMixin
|
|
|
|
class BaseCommandMixin(BaseTestMixin):
|
|
def create_test_inventories(self):
|
|
self.setup_users()
|
|
self.organizations = self.make_organizations(self.super_django_user, 2)
|
|
self.projects = self.make_projects(self.normal_django_user, 2)
|
|
self.organizations[0].projects.add(self.projects[1])
|
|
self.organizations[1].projects.add(self.projects[0])
|
|
self.inventories = []
|
|
self.hosts = []
|
|
self.groups = []
|
|
for n, organization in enumerate(self.organizations):
|
|
inventory = Inventory.objects.create(name='inventory-%d' % n,
|
|
description='description for inventory %d' % n,
|
|
organization=organization,
|
|
variables=json.dumps({'n': n}) if n else '')
|
|
self.inventories.append(inventory)
|
|
hosts = []
|
|
for x in xrange(10):
|
|
if n > 0:
|
|
variables = json.dumps({'ho': 'hum-%d' % x})
|
|
else:
|
|
variables = ''
|
|
host = inventory.hosts.create(name='host-%02d-%02d.example.com' % (n, x),
|
|
inventory=inventory,
|
|
variables=variables)
|
|
hosts.append(host)
|
|
self.hosts.extend(hosts)
|
|
groups = []
|
|
for x in xrange(5):
|
|
if n > 0:
|
|
variables = json.dumps({'gee': 'whiz-%d' % x})
|
|
else:
|
|
variables = ''
|
|
group = inventory.groups.create(name='group-%d' % x,
|
|
inventory=inventory,
|
|
variables=variables)
|
|
groups.append(group)
|
|
group.hosts.add(hosts[x])
|
|
group.hosts.add(hosts[x + 5])
|
|
if n > 0 and x == 4:
|
|
group.parents.add(groups[3])
|
|
self.groups.extend(groups)
|
|
|
|
def run_command(self, name, *args, **options):
|
|
'''
|
|
Run a management command and capture its stdout/stderr along with any
|
|
exceptions.
|
|
'''
|
|
command_runner = options.pop('command_runner', call_command)
|
|
stdin_fileobj = options.pop('stdin_fileobj', None)
|
|
options.setdefault('verbosity', 1)
|
|
options.setdefault('interactive', False)
|
|
original_stdin = sys.stdin
|
|
original_stdout = sys.stdout
|
|
original_stderr = sys.stderr
|
|
if stdin_fileobj:
|
|
sys.stdin = stdin_fileobj
|
|
sys.stdout = StringIO.StringIO()
|
|
sys.stderr = StringIO.StringIO()
|
|
result = None
|
|
try:
|
|
result = command_runner(name, *args, **options)
|
|
except Exception as e:
|
|
result = e
|
|
finally:
|
|
captured_stdout = sys.stdout.getvalue()
|
|
captured_stderr = sys.stderr.getvalue()
|
|
sys.stdin = original_stdin
|
|
sys.stdout = original_stdout
|
|
sys.stderr = original_stderr
|
|
return result, captured_stdout, captured_stderr
|