From 2f30751db3c9bc50c5d0ab6f2c28ce7e192f76cf Mon Sep 17 00:00:00 2001 From: Weibin Lin Date: Thu, 16 Aug 2018 11:32:17 +0800 Subject: [PATCH] add ut for PortPart() --- pkg/proxy/util/endpoints_test.go | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/pkg/proxy/util/endpoints_test.go b/pkg/proxy/util/endpoints_test.go index bb28cbd714a..2d23b5b0b5d 100644 --- a/pkg/proxy/util/endpoints_test.go +++ b/pkg/proxy/util/endpoints_test.go @@ -50,6 +50,58 @@ func TestIPPart(t *testing.T) { } } +func TestPortPart(t *testing.T) { + tests := []struct { + name string + endpoint string + want int + wantErr bool + }{ + { + "no error parsing from ipv4-ip:port", + "1.2.3.4:1024", + 1024, + false, + }, + { + "no error parsing from ipv6-ip:port", + "[2001:db8::2:2]:9999", + 9999, + false, + }, + { + "error: missing port", + "1.2.3.4", + -1, + true, + }, + { + "error: invalid port '1-2'", + "1.2.3.4:1-2", + -1, + true, + }, + { + "error: invalid port 'port'", + "100.200.3.4:port", + -1, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := PortPart(tt.endpoint) + if (err != nil) != tt.wantErr { + t.Errorf("PortPart() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("PortPart() = %v, want %v", got, tt.want) + } + }) + } +} + func TestToCIDR(t *testing.T) { testCases := []struct { ip string