Skip to content

Commit

Permalink
Merge branch 'dev' into dev-stephyr
Browse files Browse the repository at this point in the history
  • Loading branch information
Ephylouise authored Jun 27, 2024
2 parents 963a9e1 + 4635d27 commit 86302d9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion agent/engine/engine_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ func TestPullContainerWithAndWithoutDigestInteg(t *testing.T) {
// Tests that pullContainer pulls the same image when a digest is used versus when a digest
// is not used.
func TestPullContainerWithAndWithoutDigestConsistency(t *testing.T) {
image := "public.ecr.aws/docker/library/alpine:3.19"
image := "public.ecr.aws/docker/library/alpine:3.19.1"
imageDigest := "sha256:c5b1261d6d3e43071626931fc004f70149baeba2c8ec672bd4f27761f8e1ad6b"

// Prepare task
Expand Down
8 changes: 4 additions & 4 deletions ecs-agent/api/container/restart/restart_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ type RestartTracker struct {
// RestartPolicy represents a policy that contains key information considered when
// deciding whether or not a container should be restarted after it has exited.
type RestartPolicy struct {
Enabled bool `json:"enabled"`
IgnoredExitCodes []int `json:"ignoredExitCodes"`
RestartAttemptPeriod time.Duration `json:"restartAttemptPeriod"`
Enabled bool `json:"enabled"`
IgnoredExitCodes []int `json:"ignoredExitCodes"`
RestartAttemptPeriod int `json:"restartAttemptPeriod"`
}

func NewRestartTracker(restartPolicy RestartPolicy) *RestartTracker {
Expand Down Expand Up @@ -92,7 +92,7 @@ func (rt *RestartTracker) ShouldRestart(exitCode *int, startedAt time.Time,
if !rt.LastRestartAt.IsZero() {
startTime = rt.LastRestartAt
}
if time.Since(startTime) < rt.RestartPolicy.RestartAttemptPeriod {
if time.Since(startTime) < time.Duration(rt.RestartPolicy.RestartAttemptPeriod)*time.Second {
return false, "attempt reset period has not elapsed"
}
return true, ""
Expand Down
42 changes: 26 additions & 16 deletions ecs-agent/api/container/restart/restart_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,34 @@
package restart

import (
"encoding/json"
"fmt"
"testing"
"time"

apicontainerstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/container/status"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// unmarshalStandardRestartPolicy allows us to verify that the restart policy unmarshalled
// from JSON (as it comes in the task payload) is translated to expected behavior in
// the restart tracker.
func unmarshalStandardRestartPolicy(t *testing.T, ignoredCode int) RestartPolicy {
rp := RestartPolicy{}
jsonBlob := fmt.Sprintf(`{"enabled": true, "restartAttemptPeriod": 60, "ignoredExitCodes": [%d]}`, ignoredCode)
err := json.Unmarshal([]byte(jsonBlob), &rp)
require.NoError(t, err)
return rp
}

func TestShouldRestart(t *testing.T) {
ignoredCode := 0
rt := NewRestartTracker(RestartPolicy{
Enabled: false,
IgnoredExitCodes: []int{ignoredCode},
RestartAttemptPeriod: 60 * time.Second,
RestartAttemptPeriod: 60,
})
testCases := []struct {
name string
Expand All @@ -46,7 +60,7 @@ func TestShouldRestart(t *testing.T) {
rp: RestartPolicy{
Enabled: false,
IgnoredExitCodes: []int{ignoredCode},
RestartAttemptPeriod: 60 * time.Second,
RestartAttemptPeriod: 60,
},
exitCode: 1,
startedAt: time.Now().Add(2 * time.Minute),
Expand All @@ -55,12 +69,8 @@ func TestShouldRestart(t *testing.T) {
expectedReason: "restart policy is not enabled",
},
{
name: "ignored exit code",
rp: RestartPolicy{
Enabled: true,
IgnoredExitCodes: []int{ignoredCode},
RestartAttemptPeriod: 60 * time.Second,
},
name: "ignored exit code",
rp: unmarshalStandardRestartPolicy(t, ignoredCode),
exitCode: 0,
startedAt: time.Now().Add(-2 * time.Minute),
desiredStatus: apicontainerstatus.ContainerRunning,
Expand All @@ -69,7 +79,7 @@ func TestShouldRestart(t *testing.T) {
},
{
name: "non ignored exit code",
rp: RestartPolicy{Enabled: true, IgnoredExitCodes: []int{ignoredCode}, RestartAttemptPeriod: 60 * time.Second},
rp: unmarshalStandardRestartPolicy(t, ignoredCode),
exitCode: 1,
startedAt: time.Now().Add(-2 * time.Minute),
desiredStatus: apicontainerstatus.ContainerRunning,
Expand All @@ -78,7 +88,7 @@ func TestShouldRestart(t *testing.T) {
},
{
name: "nil exit code",
rp: RestartPolicy{Enabled: true, IgnoredExitCodes: []int{ignoredCode}, RestartAttemptPeriod: 60 * time.Second},
rp: unmarshalStandardRestartPolicy(t, ignoredCode),
exitCode: -1,
startedAt: time.Now().Add(-2 * time.Minute),
desiredStatus: apicontainerstatus.ContainerRunning,
Expand All @@ -87,7 +97,7 @@ func TestShouldRestart(t *testing.T) {
},
{
name: "desired status stopped",
rp: RestartPolicy{Enabled: true, IgnoredExitCodes: []int{ignoredCode}, RestartAttemptPeriod: 60 * time.Second},
rp: unmarshalStandardRestartPolicy(t, ignoredCode),
exitCode: 1,
startedAt: time.Now().Add(2 * time.Minute),
desiredStatus: apicontainerstatus.ContainerStopped,
Expand All @@ -96,7 +106,7 @@ func TestShouldRestart(t *testing.T) {
},
{
name: "attempt reset period not elapsed",
rp: RestartPolicy{Enabled: true, IgnoredExitCodes: []int{ignoredCode}, RestartAttemptPeriod: 60 * time.Second},
rp: unmarshalStandardRestartPolicy(t, ignoredCode),
exitCode: 1,
startedAt: time.Now(),
desiredStatus: apicontainerstatus.ContainerRunning,
Expand All @@ -105,7 +115,7 @@ func TestShouldRestart(t *testing.T) {
},
{
name: "attempt reset period not elapsed within one second",
rp: RestartPolicy{Enabled: true, IgnoredExitCodes: []int{ignoredCode}, RestartAttemptPeriod: 60 * time.Second},
rp: unmarshalStandardRestartPolicy(t, ignoredCode),
exitCode: 1,
startedAt: time.Now().Add(-time.Second * 59),
desiredStatus: apicontainerstatus.ContainerRunning,
Expand Down Expand Up @@ -137,7 +147,7 @@ func TestShouldRestartUsesLastRestart(t *testing.T) {
rt := NewRestartTracker(RestartPolicy{
Enabled: true,
IgnoredExitCodes: []int{0},
RestartAttemptPeriod: 60 * time.Second,
RestartAttemptPeriod: 60,
})
exitCode := 1

Expand All @@ -154,7 +164,7 @@ func TestShouldRestartUsesLastRestart(t *testing.T) {
func TestRecordRestart(t *testing.T) {
rt := NewRestartTracker(RestartPolicy{
Enabled: false,
RestartAttemptPeriod: 60 * time.Second,
RestartAttemptPeriod: 60,
})
assert.Equal(t, 0, rt.RestartCount)
for i := 1; i < 1000; i++ {
Expand All @@ -168,7 +178,7 @@ func TestRecordRestart(t *testing.T) {
func TestRecordRestartPolicy(t *testing.T) {
rt := NewRestartTracker(RestartPolicy{
Enabled: false,
RestartAttemptPeriod: 60 * time.Second,
RestartAttemptPeriod: 60,
})
assert.Equal(t, 0, rt.RestartCount)
assert.Equal(t, 0, len(rt.RestartPolicy.IgnoredExitCodes))
Expand Down

0 comments on commit 86302d9

Please sign in to comment.