From d6832541c083ddca24622c21556319e7b81cf58b Mon Sep 17 00:00:00 2001 From: Peng Yin Date: Thu, 9 Nov 2017 20:58:40 +0000 Subject: [PATCH] eventhandler: fix the bug where container state change was not submitted --- CHANGELOG.md | 1 + agent/api/ecsclient/client.go | 4 +-- agent/api/ecsclient/client_test.go | 40 +++++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97efc118ae6..94e6eea3a56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ #Changelog ## 1.15.1 +* Bug - Fixed a bug where container state information wasn't reported. [#1067](https://github.com/aws/amazon-ecs-agent/pull/1067) * Bug - Fixed a bug where a task can be blocked in creating state. [#1048](https://github.com/aws/amazon-ecs-agent/pull/1048) * Bug - Fixed dynamic HostPort in container metadata. [#1052](https://github.com/aws/amazon-ecs-agent/pull/1052) diff --git a/agent/api/ecsclient/client.go b/agent/api/ecsclient/client.go index 73d8c151514..f1ea7894a12 100644 --- a/agent/api/ecsclient/client.go +++ b/agent/api/ecsclient/client.go @@ -318,13 +318,13 @@ func (client *APIECSClient) SubmitTaskStateChange(change api.TaskStateChange) er return errors.New("ecs api client: SubmitTaskStateChange called with an invalid change") } - if change.Status != api.TaskRunning && change.Status != api.TaskStopped { + if change.Status != api.TaskRunning && change.Status != api.TaskStopped && len(change.Containers) == 0 { seelog.Debugf("Not submitting unsupported upstream task state: %s", change.Status.String()) // Not really an error return nil } - status := change.Status.String() + status := change.Status.BackendStatus() req := ecs.SubmitTaskStateChangeInput{ Cluster: aws.String(client.config.Cluster), diff --git a/agent/api/ecsclient/client_test.go b/agent/api/ecsclient/client_test.go index e284ce98b6b..d8cc3e38dcf 100644 --- a/agent/api/ecsclient/client_test.go +++ b/agent/api/ecsclient/client_test.go @@ -675,7 +675,6 @@ func TestSubmitTaskStateChangeWithAttachments(t *testing.T) { assert.NoError(t, err, "Unable to submit task state change with attachments") } -// func TestSubmitTaskStateChangeWithoutAttachments(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() @@ -696,3 +695,42 @@ func TestSubmitTaskStateChangeWithoutAttachments(t *testing.T) { }) assert.NoError(t, err, "Unable to submit task state change with no attachments") } + +// TestSubmitContainerStateChangeWhileTaskInPending tests the container state change was submitted +// when the task is still in pending state +func TestSubmitContainerStateChangeWhileTaskInPending(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + + client, _, mockSubmitStateClient := NewMockClient(mockCtrl, ec2.NewBlackholeEC2MetadataClient(), nil) + mockSubmitStateClient.EXPECT().SubmitTaskStateChange(&taskSubmitInputMatcher{ + ecs.SubmitTaskStateChangeInput{ + Cluster: strptr(configuredCluster), + Task: strptr("arn"), + Status: strptr("PENDING"), + Reason: strptr(""), + Containers: []*ecs.ContainerStateChange{ + { + ContainerName: strptr("container"), + Status: strptr("RUNNING"), + NetworkBindings: []*ecs.NetworkBinding{}, + }, + }, + }, + }) + + taskStateChangePending := api.TaskStateChange{ + Status: api.TaskCreated, + TaskARN: "arn", + Containers: []api.ContainerStateChange{ + { + TaskArn: "arn", + ContainerName: "container", + Status: api.ContainerRunning, + }, + }, + } + + err := client.SubmitTaskStateChange(taskStateChangePending) + assert.NoError(t, err) +}