mirror of
https://github.com/scylladb/scylladb.git
synced 2026-04-20 16:40:35 +00:00
With migration to pyest this fixture is useless. Removing and setting the session to the module for the most of the tests. Add dynamic_scope function to support running alternator fixtures in session scope, while Test and TestSuite are not deleted. This is for migration period, later on this function should be deleted.
82 lines
2.7 KiB
Python
82 lines
2.7 KiB
Python
#
|
|
# Copyright (C) 2025-present ScyllaDB
|
|
#
|
|
# SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.1
|
|
#
|
|
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
from typing import TYPE_CHECKING
|
|
|
|
import pytest
|
|
|
|
from test.cqlpy.conftest import host, cql, this_dc # add required fixtures
|
|
from test.pylib.cql_repl import CQL_TEST_SUFFIX, CqlFile
|
|
from test.pylib.suite.base import get_testpy_test
|
|
from test.pylib.suite.python import add_host_option, add_cql_connection_options
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Generator
|
|
from pathlib import Path
|
|
|
|
from _pytest.nodes import Collector
|
|
from cassandra.cluster import Session
|
|
|
|
|
|
def pytest_addoption(parser: pytest.Parser) -> None:
|
|
"""Set up command line parameters.
|
|
|
|
By default, tests run against a CQL server (ScyllaDB or Cassandra) listening on `localhost:9042`.
|
|
Add the `--host` and `--port` options to allow overriding these defaults.
|
|
"""
|
|
add_host_option(parser)
|
|
add_cql_connection_options(parser)
|
|
|
|
|
|
def pytest_collect_file(file_path: Path, parent: Collector) -> Collector | None:
|
|
if file_path.name.endswith(CQL_TEST_SUFFIX):
|
|
return CqlFile.from_parent(parent=parent, path=file_path)
|
|
return None
|
|
|
|
|
|
# Test files are not Python files, so, we need to define all fixtures with `autouse=True` to have
|
|
# access to them in `CqlFile` instance via the `self.funcargs` attribute.
|
|
#
|
|
# Fixture `cql` will be used automatically too as a dependency.
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def cql_test_connection(cql: Session) -> Generator[None]:
|
|
"""Check if a DB node is still up after test finished."""
|
|
|
|
yield
|
|
|
|
try:
|
|
# We want to run a do-nothing CQL command.
|
|
# "use system" is the closest to do-nothing I could find...
|
|
cql.execute("use system")
|
|
except Exception as exc:
|
|
pytest.fail(f"Scylla appears to have crashed: {exc}")
|
|
|
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
|
def keyspace(cql: Session, this_dc: str) -> Generator[str]:
|
|
"""Create a random keyspace for this pytest session.
|
|
|
|
The keyspace is created with RF=1, and automatically deleted at the end.
|
|
"""
|
|
keyspace_name = f"test_{uuid.uuid4().hex}"
|
|
cql.execute(f"CREATE KEYSPACE {keyspace_name} "
|
|
f"WITH REPLICATION = {{ 'class' : 'NetworkTopologyStrategy', '{this_dc}' : 1 }}")
|
|
yield keyspace_name
|
|
cql.execute(f"DROP KEYSPACE {keyspace_name}")
|
|
|
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
|
async def output_path(request: pytest.FixtureRequest, build_mode: str) -> Path:
|
|
"""A file to collect real output of test's CQL queries to compare with .result file."""
|
|
|
|
testpy_test = await get_testpy_test(path=request.path, options=request.config.option, mode=build_mode)
|
|
return testpy_test.suite.log_dir / f"{testpy_test.uname}.reject"
|