Skip to content

nat: PortBinding: implement stringer #136

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ type PortBinding struct {
HostPort string
}

func (pb *PortBinding) String() string {
if pb.HostPort == "" {
return ""
}
if pb.HostIP == "" {
return pb.HostPort
}
return net.JoinHostPort(pb.HostIP, pb.HostPort)
}

// PortMap is a collection of PortBinding indexed by Port
type PortMap map[Port][]PortBinding

Expand Down
62 changes: 62 additions & 0 deletions nat/nat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,68 @@ func TestStringer(t *testing.T) {
}
}

func TestPortBinding(t *testing.T) {
tests := []struct {
doc string
binding *PortBinding
expect string
}{
{
doc: "no host mapping",
binding: &PortBinding{},
expect: "",
},
{
doc: "ipv4 missing port",
binding: &PortBinding{
HostIP: "192.168.1.100",
},
expect: "",
},
{
doc: "ipv4",
binding: &PortBinding{
HostIP: "192.168.1.100",
HostPort: "6000",
},
expect: "192.168.1.100:6000",
},
{
doc: "ipv6 missing port",
binding: &PortBinding{
HostIP: "::1",
},
expect: "",
},
{
doc: "ipv6",
binding: &PortBinding{
HostIP: "::1",
HostPort: "6000",
},
expect: "[::1]:6000",
},
// FIXME(thaJeztah): this should be invalid
{
doc: "ipv6 with braces",
binding: &PortBinding{
HostIP: "[::1]",
HostPort: "6000",
},
expect: "[[::1]]:6000",
},
Comment on lines +886 to +894
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to look at this one probably (test is just keeping the status quo)

}

for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
actual := tc.binding.String()
if actual != tc.expect {
t.Errorf("Expected %s got %s", tc.expect, actual)
}
})
}
}

func BenchmarkParsePortSpecs(b *testing.B) {
specs := [][]string{
{"1234/tcp", "2345/udp", "3456/sctp"},
Expand Down
Loading