allow hostnames in join

Signed-off-by: Victor Vieux <vieux@docker.com>
This commit is contained in:
Victor Vieux 2015-01-16 01:29:44 +00:00
parent 01633efaa0
commit bcbd0fec0f
2 changed files with 35 additions and 2 deletions

10
join.go
View File

@ -9,6 +9,11 @@ import (
"github.com/docker/swarm/discovery"
)
func checkAddrFormat(addr string) bool {
m, _ := regexp.MatchString("^[0-9a-zA-Z._-]+:[0-9]{1,5}$", addr)
return m
}
func join(c *cli.Context) {
if c.String("discovery") == "" {
@ -21,8 +26,9 @@ func join(c *cli.Context) {
}
addr := c.String("addr")
if m, _ := regexp.MatchString("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}:[0-9]{1,5}$", addr); !m {
log.Fatal("--addr should be of the form ip:port")
if !checkAddrFormat(addr) {
log.Fatal("--addr should be of the form ip:port or hostname:port")
}
if err := d.Register(addr); err != nil {

27
join_test.go Normal file
View File

@ -0,0 +1,27 @@
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCheckAddrFormat(t *testing.T) {
assert.False(t, checkAddrFormat("1.1.1.1"))
assert.False(t, checkAddrFormat("hostname"))
assert.False(t, checkAddrFormat("1.1.1.1:"))
assert.False(t, checkAddrFormat("hostname:"))
assert.False(t, checkAddrFormat("1.1.1.1:111111"))
assert.False(t, checkAddrFormat("hostname:111111"))
assert.False(t, checkAddrFormat("http://1.1.1.1"))
assert.False(t, checkAddrFormat("http://hostname"))
assert.False(t, checkAddrFormat("http://1.1.1.1:1"))
assert.False(t, checkAddrFormat("http://hostname:1"))
assert.False(t, checkAddrFormat(":1.1.1.1"))
assert.False(t, checkAddrFormat(":hostname"))
assert.False(t, checkAddrFormat(":1.1.1.1:1"))
assert.False(t, checkAddrFormat(":hostname:1"))
assert.True(t, checkAddrFormat("1.1.1.1:1111"))
assert.True(t, checkAddrFormat("hostname:1111"))
assert.True(t, checkAddrFormat("host-name_42:1111"))
}