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 27, 2018
1 parent 8774ed4 commit bd4bda0
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 0 deletions.
40 changes: 40 additions & 0 deletions agent/api/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package api

import (
"encoding/json"
"fmt"
"reflect"
"runtime"
"testing"
Expand Down Expand Up @@ -1386,3 +1387,42 @@ func TestContainerHealthConfig(t *testing.T) {
assert.Equal(t, config.Healthcheck.Timeout, 4*time.Second)
assert.Equal(t, config.Healthcheck.StartPeriod, 1*time.Minute)
}

func TestRecordExecutionStoppedAt(t *testing.T) {
testCases := []struct {
essential bool
status ContainerStatus
executionStoppedAtSet bool
msg string
}{
{
essential: true,
status: ContainerStopped,
executionStoppedAtSet: true,
msg: "essential container stopped should have executionStoppedAt set",
},
{
essential: false,
status: ContainerStopped,
executionStoppedAtSet: false,
msg: "non essential container stopped should not cause executionStoppedAt set",
},
{
essential: true,
status: ContainerRunning,
executionStoppedAtSet: false,
msg: "essential non-stop status change should not cause executionStoppedAt set",
},
}

for _, tc := range testCases {
t.Run(fmt.Sprintf("Container status: %s, essential: %v, executionStoppedAt should be set: %v", tc.status, tc.essential, tc.executionStoppedAtSet), func(t *testing.T) {
task := &Task{}
task.RecordExecutionStoppedAt(&Container{
Essential: tc.essential,
KnownStatusUnsafe: tc.status,
})
assert.Equal(t, !tc.executionStoppedAtSet, task.GetExecutionStoppedAt().IsZero(), tc.msg)
})
}
}
167 changes: 167 additions & 0 deletions agent/engine/docker_task_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1720,3 +1720,170 @@ func TestHandleDockerHealthEvent(t *testing.T) {
})
assert.Equal(t, testContainer.Health.Status, api.ContainerHealthy)
}

func TestCreatedContainerMetadataUpdateOnRestart(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
ctrl, client, _, taskEngine, _, imageManager, _ := mocks(t, ctx, &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) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
ctrl, client, _, taskEngine, _, imageManager, _ := mocks(t, ctx, &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) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
ctrl, client, _, taskEngine, _, imageManager, _ := mocks(t, ctx, &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),
)
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) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
ctrl, client, _, taskEngine, _, _, _ := mocks(t, ctx, &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 bd4bda0

Please sign in to comment.