diff --git a/agent/functional_tests/tests/functionaltests_test.go b/agent/functional_tests/tests/functionaltests_test.go index 1f0e9dd56c6..f88309c08c9 100644 --- a/agent/functional_tests/tests/functionaltests_test.go +++ b/agent/functional_tests/tests/functionaltests_test.go @@ -730,7 +730,7 @@ func testV3TaskEndpointTags(t *testing.T, taskName, containerName, networkMode s _, err := ECS.PutAccountSetting(&putAccountSettingInput) assert.NoError(t, err) - awslogsPrefix := "ecs-functional-tests-v3-task-endpoint-validator" + awslogsPrefix := "ecs-functional-tests-v3-task-endpoint-with-tags-validator" agentOptions := &AgentOptions{ ExtraEnvironment: map[string]string{ "ECS_AVAILABLE_LOGGING_DRIVERS": `["awslogs"]`, diff --git a/agent/handlers/task_server_setup.go b/agent/handlers/task_server_setup.go index 00dde2ab66b..dc187e37e83 100644 --- a/agent/handlers/task_server_setup.go +++ b/agent/handlers/task_server_setup.go @@ -106,6 +106,7 @@ func v2HandlersSetup(muxRouter *mux.Router, muxRouter.HandleFunc(v2.TaskMetadataPath, v2.TaskContainerMetadataHandler(state, ecsClient, cluster, availabilityZone, containerInstanceArn, false)) muxRouter.HandleFunc(v2.TaskWithTagsMetadataPath, v2.TaskContainerMetadataHandler(state, ecsClient, cluster, availabilityZone, containerInstanceArn, true)) muxRouter.HandleFunc(v2.TaskMetadataPathWithSlash, v2.TaskContainerMetadataHandler(state, ecsClient, cluster, availabilityZone, containerInstanceArn, false)) + muxRouter.HandleFunc(v2.TaskWithTagsMetadataPathWithSlash, v2.TaskContainerMetadataHandler(state, ecsClient, cluster, availabilityZone, containerInstanceArn, true)) muxRouter.HandleFunc(v2.ContainerStatsPath, v2.TaskContainerStatsHandler(state, statsEngine)) muxRouter.HandleFunc(v2.TaskStatsPath, v2.TaskContainerStatsHandler(state, statsEngine)) muxRouter.HandleFunc(v2.TaskStatsPathWithSlash, v2.TaskContainerStatsHandler(state, statsEngine)) diff --git a/agent/handlers/task_server_setup_test.go b/agent/handlers/task_server_setup_test.go index 2572eef83f7..20f28f5ac8a 100644 --- a/agent/handlers/task_server_setup_test.go +++ b/agent/handlers/task_server_setup_test.go @@ -438,73 +438,88 @@ func TestV2TaskMetadata(t *testing.T) { } func TestV2TaskWithTagsMetadata(t *testing.T) { + testCases := []struct { + path string + }{ + { + v2BaseMetadataWithTagsPath, + }, + { + v2BaseMetadataWithTagsPath + "/", + }, + } + ctrl := gomock.NewController(t) defer ctrl.Finish() - state := mock_dockerstate.NewMockTaskEngineState(ctrl) - auditLog := mock_audit.NewMockAuditLogger(ctrl) - statsEngine := mock_stats.NewMockEngine(ctrl) - ecsClient := mock_api.NewMockECSClient(ctrl) + for _, tc := range testCases { + t.Run(fmt.Sprintf("Testing path: %s", tc.path), func(t *testing.T) { + state := mock_dockerstate.NewMockTaskEngineState(ctrl) + auditLog := mock_audit.NewMockAuditLogger(ctrl) + statsEngine := mock_stats.NewMockEngine(ctrl) + ecsClient := mock_api.NewMockECSClient(ctrl) - expectedTaskResponseWithTags := expectedTaskResponse - expectedContainerInstanceTags := map[string]string{ - "ContainerInstanceTag1": "firstTag", - "ContainerInstanceTag2": "secondTag", - } - expectedTaskResponseWithTags.ContainerInstanceTags = expectedContainerInstanceTags - expectedTaskTags := map[string]string{ - "TaskTag1": "firstTag", - "TaskTag2": "secondTag", - } - expectedTaskResponseWithTags.TaskTags = expectedTaskTags + expectedTaskResponseWithTags := expectedTaskResponse + expectedContainerInstanceTags := map[string]string{ + "ContainerInstanceTag1": "firstTag", + "ContainerInstanceTag2": "secondTag", + } + expectedTaskResponseWithTags.ContainerInstanceTags = expectedContainerInstanceTags + expectedTaskTags := map[string]string{ + "TaskTag1": "firstTag", + "TaskTag2": "secondTag", + } + expectedTaskResponseWithTags.TaskTags = expectedTaskTags - contInstTag1Key := "ContainerInstanceTag1" - contInstTag1Val := "firstTag" - contInstTag2Key := "ContainerInstanceTag2" - contInstTag2Val := "secondTag" - taskTag1Key := "TaskTag1" - taskTag1Val := "firstTag" - taskTag2Key := "TaskTag2" - taskTag2Val := "secondTag" + contInstTag1Key := "ContainerInstanceTag1" + contInstTag1Val := "firstTag" + contInstTag2Key := "ContainerInstanceTag2" + contInstTag2Val := "secondTag" + taskTag1Key := "TaskTag1" + taskTag1Val := "firstTag" + taskTag2Key := "TaskTag2" + taskTag2Val := "secondTag" - gomock.InOrder( - state.EXPECT().GetTaskByIPAddress(remoteIP).Return(taskARN, true), - state.EXPECT().TaskByArn(taskARN).Return(task, true), - state.EXPECT().ContainerMapByArn(taskARN).Return(containerNameToDockerContainer, true), - ecsClient.EXPECT().GetResourceTags(containerInstanceArn).Return([]*ecs.Tag{ - &ecs.Tag{ - Key: &contInstTag1Key, - Value: &contInstTag1Val, - }, - &ecs.Tag{ - Key: &contInstTag2Key, - Value: &contInstTag2Val, - }, - }, nil), - ecsClient.EXPECT().GetResourceTags(taskARN).Return([]*ecs.Tag{ - &ecs.Tag{ - Key: &taskTag1Key, - Value: &taskTag1Val, - }, - &ecs.Tag{ - Key: &taskTag2Key, - Value: &taskTag2Val, - }, - }, nil), - ) - server := taskServerSetup(credentials.NewManager(), auditLog, state, ecsClient, clusterName, statsEngine, - config.DefaultTaskMetadataSteadyStateRate, config.DefaultTaskMetadataBurstRate, availabilityzone, containerInstanceArn) - recorder := httptest.NewRecorder() - req, _ := http.NewRequest("GET", v2BaseMetadataWithTagsPath, nil) - req.RemoteAddr = remoteIP + ":" + remotePort - server.Handler.ServeHTTP(recorder, req) - res, err := ioutil.ReadAll(recorder.Body) - assert.NoError(t, err) - assert.Equal(t, http.StatusOK, recorder.Code) - var taskResponse v2.TaskResponse - err = json.Unmarshal(res, &taskResponse) - assert.NoError(t, err) - assert.Equal(t, expectedTaskResponseWithTags, taskResponse) + gomock.InOrder( + state.EXPECT().GetTaskByIPAddress(remoteIP).Return(taskARN, true), + state.EXPECT().TaskByArn(taskARN).Return(task, true), + state.EXPECT().ContainerMapByArn(taskARN).Return(containerNameToDockerContainer, true), + ecsClient.EXPECT().GetResourceTags(containerInstanceArn).Return([]*ecs.Tag{ + &ecs.Tag{ + Key: &contInstTag1Key, + Value: &contInstTag1Val, + }, + &ecs.Tag{ + Key: &contInstTag2Key, + Value: &contInstTag2Val, + }, + }, nil), + ecsClient.EXPECT().GetResourceTags(taskARN).Return([]*ecs.Tag{ + &ecs.Tag{ + Key: &taskTag1Key, + Value: &taskTag1Val, + }, + &ecs.Tag{ + Key: &taskTag2Key, + Value: &taskTag2Val, + }, + }, nil), + ) + server := taskServerSetup(credentials.NewManager(), auditLog, state, ecsClient, clusterName, statsEngine, + config.DefaultTaskMetadataSteadyStateRate, config.DefaultTaskMetadataBurstRate, availabilityzone, containerInstanceArn) + recorder := httptest.NewRecorder() + req, _ := http.NewRequest("GET", v2BaseMetadataWithTagsPath, nil) + req.RemoteAddr = remoteIP + ":" + remotePort + server.Handler.ServeHTTP(recorder, req) + res, err := ioutil.ReadAll(recorder.Body) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, recorder.Code) + var taskResponse v2.TaskResponse + err = json.Unmarshal(res, &taskResponse) + assert.NoError(t, err) + assert.Equal(t, expectedTaskResponseWithTags, taskResponse) + }) + } } func TestV2ContainerMetadata(t *testing.T) { diff --git a/agent/handlers/v2/task_container_metadata_handler.go b/agent/handlers/v2/task_container_metadata_handler.go index 8d07ff65a51..726c0432de3 100644 --- a/agent/handlers/v2/task_container_metadata_handler.go +++ b/agent/handlers/v2/task_container_metadata_handler.go @@ -37,6 +37,9 @@ const ( // TaskMetadataPathWithSlash specifies the relative URI path for serving task metadata. TaskMetadataPathWithSlash = TaskMetadataPath + "/" + + // TaskWithTagsMetadataPath specifies the relative URI path for serving task metadata with Container Instance and Task Tags. + TaskWithTagsMetadataPathWithSlash = TaskWithTagsMetadataPath + "/" ) // ContainerMetadataPath specifies the relative URI path for serving container metadata. diff --git a/agent/handlers/v3/task_metadata_handler.go b/agent/handlers/v3/task_metadata_handler.go index 90bcfdffc2e..8f8f6786950 100644 --- a/agent/handlers/v3/task_metadata_handler.go +++ b/agent/handlers/v3/task_metadata_handler.go @@ -30,6 +30,9 @@ const v3EndpointIDMuxName = "v3EndpointIDMuxName" // TaskMetadataPath specifies the relative URI path for serving task metadata. var TaskMetadataPath = "/v3/" + utils.ConstructMuxVar(v3EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/task" + +// TaskWithTagsMetadataPath specifies the relative URI path for serving task metdata +// with Container Instance and Task Tags retrieved through the ECS API var TaskWithTagsMetadataPath = "/v3/" + utils.ConstructMuxVar(v3EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/taskWithTags" // TaskMetadataHandler returns the handler method for handling task metadata requests.