Skip to content

Commit

Permalink
Create separate createVolumeTask helper func for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrc committed Feb 11, 2023
1 parent 117686a commit bccbb18
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 61 deletions.
59 changes: 0 additions & 59 deletions agent/engine/engine_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/aws/amazon-ecs-agent/agent/dockerclient/dockerapi"
"github.com/aws/amazon-ecs-agent/agent/dockerclient/sdkclientfactory"
"github.com/aws/amazon-ecs-agent/agent/engine/dockerstate"
taskresourcevolume "github.com/aws/amazon-ecs-agent/agent/taskresource/volume"
"github.com/aws/aws-sdk-go/aws"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
Expand Down Expand Up @@ -418,64 +417,6 @@ func TestEngineSynchronize(t *testing.T) {
verifyTaskStoppedStateChange(t, taskEngine)
}

func TestSharedAutoprovisionVolume(t *testing.T) {
taskEngine, done, _ := setupWithDefaultConfig(t)
defer done()
stateChangeEvents := taskEngine.StateChangeEvents()
// Set the task clean up duration to speed up the test
taskEngine.(*DockerTaskEngine).cfg.TaskCleanupWaitDuration = 1 * time.Second

testTask, err := createVolumeTask(t, "shared", "TestSharedAutoprovisionVolume", "TestSharedAutoprovisionVolume", true)
require.NoError(t, err, "creating test task failed")

go taskEngine.AddTask(testTask)

verifyTaskIsRunning(stateChangeEvents, testTask)
verifyTaskIsStopped(stateChangeEvents, testTask)
assert.Equal(t, *testTask.Containers[0].GetKnownExitCode(), 0)
assert.Equal(t, testTask.ResourcesMapUnsafe["dockerVolume"][0].(*taskresourcevolume.VolumeResource).VolumeConfig.DockerVolumeName, "TestSharedAutoprovisionVolume", "task volume name is not the same as specified in task definition")
// Wait for task to be cleaned up
testTask.SetSentStatus(apitaskstatus.TaskStopped)
waitForTaskCleanup(t, taskEngine, testTask.Arn, 5)
client := taskEngine.(*DockerTaskEngine).client
response := client.InspectVolume(context.TODO(), "TestSharedAutoprovisionVolume", 1*time.Second)
assert.NoError(t, response.Error, "expect shared volume not removed")

cleanVolumes(testTask, taskEngine)
}

func TestSharedDoNotAutoprovisionVolume(t *testing.T) {
taskEngine, done, _ := setupWithDefaultConfig(t)
defer done()
stateChangeEvents := taskEngine.StateChangeEvents()
client := taskEngine.(*DockerTaskEngine).client
// Set the task clean up duration to speed up the test
taskEngine.(*DockerTaskEngine).cfg.TaskCleanupWaitDuration = 1 * time.Second

testTask, err := createVolumeTask(t, "shared", "TestSharedDoNotAutoprovisionVolume", "TestSharedDoNotAutoprovisionVolume", false)
require.NoError(t, err, "creating test task failed")

// creating volume to simulate previously provisioned volume
volumeConfig := testTask.Volumes[0].Volume.(*taskresourcevolume.DockerVolumeConfig)
volumeMetadata := client.CreateVolume(context.TODO(), "TestSharedDoNotAutoprovisionVolume",
volumeConfig.Driver, volumeConfig.DriverOpts, volumeConfig.Labels, 1*time.Minute)
require.NoError(t, volumeMetadata.Error)

go taskEngine.AddTask(testTask)

verifyTaskIsRunning(stateChangeEvents, testTask)
verifyTaskIsStopped(stateChangeEvents, testTask)
assert.Equal(t, *testTask.Containers[0].GetKnownExitCode(), 0)
assert.Len(t, testTask.ResourcesMapUnsafe["dockerVolume"], 0, "volume that has been provisioned does not require the agent to create it again")
// Wait for task to be cleaned up
testTask.SetSentStatus(apitaskstatus.TaskStopped)
waitForTaskCleanup(t, taskEngine, testTask.Arn, 5)
response := client.InspectVolume(context.TODO(), "TestSharedDoNotAutoprovisionVolume", 1*time.Second)
assert.NoError(t, response.Error, "expect shared volume not removed")

cleanVolumes(testTask, taskEngine)
}

func TestLabels(t *testing.T) {
taskEngine, done, _ := setupWithDefaultConfig(t)
defer done()
Expand Down
59 changes: 59 additions & 0 deletions agent/engine/engine_unix_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/aws/amazon-ecs-agent/agent/utils/ttime"
"github.com/aws/aws-sdk-go/aws"
sdkClient "github.com/docker/docker/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -196,6 +197,64 @@ func createVolumeTask(t *testing.T, scope, arn, volume string, autoprovision boo
return testTask, nil
}

func TestSharedAutoprovisionVolume(t *testing.T) {
taskEngine, done, _ := setupWithDefaultConfig(t)
defer done()
stateChangeEvents := taskEngine.StateChangeEvents()
// Set the task clean up duration to speed up the test
taskEngine.(*DockerTaskEngine).cfg.TaskCleanupWaitDuration = 1 * time.Second

testTask, err := createVolumeTask(t, "shared", "TestSharedAutoprovisionVolume", "TestSharedAutoprovisionVolume", true)
require.NoError(t, err, "creating test task failed")

go taskEngine.AddTask(testTask)

verifyTaskIsRunning(stateChangeEvents, testTask)
verifyTaskIsStopped(stateChangeEvents, testTask)
assert.Equal(t, *testTask.Containers[0].GetKnownExitCode(), 0)
assert.Equal(t, testTask.ResourcesMapUnsafe["dockerVolume"][0].(*taskresourcevolume.VolumeResource).VolumeConfig.DockerVolumeName, "TestSharedAutoprovisionVolume", "task volume name is not the same as specified in task definition")
// Wait for task to be cleaned up
testTask.SetSentStatus(apitaskstatus.TaskStopped)
waitForTaskCleanup(t, taskEngine, testTask.Arn, 5)
client := taskEngine.(*DockerTaskEngine).client
response := client.InspectVolume(context.TODO(), "TestSharedAutoprovisionVolume", 1*time.Second)
assert.NoError(t, response.Error, "expect shared volume not removed")

cleanVolumes(testTask, taskEngine)
}

func TestSharedDoNotAutoprovisionVolume(t *testing.T) {
taskEngine, done, _ := setupWithDefaultConfig(t)
defer done()
stateChangeEvents := taskEngine.StateChangeEvents()
client := taskEngine.(*DockerTaskEngine).client
// Set the task clean up duration to speed up the test
taskEngine.(*DockerTaskEngine).cfg.TaskCleanupWaitDuration = 1 * time.Second

testTask, err := createVolumeTask(t, "shared", "TestSharedDoNotAutoprovisionVolume", "TestSharedDoNotAutoprovisionVolume", false)
require.NoError(t, err, "creating test task failed")

// creating volume to simulate previously provisioned volume
volumeConfig := testTask.Volumes[0].Volume.(*taskresourcevolume.DockerVolumeConfig)
volumeMetadata := client.CreateVolume(context.TODO(), "TestSharedDoNotAutoprovisionVolume",
volumeConfig.Driver, volumeConfig.DriverOpts, volumeConfig.Labels, 1*time.Minute)
require.NoError(t, volumeMetadata.Error)

go taskEngine.AddTask(testTask)

verifyTaskIsRunning(stateChangeEvents, testTask)
verifyTaskIsStopped(stateChangeEvents, testTask)
assert.Equal(t, *testTask.Containers[0].GetKnownExitCode(), 0)
assert.Len(t, testTask.ResourcesMapUnsafe["dockerVolume"], 0, "volume that has been provisioned does not require the agent to create it again")
// Wait for task to be cleaned up
testTask.SetSentStatus(apitaskstatus.TaskStopped)
waitForTaskCleanup(t, taskEngine, testTask.Arn, 5)
response := client.InspectVolume(context.TODO(), "TestSharedDoNotAutoprovisionVolume", 1*time.Second)
assert.NoError(t, response.Error, "expect shared volume not removed")

cleanVolumes(testTask, taskEngine)
}

// TestStartStopUnpulledImage ensures that an unpulled image is successfully
// pulled, run, and stopped via docker.
func TestStartStopUnpulledImage(t *testing.T) {
Expand Down
64 changes: 62 additions & 2 deletions agent/engine/engine_windows_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func createTestHealthCheckTask(arn string) *apitask.Task {
return testTask
}

func createVolumeTask(scope, arn, volume string, autoprovision bool) (*apitask.Task, string, error) {
func createVolumeTaskWindows(scope, arn, volume string, autoprovision bool) (*apitask.Task, string, error) {
testTask := createTestTask(arn)

volumeConfig := &taskresourcevolume.DockerVolumeConfig{
Expand Down Expand Up @@ -175,6 +175,66 @@ func createVolumeTask(scope, arn, volume string, autoprovision bool) (*apitask.T
return testTask, volumePath, nil
}

func TestSharedAutoprovisionVolume(t *testing.T) {
taskEngine, done, _ := setupWithDefaultConfig(t)
defer done()
stateChangeEvents := taskEngine.StateChangeEvents()
// Set the task clean up duration to speed up the test
taskEngine.(*DockerTaskEngine).cfg.TaskCleanupWaitDuration = 1 * time.Second

testTask, tmpDirectory, err := createVolumeTaskWindows("shared", "TestSharedAutoprovisionVolume", "TestSharedAutoprovisionVolume", true)
defer os.Remove(tmpDirectory)
require.NoError(t, err, "creating test task failed")

go taskEngine.AddTask(testTask)

verifyTaskIsRunning(stateChangeEvents, testTask)
verifyTaskIsStopped(stateChangeEvents, testTask)
assert.Equal(t, *testTask.Containers[0].GetKnownExitCode(), 0)
assert.Equal(t, testTask.ResourcesMapUnsafe["dockerVolume"][0].(*taskresourcevolume.VolumeResource).VolumeConfig.DockerVolumeName, "TestSharedAutoprovisionVolume", "task volume name is not the same as specified in task definition")
// Wait for task to be cleaned up
testTask.SetSentStatus(apitaskstatus.TaskStopped)
waitForTaskCleanup(t, taskEngine, testTask.Arn, 5)
client := taskEngine.(*DockerTaskEngine).client
response := client.InspectVolume(context.TODO(), "TestSharedAutoprovisionVolume", 1*time.Second)
assert.NoError(t, response.Error, "expect shared volume not removed")

cleanVolumes(testTask, taskEngine)
}

func TestSharedDoNotAutoprovisionVolume(t *testing.T) {
taskEngine, done, _ := setupWithDefaultConfig(t)
defer done()
stateChangeEvents := taskEngine.StateChangeEvents()
client := taskEngine.(*DockerTaskEngine).client
// Set the task clean up duration to speed up the test
taskEngine.(*DockerTaskEngine).cfg.TaskCleanupWaitDuration = 1 * time.Second

testTask, tmpDirectory, err := createVolumeTaskWindows("shared", "TestSharedDoNotAutoprovisionVolume", "TestSharedDoNotAutoprovisionVolume", false)
defer os.Remove(tmpDirectory)
require.NoError(t, err, "creating test task failed")

// creating volume to simulate previously provisioned volume
volumeConfig := testTask.Volumes[0].Volume.(*taskresourcevolume.DockerVolumeConfig)
volumeMetadata := client.CreateVolume(context.TODO(), "TestSharedDoNotAutoprovisionVolume",
volumeConfig.Driver, volumeConfig.DriverOpts, volumeConfig.Labels, 1*time.Minute)
require.NoError(t, volumeMetadata.Error)

go taskEngine.AddTask(testTask)

verifyTaskIsRunning(stateChangeEvents, testTask)
verifyTaskIsStopped(stateChangeEvents, testTask)
assert.Equal(t, *testTask.Containers[0].GetKnownExitCode(), 0)
assert.Len(t, testTask.ResourcesMapUnsafe["dockerVolume"], 0, "volume that has been provisioned does not require the agent to create it again")
// Wait for task to be cleaned up
testTask.SetSentStatus(apitaskstatus.TaskStopped)
waitForTaskCleanup(t, taskEngine, testTask.Arn, 5)
response := client.InspectVolume(context.TODO(), "TestSharedDoNotAutoprovisionVolume", 1*time.Second)
assert.NoError(t, response.Error, "expect shared volume not removed")

cleanVolumes(testTask, taskEngine)
}

// TODO Modify the container ip to localhost after the AMI has the required feature
// https://github.com/docker/for-win/issues/204#issuecomment-352899657

Expand Down Expand Up @@ -342,7 +402,7 @@ func TestTaskLevelVolume(t *testing.T) {
defer done()
stateChangeEvents := taskEngine.StateChangeEvents()

testTask, tmpDirectory, err := createVolumeTask("task", "TestTaskLevelVolume", "TestTaskLevelVolume", true)
testTask, tmpDirectory, err := createVolumeTaskWindows("task", "TestTaskLevelVolume", "TestTaskLevelVolume", true)
defer os.Remove(tmpDirectory)
require.NoError(t, err, "creating test task failed")

Expand Down

0 comments on commit bccbb18

Please sign in to comment.