Files
fm-orchestrator/tests/integration/conftest.py
Hunor Csomortáni 5f7442f8c1 Tests: Create a fixture for scenario configuration
Until now, to access the configuration of a scenario, the full path in
test_env had to be specified.

This might be cumbersome and error prone.

Create a scenario fixture, to make it easier to access the same
configuration.

Instead of

    test_env["testdata"]["my_scenario"]["my_config"]

one can use the following:

    scenario["my_config"]

Signed-off-by: Hunor Csomortáni <csomh@redhat.com>
2020-01-16 10:29:14 +01:00

80 lines
2.1 KiB
Python

# -*- coding: utf-8 -*-
# SPDX-License-Identifier: MIT
import os
import sys
import tempfile
import pytest
import sh
import yaml
import utils
our_sh = sh(_out=sys.stdout, _err=sys.stderr, _tee=True)
from our_sh import pushd, Command # noqa
@pytest.fixture(scope="session")
def test_env():
"""Load test environment configuration
:return: Test environment configuration.
:rtype: dict
"""
config_file = os.getenv("MBS_TEST_CONFIG", "test.env.yaml")
with open(config_file) as f:
env = yaml.safe_load(f)
return env
@pytest.fixture(scope="function")
def scenario(request, test_env):
"""Configuration data for the scenario
Find out the name of the scenario (anything that follows "test_"),
and return the corresponding configuration.
This is a convenience fixture to serve as a shortcut to access
scenario configuration.
"""
scenario_name = request.function.__name__.split("test_", 1)[1]
return test_env["testdata"][scenario_name]
@pytest.fixture(scope="function")
def repo(scenario, test_env):
"""Clone the module repo to be used by the scenario
Get the module repo from the scenario configuration.
Clone the repo in a temporary location and switch the current working
directory into it.
:param pytest.FixtureRequest request: request object giving access
to the requesting test context
:param pytest.fixture test_env: test environment fixture
:return: repository object the tests can work with
:rtype: utils.Repo
"""
with tempfile.TemporaryDirectory() as tempdir:
packaging_util = Command(test_env["packaging_utility"]).bake(
_out=sys.stdout, _err=sys.stderr, _tee=True
)
args = [
"--branch",
scenario["branch"],
f"modules/{scenario['module']}",
tempdir,
]
packaging_util("clone", *args)
with pushd(tempdir):
yield utils.Repo(scenario["module"])
@pytest.fixture(scope="session")
def koji(test_env):
"""Koji session for the instance MBS is configured to work with
"""
return utils.Koji(**test_env["koji"])