@@ -7,10 +7,12 @@ import (
7
7
"fmt"
8
8
"io"
9
9
"os"
10
+ "os/signal"
10
11
"path/filepath"
11
12
"runtime"
12
13
"strconv"
13
14
"strings"
15
+ "sync"
14
16
"testing"
15
17
"time"
16
18
@@ -203,12 +205,24 @@ func setup(ctx context.Context, t *testing.T, name, coderImg, coderVersion strin
203
205
require .NoError (t ,err ,"create test deployment" )
204
206
205
207
t .Logf ("created container %s\n " ,ctr .ID )
206
- t . Cleanup ( func () { // Make sure we clean up after ourselves.
207
- // TODO: also have this execute if you Ctrl+C!
208
+ var cleanupOnce sync. Once
209
+ removeContainer := func () {
208
210
t .Logf ("stopping container %s\n " ,ctr .ID )
209
211
_ = cli .ContainerRemove (ctx ,ctr .ID , container.RemoveOptions {
210
212
Force :true ,
211
213
})
214
+ }
215
+ // Ensure the container is cleaned up if you press Ctrl+C.
216
+ sigCh := make (chan os.Signal ,1 )
217
+ signal .Notify (sigCh ,os .Interrupt )
218
+ go func () {
219
+ <- sigCh
220
+ cleanupOnce .Do (removeContainer )
221
+ os .Exit (1 )
222
+ }()
223
+
224
+ t .Cleanup (func () {// Make sure we clean up after ourselves.
225
+ cleanupOnce .Do (removeContainer )
212
226
})
213
227
214
228
err = cli .ContainerStart (ctx ,ctr .ID , container.StartOptions {})