Merge pull request #67472 from islinwb/pkg_proxy_util-add-ut

Automatic merge from submit-queue (batch tested with PRs 66733, 67472). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

add ut for PortPart()

**What this PR does / why we need it**:
improve ut coverage

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
pull/8/head
Kubernetes Submit Queue 2018-08-17 17:50:05 -07:00 committed by GitHub
commit c2c0d9e441
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 52 additions and 0 deletions

View File

@ -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