-
-
Notifications
You must be signed in to change notification settings - Fork 512
/
parallel_test.go
158 lines (145 loc) · 3.09 KB
/
parallel_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package testcontainers
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/wait"
)
func TestParallelContainers(t *testing.T) {
tests := []struct {
name string
reqs ParallelContainerRequest
resLen int
expErrors int
}{
{
name: "running two containers (one error)",
reqs: ParallelContainerRequest{
{
ContainerRequest: ContainerRequest{
Image: "nginx",
ExposedPorts: []string{
"10080/tcp",
},
},
Started: true,
},
{
ContainerRequest: ContainerRequest{
Image: "bad bad bad",
ExposedPorts: []string{
"10081/tcp",
},
},
Started: true,
},
},
resLen: 1,
expErrors: 1,
},
{
name: "running two containers (all errors)",
reqs: ParallelContainerRequest{
{
ContainerRequest: ContainerRequest{
Image: "bad bad bad",
ExposedPorts: []string{
"10081/tcp",
},
},
Started: true,
},
{
ContainerRequest: ContainerRequest{
Image: "bad bad bad",
ExposedPorts: []string{
"10081/tcp",
},
},
Started: true,
},
},
resLen: 0,
expErrors: 2,
},
{
name: "running two containers (success)",
reqs: ParallelContainerRequest{
{
ContainerRequest: ContainerRequest{
Image: "nginx",
ExposedPorts: []string{
"10080/tcp",
},
},
Started: true,
},
{
ContainerRequest: ContainerRequest{
Image: "nginx",
ExposedPorts: []string{
"10081/tcp",
},
},
Started: true,
},
},
resLen: 2,
expErrors: 0,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
res, err := ParallelContainers(context.Background(), tc.reqs, ParallelContainersOptions{})
for _, c := range res {
CleanupContainer(t, c)
}
if tc.expErrors != 0 {
require.Error(t, err)
var errs ParallelContainersError
require.ErrorAs(t, err, &errs)
require.Len(t, errs.Errors, tc.expErrors)
}
require.Len(t, res, tc.resLen)
})
}
}
func TestParallelContainersWithReuse(t *testing.T) {
const (
postgresPort = 5432
postgresPassword = "test"
postgresUser = "test"
postgresDb = "test"
)
natPort := fmt.Sprintf("%d/tcp", postgresPort)
req := GenericContainerRequest{
ContainerRequest: ContainerRequest{
Image: "postgis/postgis",
Name: "test-postgres",
ExposedPorts: []string{natPort},
Env: map[string]string{
"POSTGRES_PASSWORD": postgresPassword,
"POSTGRES_USER": postgresUser,
"POSTGRES_DATABASE": postgresDb,
},
WaitingFor: wait.ForLog("database system is ready to accept connections").
WithPollInterval(100 * time.Millisecond).
WithOccurrence(2),
},
Started: true,
Reuse: true,
}
parallelRequest := ParallelContainerRequest{
req,
req,
req,
}
ctx := context.Background()
res, err := ParallelContainers(ctx, parallelRequest, ParallelContainersOptions{})
for _, c := range res {
CleanupContainer(t, c)
}
require.NoError(t, err)
}