@@ -7,10 +7,12 @@ import (
77"fmt"
88"io"
99"os"
10+ "os/signal"
1011"path/filepath"
1112"runtime"
1213"strconv"
1314"strings"
15+ "sync"
1416"testing"
1517"time"
1618
@@ -203,12 +205,24 @@ func setup(ctx context.Context, t *testing.T, name, coderImg, coderVersion strin
203205require .NoError (t ,err ,"create test deployment" )
204206
205207t .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 () {
208210t .Logf ("stopping container %s\n " ,ctr .ID )
209211_ = cli .ContainerRemove (ctx ,ctr .ID , container.RemoveOptions {
210212Force :true ,
211213})
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 )
212226})
213227
214228err = cli .ContainerStart (ctx ,ctr .ID , container.StartOptions {})