[execution] Add a test for context cancelation
fxrev.dev/546922 ensures that `Exec()` will return a context.Canceled
error if the command fails due to a context cancelation, so add a test
to make sure that we preserve that behavior, since it's important for
producing understandable error messages.
Change-Id: I1bf5621b85006196223d37e882e65d262626e9b6
Reviewed-on: https://fuchsia-review.googlesource.com/c/infra/infra/+/547941
Fuchsia-Auto-Submit: Oliver Newman <olivernewman@google.com>
Commit-Queue: Auto-Submit <auto-submit@fuchsia-infra.iam.gserviceaccount.com>
Reviewed-by: Mahesh Saripalli <maheshsr@google.com>
diff --git a/execution/executor_test.go b/execution/executor_test.go
index 358876a..1a75006 100644
--- a/execution/executor_test.go
+++ b/execution/executor_test.go
@@ -7,6 +7,8 @@
import (
"bytes"
"context"
+ "errors"
+ "io"
"os/exec"
"testing"
)
@@ -28,9 +30,9 @@
name string
cmds []command
}{
- {"All pass", []command{helloCmd, byeCmd}},
- {"Middle command fails", []command{helloCmd, unknownCmd, byeCmd}},
- {"Last command fails", []command{helloCmd, byeCmd, unknownCmd}},
+ {"all pass", []command{helloCmd, byeCmd}},
+ {"middle command fails", []command{helloCmd, unknownCmd, byeCmd}},
+ {"last command fails", []command{helloCmd, byeCmd, unknownCmd}},
}
for _, test := range tests {
@@ -55,4 +57,13 @@
}
})
}
+
+ t.Run("context canceled", func(t *testing.T) {
+ executor := NewExecutor(io.Discard, io.Discard, t.TempDir())
+ ctx, cancel := context.WithCancel(context.Background())
+ go cancel()
+ if err := executor.Exec(ctx, "sleep", "2"); !errors.Is(err, context.Canceled) {
+ t.Fatalf("Expected Exec to return context.Canceled after context cancelation but got: %s", err)
+ }
+ })
}