Merge pull request #6325 from AlanCoding/autohack

Automatically hack sys.path to make running tests easier

Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
softwarefactory-project-zuul[bot]
2020-03-18 17:46:59 +00:00
committed by GitHub
3 changed files with 22 additions and 23 deletions

View File

@@ -408,7 +408,7 @@ test_collection:
@if [ "$(VENV_BASE)" ]; then \ @if [ "$(VENV_BASE)" ]; then \
. $(VENV_BASE)/awx/bin/activate; \ . $(VENV_BASE)/awx/bin/activate; \
fi; \ fi; \
PYTHONPATH=$(COLLECTION_VENV):/awx_devel/awx_collection:$PYTHONPATH:/usr/lib/python3.6/site-packages py.test $(COLLECTION_TEST_DIRS) PYTHONPATH=$(COLLECTION_VENV):$PYTHONPATH:/usr/lib/python3.6/site-packages py.test $(COLLECTION_TEST_DIRS)
flake8_collection: flake8_collection:
flake8 awx_collection/ # Different settings, in main exclude list flake8 awx_collection/ # Different settings, in main exclude list

View File

@@ -53,26 +53,11 @@ The following notes are changes that may require changes to playbooks:
## Running Unit Tests ## Running Unit Tests
Tests to verify compatibility with the most recent AWX code are Tests to verify compatibility with the most recent AWX code are in `awx_collection/test/awx`.
in `awx_collection/test/awx`. These tests require that Python packages These can be ran by `make test_collection` in the development container.
are available for all of `awx`, `ansible`, `tower_cli`, and the collection
itself.
### Inside Development Container To run outside of the development container, or to run against
Ansible or `tower-cli` source, set up a working environment:
The target `make prepare_collection_venv` will prepare some requirements
in the `awx_collection_test_venv` folder so that `make test_collection` can
be executed to actually run the tests. A single test can be run via:
```
make test_collection COLLECTION_TEST_DIRS=awx_collection/test/awx/test_organization.py
```
### Manually
As a faster alternative (if you do not want to use the container), or to
run against Ansible or `tower-cli` source, it is possible to set up a
working environment yourself:
``` ```
mkvirtualenv my_new_venv mkvirtualenv my_new_venv
@@ -80,11 +65,12 @@ mkvirtualenv my_new_venv
pip install -r requirements/requirements.txt -r requirements/requirements_dev.txt -r requirements/requirements_git.txt pip install -r requirements/requirements.txt -r requirements/requirements_dev.txt -r requirements/requirements_git.txt
make clean-api make clean-api
pip install -e <path to your Ansible> pip install -e <path to your Ansible>
pip install -e <path to your tower-cli>
pip install -e . pip install -e .
PYTHONPATH=awx_collection:$PYTHONPATH py.test awx_collection/test/awx/ py.test awx_collection/test/awx/
``` ```
If you do not install tower-cli, it will skip tests for modules that require it.
## Running Integration Tests ## Running Integration Tests
The integration tests require a virtualenv with `ansible` >= 2.9 and `tower_cli`. The integration tests require a virtualenv with `ansible` >= 2.9 and `tower_cli`.

View File

@@ -2,6 +2,7 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import io import io
import os
import json import json
import datetime import datetime
import importlib import importlib
@@ -46,10 +47,22 @@ def sanitize_dict(din):
return str(din) # translation proxies often not string but stringlike return str(din) # translation proxies often not string but stringlike
@pytest.fixture(autouse=True)
def collection_path_set(monkeypatch):
"""Monkey patch sys.path, insert the root of the collection folder
so that content can be imported without being fully packaged
"""
base_folder = os.path.abspath(
os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
)
monkeypatch.syspath_prepend(base_folder)
@pytest.fixture @pytest.fixture
def collection_import(): def collection_import():
"""These tests run assuming that the awx_collection folder is inserted """These tests run assuming that the awx_collection folder is inserted
into the PATH before-hand. But all imports internally to the collection into the PATH before-hand by collection_path_set.
But all imports internally to the collection
go through this fixture so that can be changed if needed. go through this fixture so that can be changed if needed.
For instance, we could switch to fully-qualified import paths. For instance, we could switch to fully-qualified import paths.
""" """