forked from testcontainers/testcontainers-go
/
log_test.go
90 lines (78 loc) · 2.37 KB
/
log_test.go
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
package wait
import (
"bytes"
"context"
"io"
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/go-connections/nat"
tcexec "github.com/testcontainers/testcontainers-go/exec"
)
type noopStrategyTarget struct {
ioReaderCloser io.ReadCloser
}
func (st noopStrategyTarget) Host(ctx context.Context) (string, error) {
return "", nil
}
func (st noopStrategyTarget) Ports(ctx context.Context) (nat.PortMap, error) {
return nil, nil
}
func (st noopStrategyTarget) MappedPort(ctx context.Context, n nat.Port) (nat.Port, error) {
return n, nil
}
func (st noopStrategyTarget) Logs(ctx context.Context) (io.ReadCloser, error) {
return st.ioReaderCloser, nil
}
func (st noopStrategyTarget) Exec(ctx context.Context, cmd []string, options ...tcexec.ProcessOption) (int, io.Reader, error) {
return 0, nil, nil
}
func (st noopStrategyTarget) State(ctx context.Context) (*types.ContainerState, error) {
return nil, nil
}
func TestWaitForLog(t *testing.T) {
target := noopStrategyTarget{
ioReaderCloser: io.NopCloser(bytes.NewReader([]byte("docker"))),
}
wg := NewLogStrategy("docker").WithStartupTimeout(100 * time.Microsecond)
err := wg.WaitUntilReady(context.Background(), target)
if err != nil {
t.Fatal(err)
}
}
func TestWaitWithExactNumberOfOccurrences(t *testing.T) {
target := noopStrategyTarget{
ioReaderCloser: io.NopCloser(bytes.NewReader([]byte("kubernetes\r\ndocker\n\rdocker"))),
}
wg := NewLogStrategy("docker").
WithStartupTimeout(100 * time.Microsecond).
WithOccurrence(2)
err := wg.WaitUntilReady(context.Background(), target)
if err != nil {
t.Fatal(err)
}
}
func TestWaitWithExactNumberOfOccurrencesButItWillNeverHappen(t *testing.T) {
target := noopStrategyTarget{
ioReaderCloser: io.NopCloser(bytes.NewReader([]byte("kubernetes\r\ndocker"))),
}
wg := NewLogStrategy("containerd").
WithStartupTimeout(100 * time.Microsecond).
WithOccurrence(2)
err := wg.WaitUntilReady(context.Background(), target)
if err == nil {
t.Fatal("expected error")
}
}
func TestWaitShouldFailWithExactNumberOfOccurrences(t *testing.T) {
target := noopStrategyTarget{
ioReaderCloser: io.NopCloser(bytes.NewReader([]byte("kubernetes\r\ndocker"))),
}
wg := NewLogStrategy("docker").
WithStartupTimeout(100 * time.Microsecond).
WithOccurrence(2)
err := wg.WaitUntilReady(context.Background(), target)
if err == nil {
t.Fatal("expected error")
}
}