2018-02-05 16:05:59 -05:00
|
|
|
package discovery // import "github.com/docker/docker/pkg/discovery"
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2015-10-01 20:02:38 -04:00
|
|
|
"github.com/go-check/check"
|
2015-08-31 16:23:17 -04:00
|
|
|
)
|
|
|
|
|
2015-10-01 20:02:38 -04:00
|
|
|
// Hook up gocheck into the "go test" runner.
|
|
|
|
func Test(t *testing.T) { check.TestingT(t) }
|
|
|
|
|
|
|
|
type DiscoverySuite struct{}
|
|
|
|
|
|
|
|
var _ = check.Suite(&DiscoverySuite{})
|
|
|
|
|
|
|
|
func (s *DiscoverySuite) TestNewEntry(c *check.C) {
|
2015-08-31 16:23:17 -04:00
|
|
|
entry, err := NewEntry("127.0.0.1:2375")
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
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")
|
2015-08-31 16:23:17 -04:00
|
|
|
|
2016-03-01 18:13:08 -05:00
|
|
|
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")
|
|
|
|
|
2015-08-31 16:23:17 -04:00
|
|
|
_, err = NewEntry("127.0.0.1")
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
2015-08-31 16:23:17 -04:00
|
|
|
}
|
|
|
|
|
2015-10-01 20:02:38 -04:00
|
|
|
func (s *DiscoverySuite) TestParse(c *check.C) {
|
2015-08-31 16:23:17 -04:00
|
|
|
scheme, uri := parse("127.0.0.1:2375")
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(scheme, check.Equals, "nodes")
|
|
|
|
c.Assert(uri, check.Equals, "127.0.0.1:2375")
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
scheme, uri = parse("localhost:2375")
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(scheme, check.Equals, "nodes")
|
|
|
|
c.Assert(uri, check.Equals, "localhost:2375")
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
scheme, uri = parse("scheme://127.0.0.1:2375")
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(scheme, check.Equals, "scheme")
|
|
|
|
c.Assert(uri, check.Equals, "127.0.0.1:2375")
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
scheme, uri = parse("scheme://localhost:2375")
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(scheme, check.Equals, "scheme")
|
|
|
|
c.Assert(uri, check.Equals, "localhost:2375")
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
scheme, uri = parse("")
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(scheme, check.Equals, "nodes")
|
|
|
|
c.Assert(uri, check.Equals, "")
|
2015-08-31 16:23:17 -04:00
|
|
|
}
|
|
|
|
|
2015-10-01 20:02:38 -04:00
|
|
|
func (s *DiscoverySuite) TestCreateEntries(c *check.C) {
|
2015-08-31 16:23:17 -04:00
|
|
|
entries, err := CreateEntries(nil)
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(entries, check.DeepEquals, Entries{})
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-08-31 16:23:17 -04:00
|
|
|
|
2016-03-01 18:13:08 -05:00
|
|
|
entries, err = CreateEntries([]string{"127.0.0.1:2375", "127.0.0.2:2375", "[2001:db8:0:f101::2]:2375", ""})
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-08-31 16:23:17 -04:00
|
|
|
expected := Entries{
|
|
|
|
&Entry{Host: "127.0.0.1", Port: "2375"},
|
|
|
|
&Entry{Host: "127.0.0.2", Port: "2375"},
|
2016-03-01 18:13:08 -05:00
|
|
|
&Entry{Host: "2001:db8:0:f101::2", Port: "2375"},
|
2015-08-31 16:23:17 -04:00
|
|
|
}
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(entries.Equals(expected), check.Equals, true)
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
_, err = CreateEntries([]string{"127.0.0.1", "127.0.0.2"})
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
2015-08-31 16:23:17 -04:00
|
|
|
}
|
|
|
|
|
2015-10-01 20:02:38 -04:00
|
|
|
func (s *DiscoverySuite) TestContainsEntry(c *check.C) {
|
2015-08-31 16:23:17 -04:00
|
|
|
entries, err := CreateEntries([]string{"127.0.0.1:2375", "127.0.0.2:2375", ""})
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(entries.Contains(&Entry{Host: "127.0.0.1", Port: "2375"}), check.Equals, true)
|
|
|
|
c.Assert(entries.Contains(&Entry{Host: "127.0.0.3", Port: "2375"}), check.Equals, false)
|
2015-08-31 16:23:17 -04:00
|
|
|
}
|
|
|
|
|
2015-10-01 20:02:38 -04:00
|
|
|
func (s *DiscoverySuite) TestEntriesEquality(c *check.C) {
|
2015-08-31 16:23:17 -04:00
|
|
|
entries := Entries{
|
|
|
|
&Entry{Host: "127.0.0.1", Port: "2375"},
|
|
|
|
&Entry{Host: "127.0.0.2", Port: "2375"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(entries.Equals(Entries{
|
2015-08-31 16:23:17 -04:00
|
|
|
&Entry{Host: "127.0.0.1", Port: "2375"},
|
|
|
|
&Entry{Host: "127.0.0.2", Port: "2375"},
|
2015-10-01 20:02:38 -04:00
|
|
|
}), check.
|
|
|
|
Equals, true)
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
// Different size
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(entries.Equals(Entries{
|
2015-08-31 16:23:17 -04:00
|
|
|
&Entry{Host: "127.0.0.1", Port: "2375"},
|
|
|
|
&Entry{Host: "127.0.0.2", Port: "2375"},
|
|
|
|
&Entry{Host: "127.0.0.3", Port: "2375"},
|
2015-10-01 20:02:38 -04:00
|
|
|
}), check.
|
|
|
|
Equals, false)
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
// Different content
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(entries.Equals(Entries{
|
2015-08-31 16:23:17 -04:00
|
|
|
&Entry{Host: "127.0.0.1", Port: "2375"},
|
|
|
|
&Entry{Host: "127.0.0.42", Port: "2375"},
|
2015-10-01 20:02:38 -04:00
|
|
|
}), check.
|
|
|
|
Equals, false)
|
|
|
|
|
2015-08-31 16:23:17 -04:00
|
|
|
}
|
|
|
|
|
2015-10-01 20:02:38 -04:00
|
|
|
func (s *DiscoverySuite) TestEntriesDiff(c *check.C) {
|
2015-08-31 16:23:17 -04:00
|
|
|
entry1 := &Entry{Host: "1.1.1.1", Port: "1111"}
|
|
|
|
entry2 := &Entry{Host: "2.2.2.2", Port: "2222"}
|
|
|
|
entry3 := &Entry{Host: "3.3.3.3", Port: "3333"}
|
|
|
|
entries := Entries{entry1, entry2}
|
|
|
|
|
|
|
|
// No diff
|
|
|
|
added, removed := entries.Diff(Entries{entry2, entry1})
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(added, check.HasLen, 0)
|
|
|
|
c.Assert(removed, check.HasLen, 0)
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
// Add
|
|
|
|
added, removed = entries.Diff(Entries{entry2, entry3, entry1})
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(added, check.HasLen, 1)
|
|
|
|
c.Assert(added.Contains(entry3), check.Equals, true)
|
|
|
|
c.Assert(removed, check.HasLen, 0)
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
// Remove
|
|
|
|
added, removed = entries.Diff(Entries{entry2})
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(added, check.HasLen, 0)
|
|
|
|
c.Assert(removed, check.HasLen, 1)
|
|
|
|
c.Assert(removed.Contains(entry1), check.Equals, true)
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
// Add and remove
|
|
|
|
added, removed = entries.Diff(Entries{entry1, entry3})
|
2015-10-01 20:02:38 -04:00
|
|
|
c.Assert(added, check.HasLen, 1)
|
|
|
|
c.Assert(added.Contains(entry3), check.Equals, true)
|
|
|
|
c.Assert(removed, check.HasLen, 1)
|
|
|
|
c.Assert(removed.Contains(entry2), check.Equals, true)
|
2015-08-31 16:23:17 -04:00
|
|
|
}
|