def test_kubernetes_metrics_get_memory_used(
        kubernetes_metrics_obj: KubernetesMetrics) -> None:
    """
    This test verifies that KubernetesMetrics.get_memory_used() returns the correct value for defined metrics.

    :param kubernetes_metrics_obj: The pre-loaded KubernetesMetrics fixture object for testing.
    """
    assert kubernetes_metrics_obj.get_memory_used("node") == "14%"
    assert kubernetes_metrics_obj.get_memory_used("pod") is None
Example #2
0
    def top_pods(self, ignore_errors: bool = False) -> KubernetesMetrics:
        # get the top values #
        top_pods_raw: AnyStr = self.do("top pods --no-headers=true",
                                       ignore_errors)
        top_pods_lines: List = top_pods_raw.decode().split("\n")

        # store the top pods values #
        top_pods_info: Dict = dict()

        # format data from call #
        for top_pods_line in top_pods_lines:
            # split by whitespace #
            pod_info: List = top_pods_line.split()

            # make sure the right number of attributes exist #
            if len(pod_info) == 3:
                pod_name: Text = pod_info[0]

                top_pods_info[pod_name]: Dict = dict()
                top_pods_info[pod_name][
                    KubernetesMetrics.Keys.CPU_CORES]: Text = pod_info[1]
                top_pods_info[pod_name][KubernetesMetrics.Keys.CPU_USED] = None
                top_pods_info[pod_name][
                    KubernetesMetrics.Keys.MEMORY_BYTES]: Text = pod_info[2]
                top_pods_info[pod_name][
                    KubernetesMetrics.Keys.MEMORY_USED] = None

        return KubernetesMetrics(top_pods_info)
def test_kubernetes_metrics_resource_metrics_as_dict_missing(
        kubernetes_metrics_obj: KubernetesMetrics) -> None:
    """
    This test verifies that KubernetesMetrics.resource_metrics_as_dict() returns None for undefined resource metrics.

    :param kubernetes_metrics_obj: The pre-loaded KubernetesMetrics fixture object for testing.
    """
    assert kubernetes_metrics_obj.resource_metrics_as_dict("foo") is None
def test_kubernetes_metrics_get_memory_used_missing(
        kubernetes_metrics_obj: KubernetesMetrics) -> None:
    """
    This test verifies that KubernetesMetrics.get_memory_used() returns None for undefined metrics.

    :param kubernetes_metrics_obj: The pre-loaded KubernetesMetrics fixture object for testing.
    """
    assert kubernetes_metrics_obj.get_memory_used("foo") is None
def kubernetes_metrics_obj() -> KubernetesMetrics:
    """
    This pytest fixture loads a KubernetesMetrics object representing the Metrics data as it might be returned from
    kubectl. With the "module" scope, this is only run once before the tests defined in this file are executed.

    :return: A KubernetesMetrics object loaded with Kubernetes metrics data for testing.
    """
    metrics_dict: Dict = load_test_data(_METRICS_TEST_DATA_)
    return KubernetesMetrics(metrics_dict)
Example #6
0
    def top_pods(self, ignore_errors: bool = False) -> KubernetesMetrics:
        # raise a CalledProcessError if an unexpected namespace is given
        if self.namespace != self.Values.NAMESPACE:
            raise CalledProcessError(1,
                                     f"kubectl -n {self.namespace} top pods")

        # if metrics should not be included, raise an error like the live Kubectl method would
        if not self.include_metrics or self.simulate_empty_deployment:
            raise CalledProcessError(1, "kubectl top pods")

        # otherwise load the data from file and return as a KubernetesMetrics object
        raw_metrics: Dict = KubectlTest._load_response_data(_TOP_PODS_DATA_)
        return KubernetesMetrics(raw_metrics)
def test_kubernetes_metrics_as_dict(
        kubernetes_metrics_obj: KubernetesMetrics) -> None:
    """
    This test verifies that KubernetesMetrics.as_dict() returns the correct dictionary for the defined metrics.

    :param kubernetes_metrics_obj: The pre-loaded KubernetesMetrics fixture object for testing.
    """
    # convert the object to a dict
    converted_metrics: Dict = kubernetes_metrics_obj.as_dict()

    # make sure it's the correct type, length, and has the expected keys
    assert isinstance(converted_metrics, dict)
    assert len(converted_metrics) == 2
    assert "node" in converted_metrics
    assert "pod" in converted_metrics
def test_kubernetes_metrics_resource_metrics_as_dict(
        kubernetes_metrics_obj: KubernetesMetrics) -> None:
    """
    This test verifies that KubernetesMetrics.resource_metrics_as_dict() returns the correct dictionary for defined
    resource metrics.

    :param kubernetes_metrics_obj: The pre-loaded KubernetesMetrics fixture object for testing.
    """
    # get a single metrics dict
    node_metrics: Dict = kubernetes_metrics_obj.resource_metrics_as_dict(
        "node")

    # make sure it is the correct type, length, and has the expected keys
    assert isinstance(node_metrics, dict)
    assert len(node_metrics) == 4
    assert KubernetesMetrics.Keys.CPU_CORES in node_metrics
    assert KubernetesMetrics.Keys.CPU_USED in node_metrics
    assert KubernetesMetrics.Keys.MEMORY_BYTES in node_metrics
    assert KubernetesMetrics.Keys.MEMORY_USED in node_metrics
def test_kubernetes_metrics_resource_metrics_as_dict_incomplete() -> None:
    """
    This test verifies that KubernetesMetrics.resource_metrics_as_dict() returns None when no metrics are defined.
    """
    assert KubernetesMetrics(dict()).resource_metrics_as_dict("node") is None
def test_kubernetes_metrics_get_memory_used_incomplete() -> None:
    """
    This test verifies that KubernetesMetrics.get_memory_used() returns None when no metrics are defined.
    """
    assert KubernetesMetrics(dict()).get_memory_used("node") is None