@@ -4,12 +4,14 @@ import (
44 "errors"
55 "fmt"
66 "io"
7+ "net/netip"
78 "os"
89 "runtime"
910 "strings"
1011 "testing"
1112 "time"
1213
14+ "github.com/google/go-cmp/cmp/cmpopts"
1315 "github.com/moby/moby/api/types/container"
1416 networktypes "github.com/moby/moby/api/types/network"
1517 "github.com/spf13/pflag"
@@ -430,14 +432,14 @@ func TestParseHostnameDomainname(t *testing.T) {
430432func TestParseWithExpose (t * testing.T ) {
431433 t .Run ("invalid" , func (t * testing.T ) {
432434 tests := map [string ]string {
433- ":" : " invalid port format for --expose: :" ,
434- "8080:9090" : " invalid port format for --expose: 8080:9090" ,
435- "/tcp" : " invalid range format for --expose: /tcp, error: empty string specified for ports" ,
436- "/udp" : " invalid range format for --expose: /udp, error: empty string specified for ports" ,
437- "NaN/tcp" : `invalid range format for --expose: NaN/tcp, error: strconv.ParseUint: parsing " NaN" : invalid syntax` ,
438- "NaN-NaN/tcp" : `invalid range format for --expose: NaN-NaN/tcp, error: strconv.ParseUint: parsing " NaN" : invalid syntax` ,
439- "8080-NaN/tcp" : `invalid range format for --expose: 8080-NaN/tcp, error: strconv.ParseUint: parsing " NaN" : invalid syntax` ,
440- "1234567890-8080/tcp" : `invalid range format for --expose: 1234567890-8080/tcp, error: strconv.ParseUint: parsing " 1234567890" : value out of range` ,
435+ ":" : ` invalid port format for --expose: invalid start port ':': invalid syntax` ,
436+ "8080:9090" : ` invalid port format for --expose: invalid start port ' 8080:9090': invalid syntax` ,
437+ "/tcp" : ` invalid port format for --expose: invalid start port '': value is empty` ,
438+ "/udp" : ` invalid port format for --expose: invalid start port '': value is empty` ,
439+ "NaN/tcp" : `invalid port format for --expose: invalid start port ' NaN' : invalid syntax` ,
440+ "NaN-NaN/tcp" : `invalid port format for --expose: invalid start port ' NaN' : invalid syntax` ,
441+ "8080-NaN/tcp" : `invalid port format for --expose: invalid end port ' NaN' : invalid syntax` ,
442+ "1234567890-8080/tcp" : `invalid port format for --expose: invalid start port ' 1234567890' : value out of range` ,
441443 }
442444 for expose , expectedError := range tests {
443445 t .Run (expose , func (t * testing.T ) {
@@ -447,20 +449,20 @@ func TestParseWithExpose(t *testing.T) {
447449 }
448450 })
449451 t .Run ("valid" , func (t * testing.T ) {
450- tests := map [string ][]container. PortRangeProto {
451- "8080/tcp" : {"8080/tcp" },
452- "8080/udp" : {"8080/udp" },
453- "8080/ncp" : {"8080/ncp" },
454- "8080-8080/udp" : {"8080/udp" },
455- "8080-8082/tcp" : {"8080/tcp" , "8081/tcp" , "8082/tcp" },
452+ tests := map [string ][]networktypes. Port {
453+ "8080/tcp" : {networktypes . MustParsePort ( "8080/tcp" ) },
454+ "8080/udp" : {networktypes . MustParsePort ( "8080/udp" ) },
455+ "8080/ncp" : {networktypes . MustParsePort ( "8080/ncp" ) },
456+ "8080-8080/udp" : {networktypes . MustParsePort ( "8080/udp" ) },
457+ "8080-8082/tcp" : {networktypes . MustParsePort ( "8080/tcp" ), networktypes . MustParsePort ( "8081/tcp" ), networktypes . MustParsePort ( "8082/tcp" ) },
456458 }
457459 for expose , exposedPorts := range tests {
458460 t .Run (expose , func (t * testing.T ) {
459461 config , _ , _ , err := parseRun ([]string {fmt .Sprintf ("--expose=%v" , expose ), "img" , "cmd" })
460462 assert .NilError (t , err )
461463 for _ , port := range exposedPorts {
462464 _ , ok := config .ExposedPorts [port ]
463- assert .Check (t , ok , "missing port %q in exposed ports" , port )
465+ assert .Check (t , ok , "missing port %q in exposed ports: %#+v " , port , config . ExposedPorts [ port ] )
464466 }
465467 })
466468 }
@@ -471,10 +473,10 @@ func TestParseWithExpose(t *testing.T) {
471473 config , _ , _ , err := parseRun ([]string {"--publish=80" , "--expose=80-81/tcp" , "img" , "cmd" })
472474 assert .NilError (t , err )
473475 assert .Check (t , is .Len (config .ExposedPorts , 2 ))
474- ports := []container. PortRangeProto { "80/tcp" , "81/tcp" }
476+ ports := []networktypes. Port { networktypes . MustParsePort ( "80/tcp" ), networktypes . MustParsePort ( "81/tcp" ) }
475477 for _ , port := range ports {
476478 _ , ok := config .ExposedPorts [port ]
477- assert .Check (t , ok , "missing port %q in exposed ports" , port )
479+ assert .Check (t , ok , "missing port %q in exposed ports: %#+v " , port , config . ExposedPorts [ port ] )
478480 }
479481 })
480482}
@@ -606,9 +608,9 @@ func TestParseNetworkConfig(t *testing.T) {
606608 expected : map [string ]* networktypes.EndpointSettings {
607609 "net1" : {
608610 IPAMConfig : & networktypes.EndpointIPAMConfig {
609- IPv4Address : "172.20.88.22" ,
610- IPv6Address : "2001:db8::8822" ,
611- LinkLocalIPs : []string { "169.254.2.2" , "fe80::169:254:2:2" },
611+ IPv4Address : netip . MustParseAddr ( "172.20.88.22" ) ,
612+ IPv6Address : netip . MustParseAddr ( "2001:db8::8822" ) ,
613+ LinkLocalIPs : []netip. Addr { netip . MustParseAddr ( "169.254.2.2" ), netip . MustParseAddr ( "fe80::169:254:2:2" ) },
612614 },
613615 Links : []string {"foo:bar" , "bar:baz" },
614616 Aliases : []string {"web1" , "web2" },
@@ -636,9 +638,9 @@ func TestParseNetworkConfig(t *testing.T) {
636638 "net1" : {
637639 DriverOpts : map [string ]string {"field1" : "value1" },
638640 IPAMConfig : & networktypes.EndpointIPAMConfig {
639- IPv4Address : "172.20.88.22" ,
640- IPv6Address : "2001:db8::8822" ,
641- LinkLocalIPs : []string { "169.254.2.2" , "fe80::169:254:2:2" },
641+ IPv4Address : netip . MustParseAddr ( "172.20.88.22" ) ,
642+ IPv6Address : netip . MustParseAddr ( "2001:db8::8822" ) ,
643+ LinkLocalIPs : []netip. Addr { netip . MustParseAddr ( "169.254.2.2" ), netip . MustParseAddr ( "fe80::169:254:2:2" ) },
642644 },
643645 Links : []string {"foo:bar" , "bar:baz" },
644646 Aliases : []string {"web1" , "web2" },
@@ -647,15 +649,15 @@ func TestParseNetworkConfig(t *testing.T) {
647649 "net3" : {
648650 DriverOpts : map [string ]string {"field3" : "value3" },
649651 IPAMConfig : & networktypes.EndpointIPAMConfig {
650- IPv4Address : "172.20.88.22" ,
651- IPv6Address : "2001:db8::8822" ,
652+ IPv4Address : netip . MustParseAddr ( "172.20.88.22" ) ,
653+ IPv6Address : netip . MustParseAddr ( "2001:db8::8822" ) ,
652654 },
653655 Aliases : []string {"web3" },
654656 },
655657 "net4" : {
656658 MacAddress : "02:32:1c:23:00:04" ,
657659 IPAMConfig : & networktypes.EndpointIPAMConfig {
658- LinkLocalIPs : []string { "169.254.169.254" },
660+ LinkLocalIPs : []netip. Addr { netip . MustParseAddr ( "169.254.169.254" ) },
659661 },
660662 },
661663 },
@@ -671,8 +673,8 @@ func TestParseNetworkConfig(t *testing.T) {
671673 "field2" : "value2" ,
672674 },
673675 IPAMConfig : & networktypes.EndpointIPAMConfig {
674- IPv4Address : "172.20.88.22" ,
675- IPv6Address : "2001:db8::8822" ,
676+ IPv4Address : netip . MustParseAddr ( "172.20.88.22" ) ,
677+ IPv6Address : netip . MustParseAddr ( "2001:db8::8822" ) ,
676678 },
677679 Aliases : []string {"web1" , "web2" },
678680 MacAddress : "02:32:1c:23:00:04" ,
@@ -753,7 +755,7 @@ func TestParseNetworkConfig(t *testing.T) {
753755 assert .NilError (t , err )
754756 assert .DeepEqual (t , config .MacAddress , tc .expectedCfg .MacAddress ) //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
755757 assert .DeepEqual (t , hConfig .NetworkMode , tc .expectedHostCfg .NetworkMode )
756- assert .DeepEqual (t , nwConfig .EndpointsConfig , tc .expected )
758+ assert .DeepEqual (t , nwConfig .EndpointsConfig , tc .expected , cmpopts . IgnoreUnexported (netip. Addr {}) )
757759 })
758760 }
759761}
0 commit comments