Esempio n. 1
0
def test_check_alert_resets(get_value_store, time_time) -> None:

    params: kube_pod_status.Params = {
        "Pending": ("levels", (60, 120)),
        "Running": ("levels", (60, 120)),
    }
    section_kube_pod_containers = None

    pod_cycles = (
        PodLifeCycle(phase="pending"),
        PodLifeCycle(phase="pending"),
        PodLifeCycle(phase="running"),
        PodLifeCycle(phase="pending"),
    )

    expected_results = (
        (State.OK, "0 seconds"),
        (
            State.WARN,
            "1 minute 0 seconds (warn/crit at 1 minute 0 seconds/2 minutes 0 seconds)",
        ),
        (State.OK, "0 seconds"),
        (State.OK, "0 seconds"),
    )

    for expected_result, section_kube_pod_lifecycle in zip(
            expected_results, pod_cycles):
        expected_state, expected_message = expected_result
        for result in check_kube_pod_status(params,
                                            section_kube_pod_containers, None,
                                            section_kube_pod_lifecycle):
            assert isinstance(result, Result)
            assert result.state == expected_state
            assert result.summary.endswith(expected_message)
Esempio n. 2
0
def test_check_alert_resets(get_value_store, time_time) -> None:

    params = kube_pod_status.Params(groups=[
        (("levels", (60, 120)), ["Pending"]),
        (("levels", (60, 120)), ["Running"]),
        ("no_levels", [".*"]),
    ])
    section_kube_pod_containers = None

    pod_cycles = (
        PodLifeCycle(phase="pending"),
        PodLifeCycle(phase="pending"),
        PodLifeCycle(phase="running"),
        PodLifeCycle(phase="pending"),
    )

    expectations = (
        (
            State.OK,
            "0 seconds",
            None,
        ),
        (
            State.WARN,
            "1 minute 0 seconds (warn/crit at 1 minute 0 seconds/2 minutes 0 seconds)",
            None,
        ),
        (
            State.OK,
            "0 seconds",
            None,
        ),
        (
            State.OK,
            "0 seconds",
            None,
        ),
    )

    for expected, section_kube_pod_lifecycle in zip(expectations, pod_cycles):
        expected_state, expected_summary, expected_notice = expected
        summary_result, *notice = check_kube_pod_status(
            params, section_kube_pod_containers, None,
            section_kube_pod_lifecycle)
        assert isinstance(summary_result, Result)
        assert summary_result.state == expected_state
        assert summary_result.summary.endswith(expected_summary)
        if expected_notice is None:
            assert [] == notice
        else:
            assert isinstance(notice[0], Result)
            assert expected_notice == notice[0].details
Esempio n. 3
0
def test_check_alert_if_pending_too_long(get_value_store, time_time) -> None:

    section_kube_pod_containers = None
    section_kube_pod_lifecycle = PodLifeCycle(phase="pending")
    params = kube_pod_status.Params(groups=[
        (("levels", (60, 120)), ["Pending"]),
        ("no_levels", [".*"]),
    ])

    expected_results = (
        (State.OK, "0 seconds"),
        (State.WARN,
         "1 minute 0 seconds (warn/crit at 1 minute 0 seconds/2 minutes 0 seconds)"
         ),
        (State.CRIT,
         "2 minutes 0 seconds (warn/crit at 1 minute 0 seconds/2 minutes 0 seconds)"
         ),
    )
    for expected_result in expected_results:
        expected_state, expected_message = expected_result
        summary_result, *_ = check_kube_pod_status(
            params, section_kube_pod_containers, None,
            section_kube_pod_lifecycle)
        assert isinstance(summary_result, Result)
        assert summary_result.state == expected_state
        assert summary_result.summary.endswith(expected_message)
Esempio n. 4
0
def test_schemata_did_not_diverge() -> None:
    assert ClusterInfoA.schema() == ClusterInfoC.schema()
    assert agent.CollectorProcessingLogs.schema(
    ) == check.CollectorProcessingLogs.schema()
    assert agent.CollectorComponentsMetadata.schema(
    ) == check.CollectorComponentsMetadata.schema()
    assert ContainerCountA.schema() == ContainerCountC.schema()
    assert ContainerRunningStateA.schema() == ContainerRunningStateC.schema()
    assert ContainerTerminatedStateA.schema(
    ) == ContainerTerminatedStateC.schema()
    assert ContainerWaitingStateA.schema() == ContainerWaitingStateC.schema()
    assert agent.DeploymentConditions.schema(
    ) == check.DeploymentConditions.schema()
    assert KubeletInfoA.schema() == KubeletInfoC.schema()
    assert NodeCountA.schema() == NodeCountC.schema()
    assert NodeInfoA.schema() == NodeInfoC.schema()
    assert PodConditionA.schema() == PodConditionC.schema()
    assert PodConditionsA.schema() == PodConditionsC.schema()
    assert PodContainersA.schema() == PodContainersC.schema()
    assert PodResourcesA.schema() == PodResourcesC.schema()
    assert PodLifeCycleA.schema() == PodLifeCycleC.schema()
    assert AllocatablePodsA.schema() == AllocatablePodsC.schema()
    assert ResourcesA.schema() == ResourcesC.schema()
    assert StartTimeA.schema() == StartTimeC.schema()
    assert PodInfoA.schema() == PodInfoC.schema()
    assert ReplicasA.schema() == ReplicasC.schema()
    assert agent.DeploymentStrategy.schema(
    ) == k8s_check.DeploymentStrategy.schema()
    assert agent.NodeConditions.schema() == check.NodeConditions.schema()
    assert agent.PerformanceUsage.schema(
    ) == k8s_check.PerformanceUsage.schema()
Esempio n. 5
0
def test_schemata_did_not_diverge() -> None:
    assert ClusterInfoA.schema() == ClusterInfoC.schema()
    assert agent.CollectorLogs.schema() == check.CollectorLogs.schema()
    assert ContainerCountA.schema() == ContainerCountC.schema()
    assert ContainerInfoA.schema() == ContainerInfoC.schema()
    assert ContainerRunningStateA.schema() == ContainerRunningStateC.schema()
    assert ContainerTerminatedStateA.schema(
    ) == ContainerTerminatedStateC.schema()
    assert ContainerWaitingStateA.schema() == ContainerWaitingStateC.schema()
    assert agent.DeploymentConditions.schema(
    ) == check.DeploymentConditions.schema()
    assert KubeletInfoA.schema() == KubeletInfoC.schema()
    assert MemoryA.schema() == MemoryC.schema()
    assert NodeCountA.schema() == NodeCountC.schema()
    assert NodeInfoA.schema() == NodeInfoC.schema()
    assert PodConditionA.schema() == PodConditionC.schema()
    assert PodConditionsA.schema() == PodConditionsC.schema()
    assert PodContainersA.schema() == PodContainersC.schema()
    assert PodResourcesA.schema() == PodResourcesC.schema()
    assert PodLifeCycleA.schema() == PodLifeCycleC.schema()
    assert AllocatablePodsA.schema() == AllocatablePodsC.schema()
    assert ResourcesA.schema() == ResourcesC.schema()
    assert StartTimeA.schema() == StartTimeC.schema()
    assert PodInfoA.schema() == PodInfoC.schema()
    assert ReplicasA.schema() == ReplicasC.schema()
    assert DeploymentSpecA.schema() == DeploymentSpecC.schema()
Esempio n. 6
0
def test_check_group_order_matters(get_value_store, time_time) -> None:

    params = kube_pod_status.Params(groups=[
        ("no_levels", [".*"]),
        (("levels", (60, 120)), [".*"]),
    ])
    section_kube_pod_containers = None

    pod_cycles = (
        PodLifeCycle(phase="pending"),
        PodLifeCycle(phase="pending"),
        PodLifeCycle(phase="pending"),
    )

    for section_kube_pod_lifecycle in pod_cycles:
        for summary_result in check_kube_pod_status(
                params, section_kube_pod_containers, None,
                section_kube_pod_lifecycle):
            assert isinstance(summary_result, Result)
            assert summary_result.state == State.OK
def test_schemata_did_not_diverge() -> None:
    assert ClusterInfoA.schema() == ClusterInfoC.schema()
    assert ContainerCountA.schema() == ContainerCountC.schema()
    assert ContainerInfoA.schema() == ContainerInfoC.schema()
    assert ContainerRunningStateA.schema() == ContainerRunningStateC.schema()
    assert ContainerTerminatedStateA.schema(
    ) == ContainerTerminatedStateC.schema()
    assert ContainerWaitingStateA.schema() == ContainerWaitingStateC.schema()
    assert KubeletInfoA.schema() == KubeletInfoC.schema()
    assert MemoryA.schema() == MemoryC.schema()
    assert NodeCountA.schema() == NodeCountC.schema()
    assert NodeInfoA.schema() == NodeInfoC.schema()
    assert PodConditionA.schema() == PodConditionC.schema()
    assert PodConditionsA.schema() == PodConditionsC.schema()
    assert PodContainersA.schema() == PodContainersC.schema()
    assert PodResourcesA.schema() == PodResourcesC.schema()
    assert PodLifeCycleA.schema() == PodLifeCycleC.schema()
    assert PodResourcesWithCapacityA.schema(
    ) == PodResourcesWithCapacityC.schema()
    assert ResourcesA.schema() == ResourcesC.schema()
    assert StartTimeA.schema() == StartTimeC.schema()
    assert PodInfoA.schema() == PodInfoC.schema()
Esempio n. 8
0
def parse_kube_pod_lifecycle(string_table: StringTable) -> PodLifeCycle:
    """
    >>> parse_kube_pod_lifecycle([['{"phase": "running"}']])
    PodLifeCycle(phase=<Phase.RUNNING: 'running'>)
    """
    return PodLifeCycle(**json.loads(string_table[0][0]))
Esempio n. 9
0
        state=state,
        restart_count=0,
    )


@pytest.mark.parametrize(
    "section_kube_pod_containers, section_kube_pod_lifecycle, expected_result",
    [
        pytest.param(
            PodContainers(containers={
                "running":
                _mocked_container_info_from_state(
                    ContainerRunningState(type="running",
                                          start_time=1639135964)),
            }, ),
            PodLifeCycle(phase="running"),
            [Result(state=State.OK, summary="Running")],
            id="A single running container",
        ),
        pytest.param(
            PodContainers(
                containers={
                    "downloading-container":
                    _mocked_container_info_from_state(
                        ContainerWaitingState(type="waiting",
                                              reason="ContainerCreating",
                                              detail=None))
                }),
            PodLifeCycle(phase="pending"),
            [
                Result(state=State.OK, summary="Pending: since 0 seconds"),
Esempio n. 10
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2021 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.
import pytest

from cmk.base.plugins.agent_based.agent_based_api.v1 import Result, State
from cmk.base.plugins.agent_based.kube_pod_phase import check_kube_pod_phase
from cmk.base.plugins.agent_based.utils.kube import PodLifeCycle


@pytest.mark.parametrize(
    "section, expected_result",
    [
        pytest.param(
            PodLifeCycle(phase="running"),
            (Result(state=State.OK, summary="Running"),),
            id="running",
        ),
    ],
)
def test_check_kube_pod_phase(section: PodLifeCycle, expected_result) -> None:
    assert expected_result == tuple(check_kube_pod_phase(section))