podman

Форк
0
/
networking_linux_test.go 
531 строка · 11.3 Кб
1
//go:build !remote
2

3
package libpod
4

5
import (
6
	"fmt"
7
	"net"
8
	"reflect"
9
	"testing"
10

11
	"github.com/stretchr/testify/assert"
12

13
	"github.com/containers/common/libnetwork/types"
14
	"github.com/containers/podman/v5/libpod/define"
15
)
16

17
func Test_ocicniPortsToNetTypesPorts(t *testing.T) {
18
	tests := []struct {
19
		name string
20
		arg  []types.OCICNIPortMapping
21
		want []types.PortMapping
22
	}{
23
		{
24
			name: "no ports",
25
			arg:  nil,
26
			want: nil,
27
		},
28
		{
29
			name: "empty ports",
30
			arg:  []types.OCICNIPortMapping{},
31
			want: nil,
32
		},
33
		{
34
			name: "single port",
35
			arg: []types.OCICNIPortMapping{
36
				{
37
					HostPort:      8080,
38
					ContainerPort: 80,
39
					Protocol:      "tcp",
40
				},
41
			},
42
			want: []types.PortMapping{
43
				{
44
					HostPort:      8080,
45
					ContainerPort: 80,
46
					Protocol:      "tcp",
47
					Range:         1,
48
				},
49
			},
50
		},
51
		{
52
			name: "two separate ports",
53
			arg: []types.OCICNIPortMapping{
54
				{
55
					HostPort:      8080,
56
					ContainerPort: 80,
57
					Protocol:      "tcp",
58
				},
59
				{
60
					HostPort:      9000,
61
					ContainerPort: 90,
62
					Protocol:      "tcp",
63
				},
64
			},
65
			want: []types.PortMapping{
66
				{
67
					HostPort:      8080,
68
					ContainerPort: 80,
69
					Protocol:      "tcp",
70
					Range:         1,
71
				},
72
				{
73
					HostPort:      9000,
74
					ContainerPort: 90,
75
					Protocol:      "tcp",
76
					Range:         1,
77
				},
78
			},
79
		},
80
		{
81
			name: "two ports joined",
82
			arg: []types.OCICNIPortMapping{
83
				{
84
					HostPort:      8080,
85
					ContainerPort: 80,
86
					Protocol:      "tcp",
87
				},
88
				{
89
					HostPort:      8081,
90
					ContainerPort: 81,
91
					Protocol:      "tcp",
92
				},
93
			},
94
			want: []types.PortMapping{
95
				{
96
					HostPort:      8080,
97
					ContainerPort: 80,
98
					Protocol:      "tcp",
99
					Range:         2,
100
				},
101
			},
102
		},
103
		{
104
			name: "three ports with different container port are not joined",
105
			arg: []types.OCICNIPortMapping{
106
				{
107
					HostPort:      8080,
108
					ContainerPort: 80,
109
					Protocol:      "tcp",
110
				},
111
				{
112
					HostPort:      8081,
113
					ContainerPort: 79,
114
					Protocol:      "tcp",
115
				},
116
				{
117
					HostPort:      8082,
118
					ContainerPort: 82,
119
					Protocol:      "tcp",
120
				},
121
			},
122
			want: []types.PortMapping{
123
				{
124
					HostPort:      8080,
125
					ContainerPort: 80,
126
					Protocol:      "tcp",
127
					Range:         1,
128
				},
129
				{
130
					HostPort:      8081,
131
					ContainerPort: 79,
132
					Protocol:      "tcp",
133
					Range:         1,
134
				},
135
				{
136
					HostPort:      8082,
137
					ContainerPort: 82,
138
					Protocol:      "tcp",
139
					Range:         1,
140
				},
141
			},
142
		},
143
		{
144
			name: "three ports joined (not sorted)",
145
			arg: []types.OCICNIPortMapping{
146
				{
147
					HostPort:      8081,
148
					ContainerPort: 81,
149
					Protocol:      "tcp",
150
				},
151
				{
152
					HostPort:      8080,
153
					ContainerPort: 80,
154
					Protocol:      "tcp",
155
				},
156
				{
157
					HostPort:      8082,
158
					ContainerPort: 82,
159
					Protocol:      "tcp",
160
				},
161
			},
162
			want: []types.PortMapping{
163
				{
164
					HostPort:      8080,
165
					ContainerPort: 80,
166
					Protocol:      "tcp",
167
					Range:         3,
168
				},
169
			},
170
		},
171
		{
172
			name: "different protocols ports are not joined",
173
			arg: []types.OCICNIPortMapping{
174
				{
175
					HostPort:      8080,
176
					ContainerPort: 80,
177
					Protocol:      "tcp",
178
				},
179
				{
180
					HostPort:      8081,
181
					ContainerPort: 81,
182
					Protocol:      "udp",
183
				},
184
			},
185
			want: []types.PortMapping{
186
				{
187
					HostPort:      8080,
188
					ContainerPort: 80,
189
					Protocol:      "tcp",
190
					Range:         1,
191
				},
192
				{
193
					HostPort:      8081,
194
					ContainerPort: 81,
195
					Protocol:      "udp",
196
					Range:         1,
197
				},
198
			},
199
		},
200
		{
201
			name: "different host ip ports are not joined",
202
			arg: []types.OCICNIPortMapping{
203
				{
204
					HostPort:      8080,
205
					ContainerPort: 80,
206
					Protocol:      "tcp",
207
					HostIP:        "192.168.1.1",
208
				},
209
				{
210
					HostPort:      8081,
211
					ContainerPort: 81,
212
					Protocol:      "tcp",
213
					HostIP:        "192.168.1.2",
214
				},
215
			},
216
			want: []types.PortMapping{
217
				{
218
					HostPort:      8080,
219
					ContainerPort: 80,
220
					Protocol:      "tcp",
221
					Range:         1,
222
					HostIP:        "192.168.1.1",
223
				},
224
				{
225
					HostPort:      8081,
226
					ContainerPort: 81,
227
					Protocol:      "tcp",
228
					Range:         1,
229
					HostIP:        "192.168.1.2",
230
				},
231
			},
232
		},
233
	}
234
	for _, tt := range tests {
235
		tt := tt
236
		t.Run(tt.name, func(t *testing.T) {
237
			result := ocicniPortsToNetTypesPorts(tt.arg)
238
			assert.Equal(t, tt.want, result, "ports do not match")
239
		})
240
	}
241
}
242

243
func Test_resultToBasicNetworkConfig(t *testing.T) {
244
	testCases := []struct {
245
		description           string
246
		inputResult           types.StatusBlock
247
		expectedNetworkConfig define.InspectBasicNetworkConfig
248
	}{
249
		{
250
			description: "single secondary IPv4 address is shown as define.Address",
251
			inputResult: types.StatusBlock{
252
				Interfaces: map[string]types.NetInterface{
253
					"eth1": {
254
						Subnets: []types.NetAddress{
255
							{
256
								Gateway: net.ParseIP("172.26.0.1"),
257
								IPNet: types.IPNet{
258
									IPNet: net.IPNet{
259
										IP:   net.ParseIP("172.26.0.2"),
260
										Mask: net.CIDRMask(20, 32),
261
									},
262
								},
263
							},
264
							{
265
								IPNet: types.IPNet{
266
									IPNet: net.IPNet{
267
										IP:   net.ParseIP("172.26.0.3"),
268
										Mask: net.CIDRMask(10, 32),
269
									},
270
								},
271
							},
272
						},
273
					},
274
				},
275
			},
276
			expectedNetworkConfig: define.InspectBasicNetworkConfig{
277
				IPAddress:   "172.26.0.2",
278
				IPPrefixLen: 20,
279
				Gateway:     "172.26.0.1",
280
				SecondaryIPAddresses: []define.Address{
281
					{
282
						Addr:         "172.26.0.3",
283
						PrefixLength: 10,
284
					},
285
				},
286
			},
287
		},
288
		{
289
			description: "multiple secondary IPv4 addresses are shown as define.Address",
290
			inputResult: types.StatusBlock{
291
				Interfaces: map[string]types.NetInterface{
292
					"eth1": {
293
						Subnets: []types.NetAddress{
294
							{
295
								Gateway: net.ParseIP("172.26.0.1"),
296
								IPNet: types.IPNet{
297
									IPNet: net.IPNet{
298
										IP:   net.ParseIP("172.26.0.2"),
299
										Mask: net.CIDRMask(20, 32),
300
									},
301
								},
302
							},
303
							{
304
								IPNet: types.IPNet{
305
									IPNet: net.IPNet{
306
										IP:   net.ParseIP("172.26.0.3"),
307
										Mask: net.CIDRMask(10, 32),
308
									},
309
								},
310
							},
311
							{
312
								IPNet: types.IPNet{
313
									IPNet: net.IPNet{
314
										IP:   net.ParseIP("172.26.0.4"),
315
										Mask: net.CIDRMask(24, 32),
316
									},
317
								},
318
							},
319
						},
320
					},
321
				},
322
			},
323
			expectedNetworkConfig: define.InspectBasicNetworkConfig{
324
				IPAddress:   "172.26.0.2",
325
				IPPrefixLen: 20,
326
				Gateway:     "172.26.0.1",
327
				SecondaryIPAddresses: []define.Address{
328
					{
329
						Addr:         "172.26.0.3",
330
						PrefixLength: 10,
331
					},
332
					{
333
						Addr:         "172.26.0.4",
334
						PrefixLength: 24,
335
					},
336
				},
337
			},
338
		},
339
		{
340
			description: "single secondary IPv6 address is shown as define.Address",
341
			inputResult: types.StatusBlock{
342
				Interfaces: map[string]types.NetInterface{
343
					"eth1": {
344
						Subnets: []types.NetAddress{
345
							{
346
								Gateway: net.ParseIP("ff02::fb"),
347
								IPNet: types.IPNet{
348
									IPNet: net.IPNet{
349
										IP:   net.ParseIP("ff02::fc"),
350
										Mask: net.CIDRMask(20, 128),
351
									},
352
								},
353
							},
354
							{
355
								IPNet: types.IPNet{
356
									IPNet: net.IPNet{
357
										IP:   net.ParseIP("ff02::fd"),
358
										Mask: net.CIDRMask(10, 128),
359
									},
360
								},
361
							},
362
						},
363
					},
364
				},
365
			},
366
			expectedNetworkConfig: define.InspectBasicNetworkConfig{
367
				GlobalIPv6Address:   "ff02::fc",
368
				GlobalIPv6PrefixLen: 20,
369
				IPv6Gateway:         "ff02::fb",
370
				SecondaryIPv6Addresses: []define.Address{
371
					{
372
						Addr:         "ff02::fd",
373
						PrefixLength: 10,
374
					},
375
				},
376
			},
377
		},
378
		{
379
			description: "multiple secondary IPv6 addresses are shown as define.Address",
380
			inputResult: types.StatusBlock{
381
				Interfaces: map[string]types.NetInterface{
382
					"eth1": {
383
						Subnets: []types.NetAddress{
384
							{
385
								Gateway: net.ParseIP("ff02::fb"),
386
								IPNet: types.IPNet{
387
									IPNet: net.IPNet{
388
										IP:   net.ParseIP("ff02::fc"),
389
										Mask: net.CIDRMask(20, 128),
390
									},
391
								},
392
							},
393
							{
394
								IPNet: types.IPNet{
395
									IPNet: net.IPNet{
396
										IP:   net.ParseIP("ff02::fd"),
397
										Mask: net.CIDRMask(10, 128),
398
									},
399
								},
400
							},
401
							{
402
								IPNet: types.IPNet{
403
									IPNet: net.IPNet{
404
										IP:   net.ParseIP("ff02::fe"),
405
										Mask: net.CIDRMask(24, 128),
406
									},
407
								},
408
							},
409
						},
410
					},
411
				},
412
			},
413
			expectedNetworkConfig: define.InspectBasicNetworkConfig{
414
				GlobalIPv6Address:   "ff02::fc",
415
				GlobalIPv6PrefixLen: 20,
416
				IPv6Gateway:         "ff02::fb",
417
				SecondaryIPv6Addresses: []define.Address{
418
					{
419
						Addr:         "ff02::fd",
420
						PrefixLength: 10,
421
					},
422
					{
423
						Addr:         "ff02::fe",
424
						PrefixLength: 24,
425
					},
426
				},
427
			},
428
		},
429
	}
430

431
	for _, tcl := range testCases {
432
		tc := tcl
433
		t.Run(tc.description, func(t *testing.T) {
434
			t.Parallel()
435
			actualNetworkConfig := resultToBasicNetworkConfig(tc.inputResult)
436

437
			if !reflect.DeepEqual(tc.expectedNetworkConfig, actualNetworkConfig) {
438
				t.Fatalf(
439
					"Expected networkConfig %+v didn't match actual value %+v", tc.expectedNetworkConfig, actualNetworkConfig)
440
			}
441
		})
442
	}
443
}
444

445
func benchmarkOCICNIPortsToNetTypesPorts(b *testing.B, ports []types.OCICNIPortMapping) {
446
	for n := 0; n < b.N; n++ {
447
		ocicniPortsToNetTypesPorts(ports)
448
	}
449
}
450

451
func Benchmark_ocicniPortsToNetTypesPortsNoPorts(b *testing.B) {
452
	benchmarkOCICNIPortsToNetTypesPorts(b, nil)
453
}
454

455
func Benchmark_ocicniPortsToNetTypesPorts1(b *testing.B) {
456
	benchmarkOCICNIPortsToNetTypesPorts(b, []types.OCICNIPortMapping{
457
		{
458
			HostPort:      8080,
459
			ContainerPort: 80,
460
			Protocol:      "tcp",
461
		},
462
	})
463
}
464

465
func Benchmark_ocicniPortsToNetTypesPorts10(b *testing.B) {
466
	ports := make([]types.OCICNIPortMapping, 0, 10)
467
	for i := int32(8080); i < 8090; i++ {
468
		ports = append(ports, types.OCICNIPortMapping{
469
			HostPort:      i,
470
			ContainerPort: i,
471
			Protocol:      "tcp",
472
		})
473
	}
474
	b.ResetTimer()
475
	benchmarkOCICNIPortsToNetTypesPorts(b, ports)
476
}
477

478
func Benchmark_ocicniPortsToNetTypesPorts100(b *testing.B) {
479
	ports := make([]types.OCICNIPortMapping, 0, 100)
480
	for i := int32(8080); i < 8180; i++ {
481
		ports = append(ports, types.OCICNIPortMapping{
482
			HostPort:      i,
483
			ContainerPort: i,
484
			Protocol:      "tcp",
485
		})
486
	}
487
	b.ResetTimer()
488
	benchmarkOCICNIPortsToNetTypesPorts(b, ports)
489
}
490

491
func Benchmark_ocicniPortsToNetTypesPorts1k(b *testing.B) {
492
	ports := make([]types.OCICNIPortMapping, 0, 1000)
493
	for i := int32(8080); i < 9080; i++ {
494
		ports = append(ports, types.OCICNIPortMapping{
495
			HostPort:      i,
496
			ContainerPort: i,
497
			Protocol:      "tcp",
498
		})
499
	}
500
	b.ResetTimer()
501
	benchmarkOCICNIPortsToNetTypesPorts(b, ports)
502
}
503

504
func Benchmark_ocicniPortsToNetTypesPorts10k(b *testing.B) {
505
	ports := make([]types.OCICNIPortMapping, 0, 30000)
506
	for i := int32(8080); i < 18080; i++ {
507
		ports = append(ports, types.OCICNIPortMapping{
508
			HostPort:      i,
509
			ContainerPort: i,
510
			Protocol:      "tcp",
511
		})
512
	}
513
	b.ResetTimer()
514
	benchmarkOCICNIPortsToNetTypesPorts(b, ports)
515
}
516

517
func Benchmark_ocicniPortsToNetTypesPorts1m(b *testing.B) {
518
	ports := make([]types.OCICNIPortMapping, 0, 1000000)
519
	for j := 0; j < 20; j++ {
520
		for i := int32(1); i <= 50000; i++ {
521
			ports = append(ports, types.OCICNIPortMapping{
522
				HostPort:      i,
523
				ContainerPort: i,
524
				Protocol:      "tcp",
525
				HostIP:        fmt.Sprintf("192.168.1.%d", j),
526
			})
527
		}
528
	}
529
	b.ResetTimer()
530
	benchmarkOCICNIPortsToNetTypesPorts(b, ports)
531
}
532

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.