# Path: core/models.py
# def create_auth_token(sender, instance=None, created=False, **kwargs):
# def validate_cron(value):
#     def __init__(self, *args, **kwargs):
#     def formfield(self, **kwargs):
#     def validate(self, value, model_instance):
#     def get_actual_hosts(self) -> models.QuerySet:
#     def get_playbook_name(self) -> str:
#     def get_hosts_without_groups(self) -> models.QuerySet:
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def get_vars(self):
#     def __str__(self):
#     def __str__(self):
#     def create_task(self, user, is_automatically_created=False, repeat_number=0):
#     def have_uncompleted_task(self):
#     def __str__(self):
#     def get_ansible_command(self):
#     def get_duration(self) -> datetime.timedelta:
#     def stop(self):
#     def __str__(self):
# class CronField(models.CharField):
# class TaskOperationsMixin:
# class Variable(models.Model):
#     class Meta:
# class HostGroup(models.Model):
#     class Meta:
# class Host(models.Model):
#     class Meta:
# class AnsibleUser(models.Model):
#     class Meta:
# class TaskTemplate(TaskOperationsMixin, models.Model):
#     class Meta:
# class RepeatSetting(models.Model):
# class Task(TaskOperationsMixin, models.Model):
#     class Meta:
# class TaskLog(models.Model):
#     class Meta:
# 
# Path: core/datatools/ansible.py
# def make_command(task) -> str:
# def create_inventory(task) -> str:
# def get_inventory_file_path(ansible_shell_command: str):
# 
# Path: core/datatools/tasks.py
# class TaskManager:
#     def run(self):
#     def check_in_progress_tasks(self):
#     def start_waiting_tasks(self):
#     def run_task(cls, task_id):
#     def stop_task(task):
#     def run_task_process(self, task):
#     def _is_process_run(pid):
#     async def _log_output(task, stream, status):


import mock
import os
import shutil
from django.contrib.auth.models import User
from django.test import TestCase, override_settings
from core import models
from core.datatools import ansible, tasks
            status='wait',
            user=self.user,
            pid=99999999,
            ansible_user=an_user,
        )

        task_manager = tasks.TaskManager()
        task_manager.run_task(task.id)

        self.assertIn('Command: ', models.TaskLog.objects.get(id=1).message)
        self.assertIn('Working directory: ', models.TaskLog.objects.get(id=2).message)
        self.assertIn('Failed with status code ', models.TaskLog.objects.all().last().message)

    @mock.patch('asyncio.get_event_loop')
    @mock.patch('django.db.connection')
    def test_run_task_exception(self, connection, p):
        connection.return_value = True
        p.return_value = 0

        an_user = models.AnsibleUser.objects.create(
            name='Test',
        )
        task = models.Task.objects.create(
            playbook='/home/',
            status='wait',
            user=self.user,
            pid=99999999,
            ansible_user=an_user,
        )


# Path: core/models.py
# def create_auth_token(sender, instance=None, created=False, **kwargs):
# def validate_cron(value):
#     def __init__(self, *args, **kwargs):
#     def formfield(self, **kwargs):
#     def validate(self, value, model_instance):
#     def get_actual_hosts(self) -> models.QuerySet:
#     def get_playbook_name(self) -> str:
#     def get_hosts_without_groups(self) -> models.QuerySet:
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def get_vars(self):
#     def __str__(self):
#     def __str__(self):
#     def create_task(self, user, is_automatically_created=False, repeat_number=0):
#     def have_uncompleted_task(self):
#     def __str__(self):
#     def get_ansible_command(self):
#     def get_duration(self) -> datetime.timedelta:
#     def stop(self):
#     def __str__(self):
# class CronField(models.CharField):
# class TaskOperationsMixin:
# class Variable(models.Model):
#     class Meta:
# class HostGroup(models.Model):
#     class Meta:
# class Host(models.Model):
#     class Meta:
# class AnsibleUser(models.Model):
#     class Meta:
# class TaskTemplate(TaskOperationsMixin, models.Model):
#     class Meta:
# class RepeatSetting(models.Model):
# class Task(TaskOperationsMixin, models.Model):
#     class Meta:
# class TaskLog(models.Model):
#     class Meta:
# 
# Path: core/datatools/ansible.py
# def make_command(task) -> str:
# def create_inventory(task) -> str:
# def get_inventory_file_path(ansible_shell_command: str):
# 
# Path: core/datatools/tasks.py
# class TaskManager:
#     def run(self):
#     def check_in_progress_tasks(self):
#     def start_waiting_tasks(self):
#     def run_task(cls, task_id):
#     def stop_task(task):
#     def run_task_process(self, task):
#     def _is_process_run(pid):
#     async def _log_output(task, stream, status):


# Path: core/tests/test_datatools.py
import mock
import os
import shutil
from django.contrib.auth.models import User
from django.test import TestCase, override_settings
from core import models
from core.datatools import ansible, tasks

            status='wait',
            user=self.user,
            pid=99999999,
            ansible_user=an_user,
        )

        task_manager = tasks.TaskManager()
        task_manager.run_task(task.id)

        self.assertIn('Command: ', models.TaskLog.objects.get(id=1).message)
        self.assertIn('Working directory: ', models.TaskLog.objects.get(id=2).message)
        self.assertIn('Failed with status code ', models.TaskLog.objects.all().last().message)

    @mock.patch('asyncio.get_event_loop')
    @mock.patch('django.db.connection')
    def test_run_task_exception(self, connection, p):
        connection.return_value = True
        p.return_value = 0

        an_user = models.AnsibleUser.objects.create(
            name='Test',
        )
        task = models.Task.objects.create(
            playbook='/home/',
            status='wait',
            user=self.user,
            pid=99999999,
            ansible_user=an_user,
        )


        task_manager = tasks.TaskManager()