11
"github.com/stretchr/testify/assert"
13
"github.com/containers/common/libnetwork/types"
14
"github.com/containers/podman/v5/libpod/define"
17
func Test_ocicniPortsToNetTypesPorts(t *testing.T) {
20
arg []types.OCICNIPortMapping
21
want []types.PortMapping
30
arg: []types.OCICNIPortMapping{},
35
arg: []types.OCICNIPortMapping{
42
want: []types.PortMapping{
52
name: "two separate ports",
53
arg: []types.OCICNIPortMapping{
65
want: []types.PortMapping{
81
name: "two ports joined",
82
arg: []types.OCICNIPortMapping{
94
want: []types.PortMapping{
104
name: "three ports with different container port are not joined",
105
arg: []types.OCICNIPortMapping{
122
want: []types.PortMapping{
144
name: "three ports joined (not sorted)",
145
arg: []types.OCICNIPortMapping{
162
want: []types.PortMapping{
172
name: "different protocols ports are not joined",
173
arg: []types.OCICNIPortMapping{
185
want: []types.PortMapping{
201
name: "different host ip ports are not joined",
202
arg: []types.OCICNIPortMapping{
207
HostIP: "192.168.1.1",
213
HostIP: "192.168.1.2",
216
want: []types.PortMapping{
222
HostIP: "192.168.1.1",
229
HostIP: "192.168.1.2",
234
for _, tt := range tests {
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")
243
func Test_resultToBasicNetworkConfig(t *testing.T) {
244
testCases := []struct {
246
inputResult types.StatusBlock
247
expectedNetworkConfig define.InspectBasicNetworkConfig
250
description: "single secondary IPv4 address is shown as define.Address",
251
inputResult: types.StatusBlock{
252
Interfaces: map[string]types.NetInterface{
254
Subnets: []types.NetAddress{
256
Gateway: net.ParseIP("172.26.0.1"),
259
IP: net.ParseIP("172.26.0.2"),
260
Mask: net.CIDRMask(20, 32),
267
IP: net.ParseIP("172.26.0.3"),
268
Mask: net.CIDRMask(10, 32),
276
expectedNetworkConfig: define.InspectBasicNetworkConfig{
277
IPAddress: "172.26.0.2",
279
Gateway: "172.26.0.1",
280
SecondaryIPAddresses: []define.Address{
289
description: "multiple secondary IPv4 addresses are shown as define.Address",
290
inputResult: types.StatusBlock{
291
Interfaces: map[string]types.NetInterface{
293
Subnets: []types.NetAddress{
295
Gateway: net.ParseIP("172.26.0.1"),
298
IP: net.ParseIP("172.26.0.2"),
299
Mask: net.CIDRMask(20, 32),
306
IP: net.ParseIP("172.26.0.3"),
307
Mask: net.CIDRMask(10, 32),
314
IP: net.ParseIP("172.26.0.4"),
315
Mask: net.CIDRMask(24, 32),
323
expectedNetworkConfig: define.InspectBasicNetworkConfig{
324
IPAddress: "172.26.0.2",
326
Gateway: "172.26.0.1",
327
SecondaryIPAddresses: []define.Address{
340
description: "single secondary IPv6 address is shown as define.Address",
341
inputResult: types.StatusBlock{
342
Interfaces: map[string]types.NetInterface{
344
Subnets: []types.NetAddress{
346
Gateway: net.ParseIP("ff02::fb"),
349
IP: net.ParseIP("ff02::fc"),
350
Mask: net.CIDRMask(20, 128),
357
IP: net.ParseIP("ff02::fd"),
358
Mask: net.CIDRMask(10, 128),
366
expectedNetworkConfig: define.InspectBasicNetworkConfig{
367
GlobalIPv6Address: "ff02::fc",
368
GlobalIPv6PrefixLen: 20,
369
IPv6Gateway: "ff02::fb",
370
SecondaryIPv6Addresses: []define.Address{
379
description: "multiple secondary IPv6 addresses are shown as define.Address",
380
inputResult: types.StatusBlock{
381
Interfaces: map[string]types.NetInterface{
383
Subnets: []types.NetAddress{
385
Gateway: net.ParseIP("ff02::fb"),
388
IP: net.ParseIP("ff02::fc"),
389
Mask: net.CIDRMask(20, 128),
396
IP: net.ParseIP("ff02::fd"),
397
Mask: net.CIDRMask(10, 128),
404
IP: net.ParseIP("ff02::fe"),
405
Mask: net.CIDRMask(24, 128),
413
expectedNetworkConfig: define.InspectBasicNetworkConfig{
414
GlobalIPv6Address: "ff02::fc",
415
GlobalIPv6PrefixLen: 20,
416
IPv6Gateway: "ff02::fb",
417
SecondaryIPv6Addresses: []define.Address{
431
for _, tcl := range testCases {
433
t.Run(tc.description, func(t *testing.T) {
435
actualNetworkConfig := resultToBasicNetworkConfig(tc.inputResult)
437
if !reflect.DeepEqual(tc.expectedNetworkConfig, actualNetworkConfig) {
439
"Expected networkConfig %+v didn't match actual value %+v", tc.expectedNetworkConfig, actualNetworkConfig)
445
func benchmarkOCICNIPortsToNetTypesPorts(b *testing.B, ports []types.OCICNIPortMapping) {
446
for n := 0; n < b.N; n++ {
447
ocicniPortsToNetTypesPorts(ports)
451
func Benchmark_ocicniPortsToNetTypesPortsNoPorts(b *testing.B) {
452
benchmarkOCICNIPortsToNetTypesPorts(b, nil)
455
func Benchmark_ocicniPortsToNetTypesPorts1(b *testing.B) {
456
benchmarkOCICNIPortsToNetTypesPorts(b, []types.OCICNIPortMapping{
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{
475
benchmarkOCICNIPortsToNetTypesPorts(b, ports)
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{
488
benchmarkOCICNIPortsToNetTypesPorts(b, ports)
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{
501
benchmarkOCICNIPortsToNetTypesPorts(b, ports)
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{
514
benchmarkOCICNIPortsToNetTypesPorts(b, ports)
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{
525
HostIP: fmt.Sprintf("192.168.1.%d", j),
530
benchmarkOCICNIPortsToNetTypesPorts(b, ports)