Пример #1
0
def test_cgroup_info_init():
    # Assert default all attributes to `None`
    info = CGroupInfo()
    for attr in ("id", "groups", "path", "container_id", "controllers", "pod_id"):
        assert getattr(info, attr) is None

    # Assert init with property sets property
    info = CGroupInfo(container_id="test-container-id")
    assert info.container_id == "test-container-id"
def test_cgroup_info_init():
    # Assert default all attributes to `None`
    info = CGroupInfo()
    for attr in ('id', 'groups', 'path', 'container_id', 'controllers', 'pod_id'):
        assert getattr(info, attr) is None

    # Assert init with property sets property
    info = CGroupInfo(container_id='test-container-id')
    assert info.container_id == 'test-container-id'
Пример #3
0
def test_get_container_id_when_container_exists():
    """
    validates the return value of _get_container_id when get_container_info()
    can parse /proc/{pid}/cgroup
    """
    # mocks ddtrace.internal.runtime.container.get_container_info import in data.py
    with mock.patch("ddtrace.internal.telemetry.data.get_container_info") as gci:
        cgroupInfo = CGroupInfo()
        cgroupInfo.container_id = "d39b145254d1f9c337fdd2be132f6650c6f5bc274bfa28aaa204a908a1134096"
        gci.return_value = cgroupInfo
        assert _get_container_id() == "d39b145254d1f9c337fdd2be132f6650c6f5bc274bfa28aaa204a908a1134096"
Пример #4
0
def test_cgroup_info_from_line(line, expected_info):
    info = CGroupInfo.from_line(line)
    info2 = CGroupInfo.from_line(line)

    # Check __eq__
    assert info == info2

    if expected_info is None:
        assert info is None, line
    else:
        for attr in ("id", "groups", "path", "container_id", "controllers", "pod_id"):
            assert getattr(info, attr) == getattr(expected_info, attr), line
def test_cgroup_info_from_line(line, expected_info):
    info = CGroupInfo.from_line(line)

    if expected_info is None:
        assert info is None, line
    else:
        for attr in ('id', 'groups', 'path', 'container_id', 'controllers', 'pod_id'):
            assert getattr(info, attr) == getattr(expected_info, attr), line
Пример #6
0
    def setUp(self, get_container_info):
        """
        Create a tracer without workers, while spying the ``send()`` method
        """
        # Mock the container id we use for making requests
        get_container_info.return_value = CGroupInfo(container_id="test-container-id")

        # create a new API object to test the transport using synchronous calls
        self.tracer = get_dummy_tracer()
        self.api_json = API("localhost", 8126, encoder=JSONEncoder())
        self.api_msgpack = API("localhost", 8126, encoder=MsgpackEncoder())
Пример #7
0
def test_api_container_info(get_container_info):
    # When we have container information
    # DEV: `get_container_info` will return a `CGroupInfo` with a `container_id` or `None`
    info = CGroupInfo(container_id="test-container-id")
    get_container_info.return_value = info

    api = API(_HOST, 8126)
    assert api._container_info is info
    assert api._headers["Datadog-Container-Id"] == "test-container-id"

    # When we do not have container information
    get_container_info.return_value = None

    api = API(_HOST, 8126)
    assert api._container_info is None
    assert "Datadog-Container-Id" not in api._headers
    info = CGroupInfo(container_id='test-container-id')
    assert info.container_id == 'test-container-id'


@pytest.mark.parametrize(
    'line,expected_info',

    # Valid generated cases + one off cases
    cgroup_line_valid_test_cases() + [
        # Valid, extra spaces
        (
            '    13:name=systemd:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860    ',
            CGroupInfo(
                id='13',
                groups='name=systemd',
                controllers=['name=systemd'],
                path='/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860',
                container_id='3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860',
                pod_id=None,
            ),
        ),
        # Valid, bookended newlines
        (
            '\r\n13:name=systemd:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\r\n',
            CGroupInfo(
                id='13',
                groups='name=systemd',
                controllers=['name=systemd'],
                path='/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860',
                container_id='3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860',
                pod_id=None,
            ),
Пример #9
0
def cgroup_line_valid_test_cases():
    controllers = [
        ['name=systemd'],
        ['pids'],
        ['cpu', 'cpuacct'],
        ['perf_event'],
        ['net_cls', 'net_prio'],
    ]

    ids = [str(i) for i in range(10)]

    container_ids = [
        '3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860',
        '37261842-26f5-d314-7c25-fdeab5b60097',
        '37261842_26f5_d314_7c25_fdeab5b60097',
    ]

    pod_ids = [
        '3d274242-8ee0-11e9-a8a6-1e68d864ef1a',
        '3d274242_8ee0_11e9_a8a6_1e68d864ef1a',
    ]

    paths = [
        # Docker
        '/docker/{0}',
        '/docker/{0}.scope',

        # k8s
        '/kubepods/test/pod{1}/{0}',
        '/kubepods/test/pod{1}.slice/{0}',
        '/kubepods/test/pod{1}/{0}.scope',
        '/kubepods/test/pod{1}.slice/{0}.scope',

        # ECS
        '/ecs/test-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/{0}',
        '/ecs/test-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/{0}.scope',

        # Fargate
        '/ecs/55091c13-b8cf-4801-b527-f4601742204d/{0}',
        '/ecs/55091c13-b8cf-4801-b527-f4601742204d/{0}.scope',

        # Linux non-containerized
        '/user.slice/user-0.slice/session-83.scope',
    ]

    valid_test_cases = dict(
        (':'.join([id_, ','.join(groups),
                   path.format(container_id, pod_id)]),
         CGroupInfo(
             id=id_,
             groups=','.join(groups),
             path=path.format(container_id, pod_id),
             controllers=groups,
             container_id=container_id if '{0}' in path else None,
             pod_id=pod_id if '{1}' in path else None,
         )) for path, id_, groups, container_id, pod_id in itertools.product(
             paths, ids, controllers, container_ids, pod_ids))
    # Dedupe test cases
    valid_test_cases = list(valid_test_cases.items())

    # Assert here to ensure we are always testing the number of cases we expect
    assert len(valid_test_cases) == 2150

    return valid_test_cases
Пример #10
0
    info = CGroupInfo(container_id="test-container-id")
    assert info.container_id == "test-container-id"


@pytest.mark.parametrize(
    "line,expected_info",
    # Valid generated cases + one off cases
    cgroup_line_valid_test_cases() + [
        # Valid, extra spaces
        (
            "    13:name=systemd:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860    ",
            CGroupInfo(
                id="13",
                groups="name=systemd",
                controllers=["name=systemd"],
                path=
                "/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860",
                container_id=
                "3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860",
                pod_id=None,
            ),
        ),
        # Valid, bookended newlines
        (
            "\r\n13:name=systemd:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\r\n",
            CGroupInfo(
                id="13",
                groups="name=systemd",
                controllers=["name=systemd"],
                path=
                "/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860",
                container_id=
Пример #11
0
    info = CGroupInfo(container_id="test-container-id")
    assert info.container_id == "test-container-id"


@pytest.mark.parametrize(
    "line,expected_info",
    # Valid generated cases + one off cases
    cgroup_line_valid_test_cases()
    + [
        # Valid, extra spaces
        (
            "    13:name=systemd:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860    ",
            CGroupInfo(
                id="13",
                groups="name=systemd",
                controllers=["name=systemd"],
                path="/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860",
                container_id="3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860",
                pod_id=None,
            ),
        ),
        # Valid, bookended newlines
        (
            "\r\n13:name=systemd:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\r\n",
            CGroupInfo(
                id="13",
                groups="name=systemd",
                controllers=["name=systemd"],
                path="/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860",
                container_id="3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860",
                pod_id=None,
            ),