Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
amogh09 committed Apr 29, 2024
1 parent 9f9aa2a commit ab40ae3
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
8 changes: 8 additions & 0 deletions agent/engine/common_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ func verifyContainerStoppedStateChange(t *testing.T, taskEngine TaskEngine) {
"Expected container to be STOPPED")
}

func verifyContainerStoppedStateChangeWithReason(t *testing.T, taskEngine TaskEngine, reason string) {
stateChangeEvents := taskEngine.StateChangeEvents()
event := <-stateChangeEvents
assert.Equal(t, event.(api.ContainerStateChange).Status, apicontainerstatus.ContainerStopped,
"Expected container to be STOPPED")
assert.Equal(t, reason, event.(api.ContainerStateChange).Reason)
}

func verifyContainerStoppedStateChangeWithRuntimeID(t *testing.T, taskEngine TaskEngine) {
stateChangeEvents := taskEngine.StateChangeEvents()
event := <-stateChangeEvents
Expand Down
78 changes: 74 additions & 4 deletions agent/engine/engine_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ const (
"StartPeriod":100000000,
"Retries":3}
}`

// busybox image avaialble in a local registry set up by `make test-registry`
localRegistryBusyboxImage = "127.0.0.1:51670/busybox"
localRegistryBusyboxImageDigest = "sha256:51de9138b0cc394c813df84f334d638499333cac22edd05d0300b2c9a2dc80dd"
)

func init() {
Expand Down Expand Up @@ -675,17 +679,17 @@ func TestPullContainerManifestInteg(t *testing.T) {
},
{
name: "digest can be resolved from explicit tag",
image: "127.0.0.1:51670/busybox:latest",
image: localRegistryBusyboxImage,
imagePullBehaviors: allPullBehaviors,
},
{
name: "digest can be resolved without an explicit tag",
image: "127.0.0.1:51670/busybox",
image: localRegistryBusyboxImage,
imagePullBehaviors: allPullBehaviors,
},
{
name: "manifest pull can timeout",
image: "127.0.0.1:51670/busybox",
image: localRegistryBusyboxImage,
setConfig: func(c *config.Config) { c.ManifestPullTimeout = 0 },
imagePullBehaviors: []config.ImagePullBehaviorType{config.ImagePullAlwaysBehavior},

Expand All @@ -695,7 +699,7 @@ func TestPullContainerManifestInteg(t *testing.T) {
},
{
name: "manifest pull can timeout - non-zero timeout",
image: "127.0.0.1:51670/busybox",
image: localRegistryBusyboxImage,
setConfig: func(c *config.Config) { c.ManifestPullTimeout = 100 * time.Microsecond },
imagePullBehaviors: []config.ImagePullBehaviorType{config.ImagePullAlwaysBehavior},
assertError: func(t *testing.T, err error) {
Expand Down Expand Up @@ -831,3 +835,69 @@ func TestPullContainerWithAndWithoutDigestConsistency(t *testing.T) {
// Image should be the same
assert.Equal(t, inspectWithDigest.ID, inspectWithoutDigest.ID)
}

// Tests that a task with invalid image fails as expected.
func TestInvalidImageInteg(t *testing.T) {
// Prepare task engine
cfg := defaultTestConfigIntegTest()
cfg.ImagePullBehavior = config.ImagePullAlwaysBehavior
taskEngine, done, _ := setup(cfg, nil, t)
defer done()

// Prepare a task
container := createTestContainerWithImageAndName("127.0.0.1:51670/invalid-image", "container")
task := &apitask.Task{
Arn: "test-arn",
Family: "family",
Version: "1",
DesiredStatusUnsafe: apitaskstatus.TaskRunning,
Containers: []*apicontainer.Container{container},
}

// Start the task
go taskEngine.AddTask(task)

// The container and the task both should stop
verifyContainerStoppedStateChangeWithReason(t, taskEngine,
"CannotPullImageManifestError: Error response from daemon: manifest unknown: manifest unknown")
verifyTaskStoppedStateChange(t, taskEngine)

err := taskEngine.(*DockerTaskEngine).removeContainer(task, container)
require.NoError(t, err, "failed to remove container during cleanup")
}

// Tests that a task with an image that has a digest specified works normally.
func TestImageWithDigestInteg(t *testing.T) {
// Prepare task engine
cfg := defaultTestConfigIntegTest()
cfg.ImagePullBehavior = config.ImagePullAlwaysBehavior
taskEngine, done, _ := setup(cfg, nil, t)
defer done()

// Prepare a task with image digest
container := createTestContainerWithImageAndName(
localRegistryBusyboxImage+"@"+localRegistryBusyboxImageDigest, "container")
container.Command = []string{"sh", "-c", "sleep 1"}
task := &apitask.Task{
Arn: "test-arn",
Family: "family",
Version: "1",
DesiredStatusUnsafe: apitaskstatus.TaskRunning,
Containers: []*apicontainer.Container{container},
}

// Start the task
go taskEngine.AddTask(task)

// The task should run
verifyContainerRunningStateChange(t, taskEngine)
verifyTaskRunningStateChange(t, taskEngine)
assert.Equal(t, localRegistryBusyboxImageDigest, container.GetImageDigest())

// Cleanup
container.SetDesiredStatus(apicontainerstatus.ContainerStopped)
verifyContainerStoppedStateChange(t, taskEngine)
verifyTaskStoppedStateChange(t, taskEngine)
err := taskEngine.(*DockerTaskEngine).removeContainer(task, container)
require.NoError(t, err, "failed to remove container during cleanup")
}

0 comments on commit ab40ae3

Please sign in to comment.