Skip to content

Commit

Permalink
Adding v2 task endpoint with tags and slash path
Browse files Browse the repository at this point in the history
  • Loading branch information
linkar-ec2 committed Dec 10, 2018
1 parent d920c6e commit f1278c7
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 62 deletions.
2 changes: 1 addition & 1 deletion agent/functional_tests/tests/functionaltests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]`,
Expand Down
1 change: 1 addition & 0 deletions agent/handlers/task_server_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
137 changes: 76 additions & 61 deletions agent/handlers/task_server_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions agent/handlers/v2/task_container_metadata_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions agent/handlers/v3/task_metadata_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit f1278c7

Please sign in to comment.