Exemplo n.º 1
0
def ids_yield_fixture(scope, params, autouse=False):
    return pytest.yield_fixture(
        scope=scope, 
        ids=list(map(lambda i: i[0], params)), 
        params=list(map(lambda i: i[1], params)),  
        autouse=autouse
    )
Exemplo n.º 2
0
def set_parameters_fixture(paramname, values, key=lambda x: x):
    """Return a fixture that sets and resets a global parameter to each of
    a list of values before and after each test run.  Allows
    paramname="foo.bar.var" meaning parameters["foo"]["bar"]["var"].

    Usage:
        repr = set_parameters_fixture("form_compiler.representation", ["quadrature", "uflacs"])
        my_fixture1 = set_parameters_fixture("linear_algebra_backend", ["PETSc"])
        my_fixture2 = set_parameters_fixture("linear_algebra_backend", [("Eigen", "")], key=lambda x: x[0])

        def test_something0(repr):
            assert repr in ("quadrature", "uflacs")
            assert parameters["form_compiler"]["representation"] == repr

        def test_something1(my_fixture1):
            assert my_fixture1 in ("PETSc")
            assert parameters["linear_algebra_backend"] == my_fixture1

        def test_something2(my_fixture2):
            assert my_fixture2[0] in ("Eigen")
            assert parameters["linear_algebra_backend"] == my_fixture2[0]

    Try it and see.

    """
    global parameters

    def _pushpop(request):
        gc_barrier()
        if '.' in paramname:
            names = paramname.split('.')
            if len(names) == 2:
                prev = parameters[names[0]][names[
                    1]]  # Remember original value
                parameters[names[0]][names[1]] = key(
                    request.param)  # Set value
                yield request.param  # Let test run
                parameters[names[0]][names[1]] = prev  # Reset value
            elif len(names) == 3:
                prev = parameters[names[0]][names[1]][names[
                    2]]  # Remember original value
                parameters[names[0]][names[1]][names[2]] = key(
                    request.param)  # Set value
                yield request.param  # Let test run
                parameters[names[0]][names[1]][names[2]] = prev  # Reset value
        else:
            prev = parameters[paramname]  # Remember original value
            parameters[paramname] = key(request.param)  # Set value
            yield request.param  # Let test run
            parameters[paramname] = prev  # Reset value

    return pytest.yield_fixture(scope="function", params=values)(_pushpop)
        def _decorate(f):
            if name is not None:
                # 'name' argument is not supported in this old version, use the __name__ trick.
                f.__name__ = name

            # call hook if needed
            if hook is not None:
                f = hook(f)

            # create the fixture
            if isgeneratorfunction(f):
                return pytest.yield_fixture(**kwargs)(f)
            else:
                return pytest.fixture(**kwargs)(f)
Exemplo n.º 4
0
def fixture_for_pytest(name, factories, size=1):
    """
    Create a Pytest fixture from a list of factories.
    """
    import pytest

    def fixture_function():
        try:
            yield populate(factories=factories, size=size)
        finally:
            clear_tables()

    fixture_function.__name__ = name
    return pytest.yield_fixture(fixture_function)
Exemplo n.º 5
0
def set_parameters_fixture(paramname, values, key=lambda x: x):
    """Return a fixture that sets and resets a global parameter
    to each of a list of values before and after each test run.
    Allows paramname="foo.bar.var" meaning parameters["foo"]["bar"]["var"].

    Usage:
        repr = set_parameters_fixture("form_compiler.representation", ["quadrature", "uflacs"])
        my_fixture1 = set_parameters_fixture("linear_algebra_backend", ["PETSc", "STL"])
        my_fixture2 = set_parameters_fixture("linear_algebra_backend", [("Eigen", ""), ("STL", "")], key=lambda x: x[0])

        def test_something0(repr):
            assert repr in ("quadrature", "uflacs")
            assert parameters["form_compiler"]["representation"] == repr

        def test_something1(my_fixture1):
            assert my_fixture1 in ("PETSc", "STL")
            assert parameters["linear_algebra_backend"] == my_fixture1

        def test_something2(my_fixture2):
            assert my_fixture2[0] in ("Eigen", "STL")
            assert parameters["linear_algebra_backend"] == my_fixture2[0]

    Try it and see.
    """
    global parameters
    def _pushpop(request):
        gc_barrier()
        if '.' in paramname:
            names = paramname.split('.')
            if len(names) == 2:
                prev = parameters[names[0]][names[1]]                # Remember original value
                parameters[names[0]][names[1]] = key(request.param)  # Set value
                yield request.param                                  # Let test run
                parameters[names[0]][names[1]] = prev                # Reset value
            elif len(names) == 3:
                prev = parameters[names[0]][names[1]][names[2]]                # Remember original value
                parameters[names[0]][names[1]][names[2]] = key(request.param)  # Set value
                yield request.param                                            # Let test run
                parameters[names[0]][names[1]][names[2]] = prev                # Reset value
        else:
            prev = parameters[paramname]               # Remember original value
            parameters[paramname] = key(request.param) # Set value
            yield request.param                        # Let test run
            parameters[paramname] = prev               # Reset value

    return pytest.yield_fixture(scope="function", params=values)(_pushpop)
Exemplo n.º 6
0
def create_docker_container_fixture(name,
                                    *,
                                    image=None,
                                    tag='latest',
                                    ports=None):
    def _fixture():
        nonlocal name, image, tag, ports
        image = image or name
        full_image = '{}:{}'.format(image, tag)
        ports = ports or ()

        docker_client = docker.APIClient()

        create_container = functools.partial(
            docker_client.create_container,
            image=full_image,
            labels=[LABEL],
            ports=[port for port in ports],
            host_config=docker_client.create_host_config(
                port_bindings={port: None
                               for port in ports}))

        try:
            container = create_container()
        except ImageNotFound:
            response = docker_client.pull(full_image)
            lines = [line for line in response.splitlines() if line]
            pull_result = json.loads(lines[-1])
            if "error" in pull_result:
                raise RuntimeError("Could not pull {}: {}".format(
                    image, pull_result["error"]))
            container = create_container()

        docker_client.start(container=container["Id"])
        container_info = docker_client.inspect_container(container.get('Id'))

        yield container_info

        docker_client.remove_container(container=container["Id"], force=True)

    _fixture.__name__ = name
    return pytest.yield_fixture(_fixture)
Exemplo n.º 7
0
def set_parameters_fixture(paramname, values, key=lambda x: x):
    """Return a fixture that sets and resets a global parameter
    to each of a list of values before and after each test run.

    Usage:
        my_fixture1 = set_parameters_fixture("linear_algebra_backend", ["PETSc", "STL"])
        my_fixture2 = set_parameters_fixture("linear_algebra_backend", [("uBLAS", "Sparse"), ("STL", "")], key=lambda x: x[0])

        def test_something1(my_fixture1):
            assert my_fixture1 in ("PETSc", "STL")
            assert parameters.linear_algebra_backend == my_fixture1

        def test_something2(my_fixture2):
            assert my_fixture2[0] in ("uBLAS", "STL")
            assert parameters.linear_algebra_backend == my_fixture2[0]

    Try it and see.
    """
    global parameters

    def _pushpop(request):
        gc_barrier()
        #TODO: This should be done more robust and
        #      assumes that parameters[][][] is not used.
        #TODO: Check if the backend exists?
        if '.' in paramname:
            first, second = paramname.split('.')
            prev = parameters[first][second]
            yield request.param
            parameters[first][second] = prev
        else:
            prev = parameters[paramname]  # Remember original value
            parameters[paramname] = key(request.param)  # Set value
            yield request.param  # Let test run
            parameters[paramname] = prev  # Reset value

    return pytest.yield_fixture(scope="function", params=values)(_pushpop)
Exemplo n.º 8
0
# -*- coding: utf-8 -*-

from __future__ import absolute_import

import mock
import pytest

from django.conf import settings

from nplusone.ext.django import setup_state
from tests.utils import calls  # noqa
pytest.yield_fixture(calls)

from . import models


@pytest.fixture(scope='module', autouse=True)
def setup():
    setup_state()


@pytest.fixture
def objects():
    user = models.User.objects.create()
    pet = models.Pet.objects.create(user=user)
    allergy = models.Allergy.objects.create()
    allergy.pets.add(pet)
    occupation = models.Occupation.objects.create(user=user)
    address = models.Address.objects.create(user=user)
    hobby = models.Hobby.objects.create()
    user.hobbies.add(hobby)
Exemplo n.º 9
0
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import logging

import pytest

from mos_tests import conftest
from mos_tests.environment import devops_client
from mos_tests.functions import common
from mos_tests.ironic import testutils

logger = logging.getLogger(__name__)

ubuntu_image = pytest.yield_fixture(scope='class')(testutils.ubuntu_image)


def map_interfaces(devops_env, fuel_node):
    """Return pairs of fuel_node interfaces and devops interfaces"""
    pairs = []
    devops_node = devops_env.get_node_by_mac(fuel_node.data['mac'])
    for fuel_interface in fuel_node.get_attribute('interfaces'):
        for devops_interface in devops_node.interfaces:
            if fuel_interface['mac'] == devops_interface.mac_address:
                pairs.append((fuel_interface, devops_interface))
                continue
    return pairs


@pytest.yield_fixture(scope='class')
Exemplo n.º 10
0
#    License for the specific language governing permissions and limitations
#    under the License.

import json
import logging

import pytest

from mos_tests import conftest
from mos_tests.environment import devops_client
from mos_tests.functions import common
from mos_tests.ironic import testutils

logger = logging.getLogger(__name__)

make_image = pytest.yield_fixture(scope='class')(testutils.make_image)


def map_interfaces(devops_env, fuel_node):
    """Return pairs of fuel_node interfaces and devops interfaces"""
    pairs = []
    devops_node = devops_env.get_node_by_fuel_node(fuel_node)
    for fuel_interface in fuel_node.get_attribute('interfaces'):
        for devops_interface in devops_node.interfaces:
            if fuel_interface['mac'] == devops_interface.mac_address:
                pairs.append((fuel_interface, devops_interface))
                continue
    return pairs


def remove_ceph_from_node(remote):
    flavors = []
    for i, config in enumerate(ironic_drivers_params):
        flavor = os_conn.nova.flavors.create(
            name='baremetal_{}'.format(i),
            ram=config['node_properties']['memory_mb'],
            vcpus=config['node_properties']['cpus'],
            disk=config['node_properties']['local_gb'])
        flavors.append(flavor)

    yield flavors

    for flavor in flavors:
        flavor.delete()


ubuntu_image = pytest.yield_fixture()(testutils.ubuntu_image)


def make_ironic_node(config, devops_env, ironic, name, fuel_env):

    baremetal_interface = devops_env.get_interface_by_fuel_name('baremetal',
                                                                fuel_env)
    baremetal_net_name = baremetal_interface.network.name

    devops_node = None
    if config['driver'] == 'fuel_libvirt':
        devops_node = devops_env.add_node(
            name=name,
            vcpu=config['node_properties']['cpus'],
            memory=config['node_properties']['memory_mb'],
            disks=[config['node_properties']['local_gb']],
Exemplo n.º 12
0
def gendata(length):
    return tuple(
        DataObject(
            ref=rndstr(5, 20),
            data={
                rndstr(5, 20): rndstr(0, 25)
                for _ in range(random.randint(0, 25))
            }
        )
        for _ in range(length)
    )


available_backends = [backend for backend in get_backends() if backend.is_connected()]
backend_fixture = pytest.yield_fixture(params=available_backends, ids=[backend.__name__ for backend in available_backends])


def _backend(request):
    backend = request.param
    request.applymarker(getattr(pytest.mark, backend.__name__)())
    inst = backend(**backend.settings_for('test', 'test', request.fixturename))
    yield inst
    inst.unset_all()


def backend():
    return types.FunctionType(
        _backend.__code__,
        _backend.__globals__,
        name=_backend.__name__,
Exemplo n.º 13
0
    flavors = []
    for i, config in enumerate(ironic_drivers_params):
        flavor = os_conn.nova.flavors.create(
            name='baremetal_{}'.format(i),
            ram=config['node_properties']['memory_mb'],
            vcpus=config['node_properties']['cpus'],
            disk=config['node_properties']['local_gb'])
        flavors.append(flavor)

    yield flavors

    for flavor in flavors:
        flavor.delete()


ubuntu_image = pytest.yield_fixture()(testutils.ubuntu_image)


def make_devops_node(config, devops_env, fuel_env, name):
    """Creates devops ironic_slave node"""
    baremetal_interface = devops_env.get_interface_by_fuel_name(
        'baremetal', fuel_env)
    baremetal_net_name = baremetal_interface.network.name

    devops_node = devops_env.add_node(
        name=name,
        vcpu=config['node_properties']['cpus'],
        memory=config['node_properties']['memory_mb'],
        disks=[config['node_properties']['local_gb']],
        networks=[baremetal_net_name],
        role='ironic_slave')
Exemplo n.º 14
0
def teardown(func):
    def teardown_(*args, **kwargs):
        yield
        func(*args, **kwargs)
    return pytest.yield_fixture(autouse=True)(teardown_)
from ironblogger.tasks import import_bloggers, export_bloggers
from ironblogger.model import Blogger
from ironblogger.date import now
from six.moves import StringIO
from datetime import datetime
from random import Random
from tests.util import fresh_context
from pprint import pformat
from .util.example_data import databases as example_databases
from .util.randomize import random_database
import arrow
import pytest
import json
import difflib

fresh_context = pytest.yield_fixture(autouse=True)(fresh_context)

legacy_yaml = """
alice:
    links:
        - [Fun With Crypto, "http://example.com/alice/blog.html", "http://example.com/alice/rss.xml"]
    start: 2015-04-01
bob:
    links:
        - [Secret Messages, "http://example.com/bob/secrets/blog.html", "http://example.com/bob/secrets/feed"]
        - [Kittens, "http://example.com/bob/kittens", "http://example.com/bob/kittens/feed.atom"]
    start: 2015-04-08
"""


def test_import_bloggers():
Exemplo n.º 16
0
    flavors = []
    for i, config in enumerate(ironic_drivers_params):
        flavor = os_conn.nova.flavors.create(
            name='baremetal_{}'.format(i),
            ram=config['node_properties']['memory_mb'],
            vcpus=config['node_properties']['cpus'],
            disk=config['node_properties']['local_gb'])
        flavors.append(flavor)

    yield flavors

    for flavor in flavors:
        flavor.delete()


make_image = pytest.yield_fixture()(testutils.make_image)


def make_devops_node(config, devops_env, fuel_env, name):
    """Creates devops ironic_slave node"""
    baremetal_interface = devops_env.get_interface_by_fuel_name('baremetal',
                                                                fuel_env)
    baremetal_net_name = baremetal_interface.network.name

    devops_node = devops_env.add_node(
        name=name,
        vcpu=config['node_properties']['cpus'],
        memory=config['node_properties']['memory_mb'],
        disks=[config['node_properties']['local_gb']],
        networks=[baremetal_net_name],
        role='ironic_slave')
Exemplo n.º 17
0
"""test_level3_dithers: Test of dither rules."""
from __future__ import absolute_import
import pytest

from .helpers import (
    generate_params,
    generate_pool_paths,
    mkstemp_pool_file,
    t_path
)

from ..main import Main

pool_path = pytest.yield_fixture(
    scope='module',
    params=['data/pool_001_candidates.csv']
)(generate_pool_paths)


pool_params = pytest.fixture(
    scope='module',
    params=[
        # Invalid ACID
        (
            ['-i', 'nosuchid'],
            0
        ),
        # Basic observation ACIDs
        (
            ['-i', 'o001'],
            2
Exemplo n.º 18
0
def class_teardown(func):
    def teardown_(*args, **kwargs):
        yield
        func(*args, **kwargs)
    return pytest.yield_fixture(autouse=True, scope='class')(teardown_)
Exemplo n.º 19
0
def class_setup_teardown(func):
    return pytest.yield_fixture(autouse=True, scope='class')(func)
Exemplo n.º 20
0
                new_callable=new_callable,
                **kwargs
            )


def _mocker(pytestconfig: Any) -> Generator[MockerFixture, None, None]:
    """
    Return an object that has the same interface to the `mock` module, but
    takes care of automatically undoing all patches after each test method.
    """
    result = MockerFixture(pytestconfig)
    yield result
    result.stopall()


mocker = pytest.yield_fixture()(_mocker)  # default scope is function
class_mocker = pytest.yield_fixture(scope="class")(_mocker)
module_mocker = pytest.yield_fixture(scope="module")(_mocker)
package_mocker = pytest.yield_fixture(scope="package")(_mocker)
session_mocker = pytest.yield_fixture(scope="session")(_mocker)


_mock_module_patches = []  # type: List[Any]
_mock_module_originals = {}  # type: Dict[str, Any]


def assert_wrapper(
    __wrapped_mock_method__: Callable[..., Any], *args: Any, **kwargs: Any
) -> None:
    __tracebackhide__ = True
    try:
Exemplo n.º 21
0
    from unittest import mock
except ImportError:  # python < 3
    import mock

import pytest

from ..api import fetch_catalog_event_json

__author__ = 'Duncan Macleod <*****@*****.**>'

# unset these variables to allow the tests to run with pybuild
os.environ.pop('http_proxy', None)
os.environ.pop('https_proxy', None)

try:
    autouseyield = pytest.yield_fixture(autouse=True)
except AttributeError:  # pytest > 3.0.0
    autouseyield = pytest.fixture(autouse=True)


# force all tests to not rely on the catalog cache
@autouseyield
def clear_catalog_cache():
    with mock.patch.dict("gwosc.catalog.CACHE", clear=True):
        yield


def _event_urls(name):
    return fetch_catalog_event_json(name)['strain']

Exemplo n.º 22
0

def gendata(length):
    return tuple(
        DataObject(ref=rndstr(5, 20),
                   data={
                       rndstr(5, 20): rndstr(0, 25)
                       for _ in range(random.randint(0, 25))
                   }) for _ in range(length))


available_backends = [
    backend for backend in get_backends() if backend.is_connected()
]
backend_fixture = pytest.yield_fixture(
    params=available_backends,
    ids=[backend.__name__ for backend in available_backends])


def _backend(request):
    backend = request.param
    request.applymarker(getattr(pytest.mark, backend.__name__)())
    inst = backend(**backend.settings_for('test', 'test', request.fixturename))
    yield inst
    inst.unset_all()


def backend():
    return types.FunctionType(
        _backend.__code__,
        _backend.__globals__,
Exemplo n.º 23
0
def setup_teardown(func):
    return pytest.yield_fixture(autouse=True)(func)
from ironblogger.tasks import import_bloggers, export_bloggers
from ironblogger.model import Blogger
from ironblogger.date import now
from six.moves import StringIO
from datetime import datetime
from random import Random
from tests.util import fresh_context
from pprint import pformat
from .util.example_data import databases as example_databases
from .util.randomize import random_database
import arrow
import pytest
import json
import difflib

fresh_context = pytest.yield_fixture(autouse=True)(fresh_context)

legacy_yaml = """
alice:
    links:
        - [Fun With Crypto, "http://example.com/alice/blog.html", "http://example.com/alice/rss.xml"]
    start: 2015-04-01
bob:
    links:
        - [Secret Messages, "http://example.com/bob/secrets/blog.html", "http://example.com/bob/secrets/feed"]
        - [Kittens, "http://example.com/bob/kittens", "http://example.com/bob/kittens/feed.atom"]
    start: 2015-04-08
"""


def test_import_bloggers():
Exemplo n.º 25
0
def for_module(func):
    from arjuna import Arjuna
    return pytest.yield_fixture(scope="module")(simple_dec(func))
Exemplo n.º 26
0
        'maintenance': {
            'maintenance_project': 'maintenance',
            # Keystone url acts as dummy for posting
            'url': 'http://127.0.0.1:9999/test/'
        }
    })
    config.load_extensions()


fresh_database = pytest.fixture(fresh_database)
additional_database = pytest.fixture(additional_db)
fail_on_log_warnings = pytest.fixture(fail_on_log_warnings)
server_init = pytest.fixture(server_init)


with_request_context = pytest.yield_fixture(with_request_context)


@pytest.fixture
def set_admin_auth():
    """Set admin auth for all calls"""
    get_auth_backend().set_admin(True)


@pytest.fixture
def maintenance_proj_init():
    """Create maintenance project."""
    api.project_create('maintenance')


def new_node(name):
Exemplo n.º 27
0
def for_test(func):
    from arjuna import Arjuna
    return pytest.yield_fixture(scope="function")(simple_dec(func))
Exemplo n.º 28
0
Arquivo: auth.py Projeto: djfinn14/hil
            'hil.ext.auth.null': None,
            'hil.ext.switches.mock': '',
            'hil.ext.obm.mock': ''
        },
    })
    config.load_extensions()


@pytest.fixture
def server_init():
    server.register_drivers()
    server.validate_state()


fresh_database = pytest.fixture(fresh_database)
with_request_context = pytest.yield_fixture(with_request_context)


pytestmark = pytest.mark.usefixtures('configure',
                                     'fresh_database',
                                     'additional_db',
                                     'server_init',
                                     'with_request_context')


# We have a *lot* of different parameters with which we're going to invoke
# `test_auth_call`, below. Rather than passing one giant list to the decorator
# in-line, we construct it in stages here:


auth_call_params = [
#    License for the specific language governing permissions and limitations
#    under the License.

import json
import logging

import pytest

from mos_tests import conftest
from mos_tests.environment import devops_client
from mos_tests.functions import common
from mos_tests.ironic import testutils

logger = logging.getLogger(__name__)

ubuntu_image = pytest.yield_fixture(scope='class')(testutils.ubuntu_image)


def map_interfaces(devops_env, fuel_node):
    """Return pairs of fuel_node interfaces and devops interfaces"""
    pairs = []
    devops_node = devops_env.get_node_by_fuel_node(fuel_node)
    for fuel_interface in fuel_node.get_attribute('interfaces'):
        for devops_interface in devops_node.interfaces:
            if fuel_interface['mac'] == devops_interface.mac_address:
                pairs.append((fuel_interface, devops_interface))
                continue
    return pairs


def remove_ceph_from_node(remote):