Пример #1
0
    def refresh(obj: objects.ApiObject) -> None:
        """Refresh the underlying Kubernetes resource status and state.

        Args:
            obj: A kubetest API Object wrapper.
        """
        obj.refresh()
Пример #2
0
    def create(self, obj: objects.ApiObject) -> None:
        """Create the provided ApiObject on the Kubernetes cluster.

        If the object does not already have a namespace assigned to it, the client's
        generated test case namespace will be used.

        Args:
            obj: A kubetest API Object wrapper.
        """
        if obj.namespace is None:
            obj.namespace = self.namespace

        obj.create()
Пример #3
0
    def delete(self, obj: objects.ApiObject, options: client.V1DeleteOptions = None) -> None:
        """Delete the provided ApiObject from the Kubernetes cluster.

        If the object does not already have a namespace assigned to it, the client's
        generated test case namespace will be used.

        Args:
            obj: A kubetest API Object wrapper.
            options: Additional options for deleting the resource from the cluster.
        """
        if obj.namespace is None:
            obj.namespace = self.namespace

        obj.delete(options=options)
Пример #4
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)
Пример #5
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)
Пример #6
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)
Пример #7
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)