Skip to content

Commit

Permalink
test: add test for updating metadata on restart
Browse files Browse the repository at this point in the history
  • Loading branch information
Peng Yin committed Jan 22, 2018
1 parent 0b5e9a6 commit caf8265
Showing 1 changed file with 160 additions and 0 deletions.
160 changes: 160 additions & 0 deletions agent/engine/docker_task_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2288,3 +2288,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)
}

0 comments on commit caf8265

Please sign in to comment.