@@ -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"
@@ -438,46 +440,52 @@ func TestParseWithExpose(t *testing.T) {
438440 "8080-NaN/tcp" : `invalid range format for --expose: 8080-NaN/tcp, error: strconv.ParseUint: parsing "NaN": invalid syntax` ,
439441 "1234567890-8080/tcp" : `invalid range format for --expose: 1234567890-8080/tcp, error: strconv.ParseUint: parsing "1234567890": value out of range` ,
440442 }
441- valids := map [string ][]container. PortRangeProto {
442- "8080/tcp" : {"8080/tcp" },
443- "8080/udp" : {"8080/udp" },
444- "8080/ncp" : {"8080/ncp" },
445- "8080-8080/udp" : {"8080/udp" },
446- "8080-8082/tcp" : {"8080/tcp" , "8081/tcp" , "8082/tcp" },
443+ valids := map [string ][]networktypes. Port {
444+ "8080/tcp" : {networktypes . MustParsePort ( "8080/tcp" ) },
445+ "8080/udp" : {networktypes . MustParsePort ( "8080/udp" ) },
446+ "8080/ncp" : {networktypes . MustParsePort ( "8080/ncp" ) },
447+ "8080-8080/udp" : {networktypes . MustParsePort ( "8080/udp" ) },
448+ "8080-8082/tcp" : {networktypes . MustParsePort ( "8080/tcp" ), networktypes . MustParsePort ( "8081/tcp" ), networktypes . MustParsePort ( "8082/tcp" ) },
447449 }
448450 for expose , expectedError := range invalids {
449- if _ , _ , _ , err := parseRun ([]string {fmt .Sprintf ("--expose=%v" , expose ), "img" , "cmd" }); err == nil || err .Error () != expectedError {
450- t .Fatalf ("Expected error '%v' with '--expose=%v', got '%v'" , expectedError , expose , err )
451- }
451+ t .Run ("invalid/" + expose , func (t * testing.T ) {
452+ if _ , _ , _ , err := parseRun ([]string {fmt .Sprintf ("--expose=%v" , expose ), "img" , "cmd" }); err == nil || err .Error () != expectedError {
453+ t .Fatalf ("Expected error '%v' with '--expose=%v', got '%v'" , expectedError , expose , err )
454+ }
455+ })
452456 }
453457 for expose , exposedPorts := range valids {
454- config , _ , _ , err := parseRun ([]string {fmt .Sprintf ("--expose=%v" , expose ), "img" , "cmd" })
458+ t .Run ("valid/" + expose , func (t * testing.T ) {
459+ config , _ , _ , err := parseRun ([]string {fmt .Sprintf ("--expose=%v" , expose ), "img" , "cmd" })
460+ if err != nil {
461+ t .Fatal (err )
462+ }
463+ if len (config .ExposedPorts ) != len (exposedPorts ) {
464+ t .Fatalf ("Expected %v exposed port, got %v" , len (exposedPorts ), len (config .ExposedPorts ))
465+ }
466+ for _ , port := range exposedPorts {
467+ if _ , ok := config .ExposedPorts [port ]; ! ok {
468+ t .Fatalf ("Expected %v, got %v" , exposedPorts , config .ExposedPorts )
469+ }
470+ }
471+ })
472+ }
473+ t .Run ("merge with published" , func (t * testing.T ) {
474+ // Merge with actual published port
475+ config , _ , _ , err := parseRun ([]string {"--publish=80" , "--expose=80-81/tcp" , "img" , "cmd" })
455476 if err != nil {
456477 t .Fatal (err )
457478 }
458- if len (config .ExposedPorts ) != len ( exposedPorts ) {
459- t .Fatalf ("Expected %v exposed port , got %v" , len ( exposedPorts ), len ( config .ExposedPorts ) )
479+ if len (config .ExposedPorts ) != 2 {
480+ t .Fatalf ("Expected 2 exposed ports , got %v" , config .ExposedPorts )
460481 }
461- for _ , port := range exposedPorts {
482+ ports := []networktypes.Port {networktypes .MustParsePort ("80/tcp" ), networktypes .MustParsePort ("81/tcp" )}
483+ for _ , port := range ports {
462484 if _ , ok := config .ExposedPorts [port ]; ! ok {
463- t .Fatalf ("Expected %v, got %v" , exposedPorts , config .ExposedPorts )
485+ t .Fatalf ("Expected %v, got %v" , ports , config .ExposedPorts )
464486 }
465487 }
466- }
467- // Merge with actual published port
468- config , _ , _ , err := parseRun ([]string {"--publish=80" , "--expose=80-81/tcp" , "img" , "cmd" })
469- if err != nil {
470- t .Fatal (err )
471- }
472- if len (config .ExposedPorts ) != 2 {
473- t .Fatalf ("Expected 2 exposed ports, got %v" , config .ExposedPorts )
474- }
475- ports := []container.PortRangeProto {"80/tcp" , "81/tcp" }
476- for _ , port := range ports {
477- if _ , ok := config .ExposedPorts [port ]; ! ok {
478- t .Fatalf ("Expected %v, got %v" , ports , config .ExposedPorts )
479- }
480- }
488+ })
481489}
482490
483491func TestParseDevice (t * testing.T ) {
@@ -607,9 +615,9 @@ func TestParseNetworkConfig(t *testing.T) {
607615 expected : map [string ]* networktypes.EndpointSettings {
608616 "net1" : {
609617 IPAMConfig : & networktypes.EndpointIPAMConfig {
610- IPv4Address : "172.20.88.22" ,
611- IPv6Address : "2001:db8::8822" ,
612- LinkLocalIPs : []string { "169.254.2.2" , "fe80::169:254:2:2" },
618+ IPv4Address : netip . MustParseAddr ( "172.20.88.22" ) ,
619+ IPv6Address : netip . MustParseAddr ( "2001:db8::8822" ) ,
620+ LinkLocalIPs : []netip. Addr { netip . MustParseAddr ( "169.254.2.2" ), netip . MustParseAddr ( "fe80::169:254:2:2" ) },
613621 },
614622 Links : []string {"foo:bar" , "bar:baz" },
615623 Aliases : []string {"web1" , "web2" },
@@ -637,9 +645,9 @@ func TestParseNetworkConfig(t *testing.T) {
637645 "net1" : {
638646 DriverOpts : map [string ]string {"field1" : "value1" },
639647 IPAMConfig : & networktypes.EndpointIPAMConfig {
640- IPv4Address : "172.20.88.22" ,
641- IPv6Address : "2001:db8::8822" ,
642- LinkLocalIPs : []string { "169.254.2.2" , "fe80::169:254:2:2" },
648+ IPv4Address : netip . MustParseAddr ( "172.20.88.22" ) ,
649+ IPv6Address : netip . MustParseAddr ( "2001:db8::8822" ) ,
650+ LinkLocalIPs : []netip. Addr { netip . MustParseAddr ( "169.254.2.2" ), netip . MustParseAddr ( "fe80::169:254:2:2" ) },
643651 },
644652 Links : []string {"foo:bar" , "bar:baz" },
645653 Aliases : []string {"web1" , "web2" },
@@ -648,15 +656,15 @@ func TestParseNetworkConfig(t *testing.T) {
648656 "net3" : {
649657 DriverOpts : map [string ]string {"field3" : "value3" },
650658 IPAMConfig : & networktypes.EndpointIPAMConfig {
651- IPv4Address : "172.20.88.22" ,
652- IPv6Address : "2001:db8::8822" ,
659+ IPv4Address : netip . MustParseAddr ( "172.20.88.22" ) ,
660+ IPv6Address : netip . MustParseAddr ( "2001:db8::8822" ) ,
653661 },
654662 Aliases : []string {"web3" },
655663 },
656664 "net4" : {
657665 MacAddress : "02:32:1c:23:00:04" ,
658666 IPAMConfig : & networktypes.EndpointIPAMConfig {
659- LinkLocalIPs : []string { "169.254.169.254" },
667+ LinkLocalIPs : []netip. Addr { netip . MustParseAddr ( "169.254.169.254" ) },
660668 },
661669 },
662670 },
@@ -672,8 +680,8 @@ func TestParseNetworkConfig(t *testing.T) {
672680 "field2" : "value2" ,
673681 },
674682 IPAMConfig : & networktypes.EndpointIPAMConfig {
675- IPv4Address : "172.20.88.22" ,
676- IPv6Address : "2001:db8::8822" ,
683+ IPv4Address : netip . MustParseAddr ( "172.20.88.22" ) ,
684+ IPv6Address : netip . MustParseAddr ( "2001:db8::8822" ) ,
677685 },
678686 Aliases : []string {"web1" , "web2" },
679687 MacAddress : "02:32:1c:23:00:04" ,
@@ -754,7 +762,7 @@ func TestParseNetworkConfig(t *testing.T) {
754762 assert .NilError (t , err )
755763 assert .DeepEqual (t , config .MacAddress , tc .expectedCfg .MacAddress ) //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
756764 assert .DeepEqual (t , hConfig .NetworkMode , tc .expectedHostCfg .NetworkMode )
757- assert .DeepEqual (t , nwConfig .EndpointsConfig , tc .expected )
765+ assert .DeepEqual (t , nwConfig .EndpointsConfig , tc .expected , cmpopts . IgnoreUnexported (netip. Addr {}) )
758766 })
759767 }
760768}
0 commit comments