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 230f7bcc02 rm-gocheck: normalize to use checker
sed -E -i 's#\bcheck\.(Equals|DeepEquals|HasLen|IsNil|Matches|Not|NotNil)\b#checker.\1#g' \
-- "integration-cli/docker_api_containers_test.go" "integration-cli/docker_cli_attach_test.go" "integration-cli/docker_cli_attach_unix_test.go" "integration-cli/docker_cli_build_test.go" "integration-cli/docker_cli_build_unix_test.go" "integration-cli/docker_cli_by_digest_test.go" "integration-cli/docker_cli_create_test.go" "integration-cli/docker_cli_daemon_test.go" "integration-cli/docker_cli_external_volume_driver_unix_test.go" "integration-cli/docker_cli_health_test.go" "integration-cli/docker_cli_images_test.go" "integration-cli/docker_cli_inspect_test.go" "integration-cli/docker_cli_netmode_test.go" "integration-cli/docker_cli_network_unix_test.go" "integration-cli/docker_cli_port_test.go" "integration-cli/docker_cli_run_test.go" "integration-cli/docker_cli_run_unix_test.go" "integration-cli/docker_cli_save_load_test.go" "integration-cli/docker_cli_service_health_test.go" "integration-cli/docker_cli_swarm_test.go" "integration-cli/docker_cli_volume_test.go" "integration-cli/docker_utils_test.go" "pkg/discovery/discovery_test.go" "pkg/discovery/file/file_test.go" "pkg/discovery/generator_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:56 +00:00

134 lines
4.5 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{}
var _ = check.Suite(&DiscoverySuite{})
func (s *DiscoverySuite) TestNewEntry(c *testing.T) {
entry, err := NewEntry("127.0.0.1:2375")
assert.Assert(c, err, checker.IsNil)
assert.Assert(c, entry.Equals(&Entry{Host: "127.0.0.1", Port: "2375"}), checker.Equals, true)
assert.Assert(c, entry.String(), checker.Equals, "127.0.0.1:2375")
entry, err = NewEntry("[2001:db8:0:f101::2]:2375")
assert.Assert(c, err, checker.IsNil)
assert.Assert(c, entry.Equals(&Entry{Host: "2001:db8:0:f101::2", Port: "2375"}), checker.Equals, true)
assert.Assert(c, entry.String(), checker.Equals, "[2001:db8:0:f101::2]:2375")
_, err = NewEntry("127.0.0.1")
assert.Assert(c, err, checker.NotNil)
}
func (s *DiscoverySuite) TestParse(c *testing.T) {
scheme, uri := parse("127.0.0.1:2375")
assert.Assert(c, scheme, checker.Equals, "nodes")
assert.Assert(c, uri, checker.Equals, "127.0.0.1:2375")
scheme, uri = parse("localhost:2375")
assert.Assert(c, scheme, checker.Equals, "nodes")
assert.Assert(c, uri, checker.Equals, "localhost:2375")
scheme, uri = parse("scheme://127.0.0.1:2375")
assert.Assert(c, scheme, checker.Equals, "scheme")
assert.Assert(c, uri, checker.Equals, "127.0.0.1:2375")
scheme, uri = parse("scheme://localhost:2375")
assert.Assert(c, scheme, checker.Equals, "scheme")
assert.Assert(c, uri, checker.Equals, "localhost:2375")
scheme, uri = parse("")
assert.Assert(c, scheme, checker.Equals, "nodes")
assert.Assert(c, uri, checker.Equals, "")
}
func (s *DiscoverySuite) TestCreateEntries(c *testing.T) {
entries, err := CreateEntries(nil)
assert.Assert(c, entries, checker.DeepEquals, Entries{})
assert.Assert(c, err, checker.IsNil)
entries, err = CreateEntries([]string{"127.0.0.1:2375", "127.0.0.2:2375", "[2001:db8:0:f101::2]:2375", ""})
assert.Assert(c, err, checker.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"},
}
assert.Assert(c, entries.Equals(expected), checker.Equals, true)
_, err = CreateEntries([]string{"127.0.0.1", "127.0.0.2"})
assert.Assert(c, err, checker.NotNil)
}
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, checker.IsNil)
assert.Assert(c, entries.Contains(&Entry{Host: "127.0.0.1", Port: "2375"}), checker.Equals, true)
assert.Assert(c, entries.Contains(&Entry{Host: "127.0.0.3", Port: "2375"}), checker.Equals, 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.Assert(c, added, checker.HasLen, 0)
assert.Assert(c, removed, checker.HasLen, 0)
// Add
added, removed = entries.Diff(Entries{entry2, entry3, entry1})
assert.Assert(c, added, checker.HasLen, 1)
assert.Assert(c, added.Contains(entry3), checker.Equals, true)
assert.Assert(c, removed, checker.HasLen, 0)
// Remove
added, removed = entries.Diff(Entries{entry2})
assert.Assert(c, added, checker.HasLen, 0)
assert.Assert(c, removed, checker.HasLen, 1)
assert.Assert(c, removed.Contains(entry1), checker.Equals, true)
// Add and remove
added, removed = entries.Diff(Entries{entry1, entry3})
assert.Assert(c, added, checker.HasLen, 1)
assert.Assert(c, added.Contains(entry3), checker.Equals, true)
assert.Assert(c, removed, checker.HasLen, 1)
assert.Assert(c, removed.Contains(entry2), checker.Equals, true)
}