From 20d7ce96465f02a95a645c9ea26c95e566fccb5e Mon Sep 17 00:00:00 2001 From: Peng Yin Date: Fri, 12 Jan 2018 01:16:18 +0000 Subject: [PATCH] test: add test for updating metadata on restart --- agent/engine/docker_task_engine_test.go | 160 ++++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/agent/engine/docker_task_engine_test.go b/agent/engine/docker_task_engine_test.go index 11a10e7f6e6..04703291321 100644 --- a/agent/engine/docker_task_engine_test.go +++ b/agent/engine/docker_task_engine_test.go @@ -1676,3 +1676,163 @@ func TestSynchronizeContainerStatus(t *testing.T) { assert.Equal(t, created, dockerContainer.Container.GetCreatedAt()) assert.Equal(t, labels, dockerContainer.Container.GetLabels()) } + +func TestCreatedContainerMetadataUpdateOnRestart(t *testing.T) { + ctrl, client, _, taskEngine, _, imageManager, _ := mocks(t, &defaultConfig) + defer ctrl.Finish() + + dockerID := "dockerID" + dockerContainer := &api.DockerContainer{ + DockerName: "c1", + Container: &api.Container{ + MountPoints: []api.MountPoint{ + { + SourceVolume: "empty", + ContainerPath: "/container", + }, + }, + }, + } + + labels := map[string]string{ + "name": "metadata", + } + created := time.Now() + volumes := map[string]string{ + "/container": "/tmp", + } + emptyVolume := &api.EmptyHostVolume{} + task := &api.Task{ + Volumes: []api.TaskVolume{ + { + Name: "empty", + Volume: emptyVolume, + }, + }, + } + + gomock.InOrder( + client.EXPECT().InspectContainer("c1", gomock.Any()).Return(&docker.Container{ + ID: dockerID, + Config: &docker.Config{ + Labels: labels, + }, + Created: created, + Volumes: volumes, + }, nil), + imageManager.EXPECT().RecordContainerReference(dockerContainer.Container), + ) + taskEngine.(*DockerTaskEngine).synchronizeContainerStatus(dockerContainer, task) + assert.Equal(t, dockerID, dockerContainer.DockerID) + assert.Equal(t, created, dockerContainer.Container.GetCreatedAt()) + assert.Equal(t, labels, dockerContainer.Container.GetLabels()) + assert.Equal(t, "/tmp", task.Volumes[0].Volume.SourcePath()) +} + +func TestStartedContainerMetadataUpdateOnRestart(t *testing.T) { + ctrl, client, _, taskEngine, _, imageManager, _ := mocks(t, &defaultConfig) + defer ctrl.Finish() + + dockerID := "1234" + dockerContainer := &api.DockerContainer{ + DockerID: dockerID, + DockerName: "c1", + Container: &api.Container{}, + } + + portBindings := []api.PortBinding{ + { + ContainerPort: 80, + HostPort: 80, + BindIP: "0.0.0.0/0", + Protocol: api.TransportProtocolTCP, + }, + } + labels := map[string]string{ + "name": "metadata", + } + startedAt := time.Now() + gomock.InOrder( + client.EXPECT().DescribeContainer(dockerID).Return(api.ContainerRunning, + DockerContainerMetadata{ + Labels: labels, + DockerID: dockerID, + StartedAt: startedAt, + PortBindings: portBindings, + }), + imageManager.EXPECT().RecordContainerReference(dockerContainer.Container), + ) + taskEngine.(*DockerTaskEngine).synchronizeContainerStatus(dockerContainer, nil) + assert.Equal(t, startedAt, dockerContainer.Container.GetStartedAt()) + assert.Equal(t, labels, dockerContainer.Container.GetLabels()) + assert.Equal(t, uint16(80), dockerContainer.Container.KnownPortBindings[0].ContainerPort) +} + +func TestStoppedContainerMetadataUpdateOnRestart(t *testing.T) { + ctrl, client, mockTime, taskEngine, _, imageManager, _ := mocks(t, &defaultConfig) + defer ctrl.Finish() + + dockerID := "1234" + dockerContainer := &api.DockerContainer{ + DockerID: dockerID, + DockerName: "c1", + Container: &api.Container{ + Essential: true, + }, + } + task := &api.Task{} + + labels := map[string]string{ + "name": "metadata", + } + finishedAt := time.Now() + gomock.InOrder( + client.EXPECT().DescribeContainer(dockerID).Return(api.ContainerStopped, + DockerContainerMetadata{ + Labels: labels, + DockerID: dockerID, + FinishedAt: finishedAt, + ExitCode: aws.Int(1), + }), + imageManager.EXPECT().RecordContainerReference(dockerContainer.Container), + mockTime.EXPECT().Now().Return(time.Now()), + ) + taskEngine.(*DockerTaskEngine).synchronizeContainerStatus(dockerContainer, task) + assert.Equal(t, finishedAt, dockerContainer.Container.GetFinishedAt()) + assert.Equal(t, labels, dockerContainer.Container.GetLabels()) + assert.Equal(t, 1, aws.IntValue(dockerContainer.Container.GetKnownExitCode())) + assert.False(t, task.GetExecutionStoppedAt().IsZero()) +} + +func TestErroredContainerMetadataUpdateOnRestart(t *testing.T) { + ctrl, client, _, taskEngine, _, _, _ := mocks(t, &defaultConfig) + defer ctrl.Finish() + + dockerID := "1234" + dockerContainer := &api.DockerContainer{ + DockerID: dockerID, + DockerName: "c1", + Container: &api.Container{}, + } + task := &api.Task{} + + labels := map[string]string{ + "name": "metadata", + } + finishedAt := time.Now() + gomock.InOrder( + client.EXPECT().DescribeContainer(dockerID).Return(api.ContainerStopped, + DockerContainerMetadata{ + Labels: labels, + DockerID: dockerID, + FinishedAt: finishedAt, + Error: NewDockerStateError("failed"), + ExitCode: aws.Int(1), + }), + ) + taskEngine.(*DockerTaskEngine).synchronizeContainerStatus(dockerContainer, task) + assert.Equal(t, finishedAt, dockerContainer.Container.GetFinishedAt()) + assert.Equal(t, labels, dockerContainer.Container.GetLabels()) + assert.Equal(t, 1, aws.IntValue(dockerContainer.Container.GetKnownExitCode())) + assert.Error(t, dockerContainer.Container.ApplyingError) +}