Skip to content

Commit 5a35ec8

Browse files
committed
Editing functional test in v2 metadata validator to check for tags
1 parent 97f4938 commit 5a35ec8

File tree

3 files changed

+71
-14
lines changed

3 files changed

+71
-14
lines changed

agent/functional_tests/testdata/taskdefinitions/taskmetadata-validator-awsvpc/task-definition.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"retries": 2,
1313
"startPeriod": 1
1414
},
15+
"command": ["$$$CHECK_TAGS$$$"],
1516
"logConfiguration": {
1617
"logDriver": "awslogs",
1718
"options": {

agent/functional_tests/tests/functionaltests_unix_test.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,17 @@ func TestAgentIntrospectionValidator(t *testing.T) {
665665

666666
func TestTaskMetadataValidator(t *testing.T) {
667667
RequireDockerVersion(t, ">=17.06.0-ce")
668+
669+
// Added to test presence of tags in metadata endpoint
670+
// We need long container instance ARN for tagging APIs, PutAccountSettingInput
671+
// will enable long container instance ARN.
672+
putAccountSettingInput := ecsapi.PutAccountSettingInput{
673+
Name: aws.String("containerInstanceLongArnFormat"),
674+
Value: aws.String("enabled"),
675+
}
676+
_, err := ECS.PutAccountSetting(&putAccountSettingInput)
677+
assert.NoError(t, err)
678+
668679
// Best effort to create a log group. It should be safe to even not do this
669680
// as the log group gets created in the TestAWSLogsDriver functional test.
670681
cwlClient := cloudwatchlogs.New(session.New(), aws.NewConfig().WithRegion(*ECS.Config.Region))
@@ -674,14 +685,18 @@ func TestTaskMetadataValidator(t *testing.T) {
674685
agent := RunAgent(t, &AgentOptions{
675686
EnableTaskENI: true,
676687
ExtraEnvironment: map[string]string{
677-
"ECS_AVAILABLE_LOGGING_DRIVERS": `["awslogs"]`,
688+
"ECS_AVAILABLE_LOGGING_DRIVERS": `["awslogs"]`,
689+
"ECS_CONTAINER_INSTANCE_PROPAGATE_TAGS_FROM": "ec2_instance",
690+
"ECS_CONTAINER_INSTANCE_TAGS": fmt.Sprintf(`{"%s": "%s"}`,
691+
"localKey", "localValue"),
678692
},
679693
})
680694
defer agent.Cleanup()
681695
agent.RequireVersion(">1.20.1")
682696

683697
tdOverrides := make(map[string]string)
684698
tdOverrides["$$$TEST_REGION$$$"] = *ECS.Config.Region
699+
tdOverrides["$$$CHECK_TAGS$$$"] = "CheckTags" // Added to test presence of tags in metadata endpoint
685700

686701
task, err := agent.StartAWSVPCTask("taskmetadata-validator-awsvpc", tdOverrides)
687702
require.NoError(t, err, "Unable to start task with 'awsvpc' network mode")
@@ -697,6 +712,12 @@ func TestTaskMetadataValidator(t *testing.T) {
697712
exitCode, _ := task.ContainerExitcode("taskmetadata-validator")
698713

699714
assert.Equal(t, 42, exitCode, fmt.Sprintf("Expected exit code of 42; got %d", exitCode))
715+
716+
DeleteAccountSettingInput := ecsapi.DeleteAccountSettingInput{
717+
Name: aws.String("containerInstanceLongArnFormat"),
718+
}
719+
_, err = ECS.DeleteAccountSetting(&DeleteAccountSettingInput)
720+
assert.NoError(t, err)
700721
}
701722

702723
// TestExecutionRole verifies that task can use the execution credentials to pull from ECR and

misc/taskmetadata-validator/taskmetadata-validator.go

+48-13
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,26 @@ import (
2525
)
2626

2727
const (
28-
v2MetadataEndpoint = "http://169.254.170.2/v2/metadata"
29-
v2StatsEndpoint = "http://169.254.170.2/v2/stats"
30-
maxRetries = 4
31-
durationBetweenRetries = time.Second
28+
v2MetadataEndpoint = "http://169.254.170.2/v2/metadata"
29+
v2MetadataWithTagsEndpoint = "http://169.254.170.2/v2/metadataWithTags"
30+
v2StatsEndpoint = "http://169.254.170.2/v2/stats"
31+
maxRetries = 4
32+
durationBetweenRetries = time.Second
3233
)
3334

3435
// TaskResponse defines the schema for the task response JSON object
3536
type TaskResponse struct {
36-
Cluster string
37-
TaskARN string
38-
Family string
39-
Revision string
40-
DesiredStatus string `json:",omitempty"`
41-
KnownStatus string
42-
AvailabilityZone string
43-
Containers []ContainerResponse `json:",omitempty"`
44-
Limits LimitsResponse `json:",omitempty"`
37+
Cluster string
38+
TaskARN string
39+
Family string
40+
Revision string
41+
DesiredStatus string `json:",omitempty"`
42+
KnownStatus string
43+
AvailabilityZone string
44+
Containers []ContainerResponse `json:",omitempty"`
45+
Limits LimitsResponse `json:",omitempty"`
46+
TaskTags map[string]string `json:"TaskTags,omitempty"`
47+
ContainerInstanceTags map[string]string `json:"ContainerInstanceTags,omitempty"`
4548
}
4649

4750
// ContainerResponse defines the schema for the container response
@@ -120,6 +123,23 @@ func taskMetadata(client *http.Client) (*TaskResponse, error) {
120123
return &taskMetadata, nil
121124
}
122125

126+
func taskWithTagsMetadata(client *http.Client) (*TaskResponse, error) {
127+
body, err := metadataResponse(client, v2MetadataWithTagsEndpoint, "task with tags metadata")
128+
if err != nil {
129+
return nil, err
130+
}
131+
132+
fmt.Printf("Received task with tags metadata: %s \n", string(body))
133+
134+
var taskMetadata TaskResponse
135+
err = json.Unmarshal(body, &taskMetadata)
136+
if err != nil {
137+
return nil, fmt.Errorf("task with tags metadata: unable to parse response body: %v", err)
138+
}
139+
140+
return &taskMetadata, nil
141+
}
142+
123143
func containerMetadata(client *http.Client, id string) (*ContainerResponse, error) {
124144
body, err := metadataResponse(client, v2MetadataEndpoint+"/"+id, "container metadata")
125145
if err != nil {
@@ -214,6 +234,21 @@ func main() {
214234
// Wait for the Health information to be ready
215235
time.Sleep(5 * time.Second)
216236

237+
// If the image is built with option to check Tags
238+
argsWithoutProg := os.Args[1:]
239+
if len(argsWithoutProg) > 0 && argsWithoutProg[0] == "CheckTags" {
240+
taskWithTagsMetadata, err := taskWithTagsMetadata(client)
241+
if err != nil {
242+
fmt.Fprintf(os.Stderr, "Unable to get task stats: %v", err)
243+
os.Exit(1)
244+
}
245+
246+
if len(taskWithTagsMetadata.ContainerInstanceTags) == 0 {
247+
fmt.Fprintf(os.Stderr, "ContainerInstanceTags not found: %v", err)
248+
os.Exit(1)
249+
}
250+
}
251+
217252
taskMetadata, err := taskMetadata(client)
218253
if err != nil {
219254
fmt.Fprintf(os.Stderr, "Unable to get task metadata: %v", err)

0 commit comments

Comments
 (0)