Skip to content

Commit 07891a3

Browse files
committed
fix: return reaper handshake errors
1 parent ea854ec commit 07891a3

2 files changed

Lines changed: 51 additions & 13 deletions

File tree

reaper.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,14 @@ func (r *Reaper) connect(ctx context.Context) (chan bool, error) {
542542
return nil, fmt.Errorf("dial reaper %s: %w", r.Endpoint, err)
543543
}
544544

545+
if err := r.handshake(conn); err != nil {
546+
conn.Close()
547+
return nil, fmt.Errorf("handshake reaper %s: %w", r.Endpoint, err)
548+
}
549+
545550
terminationSignal := make(chan bool)
546551
go func() {
547552
defer conn.Close()
548-
if err := r.handshake(conn); err != nil {
549-
log.Printf("Reaper handshake failed: %s", err)
550-
}
551553
<-terminationSignal
552554
}()
553555
return terminationSignal, nil

reaper_test.go

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package testcontainers
22

33
import (
4+
"bufio"
45
"context"
56
"errors"
67
"fmt"
@@ -529,20 +530,55 @@ func TestReaper_ReuseRunning(t *testing.T) {
529530
}
530531
}
531532

532-
// reaperConnect copies the logic from Reaper.connect() but with better error handling.
533-
// Reaper.connect() neither returns the error from the handshake, nor the connection,
534-
// making the testing of the handshake flow impossible.
533+
func TestReaperConnectReturnsHandshakeError(t *testing.T) {
534+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
535+
defer cancel()
536+
537+
listener, err := net.Listen("tcp", "127.0.0.1:0")
538+
require.NoError(t, err)
539+
t.Cleanup(func() {
540+
require.NoError(t, listener.Close())
541+
})
542+
543+
done := make(chan struct{})
544+
go func() {
545+
defer close(done)
546+
547+
conn, err := listener.Accept()
548+
if err != nil {
549+
return
550+
}
551+
defer conn.Close()
552+
553+
_, _ = bufio.NewReader(conn).ReadString('\n')
554+
_, _ = conn.Write([]byte("NOPE"))
555+
}()
556+
557+
reaper := &Reaper{
558+
SessionID: testSessionID,
559+
Endpoint: listener.Addr().String(),
560+
}
561+
562+
termSignal, err := reaper.connect(ctx)
563+
require.Nil(t, termSignal)
564+
require.ErrorContains(t, err, "handshake reaper")
565+
require.ErrorContains(t, err, "unexpected reaper response: NOPE")
566+
567+
select {
568+
case <-done:
569+
case <-ctx.Done():
570+
require.FailNow(t, "test reaper server did not finish")
571+
}
572+
}
573+
535574
func reaperConnect(t *testing.T, reaper *Reaper) {
536575
t.Helper()
537576

538-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
539-
defer cancel()
540-
var d net.Dialer
541-
conn, err := d.DialContext(ctx, "tcp", reaper.Endpoint)
542-
require.NoError(t, err, "dial reaper %s: %v", reaper.Endpoint, err)
543-
defer conn.Close()
544-
err = reaper.handshake(conn)
577+
termSignal, err := reaper.Connect()
545578
require.NoError(t, err, "Reaper handshake should be successful")
579+
if termSignal != nil {
580+
termSignal <- true
581+
}
546582
}
547583

548584
func TestSpawnerBackoff(t *testing.T) {

0 commit comments

Comments
 (0)