mirror of
https://github.com/ansible/awx.git
synced 2026-05-22 08:17:39 -02:30
Race condition and refactoring for facts tests (#15938)
* Race condition and refactoring for facts tests
This commit is contained in:
@@ -3,6 +3,7 @@ import time
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import logging
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@@ -19,6 +20,9 @@ from awx.main.tests import data
|
|||||||
from awx.main.models import Project, JobTemplate, Organization, Inventory
|
from awx.main.models import Project, JobTemplate, Organization, Inventory
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
PROJ_DATA = os.path.join(os.path.dirname(data.__file__), 'projects')
|
PROJ_DATA = os.path.join(os.path.dirname(data.__file__), 'projects')
|
||||||
|
|
||||||
|
|
||||||
@@ -128,30 +132,29 @@ def podman_image_generator():
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def run_job_from_playbook(default_org, demo_inv, post, admin):
|
def project_factory(post, default_org, admin):
|
||||||
def _rf(test_name, playbook, local_path=None, scm_url=None, jt_params=None):
|
def _rf(scm_url=None, local_path=None):
|
||||||
project_name = f'{test_name} project'
|
proj_kwargs = {}
|
||||||
jt_name = f'{test_name} JT: {playbook}'
|
|
||||||
|
|
||||||
old_proj = Project.objects.filter(name=project_name).first()
|
|
||||||
if old_proj:
|
|
||||||
old_proj.delete()
|
|
||||||
|
|
||||||
old_jt = JobTemplate.objects.filter(name=jt_name).first()
|
|
||||||
if old_jt:
|
|
||||||
old_jt.delete()
|
|
||||||
|
|
||||||
proj_kwargs = {'name': project_name, 'organization': default_org.id}
|
|
||||||
if local_path:
|
if local_path:
|
||||||
# manual path
|
# manual path
|
||||||
|
project_name = f'Manual roject {local_path}'
|
||||||
proj_kwargs['scm_type'] = ''
|
proj_kwargs['scm_type'] = ''
|
||||||
proj_kwargs['local_path'] = local_path
|
proj_kwargs['local_path'] = local_path
|
||||||
elif scm_url:
|
elif scm_url:
|
||||||
|
project_name = f'Project {scm_url}'
|
||||||
proj_kwargs['scm_type'] = 'git'
|
proj_kwargs['scm_type'] = 'git'
|
||||||
proj_kwargs['scm_url'] = scm_url
|
proj_kwargs['scm_url'] = scm_url
|
||||||
else:
|
else:
|
||||||
raise RuntimeError('Need to provide scm_url or local_path')
|
raise RuntimeError('Need to provide scm_url or local_path')
|
||||||
|
|
||||||
|
proj_kwargs['name'] = project_name
|
||||||
|
proj_kwargs['organization'] = default_org.id
|
||||||
|
|
||||||
|
old_proj = Project.objects.filter(name=project_name).first()
|
||||||
|
if old_proj:
|
||||||
|
logger.info(f'Deleting existing project {project_name}')
|
||||||
|
old_proj.delete()
|
||||||
|
|
||||||
result = post(
|
result = post(
|
||||||
reverse('api:project_list'),
|
reverse('api:project_list'),
|
||||||
proj_kwargs,
|
proj_kwargs,
|
||||||
@@ -159,6 +162,23 @@ def run_job_from_playbook(default_org, demo_inv, post, admin):
|
|||||||
expect=201,
|
expect=201,
|
||||||
)
|
)
|
||||||
proj = Project.objects.get(id=result.data['id'])
|
proj = Project.objects.get(id=result.data['id'])
|
||||||
|
return proj
|
||||||
|
|
||||||
|
return _rf
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def run_job_from_playbook(demo_inv, post, admin, project_factory):
|
||||||
|
def _rf(test_name, playbook, local_path=None, scm_url=None, jt_params=None, proj=None):
|
||||||
|
jt_name = f'{test_name} JT: {playbook}'
|
||||||
|
|
||||||
|
if not proj:
|
||||||
|
proj = project_factory(scm_url=scm_url, local_path=local_path)
|
||||||
|
|
||||||
|
old_jt = JobTemplate.objects.filter(name=jt_name).first()
|
||||||
|
if old_jt:
|
||||||
|
logger.info(f'Deleting existing JT {jt_name}')
|
||||||
|
old_jt.delete()
|
||||||
|
|
||||||
if proj.current_job:
|
if proj.current_job:
|
||||||
wait_for_job(proj.current_job)
|
wait_for_job(proj.current_job)
|
||||||
@@ -183,4 +203,6 @@ def run_job_from_playbook(default_org, demo_inv, post, admin):
|
|||||||
wait_for_job(job)
|
wait_for_job(job)
|
||||||
assert job.status == 'successful'
|
assert job.status == 'successful'
|
||||||
|
|
||||||
|
return {'job': job, 'job_template': jt, 'project': proj}
|
||||||
|
|
||||||
return _rf
|
return _rf
|
||||||
|
|||||||
@@ -1,14 +1,20 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from awx.main.tests.live.tests.conftest import wait_for_events
|
from awx.main.tests.live.tests.conftest import wait_for_events, wait_for_job
|
||||||
|
|
||||||
from awx.main.models import Job, Inventory
|
from awx.main.models import Job, Inventory
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def facts_project(live_tmp_folder, project_factory):
|
||||||
|
return project_factory(scm_url=f'file://{live_tmp_folder}/facts')
|
||||||
|
|
||||||
|
|
||||||
def assert_facts_populated(name):
|
def assert_facts_populated(name):
|
||||||
job = Job.objects.filter(name__icontains=name).order_by('-created').first()
|
job = Job.objects.filter(name__icontains=name).order_by('-created').first()
|
||||||
assert job is not None
|
assert job is not None
|
||||||
wait_for_events(job)
|
wait_for_events(job)
|
||||||
|
wait_for_job(job)
|
||||||
|
|
||||||
inventory = job.inventory
|
inventory = job.inventory
|
||||||
assert inventory.hosts.count() > 0 # sanity
|
assert inventory.hosts.count() > 0 # sanity
|
||||||
@@ -17,24 +23,24 @@ def assert_facts_populated(name):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def general_facts_test(live_tmp_folder, run_job_from_playbook):
|
def general_facts_test(facts_project, run_job_from_playbook):
|
||||||
def _rf(slug, jt_params):
|
def _rf(slug, jt_params):
|
||||||
jt_params['use_fact_cache'] = True
|
jt_params['use_fact_cache'] = True
|
||||||
standard_kwargs = dict(scm_url=f'file://{live_tmp_folder}/facts', jt_params=jt_params)
|
standard_kwargs = dict(jt_params=jt_params)
|
||||||
|
|
||||||
# GATHER FACTS
|
# GATHER FACTS
|
||||||
name = f'test_gather_ansible_facts_{slug}'
|
name = f'test_gather_ansible_facts_{slug}'
|
||||||
run_job_from_playbook(name, 'gather.yml', **standard_kwargs)
|
run_job_from_playbook(name, 'gather.yml', proj=facts_project, **standard_kwargs)
|
||||||
assert_facts_populated(name)
|
assert_facts_populated(name)
|
||||||
|
|
||||||
# KEEP FACTS
|
# KEEP FACTS
|
||||||
name = f'test_clear_ansible_facts_{slug}'
|
name = f'test_clear_ansible_facts_{slug}'
|
||||||
run_job_from_playbook(name, 'no_op.yml', **standard_kwargs)
|
run_job_from_playbook(name, 'no_op.yml', proj=facts_project, **standard_kwargs)
|
||||||
assert_facts_populated(name)
|
assert_facts_populated(name)
|
||||||
|
|
||||||
# CLEAR FACTS
|
# CLEAR FACTS
|
||||||
name = f'test_clear_ansible_facts_{slug}'
|
name = f'test_clear_ansible_facts_{slug}'
|
||||||
run_job_from_playbook(name, 'clear.yml', **standard_kwargs)
|
run_job_from_playbook(name, 'clear.yml', proj=facts_project, **standard_kwargs)
|
||||||
job = Job.objects.filter(name__icontains=name).order_by('-created').first()
|
job = Job.objects.filter(name__icontains=name).order_by('-created').first()
|
||||||
|
|
||||||
assert job is not None
|
assert job is not None
|
||||||
|
|||||||
Reference in New Issue
Block a user