@@ -18,6 +18,7 @@ package e2etests
1818
1919import (
2020 "bytes"
21+ "context"
2122 "encoding/xml"
2223 "errors"
2324 "fmt"
@@ -26,9 +27,11 @@ import (
2627 "log"
2728 "os"
2829 "os/exec"
30+ "os/signal"
2931 "path"
3032 "path/filepath"
3133 "strings"
34+ "syscall"
3235 "testing"
3336
3437 "github.com/bazelbuild/rules_go/go/runfiles"
@@ -68,11 +71,13 @@ type TestContext struct {
6871 // registered cleanup functions are invoked.
6972 cleanups []func ()
7073 teardownCalled bool
74+
75+ context context.Context
76+ cancelled bool
7177}
7278
73- // Runs the given command with the given set of envvars overrided.
74- func (tc * TestContext ) RunCmdWithEnv (command []string , envvars map [string ]string ) (string , error ) {
75- cmd := exec .CommandContext (tc .t .Context (), command [0 ], command [1 :]... )
79+ func runCmdWithContextEnv (ctx context.Context , command []string , envvars map [string ]string ) (string , error ) {
80+ cmd := exec .CommandContext (ctx , command [0 ], command [1 :]... )
7681
7782 cmdOutputBuf := bytes.Buffer {}
7883 cmdWriter := io .MultiWriter (& cmdOutputBuf , log .Writer ())
@@ -84,7 +89,7 @@ func (tc *TestContext) RunCmdWithEnv(command []string, envvars map[string]string
8489 envvarPairs = append (envvarPairs , fmt .Sprintf ("%s=%s" , k , v ))
8590 }
8691 cmd .Env = os .Environ ()
87- cmd .Env = append (cmd .Env , envvarPairs ... )
92+ cmd .Env = append (cmd .Env , envvarPairs ... )
8893
8994 log .Printf ("Running `%s %s`\n " , strings .Join (envvarPairs , " " ), strings .Join (command , " " ))
9095 err := cmd .Run ()
@@ -95,6 +100,11 @@ func (tc *TestContext) RunCmdWithEnv(command []string, envvars map[string]string
95100 return cmdOutputBuf .String (), nil
96101}
97102
103+ // Runs the given command with the given set of envvars overrided.
104+ func (tc * TestContext ) RunCmdWithEnv (command []string , envvars map [string ]string ) (string , error ) {
105+ return runCmdWithContextEnv (tc .context , command , envvars )
106+ }
107+
98108// Waits for a device to be available via adb.
99109func (tc * TestContext ) RunAdbWaitForDevice () error {
100110 adbCommand := []string {
@@ -283,6 +293,8 @@ func (tc *TestContext) CVDLoad(load LoadArgs) error {
283293func (tc * TestContext ) SetUp (t * testing.T ) {
284294 tc .t = t
285295 tc .teardownCalled = false
296+ cancellableContext , cancel := context .WithCancel (t .Context ())
297+ tc .context = cancellableContext
286298
287299 log .Printf ("Initializing %s test..." , tc .t .Name ())
288300
@@ -314,6 +326,21 @@ func (tc *TestContext) SetUp(t *testing.T) {
314326
315327 log .Printf ("Initialized %s test!" , tc .t .Name ())
316328
329+ sigChan := make (chan os.Signal , 10 )
330+ signal .Notify (sigChan , os .Interrupt , syscall .SIGTERM )
331+
332+ go func () {
333+ for sig := range sigChan {
334+ t .Errorf ("Received signal '%s'" , sig .String ())
335+ tc .cancelled = true
336+ cancel ()
337+ }
338+ }()
339+
340+ if tc .cancelled {
341+ t .Fatalf ("Previous test was cancelled" )
342+ }
343+
317344 tc .t .Cleanup (func () {
318345 if ! tc .teardownCalled {
319346 tc .t .Fatalf ("Test %s forgot to call TearDown()." , tc .t .Name ())
@@ -373,7 +400,7 @@ func (tc *TestContext) TearDown() {
373400 err := os .MkdirAll (outinstancedir , os .ModePerm )
374401 if err == nil {
375402 logdir := path .Join (instancedir , "logs" )
376- _ , err := tc . RunCmd ( "cp" , "-r" , "--dereference" , logdir , outinstancedir )
403+ _ , err := runCmdWithContextEnv ( context . TODO (), [] string { "cp" , "-r" , "--dereference" , logdir , outinstancedir }, map [ string ] string {} )
377404 if err != nil {
378405 log .Printf ("failed to copy %s to %s: %w" , logdir , outinstancedir , err )
379406 }
@@ -387,7 +414,7 @@ func (tc *TestContext) TearDown() {
387414 }
388415 }
389416
390- tc . RunCmd ( "cvd" , "reset" , "-y" )
417+ runCmdWithContextEnv ( context . TODO (), [] string { "cvd" , "reset" , "-y" }, map [ string ] string {} )
391418
392419 log .Printf ("Finished cleaning up after test!" )
393420}
@@ -459,6 +486,7 @@ type XtsArgs struct {
459486func RunXts (t * testing.T , cuttlefishArgs FetchAndCreateArgs , xtsArgs XtsArgs ) {
460487 tc := TestContext {}
461488 tc .SetUp (t )
489+ defer tc .TearDown ()
462490
463491 localXtsDir := findLocalXTS (cuttlefishArgs , xtsArgs )
464492 if localXtsDir != "" {
@@ -511,9 +539,9 @@ func RunXts(t *testing.T, cuttlefishArgs FetchAndCreateArgs, xtsArgs XtsArgs) {
511539 }
512540
513541 var xtsResult xtsResult
514- if err := xml .Unmarshal ([]byte (xtsResultsBytes ), & xtsResult ); err != nil {
542+ if err := xml .Unmarshal ([]byte (xtsResultsBytes ), & xtsResult ); err != nil {
515543 t .Fatalf ("failed to parse XTS XML results from %s: %w" , xtsResultsPath , err )
516- }
544+ }
517545
518546 for _ , xtsModule := range (xtsResult .Modules ) {
519547 for _ , xtsTestCase := range (xtsModule .TestCases ) {
@@ -529,6 +557,4 @@ func RunXts(t *testing.T, cuttlefishArgs FetchAndCreateArgs, xtsArgs XtsArgs) {
529557 }
530558 }
531559 log .Printf ("Finished parsing XTS results!" , err )
532-
533- tc .TearDown ()
534560}
0 commit comments