|
| 1 | +// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Common test utilities |
| 5 | +
|
| 6 | +use std::collections::HashMap; |
| 7 | + |
| 8 | +/// Validates that entity headers (container-id, entity-id, external-env) match |
| 9 | +/// the values provided by libdd_common::entity_id |
| 10 | +/// |
| 11 | +/// # Current Limitations |
| 12 | +/// |
| 13 | +/// **NOTE:** This test helper has known limitations that should be addressed in a follow-up PR: |
| 14 | +/// |
| 15 | +/// 1. **Environment-dependent behavior**: The test changes its behavior dynamically based on the |
| 16 | +/// exact execution environment of the test runner (e.g., whether running in a container, whether |
| 17 | +/// certain environment variables are set). |
| 18 | +/// |
| 19 | +/// 2. **Non-deterministic across environments**: What passes on a local machine may fail in CI (or |
| 20 | +/// vice versa) because the underlying entity detection functions return different values in |
| 21 | +/// different environments. |
| 22 | +/// |
| 23 | +/// 3. **Incomplete test coverage**: We only exercise the codepaths that happen to be triggered in |
| 24 | +/// the current test environment, not all possible combinations of entity headers being |
| 25 | +/// present/absent. |
| 26 | +/// |
| 27 | +/// **Future improvement**: The ideal approach would be to refactor the underlying code |
| 28 | +/// (`libdd_common::entity_id::get_container_id()`, `get_entity_id()`, etc.) to be more testable, |
| 29 | +/// perhaps by making them accept injectable dependencies or configuration. Then we could test all |
| 30 | +/// combinations: container-id [Some/None] × entity-id [Some/None] × external-env [Some/None] to |
| 31 | +/// verify correct header inclusion/exclusion in all 8 cases. |
| 32 | +/// |
| 33 | +/// See discussion: https://github.com/DataDog/libdatadog/pull/1493#discussion_r2745712029 |
| 34 | +pub fn assert_entity_headers_match(headers: &HashMap<String, String>) { |
| 35 | + // Check for entity headers and validate their values match what libdd_common provides |
| 36 | + let expected_container_id = libdd_common::entity_id::get_container_id(); |
| 37 | + let expected_entity_id = libdd_common::entity_id::get_entity_id(); |
| 38 | + let expected_external_env = *libdd_common::entity_id::DD_EXTERNAL_ENV; |
| 39 | + |
| 40 | + // Validate container ID |
| 41 | + if let Some(expected) = expected_container_id { |
| 42 | + assert_eq!( |
| 43 | + headers.get("datadog-container-id"), |
| 44 | + Some(&expected.to_string()), |
| 45 | + "datadog-container-id header should match the value from entity_id::get_container_id()" |
| 46 | + ); |
| 47 | + } else { |
| 48 | + assert!( |
| 49 | + !headers.contains_key("datadog-container-id"), |
| 50 | + "datadog-container-id header should not be present when entity_id::get_container_id() is None" |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + // Validate entity ID |
| 55 | + if let Some(expected) = expected_entity_id { |
| 56 | + assert_eq!( |
| 57 | + headers.get("datadog-entity-id"), |
| 58 | + Some(&expected.to_string()), |
| 59 | + "datadog-entity-id header should match the value from entity_id::get_entity_id()" |
| 60 | + ); |
| 61 | + } else { |
| 62 | + assert!( |
| 63 | + !headers.contains_key("datadog-entity-id"), |
| 64 | + "datadog-entity-id header should not be present when entity_id::get_entity_id() is None" |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + // Validate external env |
| 69 | + if let Some(expected) = expected_external_env { |
| 70 | + assert_eq!( |
| 71 | + headers.get("datadog-external-env"), |
| 72 | + Some(&expected.to_string()), |
| 73 | + "datadog-external-env header should match the value from entity_id::DD_EXTERNAL_ENV" |
| 74 | + ); |
| 75 | + } else { |
| 76 | + assert!( |
| 77 | + !headers.contains_key("datadog-external-env"), |
| 78 | + "datadog-external-env header should not be present when entity_id::DD_EXTERNAL_ENV is None" |
| 79 | + ); |
| 80 | + } |
| 81 | +} |
0 commit comments