def test_report_pods_running_none_ids(monkeypatch, tagger): # Make sure the method is resilient to inconsistent podlists podlist = json.loads(mock_from_file('pods.json')) podlist["items"][0]['metadata']['uid'] = None podlist["items"][1]['status']['containerStatuses'][0]['containerID'] = None check = KubeletCheck('kubelet', None, {}, [{}]) monkeypatch.setattr(check, 'retrieve_pod_list', mock.Mock(return_value=podlist)) monkeypatch.setattr(check, 'gauge', mock.Mock()) pod_list = check.retrieve_pod_list() check._report_pods_running(pod_list, []) calls = [ mock.call('kubernetes.pods.running', 1, ["pod_name:fluentd-gcp-v2.0.10-9q9t4"]), mock.call( 'kubernetes.containers.running', 2, [ "kube_container_name:prometheus-to-sd-exporter", "kube_deployment:fluentd-gcp-v2.0.10" ], ), ] check.gauge.assert_has_calls(calls, any_order=True)
def test_pod_expiration(monkeypatch, aggregator, tagger): check = KubeletCheck('kubelet', {}, [{}]) check.pod_list_url = "dummyurl" # Fixtures contains four pods: # - dd-agent-ntepl old but running # - hello1-1550504220-ljnzx succeeded and old enough to expire # - hello5-1550509440-rlgvf succeeded but not old enough # - hello8-1550505780-kdnjx has one old container and a recent container, don't expire monkeypatch.setattr(check, 'perform_kubelet_query', mock.Mock(return_value=MockStreamResponse('pods_expired.json'))) monkeypatch.setattr( check, '_compute_pod_expiration_datetime', mock.Mock(return_value=parse_rfc3339("2019-02-18T16:00:06Z")) ) attrs = {'is_excluded.return_value': False} check.pod_list_utils = mock.Mock(**attrs) pod_list = check.retrieve_pod_list() assert pod_list['expired_count'] == 1 expected_names = ['dd-agent-ntepl', 'hello5-1550509440-rlgvf', 'hello8-1550505780-kdnjx'] collected_names = [p['metadata']['name'] for p in pod_list['items']] assert collected_names == expected_names # Test .pods.expired gauge is submitted check._report_container_state_metrics(pod_list, ["custom:tag"]) aggregator.assert_metric("kubernetes.pods.expired", value=1, tags=["custom:tag"]) # Ensure we can iterate twice over the podlist check._report_pods_running(pod_list, []) aggregator.assert_metric("kubernetes.pods.running", value=1, tags=["pod_name:dd-agent-ntepl"]) aggregator.assert_metric("kubernetes.containers.running", value=1, tags=["pod_name:dd-agent-ntepl"])
def test_report_pods_running(monkeypatch, tagger): check = KubeletCheck('kubelet', None, {}, [{}]) monkeypatch.setattr(check, 'retrieve_pod_list', mock.Mock(return_value=json.loads(mock_from_file('pods.json')))) monkeypatch.setattr(check, 'gauge', mock.Mock()) pod_list = check.retrieve_pod_list() check._report_pods_running(pod_list, []) calls = [ mock.call('kubernetes.pods.running', 1, ["pod_name:fluentd-gcp-v2.0.10-9q9t4"]), mock.call('kubernetes.pods.running', 1, ["pod_name:fluentd-gcp-v2.0.10-p13r3"]), mock.call('kubernetes.pods.running', 1, ['pod_name:demo-app-success-c485bc67b-klj45']), mock.call('kubernetes.containers.running', 2, [ "kube_container_name:fluentd-gcp", "kube_deployment:fluentd-gcp-v2.0.10" ]), mock.call('kubernetes.containers.running', 2, [ "kube_container_name:prometheus-to-sd-exporter", "kube_deployment:fluentd-gcp-v2.0.10" ]), mock.call('kubernetes.containers.running', 1, ['pod_name:demo-app-success-c485bc67b-klj45']), ] check.gauge.assert_has_calls(calls, any_order=True) # Make sure non running container/pods are not sent bad_calls = [ mock.call('kubernetes.pods.running', 1, ['pod_name:dd-agent-q6hpw']), mock.call('kubernetes.containers.running', 1, ['pod_name:dd-agent-q6hpw']), ] for c in bad_calls: assert c not in check.gauge.mock_calls
def test_report_pods_running(monkeypatch): check = KubeletCheck('kubelet', None, {}, [{}]) monkeypatch.setattr( check, 'retrieve_pod_list', mock.Mock(return_value=json.loads(mock_from_file('pods.json')))) monkeypatch.setattr(check, 'gauge', mock.Mock()) pod_list = check.retrieve_pod_list() with mock.patch("datadog_checks.kubelet.kubelet.get_tags", side_effect=mocked_get_tags): check._report_pods_running(pod_list, []) calls = [ mock.call('kubernetes.pods.running', 1, ["pod_name:fluentd-gcp-v2.0.10-9q9t4"]), mock.call('kubernetes.pods.running', 1, ["pod_name:fluentd-gcp-v2.0.10-fkeuj"]), mock.call('kubernetes.containers.running', 2, [ "kube_container_name:fluentd-gcp", "kube_deployment:fluentd-gcp-v2.0.10" ]), mock.call('kubernetes.containers.running', 2, [ "kube_container_name:prometheus-to-sd-exporter", "kube_deployment:fluentd-gcp-v2.0.10" ]), ] check.gauge.assert_has_calls(calls, any_order=True)