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"
|
|
|
|
|
2019-08-08 20:38:35 -04:00
|
|
|
"github.com/docker/docker/internal/test/suite"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2015-08-31 16:23:17 -04:00
|
|
|
)
|
|
|
|
|
2015-10-01 20:02:38 -04:00
|
|
|
// Hook up gocheck into the "go test" runner.
|
2019-08-08 20:38:35 -04:00
|
|
|
func Test(t *testing.T) {
|
|
|
|
suite.Run(t, &DiscoverySuite{})
|
|
|
|
}
|
2015-10-01 20:02:38 -04:00
|
|
|
|
|
|
|
type DiscoverySuite struct{}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DiscoverySuite) TestNewEntry(c *testing.T) {
|
2015-08-31 16:23:17 -04:00
|
|
|
entry, err := NewEntry("127.0.0.1:2375")
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Assert(c, err == nil)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, entry.Equals(&Entry{Host: "127.0.0.1", Port: "2375"}), true)
|
|
|
|
assert.Equal(c, entry.String(), "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")
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Assert(c, err == nil)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, entry.Equals(&Entry{Host: "2001:db8:0:f101::2", Port: "2375"}), true)
|
|
|
|
assert.Equal(c, entry.String(), "[2001:db8:0:f101::2]:2375")
|
2016-03-01 18:13:08 -05:00
|
|
|
|
2015-08-31 16:23:17 -04:00
|
|
|
_, err = NewEntry("127.0.0.1")
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Assert(c, err != nil)
|
2015-08-31 16:23:17 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DiscoverySuite) TestParse(c *testing.T) {
|
2015-08-31 16:23:17 -04:00
|
|
|
scheme, uri := parse("127.0.0.1:2375")
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, scheme, "nodes")
|
|
|
|
assert.Equal(c, uri, "127.0.0.1:2375")
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
scheme, uri = parse("localhost:2375")
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, scheme, "nodes")
|
|
|
|
assert.Equal(c, uri, "localhost:2375")
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
scheme, uri = parse("scheme://127.0.0.1:2375")
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, scheme, "scheme")
|
|
|
|
assert.Equal(c, uri, "127.0.0.1:2375")
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
scheme, uri = parse("scheme://localhost:2375")
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, scheme, "scheme")
|
|
|
|
assert.Equal(c, uri, "localhost:2375")
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
scheme, uri = parse("")
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, scheme, "nodes")
|
|
|
|
assert.Equal(c, uri, "")
|
2015-08-31 16:23:17 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DiscoverySuite) TestCreateEntries(c *testing.T) {
|
2015-08-31 16:23:17 -04:00
|
|
|
entries, err := CreateEntries(nil)
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.DeepEqual(c, entries, Entries{})
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Assert(c, err == nil)
|
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", ""})
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Assert(c, err == nil)
|
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
|
|
|
}
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, entries.Equals(expected), true)
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
_, err = CreateEntries([]string{"127.0.0.1", "127.0.0.2"})
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Assert(c, err != nil)
|
2015-08-31 16:23:17 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DiscoverySuite) TestContainsEntry(c *testing.T) {
|
2015-08-31 16:23:17 -04:00
|
|
|
entries, err := CreateEntries([]string{"127.0.0.1:2375", "127.0.0.2:2375", ""})
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Assert(c, err == nil)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, entries.Contains(&Entry{Host: "127.0.0.1", Port: "2375"}), true)
|
|
|
|
assert.Equal(c, entries.Contains(&Entry{Host: "127.0.0.3", Port: "2375"}), false)
|
2015-08-31 16:23:17 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DiscoverySuite) TestEntriesEquality(c *testing.T) {
|
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
|
2019-09-09 17:05:55 -04:00
|
|
|
assert.Assert(c, 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"},
|
2019-08-16 19:36:18 -04:00
|
|
|
}))
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
// Different size
|
2019-08-16 19:36:18 -04:00
|
|
|
assert.Assert(c, !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"},
|
2019-08-16 19:36:18 -04:00
|
|
|
}))
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
// Different content
|
2019-08-16 19:36:18 -04:00
|
|
|
assert.Assert(c, !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"},
|
2019-08-16 19:36:18 -04:00
|
|
|
}))
|
2015-10-01 20:02:38 -04:00
|
|
|
|
2015-08-31 16:23:17 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DiscoverySuite) TestEntriesDiff(c *testing.T) {
|
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})
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Equal(c, len(added), 0)
|
|
|
|
assert.Equal(c, len(removed), 0)
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
// Add
|
|
|
|
added, removed = entries.Diff(Entries{entry2, entry3, entry1})
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Equal(c, len(added), 1)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, added.Contains(entry3), true)
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Equal(c, len(removed), 0)
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
// Remove
|
|
|
|
added, removed = entries.Diff(Entries{entry2})
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Equal(c, len(added), 0)
|
|
|
|
assert.Equal(c, len(removed), 1)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, removed.Contains(entry1), true)
|
2015-08-31 16:23:17 -04:00
|
|
|
|
|
|
|
// Add and remove
|
|
|
|
added, removed = entries.Diff(Entries{entry1, entry3})
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Equal(c, len(added), 1)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, added.Contains(entry3), true)
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Equal(c, len(removed), 1)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, removed.Contains(entry2), true)
|
2015-08-31 16:23:17 -04:00
|
|
|
}
|