fix netsh checkIPExists in Chinese

Signed-off-by: sakeven <jc5930@sina.cn>
This commit is contained in:
sakeven 2017-09-25 16:06:21 +08:00
parent d945927077
commit c45a7ba4e5
2 changed files with 38 additions and 3 deletions

View File

@ -190,9 +190,8 @@ func checkIPExists(ipToCheck string, args []string, runner *runner) (bool, error
glog.V(3).Infof("Searching for IP: %v in IP dump: %v", ipToCheck, ipAddressString) glog.V(3).Infof("Searching for IP: %v in IP dump: %v", ipToCheck, ipAddressString)
showAddressArray := strings.Split(ipAddressString, "\n") showAddressArray := strings.Split(ipAddressString, "\n")
for _, showAddress := range showAddressArray { for _, showAddress := range showAddressArray {
if strings.Contains(showAddress, "IP Address:") { if strings.Contains(showAddress, "IP") {
ipFromNetsh := strings.TrimLeft(showAddress, "IP Address:") ipFromNetsh := getIP(showAddress)
ipFromNetsh = strings.TrimSpace(ipFromNetsh)
if ipFromNetsh == ipToCheck { if ipFromNetsh == ipToCheck {
return true, nil return true, nil
} }
@ -201,3 +200,12 @@ func checkIPExists(ipToCheck string, args []string, runner *runner) (bool, error
return false, nil return false, nil
} }
// getIP gets ip from showAddress (e.g. "IP Address: 10.96.0.4").
func getIP(showAddress string) string {
list := strings.SplitN(showAddress, ":", 2)
if len(list) != 2 {
return ""
}
return strings.TrimSpace(list[1])
}

View File

@ -438,3 +438,30 @@ func TestCheckIPExists(t *testing.T) {
} }
} }
} }
func TestGetIP(t *testing.T) {
testcases := []struct {
showAddress string
expectAddress string
}{
{
showAddress: "IP 地址: 10.96.0.2",
expectAddress: "10.96.0.2",
},
{
showAddress: "IP Address: 10.96.0.3",
expectAddress: "10.96.0.3",
},
{
showAddress: "IP Address:10.96.0.4",
expectAddress: "10.96.0.4",
},
}
for _, tc := range testcases {
address := getIP(tc.showAddress)
if address != tc.expectAddress {
t.Errorf("expected address=%q, got %q", tc.expectAddress, address)
}
}
}