Esempio n. 1
0
def test_get_kube_container_name():
    tags = CadvisorPrometheusScraper._get_kube_container_name([
        Label("container_name", value="datadog-agent"),
        Label(
            "id",
            value="/kubepods/burstable/"
            "podc2319815-10d0-11e8-bd5a-42010af00137/"
            "a335589109ce5506aa69ba7481fc3e6c943abd23c5277016c92dac15d0f40479"
        ),
        Label(
            "image",
            value=
            "datadog/agent-dev@sha256:894fb66f89be0332a47388d7219ab8b365520ff0e3bbf597bd0a378b19efa7ee"
        ),
        Label(
            "name",
            value=
            "k8s_datadog-agent_datadog-agent-jbm2k_default_c2319815-10d0-11e8-bd5a-42010af00137_0"
        ),
        Label("namespace", value="default"),
        Label("pod_name", value="datadog-agent-jbm2k"),
    ])
    assert tags == ["kube_container_name:datadog-agent"]

    tags = CadvisorPrometheusScraper._get_kube_container_name([])
    assert tags == []
Esempio n. 2
0
def test_is_pod_metric():
    false_metrics = [
        MockMetric('foo', []),
        MockMetric('bar', [Label(name='container_name', value='ctr0')]),
        MockMetric('foobar', [
            Label(name='container_name', value='ctr0'),
            Label(name='id', value='deadbeef')
        ]),
    ]

    true_metrics = [
        MockMetric('foo', [Label(name='container_name', value='POD')]),
        MockMetric('bar', [
            Label(
                name='id',
                value=
                '/kubepods/burstable/pod531c80d9-9fc4-11e7-ba8b-42010af002bb')
        ]),
        MockMetric('foobar', [
            Label(name='container_name', value='POD'),
            Label(
                name='id',
                value=
                '/kubepods/burstable/pod531c80d9-9fc4-11e7-ba8b-42010af002bb')
        ]),
    ]

    for metric in false_metrics:
        assert CadvisorPrometheusScraper._is_pod_metric(metric.label) is False

    for metric in true_metrics:
        assert CadvisorPrometheusScraper._is_pod_metric(metric.label) is True
Esempio n. 3
0
def cadvisor_scraper(check):
    scraper = CadvisorPrometheusScraper(check)
    pod_list = json.loads(mock_from_file('podlist_containerd.json'))
    scraper.pod_list = pod_list
    scraper.pod_list_utils = PodListUtils(pod_list)

    return scraper
Esempio n. 4
0
def test_get_container_label():
    labels = [
        Label("container_name", value="POD"),
        Label("id", value="/kubepods/burstable/pod531c80d9-9fc4-11e7-ba8b-42010af002bb"),
    ]
    assert CadvisorPrometheusScraper._get_container_label(labels, "container_name") == "POD"
    assert CadvisorPrometheusScraper._get_container_label([], "not-in") is None
Esempio n. 5
0
def test_get_pod_uid():
    labels = [
        Label("container_name", value="POD"),
        Label("id",
              value="/kubepods/burstable/"
                    "pod260c2b1d43b094af6d6b4ccba082c2db/"
                    "0bce0ef7e6cd073e8f9cec3027e1c0057ce1baddce98113d742b816726a95ab1"),
    ]
    assert CadvisorPrometheusScraper._get_pod_uid(labels) == "260c2b1d43b094af6d6b4ccba082c2db"
    assert CadvisorPrometheusScraper._get_pod_uid([]) is None
Esempio n. 6
0
def test_get_container_id():
    labels = [
        Label("container_name", value="datadog-agent"),
        Label("id",
              value="/kubepods/burstable/"
                    "podc2319815-10d0-11e8-bd5a-42010af00137/"
                    "a335589109ce5506aa69ba7481fc3e6c943abd23c5277016c92dac15d0f40479"),
    ]
    container_id = CadvisorPrometheusScraper._get_container_id(labels)
    assert container_id == "a335589109ce5506aa69ba7481fc3e6c943abd23c5277016c92dac15d0f40479"
    assert CadvisorPrometheusScraper._get_container_id([]) is None
Esempio n. 7
0
def test_cadvisor_default_options():
    check = KubeletCheck('kubelet', None, {}, [{}])
    scraper = CadvisorPrometheusScraper(check)
    assert scraper.NAMESPACE == 'kubernetes'
    assert scraper.fs_usage_bytes == {}
    assert scraper.mem_usage_bytes == {}
    assert scraper.metrics_mapper == {}
Esempio n. 8
0
def test_is_container_metric():
    false_metrics = [
        MockMetric('foo', []),
        MockMetric(
            'bar',
            [
                Label(name='container_name', value='POD'),  # POD --> False
                Label(name='namespace', value='default'),
                Label(name='pod_name', value='pod0'),
                Label(name='name', value='foo'),
                Label(name='image', value='foo'),
                Label(name='id', value='deadbeef'),
            ]
        ),
        MockMetric(  # missing container_name
            'foobar',
            [
                Label(name='namespace', value='default'),
                Label(name='pod_name', value='pod0'),
                Label(name='name', value='foo'),
                Label(name='image', value='foo'),
                Label(name='id', value='deadbeef'),
            ]
        ),
    ]
    for metric in false_metrics:
        assert CadvisorPrometheusScraper._is_container_metric(metric.label) is False

    true_metric = MockMetric(
        'foo',
        [
            Label(name='container_name', value='ctr0'),
            Label(name='namespace', value='default'),
            Label(name='pod_name', value='pod0'),
            Label(name='name', value='foo'),
            Label(name='image', value='foo'),
            Label(name='id', value='deadbeef'),
        ]
    )
    assert CadvisorPrometheusScraper._is_container_metric(true_metric.label) is True
Esempio n. 9
0
def cadvisor_scraper(check):
    scraper = CadvisorPrometheusScraper(check)
    scraper.pod_list = json.loads(mock_from_file('pods.json'))

    return scraper