Handle IPv6 entries.

Signed-off-by: Dong Chen <dongluo.chen@docker.com>
This commit is contained in:
Dong Chen 2016-03-01 15:13:08 -08:00
parent 22684de872
commit f7c9214e29
2 changed files with 9 additions and 6 deletions

View File

@ -19,6 +19,11 @@ func (s *DiscoverySuite) TestNewEntry(c *check.C) {
c.Assert(entry.Equals(&Entry{Host: "127.0.0.1", Port: "2375"}), check.Equals, true)
c.Assert(entry.String(), check.Equals, "127.0.0.1:2375")
entry, err = NewEntry("[2001:db8:0:f101::2]:2375")
c.Assert(err, check.IsNil)
c.Assert(entry.Equals(&Entry{Host: "2001:db8:0:f101::2", Port: "2375"}), check.Equals, true)
c.Assert(entry.String(), check.Equals, "[2001:db8:0:f101::2]:2375")
_, err = NewEntry("127.0.0.1")
c.Assert(err, check.NotNil)
}
@ -50,11 +55,12 @@ func (s *DiscoverySuite) TestCreateEntries(c *check.C) {
c.Assert(entries, check.DeepEquals, Entries{})
c.Assert(err, check.IsNil)
entries, err = CreateEntries([]string{"127.0.0.1:2375", "127.0.0.2:2375", ""})
entries, err = CreateEntries([]string{"127.0.0.1:2375", "127.0.0.2:2375", "[2001:db8:0:f101::2]:2375", ""})
c.Assert(err, check.IsNil)
expected := Entries{
&Entry{Host: "127.0.0.1", Port: "2375"},
&Entry{Host: "127.0.0.2", Port: "2375"},
&Entry{Host: "2001:db8:0:f101::2", Port: "2375"},
}
c.Assert(entries.Equals(expected), check.Equals, true)

View File

@ -1,9 +1,6 @@
package discovery
import (
"fmt"
"net"
)
import "net"
// NewEntry creates a new entry.
func NewEntry(url string) (*Entry, error) {
@ -27,7 +24,7 @@ func (e *Entry) Equals(cmp *Entry) bool {
// String returns the string form of an entry.
func (e *Entry) String() string {
return fmt.Sprintf("%s:%s", e.Host, e.Port)
return net.JoinHostPort(e.Host, e.Port)
}
// Entries is a list of *Entry with some helpers.