Skip to content

Commit

Permalink
handle nil container Reason
Browse files Browse the repository at this point in the history
  • Loading branch information
sherzberg committed Nov 8, 2019
1 parent e86ae03 commit 9252043
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
6 changes: 5 additions & 1 deletion runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,11 @@ func writeContainerFinishedMessage(ctx context.Context, w *logWriter, task *ecs.
return fmt.Errorf("expected container to be STOPPED, got %s", *container.LastStatus)
}
if container.ExitCode == nil {
return errors.New(*container.Reason)
if container.Reason != nil {
return errors.New(*container.Reason)
} else {
return errors.New(*task.StoppedReason)
}
}
return w.WriteString(ctx, fmt.Sprintf(
"Container %s exited with %d",
Expand Down
64 changes: 63 additions & 1 deletion runner/runner_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package runner

import "testing"
import (
"context"
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/ecs"
)

func TestAWSKeyValuePairForEnvEmpty(t *testing.T) {
lookupEnv := func(key string) (string, bool) {
Expand Down Expand Up @@ -74,3 +80,59 @@ func TestAWSKeyValuePairForEnvMissing(t *testing.T) {
t.Fatalf("bad error message returned: %q", err.Error())
}
}

func TestWriteContainerFinishedMessageNotStopped(t *testing.T) {
ctx := context.Background()
w := &logWriter{}
task := ecs.Task{}
lastStatus := "NOTSTOPPED"
container := ecs.Container{
LastStatus: &lastStatus,
}

err := writeContainerFinishedMessage(ctx, w, &task, &container)

if err.Error() != fmt.Sprintf("expected container to be STOPPED, got %s", lastStatus) {
t.Fatalf("Expected a specific error, got %s", err)
}
}

func TestWriteContainerFinishedMessageNilExitCodeUseContainerReason(t *testing.T) {
ctx := context.Background()
w := &logWriter{}
task := ecs.Task{}
lastStatus := "STOPPED"
reason := "some aws container reason"
container := ecs.Container{
LastStatus: &lastStatus,
ExitCode: nil,
Reason: &reason,
}

err := writeContainerFinishedMessage(ctx, w, &task, &container)

if err.Error() != reason {
t.Fatalf("Expected a specific error, got %s", err)
}
}

func TestWriteContainerFinishedMessageNilExitCodeUseTaskReason(t *testing.T) {
ctx := context.Background()
w := &logWriter{}
reason := "some aws task reason"
task := ecs.Task{
StoppedReason: &reason,
}
lastStatus := "STOPPED"
container := ecs.Container{
LastStatus: &lastStatus,
ExitCode: nil,
Reason: nil,
}

err := writeContainerFinishedMessage(ctx, w, &task, &container)

if err.Error() != reason {
t.Fatalf("Expected a specific error, got %s", err)
}
}

0 comments on commit 9252043

Please sign in to comment.