diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-08 13:11:59 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-21 15:20:39 -0600 |
| commit | 2ddcc34ca455973598f5693d64103deea41d8d79 (patch) | |
| tree | 0b3a42aa97bca93c15c67a679c903611e5ab60c1 /vendor/github.com/testcontainers/testcontainers-go/options.go | |
| parent | 16c27cd885b9c0d1241dfead3120643f0e8c556c (diff) | |
chore: use minit to start processes from Procfile
Diffstat (limited to 'vendor/github.com/testcontainers/testcontainers-go/options.go')
| -rw-r--r-- | vendor/github.com/testcontainers/testcontainers-go/options.go | 112 |
1 files changed, 96 insertions, 16 deletions
diff --git a/vendor/github.com/testcontainers/testcontainers-go/options.go b/vendor/github.com/testcontainers/testcontainers-go/options.go index 9afbcd7..f7775f8 100644 --- a/vendor/github.com/testcontainers/testcontainers-go/options.go +++ b/vendor/github.com/testcontainers/testcontainers-go/options.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "maps" "net/url" "time" @@ -77,9 +78,7 @@ func WithEnv(envs map[string]string) CustomizeRequestOption { req.Env = map[string]string{} } - for key, val := range envs { - req.Env[key] = val - } + maps.Copy(req.Env, envs) return nil } @@ -106,14 +105,33 @@ func WithHostPortAccess(ports ...int) CustomizeRequestOption { } } +// WithName will set the name of the container. +func WithName(containerName string) CustomizeRequestOption { + return func(req *GenericContainerRequest) error { + if containerName == "" { + return errors.New("container name must be provided") + } + req.Name = containerName + return nil + } +} + +// WithNoStart will prevent the container from being started after creation. +func WithNoStart() CustomizeRequestOption { + return func(req *GenericContainerRequest) error { + req.Started = false + return nil + } +} + // WithReuseByName will mark a container to be reused if it exists or create a new one if it doesn't. // A container name must be provided to identify the container to be reused. func WithReuseByName(containerName string) CustomizeRequestOption { return func(req *GenericContainerRequest) error { - if containerName == "" { - return errors.New("container name must be provided for reuse") + if err := WithName(containerName)(req); err != nil { + return err } - req.Name = containerName + req.Reuse = true return nil } @@ -255,6 +273,17 @@ func WithLogConsumers(consumer ...LogConsumer) CustomizeRequestOption { } } +// WithLogConsumerConfig sets the log consumer config for a container. +// Beware that this option completely replaces the existing log consumer config, +// including the log consumers and the log production options, +// so it should be used with care. +func WithLogConsumerConfig(config *LogConsumerConfig) CustomizeRequestOption { + return func(req *GenericContainerRequest) error { + req.LogConsumerCfg = config + return nil + } +} + // Executable represents an executable command to be sent to a container, including options, // as part of the different lifecycle hooks. type Executable interface { @@ -281,11 +310,11 @@ type RawCommand struct { cmds []string } -func NewRawCommand(cmds []string) RawCommand { +func NewRawCommand(cmds []string, opts ...tcexec.ProcessOption) RawCommand { return RawCommand{ cmds: cmds, ExecOptions: ExecOptions{ - opts: []tcexec.ProcessOption{}, + opts: opts, }, } } @@ -343,12 +372,17 @@ func WithAfterReadyCommand(execs ...Executable) CustomizeRequestOption { } } -// WithWaitStrategy sets the wait strategy for a container, using 60 seconds as deadline +// WithWaitStrategy replaces the wait strategy for a container, using 60 seconds as deadline func WithWaitStrategy(strategies ...wait.Strategy) CustomizeRequestOption { return WithWaitStrategyAndDeadline(60*time.Second, strategies...) } -// WithWaitStrategyAndDeadline sets the wait strategy for a container, including deadline +// WithAdditionalWaitStrategy appends the wait strategy for a container, using 60 seconds as deadline +func WithAdditionalWaitStrategy(strategies ...wait.Strategy) CustomizeRequestOption { + return WithAdditionalWaitStrategyAndDeadline(60*time.Second, strategies...) +} + +// WithWaitStrategyAndDeadline replaces the wait strategy for a container, including deadline func WithWaitStrategyAndDeadline(deadline time.Duration, strategies ...wait.Strategy) CustomizeRequestOption { return func(req *GenericContainerRequest) error { req.WaitingFor = wait.ForAll(strategies...).WithDeadline(deadline) @@ -357,6 +391,24 @@ func WithWaitStrategyAndDeadline(deadline time.Duration, strategies ...wait.Stra } } +// WithAdditionalWaitStrategyAndDeadline appends the wait strategy for a container, including deadline +func WithAdditionalWaitStrategyAndDeadline(deadline time.Duration, strategies ...wait.Strategy) CustomizeRequestOption { + return func(req *GenericContainerRequest) error { + if req.WaitingFor == nil { + req.WaitingFor = wait.ForAll(strategies...).WithDeadline(deadline) + return nil + } + + wss := make([]wait.Strategy, 0, len(strategies)+1) + wss = append(wss, req.WaitingFor) + wss = append(wss, strategies...) + + req.WaitingFor = wait.ForAll(wss...).WithDeadline(deadline) + + return nil + } +} + // WithImageMount mounts an image to a container, passing the source image name, // the relative subpath to mount in that image, and the mount point in the target container. // This option validates that the subpath is a relative path, raising an error otherwise. @@ -376,6 +428,22 @@ func WithImageMount(source string, subpath string, target ContainerMountTarget) } } +// WithAlwaysPull will pull the image before starting the container +func WithAlwaysPull() CustomizeRequestOption { + return func(req *GenericContainerRequest) error { + req.AlwaysPullImage = true + return nil + } +} + +// WithImagePlatform sets the platform for a container +func WithImagePlatform(platform string) CustomizeRequestOption { + return func(req *GenericContainerRequest) error { + req.ImagePlatform = platform + return nil + } +} + // WithEntrypoint completely replaces the entrypoint of a container func WithEntrypoint(entrypoint ...string) CustomizeRequestOption { return func(req *GenericContainerRequest) error { @@ -422,9 +490,23 @@ func WithLabels(labels map[string]string) CustomizeRequestOption { if req.Labels == nil { req.Labels = make(map[string]string) } - for k, v := range labels { - req.Labels[k] = v - } + maps.Copy(req.Labels, labels) + return nil + } +} + +// WithLifecycleHooks completely replaces the lifecycle hooks for a container +func WithLifecycleHooks(hooks ...ContainerLifecycleHooks) CustomizeRequestOption { + return func(req *GenericContainerRequest) error { + req.LifecycleHooks = hooks + return nil + } +} + +// WithAdditionalLifecycleHooks appends lifecycle hooks to the existing ones for a container +func WithAdditionalLifecycleHooks(hooks ...ContainerLifecycleHooks) CustomizeRequestOption { + return func(req *GenericContainerRequest) error { + req.LifecycleHooks = append(req.LifecycleHooks, hooks...) return nil } } @@ -443,9 +525,7 @@ func WithTmpfs(tmpfs map[string]string) CustomizeRequestOption { if req.Tmpfs == nil { req.Tmpfs = make(map[string]string) } - for k, v := range tmpfs { - req.Tmpfs[k] = v - } + maps.Copy(req.Tmpfs, tmpfs) return nil } } |
