|
| 1 | +""" |
| 2 | +Test the instrumented process discovery mechanism feature. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import json |
| 7 | +import msgpack |
| 8 | +from jsonschema import validate as validation_jsonschema |
| 9 | +from utils import features, scenarios |
| 10 | + |
| 11 | + |
| 12 | +def find_dd_memfds(test_library, pid: int) -> list[str]: |
| 13 | + _, out = test_library.container_exec_run(f"find /proc/{pid}/fd -lname '/memfd:datadog-tracer-info*'") |
| 14 | + if not out: |
| 15 | + return [] |
| 16 | + |
| 17 | + memfds = out.split() |
| 18 | + return memfds |
| 19 | + |
| 20 | + |
| 21 | +def validate_schema(input: str) -> bool: |
| 22 | + schema = None |
| 23 | + with open("utils/interfaces/schemas/library/process-discovery.json", "r") as f: |
| 24 | + schema = json.load(f) |
| 25 | + |
| 26 | + try: |
| 27 | + validation_jsonschema(input, schema) |
| 28 | + return True |
| 29 | + except Exception: |
| 30 | + return False |
| 31 | + |
| 32 | + |
| 33 | +def read_memfd(test_library, memfd_path: str): |
| 34 | + rc, output = test_library.container_exec_run_raw(f"cat {memfd_path}") |
| 35 | + if rc != 0: |
| 36 | + return rc, output |
| 37 | + |
| 38 | + return rc, msgpack.unpackb(output) |
| 39 | + |
| 40 | + |
| 41 | +@scenarios.parametric |
| 42 | +@features.process_discovery |
| 43 | +class Test_ProcessDiscovery: |
| 44 | + @pytest.mark.parametrize( |
| 45 | + "library_env", |
| 46 | + [ |
| 47 | + {"DD_SERVICE": "a", "DD_ENV": "test", "DD_VERSION": "0.1.0"}, |
| 48 | + {"DD_SERVICE": "b", "DD_ENV": "second-test", "DD_VERSION": "0.2.0"}, |
| 49 | + ], |
| 50 | + ) |
| 51 | + def test_metadata_content(self, test_library, library_env): |
| 52 | + """ |
| 53 | + Verify the content of the memfd file matches the expected metadata format and structure |
| 54 | + """ |
| 55 | + with test_library: |
| 56 | + # NOTE(@dmehala): the server is started on container is always pid 1. |
| 57 | + # That's a strong assumption :hehe: |
| 58 | + # Maybe we should use `pidof pidof parametric-http-server` instead. |
| 59 | + memfds = find_dd_memfds(test_library, 1) |
| 60 | + assert len(memfds) == 1 |
| 61 | + |
| 62 | + rc, tracer_metadata = read_memfd(test_library, memfds[0]) |
| 63 | + assert rc == 0 |
| 64 | + assert validate_schema(tracer_metadata) == True |
| 65 | + |
| 66 | + assert tracer_metadata["schema_version"] == 1 |
| 67 | + assert tracer_metadata["runtime_id"] |
| 68 | + # assert tracer_metadata["hostname"] |
| 69 | + # TODO(@dmehala): how to get the version? |
| 70 | + # assert tracer_metadata["tracer_version"] == |
| 71 | + assert tracer_metadata["tracer_language"] == test_library.lang |
| 72 | + assert tracer_metadata["service_name"] == library_env["DD_SERVICE"] |
| 73 | + assert tracer_metadata["service_version"] == library_env["DD_VERSION"] |
| 74 | + assert tracer_metadata["service_env"] == library_env["DD_ENV"] |
0 commit comments