-
Notifications
You must be signed in to change notification settings - Fork 103
introduce String method on PortMapping #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
nat/nat.go
Outdated
@@ -150,6 +150,10 @@ type PortMapping struct { | |||
Binding PortBinding | |||
} | |||
|
|||
func (p *PortMapping) String() string { | |||
return fmt.Sprintf("%s:%s:%s", p.Binding.HostIP, p.Binding.HostPort, p.Port) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use net.JoinHostPort()
here, to account for IPv6 addresses; https://pkg.go.dev/net#JoinHostPort
e.g.;
return fmt.Sprintf("%s:%s:%s", p.Binding.HostIP, p.Binding.HostPort, p.Port) | |
return net.JoinHostPort(p.Binding.HostIP, p.Binding.HostPort+":"+p.Port.Port()) |
But I need to have a closer look, as there's many possible formats (ports mapped here can also have the protocol (e.g. 80/tcp
), which may have to be taken into account here.
And of course we also have PortMap
(which is a list of mapped ports) 😞
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I guess HostPort
doesn't have the scheme, so maybe that part is not an issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we know if HostIP
defaults to 0.0.0.0
(or ::
IPv6)? 🤔
008e7cc
to
a314a9d
Compare
7f68fd5
to
a2b6795
Compare
I rebased, and pushed some changes;
diff --git a/nat/nat.go b/nat/nat.go
index 5b53382..6c80825 100644
--- a/nat/nat.go
+++ b/nat/nat.go
@@ -151,7 +151,7 @@ type PortMapping struct {
}
func (p *PortMapping) String() string {
- return net.JoinHostPort(p.Binding.HostIP, fmt.Sprintf("%s:%s", p.Binding.HostPort, p.Port))
+ return net.JoinHostPort(p.Binding.HostIP, p.Binding.HostPort+":"+string(p.Port))
}
func splitParts(rawport string) (string, string, string) {
diff --git a/nat/nat_test.go b/nat/nat_test.go
index 3497643..571bf9e 100644
--- a/nat/nat_test.go
+++ b/nat/nat_test.go
@@ -699,14 +699,50 @@ func TestParseNetworkOptsSctp(t *testing.T) {
}
func TestStringer(t *testing.T) {
- spec := "192.168.1.100:8080:6000/udp"
- mappings, err := ParsePortSpec(spec)
- if err != nil {
- t.Fatal(err)
+ tests := []struct {
+ doc string
+ in string
+ expected string
+ }{
+ {
+ doc: "no host mapping",
+ in: ":8080:6000/tcp",
+ expected: ":8080:6000/tcp",
+ },
+ {
+ doc: "no proto",
+ in: "192.168.1.100:8080:6000",
+ expected: "192.168.1.100:8080:6000/tcp",
+ },
+ {
+ doc: "ipv4 mapping",
+ in: "192.168.1.100:8080:6000/udp",
+ expected: "192.168.1.100:8080:6000/udp",
+ },
+ {
+ doc: "ipv6 mapping",
+ in: "[::1]:8080:6000/udp",
+ expected: "[::1]:8080:6000/udp",
+ },
+ {
+ doc: "ipv6 legacy mapping",
+ in: "::1:8080:6000/udp",
+ expected: "[::1]:8080:6000/udp",
+ },
}
- for _, m := range mappings {
- if m.String() != spec {
- t.Fail()
- }
+ for _, tc := range tests {
+ t.Run(tc.doc, func(t *testing.T) {
+ mappings, err := ParsePortSpec(tc.in)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(mappings) != 1 {
+ // All tests produce a single mapping
+ t.Fatalf("Expected 1 got %d", len(mappings))
+ }
+ if actual := mappings[0].String(); actual != tc.expected {
+ t.Errorf("Expected %s got %s", tc.expected, actual)
+ }
+ })
}
}
--
2.44.0 |
@@ -697,3 +697,52 @@ func TestParseNetworkOptsSctp(t *testing.T) { | |||
} | |||
} | |||
} | |||
|
|||
func TestStringer(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe worth adding an in
with a missing port, just to check it doesn't come out as a zero or something.
For maximum evil, perhaps ::::80
-> [::]::80/tcp
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK; added some evil permutations 👍
Signed-off-by: Nicolas De Loof <[email protected]> Signed-off-by: Sebastiaan van Stijn <[email protected]>
a2b6795
to
d630407
Compare
This introduce
PortMapping.String()
for (some) symmetry withParsePortSpec