Skip to content

Commit c996652

Browse files
test: add ust docker label tests
1 parent 1646017 commit c996652

File tree

5 files changed

+125
-5
lines changed

5 files changed

+125
-5
lines changed

modules/ecs_fargate/datadog.tf

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ locals {
183183
),
184184
# Merge UST docker labels with any existing docker labels.
185185
dockerLabels = merge(
186-
lookup(container, "dockerLabels", {}),
187186
local.ust_docker_labels,
187+
// Placing this after local.ust_docker_labels ensures user defined UST labels are not overwritten.
188+
lookup(container, "dockerLabels", {}),
188189
),
189190
# Append new volume mounts to any existing mountPoints.
190191
mountPoints = concat(
@@ -367,10 +368,9 @@ locals {
367368
dd_log_container = local.is_fluentbit_supported ? [
368369
merge(
369370
{
370-
name = "datadog-log-router"
371-
image = "${var.dd_log_collection.fluentbit_config.registry}:${var.dd_log_collection.fluentbit_config.image_version}"
372-
essential = var.dd_log_collection.fluentbit_config.is_log_router_essential
373-
dockerLabels = local.ust_docker_labels
371+
name = "datadog-log-router"
372+
image = "${var.dd_log_collection.fluentbit_config.registry}:${var.dd_log_collection.fluentbit_config.image_version}"
373+
essential = var.dd_log_collection.fluentbit_config.is_log_router_essential
374374
firelensConfiguration = {
375375
type = "fluentbit"
376376
options = merge(
@@ -386,6 +386,7 @@ locals {
386386
user = "0"
387387
mountPoints = var.dd_log_collection.fluentbit_config.mountPoints
388388
environment = local.dd_log_agent_env
389+
dockerLabels = local.ust_docker_labels
389390
portMappings = []
390391
systemControls = []
391392
volumesFrom = []

smoke_tests/ecs_fargate/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ output "role-parsing-with-path" {
3737
output "role-parsing-without-path" {
3838
value = module.dd_task_role_parsing_without_path
3939
}
40+
41+
output "ust-docker-labels" {
42+
value = module.dd_task_ust_docker_labels
43+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Unless explicitly stated otherwise all files in this repository are licensed
2+
# under the Apache License Version 2.0.
3+
# This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
# Copyright 2025-present Datadog, Inc.
5+
6+
################################################################################
7+
# Task Definition: UST Docker Labels Test
8+
################################################################################
9+
10+
module "dd_task_ust_docker_labels" {
11+
source = "../../modules/ecs_fargate"
12+
13+
# Configure Datadog with UST tags
14+
dd_api_key = var.dd_api_key
15+
dd_site = var.dd_site
16+
dd_service = "ust-test-service"
17+
dd_env = "ust-test-env"
18+
dd_version = "1.2.3"
19+
dd_tags = "team:test"
20+
dd_essential = true
21+
dd_is_datadog_dependency_enabled = true
22+
23+
dd_log_collection = {
24+
enabled = true,
25+
}
26+
27+
dd_cws = {
28+
enabled = true,
29+
}
30+
31+
# Configure Task Definition with multiple containers
32+
family = "${var.test_prefix}-ust-docker-labels"
33+
container_definitions = jsonencode([
34+
{
35+
name = "dummy-app",
36+
image = "nginx:latest",
37+
essential = true,
38+
},
39+
{
40+
name = "app-overwritten-ust",
41+
image = "nginx:latest",
42+
essential = false,
43+
dockerLabels = {
44+
"com.datadoghq.tags.service": "different_name",
45+
"custom.label" = "custom-value"
46+
}
47+
}
48+
])
49+
50+
requires_compatibilities = ["FARGATE"]
51+
}

tests/ust_docker_labels_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed
2+
// under the Apache License Version 2.0.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
// Copyright 2025-present Datadog, Inc.
5+
6+
package test
7+
8+
import (
9+
"encoding/json"
10+
"log"
11+
12+
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
13+
"github.com/gruntwork-io/terratest/modules/terraform"
14+
)
15+
16+
// TestUSTDockerLabels tests that UST docker labels are propagated to all container definitions
17+
// when dd_service, dd_env, and dd_version are set
18+
func (s *ECSFargateSuite) TestUSTDockerLabels() {
19+
log.Println("TestUSTDockerLabels: Running test...")
20+
21+
// Retrieve the task output for the "ust-docker-labels" module
22+
var containers []types.ContainerDefinition
23+
task := terraform.OutputMap(s.T(), s.terraformOptions, "ust-docker-labels")
24+
s.Equal(s.testPrefix+"-ust-docker-labels", task["family"], "Unexpected task family name")
25+
26+
err := json.Unmarshal([]byte(task["container_definitions"]), &containers)
27+
s.NoError(err, "Failed to parse container definitions")
28+
s.Equal(5, len(containers), "Expected 4 containers in the task definition (3 app containers + 1 agent)")
29+
30+
// Expected UST docker labels that should be present on all application containers
31+
expectedUSTLabels := map[string]string{
32+
"com.datadoghq.tags.service": "ust-test-service",
33+
"com.datadoghq.tags.env": "ust-test-env",
34+
"com.datadoghq.tags.version": "1.2.3",
35+
}
36+
37+
dummyApp, found := GetContainer(containers, "dummy-app")
38+
s.True(found, "Container dummy-app not found in definitions")
39+
AssertDockerLabels(s.T(), dummyApp, expectedUSTLabels)
40+
41+
datadogContainers := []string{"datadog-agent", "datadog-log-router", "cws-instrumentation-init"}
42+
for _, containerName := range datadogContainers {
43+
container, found := GetContainer(containers, containerName)
44+
s.True(found, "Container %s not found in definitions", containerName)
45+
AssertDockerLabels(s.T(), container, expectedUSTLabels)
46+
}
47+
48+
overwrittenLabels, found := GetContainer(containers, "app-overwritten-ust")
49+
s.True(found, "Container app-overwritten-ust not found in definitions")
50+
expectedUSTLabels["com.datadoghq.tags.service"] = "different_name"
51+
AssertDockerLabels(s.T(), overwrittenLabels, expectedUSTLabels)
52+
53+
}

tests/utils.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,14 @@ func AssertContainerDependency(t *testing.T, container types.ContainerDefinition
118118
assert.True(t, found, "Expected dependency (container:%s, condition:%s) not found in %s container",
119119
*expectedDependency.ContainerName, expectedDependency.Condition, *container.Name)
120120
}
121+
122+
// AssertDockerLabels checks if the expected docker labels are all present in the container
123+
func AssertDockerLabels(t *testing.T, container types.ContainerDefinition, expectedLabels map[string]string) {
124+
assert.NotNil(t, container.Name, "Container name cannot be nil")
125+
126+
for key, expectedValue := range expectedLabels {
127+
value, found := container.DockerLabels[key]
128+
assert.True(t, found, "Docker label %s not found in %s container", key, *container.Name)
129+
assert.Equal(t, expectedValue, value, "Docker label %s value does not match expected in %s container", key, *container.Name)
130+
}
131+
}

0 commit comments

Comments
 (0)