1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/pkg/discovery/discovery_test.go
Tibor Vass 81d2a0c389 rm-gocheck: comment out check.Suite calls
sed -E -i 's#^([^*])+?((var .*)?check\.Suite\(.*\))#\1/*\2*/#g' \
-- "integration-cli/check_test.go" "integration-cli/docker_cli_external_volume_driver_unix_test.go" "integration-cli/docker_cli_network_unix_test.go" "integration-cli/docker_hub_pull_suite_test.go" "pkg/discovery/discovery_test.go" "pkg/discovery/file/file_test.go" "pkg/discovery/kv/kv_test.go" "pkg/discovery/memory/memory_test.go" "pkg/discovery/nodes/nodes_test.go"

Signed-off-by: Tibor Vass <tibor@docker.com>
2019-09-09 21:05:58 +00:00

134 lines
4 KiB
Go

package discovery // import "github.com/docker/docker/pkg/discovery"
import (
"testing"
"github.com/go-check/check"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { check.TestingT(t) }
type DiscoverySuite struct{}
/*check.Suite(&DiscoverySuite{})*/
func (s *DiscoverySuite) TestNewEntry(c *testing.T) {
entry, err := NewEntry("127.0.0.1:2375")
assert.Assert(c, err == nil)
assert.Equal(c, entry.Equals(&Entry{Host: "127.0.0.1", Port: "2375"}), true)
assert.Equal(c, entry.String(), "127.0.0.1:2375")
entry, err = NewEntry("[2001:db8:0:f101::2]:2375")
assert.Assert(c, err == nil)
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")
_, err = NewEntry("127.0.0.1")
assert.Assert(c, err != nil)
}
func (s *DiscoverySuite) TestParse(c *testing.T) {
scheme, uri := parse("127.0.0.1:2375")
assert.Equal(c, scheme, "nodes")
assert.Equal(c, uri, "127.0.0.1:2375")
scheme, uri = parse("localhost:2375")
assert.Equal(c, scheme, "nodes")
assert.Equal(c, uri, "localhost:2375")
scheme, uri = parse("scheme://127.0.0.1:2375")
assert.Equal(c, scheme, "scheme")
assert.Equal(c, uri, "127.0.0.1:2375")
scheme, uri = parse("scheme://localhost:2375")
assert.Equal(c, scheme, "scheme")
assert.Equal(c, uri, "localhost:2375")
scheme, uri = parse("")
assert.Equal(c, scheme, "nodes")
assert.Equal(c, uri, "")
}
func (s *DiscoverySuite) TestCreateEntries(c *testing.T) {
entries, err := CreateEntries(nil)
assert.DeepEqual(c, entries, Entries{})
assert.Assert(c, err == nil)
entries, err = CreateEntries([]string{"127.0.0.1:2375", "127.0.0.2:2375", "[2001:db8:0:f101::2]:2375", ""})
assert.Assert(c, err == nil)
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"},
}
assert.Equal(c, entries.Equals(expected), true)
_, err = CreateEntries([]string{"127.0.0.1", "127.0.0.2"})
assert.Assert(c, err != nil)
}
func (s *DiscoverySuite) TestContainsEntry(c *testing.T) {
entries, err := CreateEntries([]string{"127.0.0.1:2375", "127.0.0.2:2375", ""})
assert.Assert(c, err == nil)
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)
}
func (s *DiscoverySuite) TestEntriesEquality(c *testing.T) {
entries := Entries{
&Entry{Host: "127.0.0.1", Port: "2375"},
&Entry{Host: "127.0.0.2", Port: "2375"},
}
// Same
assert.Assert(c, entries.Equals(Entries{
&Entry{Host: "127.0.0.1", Port: "2375"},
&Entry{Host: "127.0.0.2", Port: "2375"},
}), checker.Equals, true)
// Different size
assert.Assert(c, entries.Equals(Entries{
&Entry{Host: "127.0.0.1", Port: "2375"},
&Entry{Host: "127.0.0.2", Port: "2375"},
&Entry{Host: "127.0.0.3", Port: "2375"},
}), checker.Equals, false)
// Different content
assert.Assert(c, entries.Equals(Entries{
&Entry{Host: "127.0.0.1", Port: "2375"},
&Entry{Host: "127.0.0.42", Port: "2375"},
}), checker.Equals, false)
}
func (s *DiscoverySuite) TestEntriesDiff(c *testing.T) {
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})
assert.Equal(c, len(added), 0)
assert.Equal(c, len(removed), 0)
// Add
added, removed = entries.Diff(Entries{entry2, entry3, entry1})
assert.Equal(c, len(added), 1)
assert.Equal(c, added.Contains(entry3), true)
assert.Equal(c, len(removed), 0)
// Remove
added, removed = entries.Diff(Entries{entry2})
assert.Equal(c, len(added), 0)
assert.Equal(c, len(removed), 1)
assert.Equal(c, removed.Contains(entry1), true)
// Add and remove
added, removed = entries.Diff(Entries{entry1, entry3})
assert.Equal(c, len(added), 1)
assert.Equal(c, added.Contains(entry3), true)
assert.Equal(c, len(removed), 1)
assert.Equal(c, removed.Contains(entry2), true)
}