[tox]
minversion = 3.18.0
# Tox 4 gives errors as if the entry_points of setup.cfg were not available
# during tests.
requires =
  virtualenv>=20.4.2
  tox<4
# python runtimes: https://governance.openstack.org/tc/reference/project-testing-interface.html#tested-runtimes
envlist = py3,pep8
skipsdist = True
# setting ignore_basepython_conflict allows tox to infer the base python
# from the environment name and override any basepython configured in this file
ignore_basepython_conflict=true

[testenv]
basepython=python3
setenv = OS_STDOUT_CAPTURE=1
         OS_STDERR_CAPTURE=1
         OS_TEST_TIMEOUT=60
         OS_TEST_PATH=./cinderlib/tests/unit
         VIRTUAL_ENV={envdir}
         TOX_INI_DIR={toxinidir}
         # make sure this is accurate for current development, both here
         # and in [testenv:functional]
         CINDERLIB_RELEASE=2023.2
usedevelop=True

# note: cannot set the special local install command here because it is also
# applied if tox has to update itself to meet the minversion specified above

# Use cinder and os-brick from the appropriate development branch instead of
# from PyPi.  Defining the egg name we won't overwrite the package installed
# by Zuul on jobs supporting cross-project dependencies (include Cinder in
# required-projects).  This allows us to also run local tests against the
# latest cinder/brick code instead of released code.
# NOTE: Functional tests may fail if host is missing bindeps from deps projects
deps =
    -r{toxinidir}/test-requirements.txt
    git+https://opendev.org/openstack/os-brick@stable/2023.2
    git+https://opendev.org/openstack/cinder@stable/2023.2

commands =
    find . -ignore_readdir_race -type f -name "*.pyc" -delete
    stestr run {posargs}
    stestr slowest

allowlist_externals =
  bash
  find
passenv =
  *_proxy
  *_PROXY
  CINDERLIB_CONSTRAINTS_FILE

[testenv:py{3,38,39,310,311}]
allowlist_externals =
  {[testenv]allowlist_externals}
  {toxinidir}/tools/special_install.sh
  {toxinidir}/tools/generate_uc.sh

install_command = {toxinidir}/tools/special_install.sh {env:PIP_OPTIONS:} {opts} {packages}

[testenv:functional]
install_command = {[testenv:py3]install_command}
usedevelop=True
passenv =
  CL_FTEST_POOL_NAME
  CL_FTEST_LOGGING
  CL_FTEST_DEBUG
  CINDERLIB_CONSTRAINTS_FILE
setenv = OS_TEST_PATH=./cinderlib/tests/functional
         CL_FTEST_CFG={env:CL_FTEST_CFG:{toxinidir}/cinderlib/tests/functional/lvm.yaml}
         CL_FTEST_ROOT_HELPER={env:CL_FTEST_ROOT_HELPER:{toxinidir}/tools/virtualenv-sudo.sh}
         TOX_INI_DIR={toxinidir}
         # make sure this is accurate for current development
         CINDERLIB_RELEASE=2023.2

sitepackages = True
# Not reusing py37's env due to https://github.com/tox-dev/tox/issues/477
# envdir = {toxworkdir}/py37

# Must run serially or test_stats_with_creation may fail occasionally
commands =
    find . -ignore_readdir_race -type f -name "*.pyc" -delete
    # run stestr through python -m so that the python from the virtualenv
    # is used; stestr's shebang may point to system python, thus breaking
    # the lookup of modules from the venv.
    python -m stestr run --serial {posargs}
    python -m stestr slowest

allowlist_externals =
  {[testenv:py3]allowlist_externals}
  stestr

[testenv:functional-py{38,39,310,311}]
install_command = {[testenv:functional]install_command}
usedevelop=True
passenv =
  {[testenv:functional]passenv}
setenv =
  {[testenv:functional]setenv}
sitepackages = True
# NOTE: We intentionally do not reuse the unit test's env due to
# https://github.com/tox-dev/tox/issues/477
commands = {[testenv:functional]commands}
allowlist_externals = {[testenv:functional]allowlist_externals}

[testenv:releasenotes]
# Not reusing doc's env due to https://github.com/tox-dev/tox/issues/477
# envdir = {toxworkdir}/docs
install_command = {[testenv:docs]install_command}
deps =
  -r{toxinidir}/doc/requirements.txt
commands = sphinx-build -a -E -W -d releasenotes/build/doctrees -b html releasenotes/source releasenotes/build/html

[testenv:docs]
# yes, we want the constraint in the install_command, not deps, so that
# https://review.opendev.org/c/openstack/glance/+/839786 does not happen
# to us
install_command = python -m pip install -c{env:TOX_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/2023.2} {opts} {packages}
deps =
  -r{toxinidir}/doc/requirements.txt
commands =
  doc8 --ignore D001 --ignore-path .tox --ignore-path *.egg-info --ignore-path doc/build --ignore-path .eggs/*/EGG-INFO/*.txt -e txt -e rst
  rm -rf doc/build .autogenerated doc/source/api
  sphinx-build -W -b html doc/source doc/build/html
  rm -rf api-ref/build
allowlist_externals = rm

[testenv:pdf-docs]
install_command = {[testenv:docs]install_command}
deps =
  {[testenv:docs]deps}
commands =
  {[testenv:docs]commands}
  sphinx-build -W -b latex doc/source doc/build/pdf
  make -C doc/build/pdf
  cp doc/build/pdf/doc-cinderlib.pdf doc/build/html
allowlist_externals =
  {[testenv:docs]allowlist_externals}
  make
  cp

[testenv:bindep]
# Do not install any requirements. We want this to be fast and work even if
# system dependencies are missing, since it's used to tell you what system
# dependencies are missing! This also means that bindep must be installed
# separately, outside of the requirements files, and develop mode disabled
# explicitly to avoid unnecessarily installing the checked-out repo too (this
# further relies on "tox.skipsdist = True" above).
install_command = python -m pip install {opts} {packages}
deps = bindep
commands = bindep {posargs}
usedevelop = False

[testenv:pylint]
install_command = {[testenv:docs]install_command}
deps = -r{toxinidir}/test-requirements.txt
       -r{toxinidir}/requirements.txt
       pylint==2.1.1
commands =
  bash ./tools/coding-checks.sh --pylint {posargs}

[testenv:cover]
# Also do not run test_coverage_ext tests while gathering coverage as those
# tests conflict with coverage.
setenv =
    {[testenv]setenv}
    PYTHON=coverage run --source cinderlib --parallel-mode
commands =
    stestr run {posargs}
    coverage combine
    coverage html -d cover
    coverage xml -o cover/coverage.xml

[flake8]
# see comment in cinderlib/cinderlib.py for why we need to ignore E402
# (import order) in these files
per-file-ignores =
    cinderlib/cinderlib.py:E402
    cinderlib/cmd/cinder_to_yaml.py:E402

[testenv:pep8]
install_command = {[testenv:docs]install_command}
commands=flake8 {posargs} .
deps=
    -r{toxinidir}/test-requirements.txt

[testenv:fast8]
# Not reusing Flake8's env due to https://github.com/tox-dev/tox/issues/477
# envdir = {toxworkdir}/flake8
install_command = {[testenv:docs]install_command}
commands={toxinidir}/tools/fast8.sh
passenv =
  FAST8_NUM_COMMITS
