Pass a config `tmp_dir: Path` to the runtime when testing

service_subpkg
Tyler Goodlet 2023-03-09 14:33:12 -05:00
parent 5aaa7f47dc
commit 79b0db4449
3 changed files with 37 additions and 31 deletions

View File

@ -1 +0,0 @@
TEST_CONFIG_DIR_PATH = '_testing'

View File

@ -2,12 +2,10 @@ from contextlib import asynccontextmanager as acm
from functools import partial from functools import partial
import os import os
from pathlib import Path from pathlib import Path
from shutil import rmtree
import pytest import pytest
import tractor import tractor
from piker import ( from piker import (
# log,
config, config,
) )
from piker.service import ( from piker.service import (
@ -71,6 +69,7 @@ def ci_env() -> bool:
@acm @acm
async def _open_test_pikerd( async def _open_test_pikerd(
tmpconfdir: str,
reg_addr: tuple[str, int] | None = None, reg_addr: tuple[str, int] | None = None,
loglevel: str = 'warning', loglevel: str = 'warning',
**kwargs, **kwargs,
@ -97,6 +96,10 @@ async def _open_test_pikerd(
maybe_open_pikerd( maybe_open_pikerd(
registry_addr=reg_addr, registry_addr=reg_addr,
loglevel=loglevel, loglevel=loglevel,
tractor_runtime_overrides={
'piker_test_dir': tmpconfdir,
},
**kwargs, **kwargs,
) as service_manager, ) as service_manager,
): ):
@ -119,18 +122,40 @@ async def _open_test_pikerd(
@pytest.fixture @pytest.fixture
def open_test_pikerd( def open_test_pikerd(
request, request: pytest.FixtureRequest,
tmp_path: Path,
loglevel: str, loglevel: str,
): ):
tmpconfdir: Path = tmp_path / '_testing'
tmpconfdir.mkdir()
tmpconfdir_str: str = str(tmpconfdir)
# NOTE: on linux the tmp config dir is generally located at:
# /tmp/pytest-of-<username>/pytest-<run#>/test_<current_test_name>/
# the default `pytest` config ensures that only the last 4 test
# suite run's dirs will be persisted, otherwise they are removed:
# https://docs.pytest.org/en/6.2.x/tmpdir.html#the-default-base-temporary-directory
print(f'CURRENT TEST CONF DIR: {tmpconfdir}')
yield partial( yield partial(
_open_test_pikerd, _open_test_pikerd,
# pass in a unique temp dir for this test request
# so that we can have multiple tests running (maybe in parallel)
# bwitout clobbering each other's config state.
tmpconfdir=tmpconfdir_str,
# bind in level from fixture, which is itself set by # bind in level from fixture, which is itself set by
# `--ll <value>` cli flag. # `--ll <value>` cli flag.
loglevel=loglevel, loglevel=loglevel,
) )
# NOTE: the `tmp_dir` fixture will wipe any files older then 3 test
# sessions by default:
# https://docs.pytest.org/en/6.2.x/tmpdir.html#the-default-base-temporary-directory
# BUT, if we wanted to always wipe conf dir and all contained files,
# rmtree(str(tmp_path))
# TODO: teardown checks such as, # TODO: teardown checks such as,
# - no leaked subprocs or shm buffers # - no leaked subprocs or shm buffers
# - all requested container service are torn down # - all requested container service are torn down
@ -169,19 +194,3 @@ def open_test_pikerd_and_ems(
loglevel, loglevel,
open_test_pikerd open_test_pikerd
) )
@pytest.fixture(scope='module')
def delete_testing_dir():
'''
This fixture removes the temp directory
used for storing all config/ledger/pp data
created during testing sessions. During test runs
this file can be found in .config/piker/_testing
'''
yield
app_dir = Path(config.get_app_dir('piker')).resolve()
if app_dir.is_dir():
rmtree(str(app_dir))
assert not app_dir.is_dir()

View File

@ -17,7 +17,6 @@ from functools import partial
from piker.log import get_logger from piker.log import get_logger
from piker.clearing._messages import Order from piker.clearing._messages import Order
from piker.pp import ( from piker.pp import (
open_trade_ledger,
open_pps, open_pps,
) )
@ -42,18 +41,19 @@ async def _async_main(
price: int = 30000, price: int = 30000,
executions: int = 1, executions: int = 1,
size: float = 0.01, size: float = 0.01,
# Assert options # Assert options
assert_entries: bool = False, assert_entries: bool = False,
assert_pps: bool = False, assert_pps: bool = False,
assert_zeroed_pps: bool = False, assert_zeroed_pps: bool = False,
assert_msg: bool = False, assert_msg: bool = False,
) -> None: ) -> None:
''' '''
Start piker, place a trade and assert data in Start piker, place a trade and assert data in
pps stream, ledger and position table. pps stream, ledger and position table.
''' '''
oid: str = '' oid: str = ''
last_msg = {} last_msg = {}
@ -151,7 +151,6 @@ def _run_test_and_check(fn):
def test_buy( def test_buy(
open_test_pikerd_and_ems: AsyncContextManager, open_test_pikerd_and_ems: AsyncContextManager,
delete_testing_dir
): ):
''' '''
Enter a trade and assert entries are made in pps and ledger files. Enter a trade and assert entries are made in pps and ledger files.
@ -178,7 +177,6 @@ def test_buy(
def test_sell( def test_sell(
open_test_pikerd_and_ems: AsyncContextManager, open_test_pikerd_and_ems: AsyncContextManager,
delete_testing_dir
): ):
''' '''
Sell position and ensure pps are zeroed. Sell position and ensure pps are zeroed.
@ -201,9 +199,9 @@ def test_sell(
), ),
) )
def test_multi_sell( def test_multi_sell(
open_test_pikerd_and_ems: AsyncContextManager, open_test_pikerd_and_ems: AsyncContextManager,
delete_testing_dir
): ):
''' '''
Make 5 market limit buy orders and Make 5 market limit buy orders and