2022-05-24 01:08:53 +08:00
|
|
|
//go:build !freebsd
|
|
|
|
|
// +build !freebsd
|
|
|
|
|
|
|
|
|
|
package testutil
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/docker/go-connections/nat"
|
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Container struct {
|
|
|
|
|
Image string
|
2022-05-24 23:28:40 +08:00
|
|
|
Entrypoint []string
|
2022-05-24 01:08:53 +08:00
|
|
|
Env map[string]string
|
|
|
|
|
ExposedPorts []string
|
2022-05-25 00:58:50 +08:00
|
|
|
BindMounts map[string]string
|
2022-05-24 01:08:53 +08:00
|
|
|
WaitingFor wait.Strategy
|
|
|
|
|
|
|
|
|
|
Address string
|
|
|
|
|
Port string
|
|
|
|
|
|
|
|
|
|
container testcontainers.Container
|
|
|
|
|
ctx context.Context
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Container) Start() error {
|
|
|
|
|
c.ctx = context.Background()
|
|
|
|
|
|
|
|
|
|
req := testcontainers.GenericContainerRequest{
|
|
|
|
|
ContainerRequest: testcontainers.ContainerRequest{
|
|
|
|
|
Image: c.Image,
|
|
|
|
|
Env: c.Env,
|
|
|
|
|
ExposedPorts: c.ExposedPorts,
|
2022-05-25 00:58:50 +08:00
|
|
|
BindMounts: c.BindMounts,
|
2022-05-24 23:28:40 +08:00
|
|
|
Entrypoint: c.Entrypoint,
|
2022-05-25 00:31:49 +08:00
|
|
|
WaitingFor: c.WaitingFor,
|
2022-05-24 01:08:53 +08:00
|
|
|
},
|
|
|
|
|
Started: true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
container, err := testcontainers.GenericContainer(c.ctx, req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("container failed to start: %s", err)
|
|
|
|
|
}
|
|
|
|
|
c.container = container
|
|
|
|
|
|
|
|
|
|
c.Address, err = c.container.Host(c.ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("container host address failed: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// assume the first port is the one the test will connect to
|
|
|
|
|
// additional ports can be used for the waiting for section
|
|
|
|
|
if len(c.ExposedPorts) > 0 {
|
|
|
|
|
p, err := c.container.MappedPort(c.ctx, nat.Port(c.ExposedPorts[0]))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("container host port failed: %s", err)
|
|
|
|
|
}
|
|
|
|
|
c.Port = p.Port()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Container) Terminate() error {
|
|
|
|
|
err := c.container.Terminate(c.ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to terminate the container: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|