Skip to content

Commit 0da135c

Browse files
authored
chore(integration): cleanup on interrupt (#283)
1 parent f69a14d commit 0da135c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

integration/integration_test.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -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
203205
require.NoError(t, err, "create test deployment")
204206

205207
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() {
208210
t.Logf("stopping container %s\n", ctr.ID)
209211
_ = cli.ContainerRemove(ctx, ctr.ID, container.RemoveOptions{
210212
Force: 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

214228
err = cli.ContainerStart(ctx, ctr.ID, container.StartOptions{})

0 commit comments

Comments
 (0)