nodes: improve handling of erroneous host names

This commit is contained in:
Di Xu
2018-06-06 14:36:15 +08:00
parent 8e2d37ee63
commit b3dfe0c652
13 changed files with 99 additions and 21 deletions

View File

@@ -89,3 +89,35 @@ func TestGetPreferredAddress(t *testing.T) {
}
}
}
func TestGetHostname(t *testing.T) {
testCases := []struct {
hostName string
expectedHostName string
expectError bool
}{
{
hostName: " ",
expectError: true,
},
{
hostName: " abc ",
expectedHostName: "abc",
expectError: false,
},
}
for idx, test := range testCases {
hostName, err := GetHostname(test.hostName)
if err != nil && !test.expectError {
t.Errorf("[%d]: unexpected error: %s", idx, err)
}
if err == nil && test.expectError {
t.Errorf("[%d]: expected error, got none", idx)
}
if test.expectedHostName != hostName {
t.Errorf("[%d]: expected output %q, got %q", idx, test.expectedHostName, hostName)
}
}
}