Creates basic XNAT instance for API tests
Project description
Xnat4Tests runs a basic XNAT repository instance in a single Docker to be used for quick demonstrations on your workstation or integrated within test suites for tools that use XNAT’s REST API.
The XNAT container service plugin is installed by default and is configured to use the same Docker host as the XNAT instance.
The home/logs, home/work, build, archive, prearchive directories are mounted in from the host system from the $HOME/.xnat4tests/xnat_root/default directory by default. This can be useful for debugging and can be used to replicate the environment under which containers run in within XNAT’s container service.
In addition to the start_xnat, stop_xnat and restart_xnat functions, which control the life-cycle of the XNAT instance, there is also a connect function that returns an XnatPy connection object to the test instance
Installation
Docker needs to be installed on your system, see Get Docker for details.
Xnat4Tests is available on PyPI so can be installed with
$ pip3 install xnat4tests
or include in your package’s test_requires if you are writing Python tests.
Usage
Command line interface
A test XNAT instance can be launched using the CLI with
$ xnat4tests start
This will spin up an empty XNAT instance that can be accessed using the default admin user account user=’admin’/password=’admin’. To add some sample data to play with you can use the add-data command
$ xnat4tests start
$ xnat4tests add-data 'dummydicom'
or in a single line
$ xnat4tests start --with-data 'dummydicom'
By default, xnat4tests will create a configuration file at $HOME/.xnat4tests/configs/default.yaml. The config file can be adapted to modify the names of the Docker images/containers used, the ports the containers run on, and which directories are mounted into the container. Multiple configurations can be used concurrently by saving the config file to a new location and passing it to the base command, i.e.
$ xnat4tests --config /path/to/my/repo/xnat4tests-config.yaml start
To stop or restart the running container you can use xnat4tests stop and xnat4tests restart commands, respectively.
Python API
If you are developing Python applications you will typically want to use the API to launch the XNAT instance using the xnat4tests.start_xnat function. An XnatPy connection session object can be accessed using xnat4tests.connect and the instanced stopped afterwards using stop_xnat.
# Import xnat4tests functions
from xnat4tests import start_xnat, stop_xnat, connect, Config
config = Config.load("default")
# Launch the instance (NB: it takes quite while for an XNAT instance to start). If an existing
# container with the reserved name is already running it is returned instead
start_xnat(config)
# Connect to the XNAT instance using XnatPy and run some tests
with connect(config) as login:
PROJECT = 'MY_TEST_PROJECT'
SUBJECT = 'MYSUBJECT'
SESSION = 'MYSESSION'
login.put(f'/data/archive/projects/MY_TEST_PROJECT')
# Create subject
xsubject = login.classes.SubjectData(label=SUBJECT,
parent=login.projects[PROJECT])
# Create session
login.classes.MrSessionData(label=SESSION, parent=xsubject)
assert [p.name for p in (config.xnat_root_dir / "archive").iterdir()] == [PROJECT]
# Remove the container after you are done (not strictly necessary). To avoid
# having to wait for XNAT to restart each time before you run your tests, you can
# skip this line and start_xnat will attempt to use the instance that is already
# running
stop_xnat(config)
Alternatively, if you are using Pytest then you can set up the connection as a fixture in your conftest.py, e.g.
import tempfile
from pathlib import Path
from xnat4tests import start_xnat, stop_xnat, connect, Config
@pytest.fixture(scope="session")
def xnat_config():
tmp_dir = Path(tempfile.mkdtemp())
return Config(
xnat_root_dir=tmp_dir,
xnat_port=9999,
docker_image="myrepo_xnat4tests",
docker_container="myrepo_xnat4tests",
build_args={
"xnat_version": "1.8.5",
"xnat_cs_plugin_version": "3.2.0",
},
)
@pytest.fixture(scope="session")
def xnat_uri(xnat_config):
xnat4tests.start_xnat(xnat_config)
xnat4tests.add_data("dummydicom")
yield xnat_config.xnat_uri
xnat4tests.stop_xnat(xnat_config)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file xnat4tests-0.3.6-py3-none-any.whl
.
File metadata
- Download URL: xnat4tests-0.3.6-py3-none-any.whl
- Upload date:
- Size: 20.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dae4272880c082a820a9f77576cccebd941821dfc31cddddfb0e8a22e1bdfbf6 |
|
MD5 | f18d4976391ac98c206ff11e2953f9b2 |
|
BLAKE2b-256 | ece98af3b8121aa12ccad609e6f5b74d8dd79edd545e2d42a78c1c6d0d1c24ad |