def test_valid_configmap(self, aggregator, mock_read_configmap, mock_incluster): mock_read_configmap.return_value = make_fake_object(RAW_VALID_RECORD) c = KubeLeaderElectionBaseCheck() c.check(CM_INSTANCE) assert c.get_warnings() == [] mock_read_configmap.assert_called_once_with("thisrecord", "myns", _request_timeout=30) aggregator.assert_metric("base.leader_election.transitions", value=7, tags=CM_TAGS) aggregator.assert_metric("base.leader_election.lease_duration", value=60, tags=CM_TAGS) aggregator.assert_service_check("base.leader_election.status", status=AgentCheck.CRITICAL, tags=CM_TAGS) aggregator.assert_all_metrics_covered()
def test_ok_configmap(self, aggregator, mock_read_configmap, mock_incluster): mock_read_configmap.return_value = make_fake_object( make_record(holder="me", duration=30, renew=datetime.utcnow(), acquire="2018-12-18T12:32:22Z") ) c = KubeLeaderElectionBaseCheck() c.check(CM_INSTANCE) assert c.get_warnings() == [] aggregator.assert_metric("base.leader_election.transitions", value=0, tags=CM_TAGS) aggregator.assert_metric("base.leader_election.lease_duration", value=30, tags=CM_TAGS) aggregator.assert_service_check("base.leader_election.status", status=AgentCheck.OK, tags=CM_TAGS) aggregator.assert_all_metrics_covered()
def test_invalid_configmap(self, aggregator, mock_read_configmap, mock_incluster): mock_read_configmap.return_value = make_fake_object( make_record(holder="me")) c = KubeLeaderElectionBaseCheck() c.check(CM_INSTANCE) assert c.get_warnings() == [] aggregator.assert_service_check("base.leader_election.status", status=AgentCheck.CRITICAL, tags=CM_TAGS) aggregator.assert_all_metrics_covered()
def test_config_kubeconfig(self, datadog_agent, config): datadog_agent.get_config.return_value = "/file/path" c = KubeLeaderElectionBaseCheck() c.check({}) datadog_agent.get_config.assert_called_once_with('kubernetes_kubeconfig_path') config.load_kube_config.assert_called_once_with(config_file="/file/path")
def test_config_incluster(self, config): c = KubeLeaderElectionBaseCheck() c.check({}) config.load_incluster_config.assert_called_once()
import json from datetime import datetime, timedelta import mock import pytest from kubernetes.client.models.v1_lease import V1Lease from kubernetes.client.models.v1_lease_spec import V1LeaseSpec from kubernetes.config.dateutil import format_rfc3339 from six import iteritems, string_types from datadog_checks.base import AgentCheck, KubeLeaderElectionBaseCheck from datadog_checks.base.checks.kube_leader import ElectionRecordAnnotation, ElectionRecordLease # Trigger lazy imports try: KubeLeaderElectionBaseCheck().check({}) except Exception: pass RAW_VALID_RECORD = ( '{"holderIdentity":"dd-cluster-agent-568f458dd6-kj6vt",' '"leaseDurationSeconds":60,' '"acquireTime":"2018-12-17T11:53:07Z",' '"renewTime":"2018-12-18T12:32:22Z",' '"leaderTransitions":7}' ) LEASE_OBJECT = V1Lease( api_version="coordination.k8s.io/v1", kind="Lease",