
If you pass just an IP address to "-s" or "-d", the iptables command will fill in the correct mask automatically. Originally, the proxier was just hardcoding "/32" for all of these, which was unnecessary but simple. But when IPv6 support was added, the code was made more complicated to deal with the fact that the "/32" needed to be "/128" in the IPv6 case, so it would parse the IPs to figure out which family they were, which in turn involved adding some checks in case the parsing fails (even though that "can't happen" and the old code didn't check for invalid IPs, even though that would break the iptables-restore if there had been any). Anyway, all of that is unnecessary because we can just pass the IP strings to iptables directly rather than parsing and unparsing them first. (The diff to proxier_test.go is just deleting "/32" everywhere.)
103 lines
2.2 KiB
Go
103 lines
2.2 KiB
Go
/*
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package util
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestIPPart(t *testing.T) {
|
|
const noError = ""
|
|
|
|
testCases := []struct {
|
|
endpoint string
|
|
expectedIP string
|
|
expectedError string
|
|
}{
|
|
{"1.2.3.4", "1.2.3.4", noError},
|
|
{"1.2.3.4:9999", "1.2.3.4", noError},
|
|
{"2001:db8::1:1", "2001:db8::1:1", noError},
|
|
{"[2001:db8::2:2]:9999", "2001:db8::2:2", noError},
|
|
{"1.2.3.4::9999", "", "too many colons"},
|
|
{"1.2.3.4:[0]", "", "unexpected '[' in address"},
|
|
{"1.2.3:8080", "", "invalid ip part"},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
ip := IPPart(tc.endpoint)
|
|
if tc.expectedError == noError {
|
|
if ip != tc.expectedIP {
|
|
t.Errorf("Unexpected IP for %s: Expected: %s, Got %s", tc.endpoint, tc.expectedIP, ip)
|
|
}
|
|
} else if ip != "" {
|
|
t.Errorf("Error did not occur for %s, expected: '%s' error", tc.endpoint, tc.expectedError)
|
|
}
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|