|
1 | 1 | package cmdrun |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
5 | 6 | "os" |
| 7 | + "os/signal" |
| 8 | + "syscall" |
| 9 | + "time" |
6 | 10 |
|
7 | 11 | "github.com/pkg/errors" |
8 | 12 | "github.com/spf13/cobra" |
@@ -31,6 +35,32 @@ func NewRunSuiteCommand(registry *extension.Registry) *cobra.Command { |
31 | 35 | "development use. Orchestration parameters, scheduling, isolation, etc are not obeyed, and Ginkgo tests are executed serially.", |
32 | 36 | SilenceUsage: true, |
33 | 37 | RunE: func(cmd *cobra.Command, args []string) error { |
| 38 | + ctx, cancelCause := context.WithCancelCause(context.Background()) |
| 39 | + defer cancelCause(errors.New("exiting")) |
| 40 | + |
| 41 | + abortCh := make(chan os.Signal, 2) |
| 42 | + go func() { |
| 43 | + <-abortCh |
| 44 | + fmt.Fprintf(os.Stderr, "Interrupted, terminating tests") |
| 45 | + cancelCause(errors.New("interrupt received")) |
| 46 | + |
| 47 | + select { |
| 48 | + case sig := <-abortCh: |
| 49 | + fmt.Fprintf(os.Stderr, "Interrupted twice, exiting (%s)", sig) |
| 50 | + switch sig { |
| 51 | + case syscall.SIGINT: |
| 52 | + os.Exit(130) |
| 53 | + default: |
| 54 | + os.Exit(130) // if we were interrupted, never return zero. |
| 55 | + } |
| 56 | + |
| 57 | + case <-time.After(30 * time.Minute): // allow time for cleanup. If we finish before this, we'll exit |
| 58 | + fmt.Fprintf(os.Stderr, "Timed out during cleanup, exiting") |
| 59 | + os.Exit(130) // if we were interrupted, never return zero. |
| 60 | + } |
| 61 | + }() |
| 62 | + signal.Notify(abortCh, syscall.SIGINT, syscall.SIGTERM) |
| 63 | + |
34 | 64 | ext := registry.Get(opts.componentFlags.Component) |
35 | 65 | if ext == nil { |
36 | 66 | return fmt.Errorf("component not found: %s", opts.componentFlags.Component) |
@@ -72,7 +102,7 @@ func NewRunSuiteCommand(registry *extension.Registry) *cobra.Command { |
72 | 102 | return errors.Wrap(err, "couldn't filter specs") |
73 | 103 | } |
74 | 104 |
|
75 | | - return specs.Run(compositeWriter, opts.concurrencyFlags.MaxConcurency) |
| 105 | + return specs.Run(ctx, compositeWriter, opts.concurrencyFlags.MaxConcurency) |
76 | 106 | }, |
77 | 107 | } |
78 | 108 | opts.componentFlags.BindFlags(cmd.Flags()) |
|
0 commit comments