diff --git a/libmachine/host.go b/libmachine/host.go index 952a57a989..e7eed24b50 100644 --- a/libmachine/host.go +++ b/libmachine/host.go @@ -92,11 +92,8 @@ func LoadHost(name string, StorePath string) (*Host, error) { return host, nil } -func ValidateHostName(name string) (string, error) { - if !validHostNamePattern.MatchString(name) { - return name, ErrInvalidHostname - } - return name, nil +func ValidateHostName(name string) bool { + return validHostNamePattern.MatchString(name) } func (h *Host) Create(name string) error { diff --git a/libmachine/host_test.go b/libmachine/host_test.go index 32b08c16d5..4f2b7603e7 100644 --- a/libmachine/host_test.go +++ b/libmachine/host_test.go @@ -122,13 +122,9 @@ func TestValidateHostnameValid(t *testing.T) { } for _, v := range hosts { - h, err := ValidateHostName(v) - if err != nil { - t.Fatal("Invalid hostname") - } - - if h != v { - t.Fatal("Hostname doesn't match") + isValid := ValidateHostName(v) + if !isValid { + t.Fatal("Thought a valid hostname was invalid: %s", v) } } } @@ -141,9 +137,9 @@ func TestValidateHostnameInvalid(t *testing.T) { } for _, v := range hosts { - _, err := ValidateHostName(v) - if err == nil { - t.Fatal("No error returned") + isValid := ValidateHostName(v) + if isValid { + t.Fatal("Thought an invalid hostname was valid: %s", v) } } } diff --git a/libmachine/machine.go b/libmachine/machine.go index 0a43394bc3..c3f71c5ba1 100644 --- a/libmachine/machine.go +++ b/libmachine/machine.go @@ -20,6 +20,10 @@ func New(store Store) (*Machine, error) { } func (m *Machine) Create(name string, driverName string, hostOptions *HostOptions, driverConfig drivers.DriverOptions) (*Host, error) { + validName := ValidateHostName(name) + if !validName { + return nil, ErrInvalidHostname + } exists, err := m.store.Exists(name) if err != nil { return nil, err