Repository navigation
Expand file tree
/
Copy pathnetwork_test.go
More file actions
297 lines (246 loc) · 8.58 KB
/
Copy pathnetwork_test.go
File metadata and controls
297 lines (246 loc) · 8.58 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright © 2026 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package dockertest_test
import (
"net/netip"
"testing"
dockertest "github.com/ory/dockertest/v4"
)
func TestPoolCreateNetwork(t *testing.T) {
t.Run("creates network with name", func(t *testing.T) {
pool := dockertest.NewPoolT(t, "")
name := t.Name()
network, err := pool.CreateNetwork(t.Context(), name, nil)
if err != nil {
t.Fatalf("CreateNetwork() error = %v, want nil", err)
}
if network == nil {
t.Fatal("CreateNetwork() network = nil, want non-nil")
}
t.Cleanup(func() {
network.Close(t.Context())
})
if network.Inspect().Name != name {
t.Errorf("network.Inspect().Name = %q, want %q", network.Inspect().Name, name)
}
if network.Inspect().ID == "" {
t.Error("network.Inspect().ID is empty, want non-empty")
}
})
t.Run("creates network with custom options", func(t *testing.T) {
pool := dockertest.NewPoolT(t, "")
opts := dockertest.NetworkCreateOptions{
Driver: "bridge",
Labels: map[string]string{
"test": "label",
},
}
name := t.Name()
network, err := pool.CreateNetwork(t.Context(), name, &opts)
if err != nil {
t.Fatalf("CreateNetwork() error = %v, want nil", err)
}
t.Cleanup(func() {
network.Close(t.Context())
})
if network.Inspect().Driver != "bridge" {
t.Errorf("network.Inspect().Driver = %q, want %q", network.Inspect().Driver, "bridge")
}
if network.Inspect().Labels["test"] != "label" {
t.Errorf("network.Inspect().Labels[test] = %q, want %q", network.Inspect().Labels["test"], "label")
}
})
t.Run("creates network with nil options", func(t *testing.T) {
pool := dockertest.NewPoolT(t, "")
name := t.Name()
network, err := pool.CreateNetwork(t.Context(), name, nil)
if err != nil {
t.Fatalf("CreateNetwork() error = %v, want nil", err)
}
t.Cleanup(func() {
network.Close(t.Context())
})
if network.Inspect().Name != name {
t.Errorf("network.Inspect().Name = %q, want %q", network.Inspect().Name, name)
}
})
}
func TestPoolCreateNetworkT(t *testing.T) {
t.Run("creates network using t.Context", func(t *testing.T) {
pool := dockertest.NewPoolT(t, "")
name := t.Name()
network := pool.CreateNetworkT(t, name, nil)
if network == nil {
t.Fatal("CreateNetworkT() network = nil, want non-nil")
}
if network.Inspect().Name != name {
t.Errorf("network.Inspect().Name = %q, want %q", network.Inspect().Name, name)
}
})
}
func TestNetworkClose(t *testing.T) {
t.Run("removes network", func(t *testing.T) {
pool := dockertest.NewPoolT(t, "")
networkName := t.Name()
// Use CreateNetwork (non-T) to get raw ClosableNetwork for manual Close testing
network, err := pool.CreateNetwork(t.Context(), networkName, nil)
if err != nil {
t.Fatalf("CreateNetwork() error = %v, want nil", err)
}
err = network.Close(t.Context())
if err != nil {
t.Fatalf("Close() error = %v, want nil", err)
}
// Network is removed - creating a new one with the same name should work
network2, err := pool.CreateNetwork(t.Context(), networkName, nil)
if err != nil {
t.Fatalf("CreateNetwork() after Close() error = %v, want nil", err)
}
t.Cleanup(func() {
network2.Close(t.Context())
})
})
}
func TestNetworkCloseIdempotent(t *testing.T) {
pool := dockertest.NewPoolT(t, "")
// Use CreateNetwork (non-T) to get raw ClosableNetwork for manual Close testing
network, err := pool.CreateNetwork(t.Context(), t.Name(), nil)
if err != nil {
t.Fatalf("CreateNetwork() error = %v, want nil", err)
}
// First close should succeed
err = network.Close(t.Context())
if err != nil {
t.Fatalf("first Close() error = %v, want nil", err)
}
// Second close should also succeed (network already removed)
err = network.Close(t.Context())
if err != nil {
t.Fatalf("second Close() error = %v, want nil (should tolerate already-removed)", err)
}
}
func TestNetworkCloseT(t *testing.T) {
t.Run("removes network using t.Context", func(t *testing.T) {
pool := dockertest.NewPoolT(t, "")
name := t.Name()
// Use CreateNetwork (non-T) to get raw ClosableNetwork for manual CloseT testing
network, err := pool.CreateNetwork(t.Context(), name, nil)
if err != nil {
t.Fatalf("CreateNetwork() error = %v, want nil", err)
}
network.CloseT(t)
// Verify network was removed by creating a new one with the same name
network2, err := pool.CreateNetwork(t.Context(), name, nil)
if err != nil {
t.Fatalf("CreateNetwork() after CloseT should succeed, got: %v", err)
}
t.Cleanup(func() {
network2.Close(t.Context())
})
})
}
func TestResourceConnectToNetwork(t *testing.T) {
t.Run("connects container to network", func(t *testing.T) {
pool := dockertest.NewPoolT(t, "")
network := pool.CreateNetworkT(t, t.Name(), nil)
resource := pool.RunT(t, "alpine", dockertest.WithTag("latest"), dockertest.WithCmd([]string{"sleep", "300"}), dockertest.WithoutReuse())
err := resource.ConnectToNetwork(t.Context(), network)
if err != nil {
t.Fatalf("ConnectToNetwork() error = %v, want nil", err)
}
// Verify connection by getting IP
ip := resource.GetIPInNetwork(network)
if ip == "" {
t.Error("GetIPInNetwork() = empty, want non-empty IP")
}
})
}
func TestResourceDisconnectFromNetwork(t *testing.T) {
t.Run("disconnects container from network", func(t *testing.T) {
pool := dockertest.NewPoolT(t, "")
network := pool.CreateNetworkT(t, t.Name(), nil)
resource := pool.RunT(t, "alpine", dockertest.WithTag("latest"), dockertest.WithCmd([]string{"sleep", "300"}), dockertest.WithoutReuse())
// First connect
err := resource.ConnectToNetwork(t.Context(), network)
if err != nil {
t.Fatalf("ConnectToNetwork() error = %v, want nil", err)
}
// Verify connection
ip := resource.GetIPInNetwork(network)
if ip == "" {
t.Error("GetIPInNetwork() before disconnect = empty, want non-empty IP")
}
// Disconnect
err = resource.DisconnectFromNetwork(t.Context(), network)
if err != nil {
t.Fatalf("DisconnectFromNetwork() error = %v, want nil", err)
}
// Verify disconnection
ip = resource.GetIPInNetwork(network)
if ip != "" {
t.Errorf("GetIPInNetwork() after disconnect = %q, want empty", ip)
}
})
}
func TestResourceGetIPInNetwork(t *testing.T) {
t.Run("returns IP in network", func(t *testing.T) {
pool := dockertest.NewPoolT(t, "")
network := pool.CreateNetworkT(t, t.Name(), nil)
resource := pool.RunT(t, "alpine", dockertest.WithTag("latest"), dockertest.WithCmd([]string{"sleep", "300"}), dockertest.WithoutReuse())
err := resource.ConnectToNetwork(t.Context(), network)
if err != nil {
t.Fatalf("ConnectToNetwork() error = %v, want nil", err)
}
ip := resource.GetIPInNetwork(network)
if ip == "" {
t.Error("GetIPInNetwork() = empty, want non-empty IP")
}
// Verify IP is a valid address
if _, err := netip.ParseAddr(ip); err != nil {
t.Errorf("GetIPInNetwork() = %q, want valid IP address: %v", ip, err)
}
})
t.Run("returns empty for non-connected network", func(t *testing.T) {
pool := dockertest.NewPoolT(t, "")
network := pool.CreateNetworkT(t, t.Name(), nil)
resource := pool.RunT(t, "alpine", dockertest.WithTag("latest"), dockertest.WithCmd([]string{"sleep", "300"}), dockertest.WithoutReuse())
// Don't connect to network
ip := resource.GetIPInNetwork(network)
if ip != "" {
t.Errorf("GetIPInNetwork() for non-connected network = %q, want empty", ip)
}
})
}
func TestNetworkIntegration(t *testing.T) {
t.Run("two containers can communicate via custom network", func(t *testing.T) {
pool := dockertest.NewPoolT(t, "")
// Create custom network
network := pool.CreateNetworkT(t, t.Name(), nil)
// Start first container
resource1 := pool.RunT(t, "alpine", dockertest.WithTag("latest"), dockertest.WithCmd([]string{"sleep", "300"}), dockertest.WithoutReuse())
// Connect to network
err := resource1.ConnectToNetwork(t.Context(), network)
if err != nil {
t.Fatalf("ConnectToNetwork(resource1) error = %v, want nil", err)
}
// Start second container
resource2 := pool.RunT(t, "alpine", dockertest.WithTag("latest"), dockertest.WithCmd([]string{"sleep", "300"}), dockertest.WithoutReuse())
// Connect to network
err = resource2.ConnectToNetwork(t.Context(), network)
if err != nil {
t.Fatalf("ConnectToNetwork(resource2) error = %v, want nil", err)
}
// Get IPs
ip1 := resource1.GetIPInNetwork(network)
ip2 := resource2.GetIPInNetwork(network)
if ip1 == "" {
t.Error("resource1 IP in network = empty, want non-empty")
}
if ip2 == "" {
t.Error("resource2 IP in network = empty, want non-empty")
}
if ip1 == ip2 {
t.Errorf("resource1 and resource2 have same IP %q, want different IPs", ip1)
}
})
}