Skip to content

Commit

Permalink
fix(ryul): test by overriding reaper endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
romainlaurent committed Oct 3, 2024
1 parent 937f3b5 commit 755f116
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math/rand"
"net"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -197,15 +198,14 @@ func reuseOrCreateReaper(ctx context.Context, sessionID string, provider ReaperP
// reuseReaperContainer constructs a Reaper from an already running reaper
// DockerContainer.
func reuseReaperContainer(ctx context.Context, sessionID string, provider ReaperProvider, reaperContainer *DockerContainer) (*Reaper, error) {
endpoint, err := reaperContainer.PortEndpoint(ctx, "8080", "")
endpoint, err := reaperEndpoint(ctx, reaperContainer)
if err != nil {
return nil, err
}

Logger.Printf("⏳ Waiting for Reaper port to be ready")

err = wait.ForLog("Started").WaitUntilReady(ctx, reaperContainer)
if err != nil {
if err := wait.ForLog("Started!").WaitUntilReady(ctx, reaperContainer); err != nil {
return nil, fmt.Errorf("failed waiting for reaper container %s to be ready: %w",
reaperContainer.ID[:8], err)
}
Expand All @@ -218,6 +218,14 @@ func reuseReaperContainer(ctx context.Context, sessionID string, provider Reaper
}, nil
}

func reaperEndpoint(ctx context.Context, c Container) (string, error) {
if _, exists := os.LookupEnv("TESTCONTAINERS_RYUK_ENDPOINT_OVERRIDE_BY_NAME"); exists {
return net.JoinHostPort(reaperContainerNameFromSessionID(c.SessionID()), "8080"), nil
}

return c.PortEndpoint(ctx, "8080", "")
}

// newReaper creates a Reaper with a sessionID to identify containers and a
// provider to use. Do not call this directly, use reuseOrCreateReaper instead.
func newReaper(ctx context.Context, sessionID string, provider ReaperProvider) (*Reaper, error) {
Expand All @@ -237,7 +245,7 @@ func newReaper(ctx context.Context, sessionID string, provider ReaperProvider) (
ExposedPorts: []string{string(listeningPort)},
Labels: core.DefaultLabels(sessionID),
Privileged: tcConfig.RyukPrivileged,
WaitingFor: wait.ForLog("Started"),
WaitingFor: wait.ForLog("Started!"),
Name: reaperContainerNameFromSessionID(sessionID),
HostConfigModifier: func(hc *container.HostConfig) {
hc.AutoRemove = true
Expand Down Expand Up @@ -313,7 +321,7 @@ func newReaper(ctx context.Context, sessionID string, provider ReaperProvider) (
}
reaper.container = c

endpoint, err := c.PortEndpoint(ctx, "8080", "")
endpoint, err := reaperEndpoint(ctx, c)
if err != nil {
return nil, err
}
Expand All @@ -332,6 +340,7 @@ type Reaper struct {

// Connect runs a goroutine which can be terminated by sending true into the returned channel
func (r *Reaper) Connect() (chan bool, error) {
time.Sleep(2 * time.Second)
conn, err := net.DialTimeout("tcp", r.Endpoint, 10*time.Second)
if err != nil {
return nil, fmt.Errorf("%w: Connecting to Ryuk on %s failed", err, r.Endpoint)
Expand Down

0 comments on commit 755f116

Please sign in to comment.