Skip to content

Commit

Permalink
Merge pull request #197 from Aiven-Open/aris-upgrade-tmp-path-fixtures
Browse files Browse the repository at this point in the history
Update legacy py.path package and fixtures to pathlib
  • Loading branch information
kmichel-aiven authored Mar 28, 2024
2 parents 9eac246 + bb0c529 commit f0e1087
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 68 deletions.
23 changes: 11 additions & 12 deletions tests/system/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import json
import logging
import os.path
import py
import pytest
import subprocess

Expand Down Expand Up @@ -98,11 +97,11 @@ def create_astacus_config_dict(

def create_astacus_config(
*,
tmpdir,
tmp_path: Path,
node: TestNode,
plugin_config: Mapping[str, Any] = MappingProxyType(DEFAULT_PLUGIN_CONFIG),
) -> Path:
a = Path(tmpdir / "node" / node.name)
a = tmp_path / "node" / node.name
node.path = a
root_path = a / "root"
root_path.mkdir(parents=True, exist_ok=True)
Expand All @@ -113,7 +112,7 @@ def create_astacus_config(
db_path.mkdir(exist_ok=True)
node.db_path = db_path
a_conf = create_astacus_config_dict(
tmpdir=Path(tmpdir), root_path=root_path, link_path=link_path, node=node, plugin_config=plugin_config
tmpdir=tmp_path, root_path=root_path, link_path=link_path, node=node, plugin_config=plugin_config
)
a_conf_path = a / "astacus.conf"
a_conf_path.write_text(json.dumps(a_conf))
Expand All @@ -140,9 +139,9 @@ def fixture_rootdir(pytestconfig: Config) -> str:


@asynccontextmanager
async def _astacus(*, tmpdir: py.path.local, index: int) -> AsyncIterator[TestNode]:
async def _astacus(*, tmp_path: Path, index: int) -> AsyncIterator[TestNode]:
node = ASTACUS_NODES[index]
a_conf_path = create_astacus_config(tmpdir=tmpdir, node=node)
a_conf_path = create_astacus_config(tmp_path=tmp_path, node=node)
astacus_source_root = os.path.join(os.path.dirname(__file__), "..", "..")
cmd = format_astacus_command("server", "-c", str(a_conf_path))
async with background_process(*cmd, env={"PYTHONPATH": astacus_source_root}) as process:
Expand All @@ -152,20 +151,20 @@ async def _astacus(*, tmpdir: py.path.local, index: int) -> AsyncIterator[TestNo


@pytest.fixture(name="astacus1")
async def fixture_astacus1(tmpdir: py.path.local) -> AsyncIterator[TestNode]:
async with _astacus(tmpdir=tmpdir, index=0) as a:
async def fixture_astacus1(tmp_path: Path) -> AsyncIterator[TestNode]:
async with _astacus(tmp_path=tmp_path, index=0) as a:
yield a


@pytest.fixture(name="astacus2")
async def fixture_astacus2(tmpdir: py.path.local) -> AsyncIterator[TestNode]:
async with _astacus(tmpdir=tmpdir, index=1) as a:
async def fixture_astacus2(tmp_path: Path) -> AsyncIterator[TestNode]:
async with _astacus(tmp_path=tmp_path, index=1) as a:
yield a


@pytest.fixture(name="astacus3")
async def fixture_astacus3(tmpdir: py.path.local) -> AsyncIterator[TestNode]:
async with _astacus(tmpdir=tmpdir, index=2) as a:
async def fixture_astacus3(tmp_path: Path) -> AsyncIterator[TestNode]:
async with _astacus(tmp_path=tmp_path, index=2) as a:
yield a


Expand Down
6 changes: 3 additions & 3 deletions tests/system/test_config_reload.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
Hot-reloading of astacus configuration
"""

from pathlib import Path
from tests.system.conftest import astacus_ls, astacus_run, create_astacus_config, TestNode

import pytest


@pytest.mark.order("last")
def test_reload_config(tmpdir, rootdir: str, astacus1: TestNode, astacus2: TestNode, astacus3: TestNode) -> None:
def test_reload_config(tmp_path: Path, rootdir: str, astacus1: TestNode, astacus2: TestNode, astacus3: TestNode) -> None:
# Update the root_globs config of the first node
create_astacus_config(tmpdir=tmpdir, node=astacus1, plugin_config={"root_globs": ["*.foo"]})
create_astacus_config(tmp_path=tmp_path, node=astacus1, plugin_config={"root_globs": ["*.foo"]})
# Check that astacus can detect that reloading config is needed
assert astacus_run(rootdir, astacus1, "check-reload", "--status", check=False).returncode == 1
check_without_status = astacus_run(rootdir, astacus1, "check-reload", check=True, capture_output=True)
Expand Down
17 changes: 8 additions & 9 deletions tests/unit/common/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from unittest.mock import Mock, patch

import json
import py
import pytest

TEST_HEXDIGEST = "deadbeef"
Expand All @@ -30,16 +29,16 @@
TEST_JSON_DATA: Json = {"foo": 7, "array": [1, 2, 3], "true": True}


def create_storage(*, tmpdir: py.path.local, engine: str, **kw):
def create_storage(*, tmpdir: Path, engine: str, **kw):
if engine == "rohmu":
config = create_rohmu_config(tmpdir, **kw)
return RohmuStorage(config=config)
if engine == "file":
path = Path(tmpdir / "test-storage-file")
path = tmpdir / "test-storage-file"
return FileStorage(path)
if engine == "cache":
# FileStorage cache, and then rohmu filestorage underneath
cache_storage = FileStorage(Path(tmpdir / "test-storage-file"))
cache_storage = FileStorage(tmpdir / "test-storage-file")
config = create_rohmu_config(tmpdir, **kw)
backend_storage = RohmuStorage(config=config)
return CachingJsonStorage(backend_storage=backend_storage, cache_storage=cache_storage)
Expand Down Expand Up @@ -85,22 +84,22 @@ def _test_jsonstorage(storage: JsonStorage) -> None:
("rohmu", {"compression": False, "encryption": False}, pytest.raises(exceptions.CompressionOrEncryptionRequired)),
],
)
def test_storage(tmpdir: py.path.local, engine: str, kw: dict[str, bool], ex: AbstractContextManager | None) -> None:
def test_storage(tmp_path: Path, engine: str, kw: dict[str, bool], ex: AbstractContextManager | None) -> None:
if ex is None:
ex = does_not_raise()
with ex:
storage = create_storage(tmpdir=tmpdir, engine=engine, **kw)
storage = create_storage(tmpdir=tmp_path, engine=engine, **kw)
if isinstance(storage, FileStorage):
_test_hexdigeststorage(storage)
if isinstance(storage, JsonStorage):
_test_jsonstorage(storage)


def test_caching_storage(tmpdir: py.path.local, mocker: MockerFixture) -> None:
storage = create_storage(tmpdir=tmpdir, engine="cache")
def test_caching_storage(tmp_path: Path, mocker: MockerFixture) -> None:
storage = create_storage(tmpdir=tmp_path, engine="cache")
storage.upload_json(TEST_JSON, TEST_JSON_DATA)

storage = create_storage(tmpdir=tmpdir, engine="cache")
storage = create_storage(tmpdir=tmp_path, engine="cache")
assert storage.list_jsons() == [TEST_JSON]

# We shouldn't wind up in download method of rohmu at all.
Expand Down
17 changes: 8 additions & 9 deletions tests/unit/common/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import asyncio
import logging
import py
import pytest
import tempfile
import time
Expand Down Expand Up @@ -149,26 +148,26 @@ def test_sizelimitedfile() -> None:
assert lf.read() == b"bar"


def test_open_path_with_atomic_rename(tmpdir: py.path.local) -> None:
def test_open_path_with_atomic_rename(tmp_path: Path) -> None:
# default is bytes
f1_path = f"{tmpdir}/f1"
f1_path = tmp_path / "f1"
with utils.open_path_with_atomic_rename(f1_path) as f1:
f1.write(b"test1")
assert Path(f1_path).read_text() == "test1"
assert f1_path.read_text() == "test1"

# text mode requires passing mode flag but should work
f2_path = f"{tmpdir}/f2"
f2_path = tmp_path / "f2"
with utils.open_path_with_atomic_rename(f2_path, mode="w") as f2:
f2.write("test2")
assert Path(f2_path).read_text() == "test2"
assert f2_path.read_text() == "test2"

# rewriting should be fine too
with utils.open_path_with_atomic_rename(f2_path, mode="w") as f2:
f2.write("test2-new")
assert Path(f2_path).read_text() == "test2-new"
assert f2_path.read_text() == "test2-new"

# erroneous cases should not produce file at all
f3_path = f"{tmpdir}/f3"
f3_path = tmp_path / "f3"

class TestException(RuntimeError):
pass
Expand All @@ -177,7 +176,7 @@ class TestException(RuntimeError):
with utils.open_path_with_atomic_rename(f3_path):
raise TestException()
# This version of MyPy works poorly with pytest.raises
assert not Path(f3_path).exists() # type: ignore[unreachable]
assert not f3_path.exists() # type: ignore[unreachable]


def test_parse_umask() -> None:
Expand Down
11 changes: 5 additions & 6 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
from pytest_mock import MockerFixture

import logging
import py
import pytest
import tempfile

logger = logging.getLogger(__name__)


class CassandraTestConfig:
def __init__(self, *, mocker: MockerFixture, tmpdir: py.path.local):
self.root = Path(tmpdir) / "root"
def __init__(self, *, mocker: MockerFixture, tmp_path: Path):
self.root = tmp_path / "root"

# Create fake snapshot files that we want to see being moved/removed/..
keyspace_path = self.root / "data" / "dummyks"
Expand All @@ -39,7 +38,7 @@ def __init__(self, *, mocker: MockerFixture, tmpdir: py.path.local):
self.other_snapshot_path = keyspace_path / "dummytable-123" / "snapshots" / f"not{SNAPSHOT_NAME}"
self.other_snapshot_path.mkdir(parents=True)

self.cassandra_conf = Path(tmpdir / "cassandra.yaml")
self.cassandra_conf = tmp_path / "cassandra.yaml"
self.cassandra_conf.write_text(
"""
listen_address: 127.0.0.1
Expand All @@ -63,5 +62,5 @@ def __init__(self, *, mocker: MockerFixture, tmpdir: py.path.local):


@pytest.fixture(name="cassandra_test_config")
def fixture_cassandra_test_config(mocker: MockerFixture, tmpdir: py.path.local) -> CassandraTestConfig:
return CassandraTestConfig(mocker=mocker, tmpdir=tmpdir)
def fixture_cassandra_test_config(mocker: MockerFixture, tmp_path: Path) -> CassandraTestConfig:
return CassandraTestConfig(mocker=mocker, tmp_path=tmp_path)
15 changes: 7 additions & 8 deletions tests/unit/coordinator/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@
from tests.utils import create_rohmu_config

import asyncio
import py
import pytest

_original_asyncio_sleep = asyncio.sleep


@pytest.fixture(name="storage")
def fixture_storage(tmpdir: py.path.local) -> RohmuStorage:
return RohmuStorage(config=create_rohmu_config(tmpdir))
def fixture_storage(tmp_path: Path) -> RohmuStorage:
return RohmuStorage(config=create_rohmu_config(tmp_path))


@pytest.fixture(name="mstorage")
def fixture_mstorage(tmpdir: py.path.local) -> MultiRohmuStorage:
return MultiRohmuStorage(config=create_rohmu_config(tmpdir))
def fixture_mstorage(tmp_path: Path) -> MultiRohmuStorage:
return MultiRohmuStorage(config=create_rohmu_config(tmp_path))


@pytest.fixture(name="populated_mstorage")
Expand Down Expand Up @@ -70,14 +69,14 @@ def fixture_sleepless(mocker: MockerFixture) -> None:


@pytest.fixture(name="app")
def fixture_app(mocker: MockerFixture, sleepless: None, storage: RohmuStorage, tmpdir: py.path.local) -> FastAPI:
def fixture_app(mocker: MockerFixture, sleepless: None, storage: RohmuStorage, tmp_path: Path) -> FastAPI:
app = FastAPI()
app.include_router(router, tags=["coordinator"])
app.state.coordinator_config = CoordinatorConfig(
object_storage=create_rohmu_config(tmpdir),
object_storage=create_rohmu_config(tmp_path),
plugin=Plugin.files,
plugin_config={"root_globs": ["*"]},
object_storage_cache=Path(f"{tmpdir}/cache/is/somewhere"),
object_storage_cache=tmp_path / "cache/is/somewhere",
)
app.state.coordinator_config.nodes = COORDINATOR_NODES[:]
mocker.patch.object(LockedCoordinatorOp, "get_locker", return_value="x")
Expand Down
31 changes: 15 additions & 16 deletions tests/unit/node/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@
from fastapi.testclient import TestClient
from pathlib import Path

import py
import pytest


@pytest.fixture(name="app")
def fixture_app(tmpdir: py.path.local) -> FastAPI:
def fixture_app(tmp_path: Path) -> FastAPI:
app = FastAPI()
app.include_router(node_router, prefix="/node", tags=["node"])
root = Path(tmpdir) / "root"
db_path = Path(tmpdir) / "db_path"
backup_root = Path(tmpdir) / "backup-root"
root = tmp_path / "root"
db_path = tmp_path / "db_path"
backup_root = tmp_path / "backup-root"
backup_root.mkdir()
tmp_path = Path(tmpdir) / "backup-tmp"
tmp_path = tmp_path / "backup-tmp"
root.mkdir()
(root / "foo").write_text("foobar")
(root / "foo2").write_text("foobar")
Expand Down Expand Up @@ -66,34 +65,34 @@ def fixture_uploader(storage):


@pytest.fixture(name="storage")
def fixture_storage(tmpdir: py.path.local) -> FileStorage:
storage_path = Path(tmpdir) / "storage"
def fixture_storage(tmp_path: Path) -> FileStorage:
storage_path = tmp_path / "storage"
storage_path.mkdir()
return FileStorage(storage_path)


@pytest.fixture(name="root")
def fixture_root(tmpdir: py.path.local) -> Path:
return Path(tmpdir)
def fixture_root(tmp_path: Path) -> Path:
return tmp_path


@pytest.fixture(name="src")
def fixture_src(tmpdir: py.path.local) -> Path:
src = Path(tmpdir) / "src"
def fixture_src(tmp_path: Path) -> Path:
src = tmp_path / "src"
src.mkdir()
return src


@pytest.fixture(name="dst")
def fixture_dst(tmpdir: py.path.local) -> Path:
dst = Path(tmpdir) / "dst"
def fixture_dst(tmp_path: Path) -> Path:
dst = tmp_path / "dst"
dst.mkdir()
return dst


@pytest.fixture(name="db")
def fixture_db(tmpdir: py.path.local) -> Path:
db = Path(tmpdir) / "db"
def fixture_db(tmp_path: Path) -> Path:
db = tmp_path / "db"
return db


Expand Down
10 changes: 5 additions & 5 deletions tests/unit/node/test_node_cassandra.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
from collections.abc import Callable, Sequence
from fastapi import FastAPI
from fastapi.testclient import TestClient
from pathlib import Path
from pytest_mock import MockerFixture
from requests import Response
from tests.unit.conftest import CassandraTestConfig
from types import ModuleType

import msgspec
import py
import pytest
import subprocess

Expand All @@ -29,8 +29,8 @@ def fixture_astacus_node_cassandra() -> ModuleType:
class CassandraTestEnv(CassandraTestConfig):
cassandra_node_config: CassandraNodeConfig

def __init__(self, *, app: FastAPI, client: TestClient, mocker: MockerFixture, tmpdir: py.path.local) -> None:
super().__init__(mocker=mocker, tmpdir=tmpdir)
def __init__(self, *, app: FastAPI, client: TestClient, mocker: MockerFixture, tmp_path: Path) -> None:
super().__init__(mocker=mocker, tmp_path=tmp_path)
self.app = app
self.client = client

Expand Down Expand Up @@ -59,8 +59,8 @@ def setup_cassandra_node_config(self) -> None:


@pytest.fixture(name="ctenv")
def fixture_ctenv(app: FastAPI, client: TestClient, mocker: MockerFixture, tmpdir: py.path.local) -> CassandraTestEnv:
return CassandraTestEnv(app=app, client=client, mocker=mocker, tmpdir=tmpdir)
def fixture_ctenv(app: FastAPI, client: TestClient, mocker: MockerFixture, tmp_path: Path) -> CassandraTestEnv:
return CassandraTestEnv(app=app, client=client, mocker=mocker, tmp_path=tmp_path)


@pytest.mark.parametrize(
Expand Down

0 comments on commit f0e1087

Please sign in to comment.