2020-03-01 16:06:34 +00:00
|
|
|
package testcontainers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
2020-09-06 12:31:23 +00:00
|
|
|
"sync"
|
2020-03-01 16:06:34 +00:00
|
|
|
"time"
|
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/go-connections/nat"
|
|
|
|
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
|
|
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
2020-03-01 16:06:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-08-21 21:04:28 +00:00
|
|
|
// Deprecated: it has been replaced by the internal testcontainersdocker.LabelLang
|
|
|
|
TestcontainerLabel = "org.testcontainers.golang"
|
|
|
|
// Deprecated: it has been replaced by the internal testcontainersdocker.LabelSessionID
|
2020-03-01 16:06:34 +00:00
|
|
|
TestcontainerLabelSessionID = TestcontainerLabel + ".sessionId"
|
2023-08-21 21:04:28 +00:00
|
|
|
// Deprecated: it has been replaced by the internal testcontainersdocker.LabelReaper
|
|
|
|
TestcontainerLabelIsReaper = TestcontainerLabel + ".reaper"
|
2020-03-01 16:06:34 +00:00
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
ReaperDefaultImage = "docker.io/testcontainers/ryuk:0.5.1"
|
2020-03-01 16:06:34 +00:00
|
|
|
)
|
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
var (
|
|
|
|
reaperInstance *Reaper // We would like to create reaper only once
|
|
|
|
mutex sync.Mutex
|
|
|
|
)
|
2020-09-06 12:31:23 +00:00
|
|
|
|
2020-03-01 16:06:34 +00:00
|
|
|
// ReaperProvider represents a provider for the reaper to run itself with
|
|
|
|
// The ContainerProvider interface should usually satisfy this as well, so it is pluggable
|
|
|
|
type ReaperProvider interface {
|
|
|
|
RunContainer(ctx context.Context, req ContainerRequest) (Container, error)
|
2023-08-21 21:04:28 +00:00
|
|
|
Config() TestcontainersConfig
|
2020-03-01 16:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewReaper creates a Reaper with a sessionID to identify containers and a provider to use
|
2023-08-21 21:04:28 +00:00
|
|
|
// Deprecated: it's not possible to create a reaper anymore.
|
2020-03-01 16:06:34 +00:00
|
|
|
func NewReaper(ctx context.Context, sessionID string, provider ReaperProvider, reaperImageName string) (*Reaper, error) {
|
2023-08-21 21:04:28 +00:00
|
|
|
return reuseOrCreateReaper(ctx, sessionID, provider, WithImageName(reaperImageName))
|
|
|
|
}
|
|
|
|
|
|
|
|
// reuseOrCreateReaper returns an existing Reaper instance if it exists and is running. Otherwise, a new Reaper instance
|
|
|
|
// will be created with a sessionID to identify containers and a provider to use
|
|
|
|
func reuseOrCreateReaper(ctx context.Context, sessionID string, provider ReaperProvider, opts ...ContainerOption) (*Reaper, error) {
|
2020-09-06 12:31:23 +00:00
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
2023-08-21 21:04:28 +00:00
|
|
|
// If reaper already exists and healthy, re-use it
|
|
|
|
if reaperInstance != nil {
|
|
|
|
// Verify this instance is still running by checking state.
|
|
|
|
// Can't use Container.IsRunning because the bool is not updated when Reaper is terminated
|
|
|
|
state, err := reaperInstance.container.State(ctx)
|
|
|
|
if err == nil && state.Running {
|
|
|
|
return reaperInstance, nil
|
|
|
|
}
|
2020-09-06 12:31:23 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
r, err := newReaper(ctx, sessionID, provider, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reaperInstance = r
|
|
|
|
return reaperInstance, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newReaper creates a Reaper with a sessionID to identify containers and a provider to use
|
|
|
|
// Should only be used internally and instead use reuseOrCreateReaper to prefer reusing an existing Reaper instance
|
|
|
|
func newReaper(ctx context.Context, sessionID string, provider ReaperProvider, opts ...ContainerOption) (*Reaper, error) {
|
|
|
|
dockerHostMount := testcontainersdocker.ExtractDockerSocket(ctx)
|
|
|
|
|
|
|
|
reaper := &Reaper{
|
2020-03-01 16:06:34 +00:00
|
|
|
Provider: provider,
|
|
|
|
SessionID: sessionID,
|
|
|
|
}
|
|
|
|
|
2020-09-06 12:31:23 +00:00
|
|
|
listeningPort := nat.Port("8080/tcp")
|
2020-03-01 16:06:34 +00:00
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
tcConfig := provider.Config().Config
|
|
|
|
|
|
|
|
reaperOpts := containerOptions{}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&reaperOpts)
|
|
|
|
}
|
|
|
|
|
2020-03-01 16:06:34 +00:00
|
|
|
req := ContainerRequest{
|
2023-08-21 21:04:28 +00:00
|
|
|
Image: reaperImage(reaperOpts.ImageName),
|
2020-09-06 12:31:23 +00:00
|
|
|
ExposedPorts: []string{string(listeningPort)},
|
2020-03-01 16:06:34 +00:00
|
|
|
Labels: map[string]string{
|
2023-08-21 21:04:28 +00:00
|
|
|
TestcontainerLabelIsReaper: "true",
|
|
|
|
testcontainersdocker.LabelReaper: "true",
|
2020-03-01 16:06:34 +00:00
|
|
|
},
|
2023-08-21 21:04:28 +00:00
|
|
|
Mounts: Mounts(BindMount(dockerHostMount, "/var/run/docker.sock")),
|
|
|
|
Privileged: tcConfig.RyukPrivileged,
|
|
|
|
WaitingFor: wait.ForListeningPort(listeningPort),
|
|
|
|
ReaperOptions: opts,
|
|
|
|
HostConfigModifier: func(hc *container.HostConfig) {
|
|
|
|
hc.AutoRemove = true
|
|
|
|
hc.NetworkMode = Bridge
|
2020-03-01 16:06:34 +00:00
|
|
|
},
|
2023-08-21 21:04:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// keep backwards compatibility
|
|
|
|
req.ReaperImage = req.Image
|
|
|
|
|
|
|
|
// include reaper-specific labels to the reaper container
|
|
|
|
for k, v := range reaper.Labels() {
|
|
|
|
if k == TestcontainerLabelSessionID || k == testcontainersdocker.LabelSessionID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
req.Labels[k] = v
|
2020-03-01 16:06:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 18:00:46 +00:00
|
|
|
// Attach reaper container to a requested network if it is specified
|
|
|
|
if p, ok := provider.(*DockerProvider); ok {
|
2023-08-21 21:04:28 +00:00
|
|
|
req.Networks = append(req.Networks, p.DefaultNetwork)
|
2021-01-17 18:00:46 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 16:06:34 +00:00
|
|
|
c, err := provider.RunContainer(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-21 21:04:28 +00:00
|
|
|
reaper.container = c
|
2020-03-01 16:06:34 +00:00
|
|
|
|
|
|
|
endpoint, err := c.PortEndpoint(ctx, "8080", "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-06 12:31:23 +00:00
|
|
|
reaper.Endpoint = endpoint
|
2020-03-01 16:06:34 +00:00
|
|
|
|
2020-09-06 12:31:23 +00:00
|
|
|
return reaper, nil
|
2020-03-01 16:06:34 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
// Reaper is used to start a sidecar container that cleans up resources
|
|
|
|
type Reaper struct {
|
|
|
|
Provider ReaperProvider
|
|
|
|
SessionID string
|
|
|
|
Endpoint string
|
|
|
|
container Container
|
2020-03-01 16:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect runs a goroutine which can be terminated by sending true into the returned channel
|
|
|
|
func (r *Reaper) Connect() (chan bool, error) {
|
|
|
|
conn, err := net.DialTimeout("tcp", r.Endpoint, 10*time.Second)
|
|
|
|
if err != nil {
|
2023-08-21 21:04:28 +00:00
|
|
|
return nil, fmt.Errorf("%w: Connecting to Ryuk on %s failed", err, r.Endpoint)
|
2020-03-01 16:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
terminationSignal := make(chan bool)
|
|
|
|
go func(conn net.Conn) {
|
|
|
|
sock := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
labelFilters := []string{}
|
|
|
|
for l, v := range r.Labels() {
|
|
|
|
labelFilters = append(labelFilters, fmt.Sprintf("label=%s=%s", l, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
retryLimit := 3
|
|
|
|
for retryLimit > 0 {
|
|
|
|
retryLimit--
|
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
if _, err := sock.WriteString(strings.Join(labelFilters, "&")); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := sock.WriteString("\n"); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-01 16:06:34 +00:00
|
|
|
if err := sock.Flush(); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := sock.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-08-21 21:04:28 +00:00
|
|
|
|
2020-03-01 16:06:34 +00:00
|
|
|
if resp == "ACK\n" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
<-terminationSignal
|
|
|
|
}(conn)
|
|
|
|
return terminationSignal, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Labels returns the container labels to use so that this Reaper cleans them up
|
|
|
|
func (r *Reaper) Labels() map[string]string {
|
|
|
|
return map[string]string{
|
2023-08-21 21:04:28 +00:00
|
|
|
TestcontainerLabel: "true",
|
|
|
|
TestcontainerLabelSessionID: r.SessionID,
|
|
|
|
testcontainersdocker.LabelSessionID: r.SessionID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func reaperImage(reaperImageName string) string {
|
|
|
|
if reaperImageName == "" {
|
|
|
|
return ReaperDefaultImage
|
2020-03-01 16:06:34 +00:00
|
|
|
}
|
2023-08-21 21:04:28 +00:00
|
|
|
return reaperImageName
|
2020-03-01 16:06:34 +00:00
|
|
|
}
|