summaryrefslogtreecommitdiff
path: root/vendor/github.com/testcontainers/testcontainers-go/wait/exec.go
blob: 72987c31a426fcf1d385b7661d317d714c9023b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package wait

import (
	"context"
	"io"
	"time"

	tcexec "github.com/testcontainers/testcontainers-go/exec"
)

// Implement interface
var (
	_ Strategy        = (*ExecStrategy)(nil)
	_ StrategyTimeout = (*ExecStrategy)(nil)
)

type ExecStrategy struct {
	// all Strategies should have a startupTimeout to avoid waiting infinitely
	timeout *time.Duration
	cmd     []string

	// additional properties
	ExitCodeMatcher func(exitCode int) bool
	ResponseMatcher func(body io.Reader) bool
	PollInterval    time.Duration
}

// NewExecStrategy constructs an Exec strategy ...
func NewExecStrategy(cmd []string) *ExecStrategy {
	return &ExecStrategy{
		cmd:             cmd,
		ExitCodeMatcher: defaultExitCodeMatcher,
		ResponseMatcher: func(_ io.Reader) bool { return true },
		PollInterval:    defaultPollInterval(),
	}
}

func defaultExitCodeMatcher(exitCode int) bool {
	return exitCode == 0
}

// WithStartupTimeout can be used to change the default startup timeout
func (ws *ExecStrategy) WithStartupTimeout(startupTimeout time.Duration) *ExecStrategy {
	ws.timeout = &startupTimeout
	return ws
}

func (ws *ExecStrategy) WithExitCode(exitCode int) *ExecStrategy {
	return ws.WithExitCodeMatcher(func(actualCode int) bool {
		return actualCode == exitCode
	})
}

func (ws *ExecStrategy) WithExitCodeMatcher(exitCodeMatcher func(exitCode int) bool) *ExecStrategy {
	ws.ExitCodeMatcher = exitCodeMatcher
	return ws
}

func (ws *ExecStrategy) WithResponseMatcher(matcher func(body io.Reader) bool) *ExecStrategy {
	ws.ResponseMatcher = matcher
	return ws
}

// WithPollInterval can be used to override the default polling interval of 100 milliseconds
func (ws *ExecStrategy) WithPollInterval(pollInterval time.Duration) *ExecStrategy {
	ws.PollInterval = pollInterval
	return ws
}

// ForExec is a convenience method to assign ExecStrategy
func ForExec(cmd []string) *ExecStrategy {
	return NewExecStrategy(cmd)
}

func (ws *ExecStrategy) Timeout() *time.Duration {
	return ws.timeout
}

func (ws *ExecStrategy) WaitUntilReady(ctx context.Context, target StrategyTarget) error {
	timeout := defaultStartupTimeout()
	if ws.timeout != nil {
		timeout = *ws.timeout
	}

	ctx, cancel := context.WithTimeout(ctx, timeout)
	defer cancel()

	for {
		select {
		case <-ctx.Done():
			return ctx.Err()
		case <-time.After(ws.PollInterval):
			exitCode, resp, err := target.Exec(ctx, ws.cmd, tcexec.Multiplexed())
			if err != nil {
				return err
			}
			if !ws.ExitCodeMatcher(exitCode) {
				continue
			}
			if ws.ResponseMatcher != nil && !ws.ResponseMatcher(resp) {
				continue
			}

			return nil
		}
	}
}