Beispiel #1
0
def apply_manifest_from_marker(item, client):
    """Load manifests and create the API objects for the specified files.

    This gets called for every `pytest.mark.applymanifests` marker on
    test cases.

    Once a manifest has been loaded, the API objects will be registered
    with the test cases' TestMeta. This allows some easier control via
    the "kube" fixture, such as waiting for all objects to be created.

    Args:
        item: The pytest test item.
        client (manager.TestMeta): The metainfo object for the marked
            test case.
    """
    for mark in item.iter_markers(name='applymanifests'):
        dir_path = mark.args[0]
        files = mark.kwargs.get('files')

        # We expect the path specified to either be absolute or relative
        # from the test file. If the path is relative, add the directory
        # that the test file resides in as a prefix to the dir_path.
        if not os.path.isabs(dir_path):
            dir_path = os.path.abspath(
                os.path.join(os.path.dirname(item.fspath), dir_path))

        # If there are any files specified, we will only load those files.
        # Otherwise, we'll load everything in the directory.
        if files is None:
            objs = load_path(dir_path)
        else:
            objs = [
                obj for objs in load_file(os.path.join(dir_path, f))
                for f in files
            ]

        # For each of the loaded Kubernetes resources, we'll want to wrap it
        # in the equivalent kubetest wrapper. If the resource does not have
        # an equivalent kubetest wrapper, error out. We cannot reliably create
        # the resource without our ApiObject wrapper semantics.
        wrapped = []
        for obj in objs:
            found = False
            for klass in ApiObject.__subclasses__():
                if obj.kind == klass.__name__:
                    wrapped.append(klass(obj))
                    found = True
                    break
            if not found:
                raise ValueError(
                    'Unable to match loaded object to an internal wrapper '
                    'class: {}'.format(obj))

        client.register_objects(wrapped)
Beispiel #2
0
def apply_manifest_from_marker(item: pytest.Item,
                               meta: manager.TestMeta) -> None:
    """Load a manifest and create the API objects for the specified file.

    This gets called for every `pytest.mark.applymanifest` marker on
    test cases.

    Once the manifest has been loaded, the API object(s) will be registered with
    the test cases' TestMeta. This allows easier control via the "kube" fixture,
    such as waiting for all objects to be created.

    Args:
        item: The pytest test item.
        meta: The metainfo object for the marked test case.
    """
    item_renderer = get_manifest_renderer_for_item(item)
    for mark in item.iter_markers(name="applymanifest"):
        path = mark.args[0]
        renderer = mark.kwargs.get("renderer", item_renderer)
        if not callable(renderer):
            raise TypeError("renderer given is not callable")

        # Normalize the path to be absolute.
        if not os.path.isabs(path):
            path = os.path.abspath(path)

        # Load the manifest
        context = dict(namespace=meta.ns,
                       test_node_id=meta.node_id,
                       test_name=meta.name)
        context_renderer = ContextRenderer(renderer, context)
        objs = load_file(path, renderer=context_renderer)

        # For each of the loaded Kubernetes resources, wrap it in the
        # equivalent kubetest wrapper. If the object does not yet have a
        # wrapper, error out. We cannot reliably create the resource
        # without our ApiObject wrapper semantics.
        wrapped = []
        found = False
        for obj in objs:
            for klass in ApiObject.__subclasses__():
                if obj.kind == klass.__name__:
                    wrapped.append(klass(obj))
                    found = True
                    break
            if not found:
                raise ValueError(
                    f"Unable to match loaded object to an internal wrapper class: {obj}",
                )

        meta.register_objects(wrapped)
Beispiel #3
0
def apply_manifest_from_marker(item, client):
    """Load a manifest and create the API objects for teh specified file.

    This gets called for every `pytest.mark.applymanifest` marker on
    test cases.

    Once the manifest has been loaded, the API object(s) will be registered
    with the test cases' TestMeta. This allows easier control via the
    "kube" fixture, such as waiting for all objects to be created.

    Args:
        item: The pytest test item.
        client (manager.TestMeta): The metainfo object for the marked
            test case.
    """
    for mark in item.iter_markers(name='applymanifest'):
        path = mark.args[0]

        # Normalize the path to be absolute.
        if not os.path.isabs(path):
            path = os.path.abspath(path)

        # Load the manifest
        objs = load_file(path)

        # For each of the loaded Kubernetes resources, wrap it in the
        # equivalent kubetest wrapper. If the object does not yet have a
        # wrapper, error out. We cannot reliably create the resource
        # without our ApiObject wrapper semantics.
        wrapped = []
        found = False
        for obj in objs:
            for klass in ApiObject.__subclasses__():
                if obj.kind == klass.__name__:
                    wrapped.append(klass(obj))
                    found = True
                    break
            if not found:
                raise ValueError(
                    'Unable to match loaded object to an internal wrapper '
                    'class: {}'.format(obj))

        client.register_objects(wrapped)
Beispiel #4
0
def apply_manifests_from_marker(item: pytest.Item,
                                meta: manager.TestMeta) -> None:
    """Load manifests and create the API objects for the specified files.

    This gets called for every `pytest.mark.applymanifests` marker on test cases.

    Once a manifest has been loaded, the API objects will be registered with the
    test cases' TestMeta. This allows some easier control via the "kube" fixture,
    such as waiting for all objects to be created.

    Args:
        item: The pytest test item.
        meta: The metainfo object for the marked test case.
    """
    item_renderer = get_manifest_renderer_for_item(item)
    for mark in item.iter_markers(name="applymanifests"):
        dir_path = mark.args[0]
        files = mark.kwargs.get("files")
        renderer = mark.kwargs.get("renderer", item_renderer)

        if not callable(renderer):
            raise TypeError("renderer given is not callable")

        # We expect the path specified to either be absolute or relative
        # from the test file. If the path is relative, add the directory
        # that the test file resides in as a prefix to the dir_path.
        if not os.path.isabs(dir_path):
            dir_path = os.path.abspath(
                os.path.join(os.path.dirname(item.fspath), dir_path))

        # Setup template rendering context
        context = dict(
            dir_path=dir_path,
            namespace=meta.ns,
            test_node_id=meta.node_id,
            test_name=meta.name,
        )
        context_renderer = ContextRenderer(renderer, context)

        # If there are any files specified, we will only load those files.
        # Otherwise, we'll load everything in the directory.
        if files is None:
            objs = load_path(dir_path, renderer=context_renderer)
        else:
            objs = []
            context_renderer.context["objs"] = objs
            for f in files:
                objs.extend(
                    load_file(os.path.join(dir_path, f),
                              renderer=context_renderer))

        # For each of the loaded Kubernetes resources, we'll want to wrap it
        # in the equivalent kubetest wrapper. If the resource does not have
        # an equivalent kubetest wrapper, error out. We cannot reliably create
        # the resource without our ApiObject wrapper semantics.
        wrapped = []
        for obj in objs:
            found = False
            for klass in ApiObject.__subclasses__():
                if obj.kind == klass.__name__:
                    wrapped.append(klass(obj))
                    found = True
                    break
            if not found:
                raise ValueError(
                    f"Unable to match loaded object to an internal wrapper class: {obj}",
                )

        meta.register_objects(wrapped)