Skip to content

Commit fb04bd1

Browse files
committed
tests: fix container leak and improve SSH server reliability
Add container cleanup via t.Cleanup to prevent resource leaks, add signal trapping in the SSH server script for clean shutdown, use the container IP instead of 127.0.0.1 for readiness checks, and propagate context errors through CloseWithError for better timeout diagnostics. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
1 parent 7a197c9 commit fb04bd1

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

test/git_services/teststate.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bufio"
55
"bytes"
66
"context"
7-
_ "embed"
87
"encoding/json"
98
"errors"
109
"fmt"
@@ -178,6 +177,10 @@ func (ts *TestState) newContainer(ctx context.Context, rootfs llb.State) gwclien
178177
t.Fatalf("could not create container: %s", err)
179178
}
180179

180+
t.Cleanup(func() {
181+
cont.Release(context.WithoutCancel(ctx)) //nolint:errcheck
182+
})
183+
181184
return cont
182185
}
183186

@@ -224,6 +227,10 @@ func (ts *TestState) StartSSHServer(ctx context.Context, gitHost llb.State) Serv
224227
#!/usr/bin/env sh
225228
set -e
226229
230+
# Make sure we trap exit signals
231+
# This is POSIX shell, not bash, so we can't use the "EXIT" shorthand
232+
trap exit INT HUP TERM
233+
227234
IP=$({{ .HTTPServerPath }} getip)
228235
PORT="{{ .SSHPort }}"
229236
@@ -235,7 +242,7 @@ func (ts *TestState) StartSSHServer(ctx context.Context, gitHost llb.State) Serv
235242
SERVER_PID=$!
236243
237244
# Wait for server to be ready
238-
while ! nc -zw5 127.0.0.1 "$PORT" 2>/dev/null; do
245+
while ! nc -zw5 "${IP}" "$PORT" 2>/dev/null; do
239246
# Check if server is still running
240247
if ! kill -0 $SERVER_PID 2>/dev/null; then
241248
echo '{"type":"error","error":{"message":"sshd exited unexpectedly"}}'
@@ -367,16 +374,14 @@ func (ts *TestState) waitForReady(ctx context.Context, proc *containerProcess, t
367374
// Close the pipe on timeout to unblock the decoder
368375
go func() {
369376
<-ctx.Done()
370-
proc.stdoutR.Close()
377+
proc.stdoutR.CloseWithError(ctx.Err())
371378
}()
372379

373380
for event, err := range proc.Events() {
374381
if err != nil {
375-
if ctx.Err() != nil {
376-
t.Fatalf("timeout waiting for server ready event")
377-
}
378382
t.Fatalf("error reading event: %v", err)
379383
}
384+
380385
switch event.Type {
381386
case EventTypeReady:
382387
if event.Ready == nil {

0 commit comments

Comments
 (0)