Skip to content

Commit

Permalink
Testing: Refactor config fixtures so that a temp file is generated an…
Browse files Browse the repository at this point in the history
…d set as Rucio config when no file is found

With this refactor, devs should be able to use pytest throughout Rucio without needing to set up a full Rucio environment
  • Loading branch information
rdimaio committed Jan 14, 2025
1 parent be439f8 commit 0ea190d
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,43 @@ def core_config_mock(request: pytest.FixtureRequest) -> "Iterator[None]":
yield


@pytest.fixture(scope="session")
def config_parser_from_temp_file() -> "Iterator[ConfigParser]":
"""
Session-scoped fixture that generates a temporary file, sets it as the Rucio config file,
and provides a ConfigParser based on this file.
Used to test when no Rucio config file is already present.
"""
import tempfile

from rucio.common.config import Config

# Create a temporary file
with tempfile.NamedTemporaryFile(delete=True) as temp:
# Set the environment variable to the name of the temporary file
with pytest.MonkeyPatch.context() as mp:
mp.setenv("RUCIO_CONFIG", temp.name)
yield Config().parser


@pytest.fixture()
def config_parser(config_parser_mock_from_temp_file) -> "Iterator[ConfigParser]":
"""
Fixture that provides a ConfigParser based on the available Rucio config.
"""
from rucio.common.config import Config
from rucio.common.exception import ConfigNotFound

try:
parser = Config().parser
except ConfigNotFound:
parser = config_parser_mock_from_temp_file

yield parser


@pytest.fixture
def file_config_mock(request: pytest.FixtureRequest) -> "Iterator[ConfigParser]":
def file_config_mock(request: pytest.FixtureRequest, config_parser) -> "Iterator[ConfigParser]":
"""
Fixture which allows to have an isolated in-memory configuration file instance which
is not persisted after exiting the fixture.
Expand All @@ -588,21 +623,20 @@ def file_config_mock(request: pytest.FixtureRequest) -> "Iterator[ConfigParser]"
"""
from unittest import mock

from rucio.common.config import Config, config_add_section, config_has_section, config_set
from rucio.common.config import config_add_section, config_has_section, config_set

# Get the fixture parameters
overrides = []
params = __get_fixture_param(request)
if params:
overrides = params.get("overrides", overrides)

parser = Config().parser
with mock.patch('rucio.common.config.get_config', side_effect=lambda: parser):
with mock.patch('rucio.common.config.get_config', side_effect=lambda: config_parser):
for section, option, value in (overrides or []):
if not config_has_section(section):
config_add_section(section)
config_set(section, option, value)
yield parser
yield config_parser


@pytest.fixture
Expand Down

0 comments on commit 0ea190d

Please sign in to comment.