remove testify asserts from pkg/discovery

Signed-off-by: Morgan Bauer <mbauer@us.ibm.com>
This commit is contained in:
Morgan Bauer 2015-10-01 17:02:38 -07:00
parent 2de02bc741
commit 890fa18dc5
5 changed files with 188 additions and 155 deletions

View file

@ -3,92 +3,103 @@ package discovery
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/go-check/check"
) )
func TestNewEntry(t *testing.T) { // 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) {
entry, err := NewEntry("127.0.0.1:2375") entry, err := NewEntry("127.0.0.1:2375")
assert.NoError(t, err) c.Assert(err, check.IsNil)
assert.True(t, entry.Equals(&Entry{Host: "127.0.0.1", Port: "2375"})) c.Assert(entry.Equals(&Entry{Host: "127.0.0.1", Port: "2375"}), check.Equals, true)
assert.Equal(t, entry.String(), "127.0.0.1:2375") c.Assert(entry.String(), check.Equals, "127.0.0.1:2375")
_, err = NewEntry("127.0.0.1") _, err = NewEntry("127.0.0.1")
assert.Error(t, err) c.Assert(err, check.NotNil)
} }
func TestParse(t *testing.T) { func (s *DiscoverySuite) TestParse(c *check.C) {
scheme, uri := parse("127.0.0.1:2375") scheme, uri := parse("127.0.0.1:2375")
assert.Equal(t, scheme, "nodes") c.Assert(scheme, check.Equals, "nodes")
assert.Equal(t, uri, "127.0.0.1:2375") c.Assert(uri, check.Equals, "127.0.0.1:2375")
scheme, uri = parse("localhost:2375") scheme, uri = parse("localhost:2375")
assert.Equal(t, scheme, "nodes") c.Assert(scheme, check.Equals, "nodes")
assert.Equal(t, uri, "localhost:2375") c.Assert(uri, check.Equals, "localhost:2375")
scheme, uri = parse("scheme://127.0.0.1:2375") scheme, uri = parse("scheme://127.0.0.1:2375")
assert.Equal(t, scheme, "scheme") c.Assert(scheme, check.Equals, "scheme")
assert.Equal(t, uri, "127.0.0.1:2375") c.Assert(uri, check.Equals, "127.0.0.1:2375")
scheme, uri = parse("scheme://localhost:2375") scheme, uri = parse("scheme://localhost:2375")
assert.Equal(t, scheme, "scheme") c.Assert(scheme, check.Equals, "scheme")
assert.Equal(t, uri, "localhost:2375") c.Assert(uri, check.Equals, "localhost:2375")
scheme, uri = parse("") scheme, uri = parse("")
assert.Equal(t, scheme, "nodes") c.Assert(scheme, check.Equals, "nodes")
assert.Equal(t, uri, "") c.Assert(uri, check.Equals, "")
} }
func TestCreateEntries(t *testing.T) { func (s *DiscoverySuite) TestCreateEntries(c *check.C) {
entries, err := CreateEntries(nil) entries, err := CreateEntries(nil)
assert.Equal(t, entries, Entries{}) c.Assert(entries, check.DeepEquals, Entries{})
assert.NoError(t, err) 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", ""})
assert.NoError(t, err) c.Assert(err, check.IsNil)
expected := Entries{ expected := Entries{
&Entry{Host: "127.0.0.1", Port: "2375"}, &Entry{Host: "127.0.0.1", Port: "2375"},
&Entry{Host: "127.0.0.2", Port: "2375"}, &Entry{Host: "127.0.0.2", Port: "2375"},
} }
assert.True(t, entries.Equals(expected)) c.Assert(entries.Equals(expected), check.Equals, true)
_, err = CreateEntries([]string{"127.0.0.1", "127.0.0.2"}) _, err = CreateEntries([]string{"127.0.0.1", "127.0.0.2"})
assert.Error(t, err) c.Assert(err, check.NotNil)
} }
func TestContainsEntry(t *testing.T) { func (s *DiscoverySuite) TestContainsEntry(c *check.C) {
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", ""})
assert.NoError(t, err) c.Assert(err, check.IsNil)
assert.True(t, entries.Contains(&Entry{Host: "127.0.0.1", Port: "2375"})) c.Assert(entries.Contains(&Entry{Host: "127.0.0.1", Port: "2375"}), check.Equals, true)
assert.False(t, entries.Contains(&Entry{Host: "127.0.0.3", Port: "2375"})) c.Assert(entries.Contains(&Entry{Host: "127.0.0.3", Port: "2375"}), check.Equals, false)
} }
func TestEntriesEquality(t *testing.T) { func (s *DiscoverySuite) TestEntriesEquality(c *check.C) {
entries := Entries{ entries := Entries{
&Entry{Host: "127.0.0.1", Port: "2375"}, &Entry{Host: "127.0.0.1", Port: "2375"},
&Entry{Host: "127.0.0.2", Port: "2375"}, &Entry{Host: "127.0.0.2", Port: "2375"},
} }
// Same // Same
assert.True(t, entries.Equals(Entries{ c.Assert(entries.Equals(Entries{
&Entry{Host: "127.0.0.1", Port: "2375"}, &Entry{Host: "127.0.0.1", Port: "2375"},
&Entry{Host: "127.0.0.2", Port: "2375"}, &Entry{Host: "127.0.0.2", Port: "2375"},
})) }), check.
Equals, true)
// Different size // Different size
assert.False(t, entries.Equals(Entries{ c.Assert(entries.Equals(Entries{
&Entry{Host: "127.0.0.1", Port: "2375"}, &Entry{Host: "127.0.0.1", Port: "2375"},
&Entry{Host: "127.0.0.2", Port: "2375"}, &Entry{Host: "127.0.0.2", Port: "2375"},
&Entry{Host: "127.0.0.3", Port: "2375"}, &Entry{Host: "127.0.0.3", Port: "2375"},
})) }), check.
Equals, false)
// Different content // Different content
assert.False(t, entries.Equals(Entries{ c.Assert(entries.Equals(Entries{
&Entry{Host: "127.0.0.1", Port: "2375"}, &Entry{Host: "127.0.0.1", Port: "2375"},
&Entry{Host: "127.0.0.42", Port: "2375"}, &Entry{Host: "127.0.0.42", Port: "2375"},
})) }), check.
Equals, false)
} }
func TestEntriesDiff(t *testing.T) { func (s *DiscoverySuite) TestEntriesDiff(c *check.C) {
entry1 := &Entry{Host: "1.1.1.1", Port: "1111"} entry1 := &Entry{Host: "1.1.1.1", Port: "1111"}
entry2 := &Entry{Host: "2.2.2.2", Port: "2222"} entry2 := &Entry{Host: "2.2.2.2", Port: "2222"}
entry3 := &Entry{Host: "3.3.3.3", Port: "3333"} entry3 := &Entry{Host: "3.3.3.3", Port: "3333"}
@ -96,25 +107,25 @@ func TestEntriesDiff(t *testing.T) {
// No diff // No diff
added, removed := entries.Diff(Entries{entry2, entry1}) added, removed := entries.Diff(Entries{entry2, entry1})
assert.Empty(t, added) c.Assert(added, check.HasLen, 0)
assert.Empty(t, removed) c.Assert(removed, check.HasLen, 0)
// Add // Add
added, removed = entries.Diff(Entries{entry2, entry3, entry1}) added, removed = entries.Diff(Entries{entry2, entry3, entry1})
assert.Len(t, added, 1) c.Assert(added, check.HasLen, 1)
assert.True(t, added.Contains(entry3)) c.Assert(added.Contains(entry3), check.Equals, true)
assert.Empty(t, removed) c.Assert(removed, check.HasLen, 0)
// Remove // Remove
added, removed = entries.Diff(Entries{entry2}) added, removed = entries.Diff(Entries{entry2})
assert.Empty(t, added) c.Assert(added, check.HasLen, 0)
assert.Len(t, removed, 1) c.Assert(removed, check.HasLen, 1)
assert.True(t, removed.Contains(entry1)) c.Assert(removed.Contains(entry1), check.Equals, true)
// Add and remove // Add and remove
added, removed = entries.Diff(Entries{entry1, entry3}) added, removed = entries.Diff(Entries{entry1, entry3})
assert.Len(t, added, 1) c.Assert(added, check.HasLen, 1)
assert.True(t, added.Contains(entry3)) c.Assert(added.Contains(entry3), check.Equals, true)
assert.Len(t, removed, 1) c.Assert(removed, check.HasLen, 1)
assert.True(t, removed.Contains(entry2)) c.Assert(removed.Contains(entry2), check.Equals, true)
} }

View file

@ -6,41 +6,49 @@ import (
"testing" "testing"
"github.com/docker/docker/pkg/discovery" "github.com/docker/docker/pkg/discovery"
"github.com/stretchr/testify/assert"
"github.com/go-check/check"
) )
func TestInitialize(t *testing.T) { // 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) TestInitialize(c *check.C) {
d := &Discovery{} d := &Discovery{}
d.Initialize("/path/to/file", 1000, 0) d.Initialize("/path/to/file", 1000, 0)
assert.Equal(t, d.path, "/path/to/file") c.Assert(d.path, check.Equals, "/path/to/file")
} }
func TestNew(t *testing.T) { func (s *DiscoverySuite) TestNew(c *check.C) {
d, err := discovery.New("file:///path/to/file", 0, 0) d, err := discovery.New("file:///path/to/file", 0, 0)
assert.NoError(t, err) c.Assert(err, check.IsNil)
assert.Equal(t, d.(*Discovery).path, "/path/to/file") c.Assert(d.(*Discovery).path, check.Equals, "/path/to/file")
} }
func TestContent(t *testing.T) { func (s *DiscoverySuite) TestContent(c *check.C) {
data := ` data := `
1.1.1.[1:2]:1111 1.1.1.[1:2]:1111
2.2.2.[2:4]:2222 2.2.2.[2:4]:2222
` `
ips := parseFileContent([]byte(data)) ips := parseFileContent([]byte(data))
assert.Len(t, ips, 5) c.Assert(ips, check.HasLen, 5)
assert.Equal(t, ips[0], "1.1.1.1:1111") c.Assert(ips[0], check.Equals, "1.1.1.1:1111")
assert.Equal(t, ips[1], "1.1.1.2:1111") c.Assert(ips[1], check.Equals, "1.1.1.2:1111")
assert.Equal(t, ips[2], "2.2.2.2:2222") c.Assert(ips[2], check.Equals, "2.2.2.2:2222")
assert.Equal(t, ips[3], "2.2.2.3:2222") c.Assert(ips[3], check.Equals, "2.2.2.3:2222")
assert.Equal(t, ips[4], "2.2.2.4:2222") c.Assert(ips[4], check.Equals, "2.2.2.4:2222")
} }
func TestRegister(t *testing.T) { func (s *DiscoverySuite) TestRegister(c *check.C) {
discovery := &Discovery{path: "/path/to/file"} discovery := &Discovery{path: "/path/to/file"}
assert.Error(t, discovery.Register("0.0.0.0")) c.Assert(discovery.Register("0.0.0.0"), check.NotNil)
} }
func TestParsingContentsWithComments(t *testing.T) { func (s *DiscoverySuite) TestParsingContentsWithComments(c *check.C) {
data := ` data := `
### test ### ### test ###
1.1.1.1:1111 # inline comment 1.1.1.1:1111 # inline comment
@ -50,12 +58,12 @@ func TestParsingContentsWithComments(t *testing.T) {
### test ### ### test ###
` `
ips := parseFileContent([]byte(data)) ips := parseFileContent([]byte(data))
assert.Len(t, ips, 2) c.Assert(ips, check.HasLen, 2)
assert.Equal(t, "1.1.1.1:1111", ips[0]) c.Assert("1.1.1.1:1111", check.Equals, ips[0])
assert.Equal(t, "3.3.3.3:3333", ips[1]) c.Assert("3.3.3.3:3333", check.Equals, ips[1])
} }
func TestWatch(t *testing.T) { func (s *DiscoverySuite) TestWatch(c *check.C) {
data := ` data := `
1.1.1.1:1111 1.1.1.1:1111
2.2.2.2:2222 2.2.2.2:2222
@ -67,9 +75,9 @@ func TestWatch(t *testing.T) {
// Create a temporary file and remove it. // Create a temporary file and remove it.
tmp, err := ioutil.TempFile(os.TempDir(), "discovery-file-test") tmp, err := ioutil.TempFile(os.TempDir(), "discovery-file-test")
assert.NoError(t, err) c.Assert(err, check.IsNil)
assert.NoError(t, tmp.Close()) c.Assert(tmp.Close(), check.IsNil)
assert.NoError(t, os.Remove(tmp.Name())) c.Assert(os.Remove(tmp.Name()), check.IsNil)
// Set up file discovery. // Set up file discovery.
d := &Discovery{} d := &Discovery{}
@ -78,7 +86,7 @@ func TestWatch(t *testing.T) {
ch, errCh := d.Watch(stopCh) ch, errCh := d.Watch(stopCh)
// Make sure it fires errors since the file doesn't exist. // Make sure it fires errors since the file doesn't exist.
assert.Error(t, <-errCh) c.Assert(<-errCh, check.NotNil)
// We have to drain the error channel otherwise Watch will get stuck. // We have to drain the error channel otherwise Watch will get stuck.
go func() { go func() {
for range errCh { for range errCh {
@ -86,21 +94,21 @@ func TestWatch(t *testing.T) {
}() }()
// Write the file and make sure we get the expected value back. // Write the file and make sure we get the expected value back.
assert.NoError(t, ioutil.WriteFile(tmp.Name(), []byte(data), 0600)) c.Assert(ioutil.WriteFile(tmp.Name(), []byte(data), 0600), check.IsNil)
assert.Equal(t, expected, <-ch) c.Assert(<-ch, check.DeepEquals, expected)
// Add a new entry and look it up. // Add a new entry and look it up.
expected = append(expected, &discovery.Entry{Host: "3.3.3.3", Port: "3333"}) expected = append(expected, &discovery.Entry{Host: "3.3.3.3", Port: "3333"})
f, err := os.OpenFile(tmp.Name(), os.O_APPEND|os.O_WRONLY, 0600) f, err := os.OpenFile(tmp.Name(), os.O_APPEND|os.O_WRONLY, 0600)
assert.NoError(t, err) c.Assert(err, check.IsNil)
assert.NotNil(t, f) c.Assert(f, check.NotNil)
_, err = f.WriteString("\n3.3.3.3:3333\n") _, err = f.WriteString("\n3.3.3.3:3333\n")
assert.NoError(t, err) c.Assert(err, check.IsNil)
f.Close() f.Close()
assert.Equal(t, expected, <-ch) c.Assert(<-ch, check.DeepEquals, expected)
// Stop and make sure it closes all channels. // Stop and make sure it closes all channels.
close(stopCh) close(stopCh)
assert.Nil(t, <-ch) c.Assert(<-ch, check.IsNil)
assert.Nil(t, <-errCh) c.Assert(<-errCh, check.IsNil)
} }

View file

@ -1,55 +1,53 @@
package discovery package discovery
import ( import (
"testing" "github.com/go-check/check"
"github.com/stretchr/testify/assert"
) )
func TestGeneratorNotGenerate(t *testing.T) { func (s *DiscoverySuite) TestGeneratorNotGenerate(c *check.C) {
ips := Generate("127.0.0.1") ips := Generate("127.0.0.1")
assert.Equal(t, len(ips), 1) c.Assert(len(ips), check.Equals, 1)
assert.Equal(t, ips[0], "127.0.0.1") c.Assert(ips[0], check.Equals, "127.0.0.1")
} }
func TestGeneratorWithPortNotGenerate(t *testing.T) { func (s *DiscoverySuite) TestGeneratorWithPortNotGenerate(c *check.C) {
ips := Generate("127.0.0.1:8080") ips := Generate("127.0.0.1:8080")
assert.Equal(t, len(ips), 1) c.Assert(len(ips), check.Equals, 1)
assert.Equal(t, ips[0], "127.0.0.1:8080") c.Assert(ips[0], check.Equals, "127.0.0.1:8080")
} }
func TestGeneratorMatchFailedNotGenerate(t *testing.T) { func (s *DiscoverySuite) TestGeneratorMatchFailedNotGenerate(c *check.C) {
ips := Generate("127.0.0.[1]") ips := Generate("127.0.0.[1]")
assert.Equal(t, len(ips), 1) c.Assert(len(ips), check.Equals, 1)
assert.Equal(t, ips[0], "127.0.0.[1]") c.Assert(ips[0], check.Equals, "127.0.0.[1]")
} }
func TestGeneratorWithPort(t *testing.T) { func (s *DiscoverySuite) TestGeneratorWithPort(c *check.C) {
ips := Generate("127.0.0.[1:11]:2375") ips := Generate("127.0.0.[1:11]:2375")
assert.Equal(t, len(ips), 11) c.Assert(len(ips), check.Equals, 11)
assert.Equal(t, ips[0], "127.0.0.1:2375") c.Assert(ips[0], check.Equals, "127.0.0.1:2375")
assert.Equal(t, ips[1], "127.0.0.2:2375") c.Assert(ips[1], check.Equals, "127.0.0.2:2375")
assert.Equal(t, ips[2], "127.0.0.3:2375") c.Assert(ips[2], check.Equals, "127.0.0.3:2375")
assert.Equal(t, ips[3], "127.0.0.4:2375") c.Assert(ips[3], check.Equals, "127.0.0.4:2375")
assert.Equal(t, ips[4], "127.0.0.5:2375") c.Assert(ips[4], check.Equals, "127.0.0.5:2375")
assert.Equal(t, ips[5], "127.0.0.6:2375") c.Assert(ips[5], check.Equals, "127.0.0.6:2375")
assert.Equal(t, ips[6], "127.0.0.7:2375") c.Assert(ips[6], check.Equals, "127.0.0.7:2375")
assert.Equal(t, ips[7], "127.0.0.8:2375") c.Assert(ips[7], check.Equals, "127.0.0.8:2375")
assert.Equal(t, ips[8], "127.0.0.9:2375") c.Assert(ips[8], check.Equals, "127.0.0.9:2375")
assert.Equal(t, ips[9], "127.0.0.10:2375") c.Assert(ips[9], check.Equals, "127.0.0.10:2375")
assert.Equal(t, ips[10], "127.0.0.11:2375") c.Assert(ips[10], check.Equals, "127.0.0.11:2375")
} }
func TestGenerateWithMalformedInputAtRangeStart(t *testing.T) { func (s *DiscoverySuite) TestGenerateWithMalformedInputAtRangeStart(c *check.C) {
malformedInput := "127.0.0.[x:11]:2375" malformedInput := "127.0.0.[x:11]:2375"
ips := Generate(malformedInput) ips := Generate(malformedInput)
assert.Equal(t, len(ips), 1) c.Assert(len(ips), check.Equals, 1)
assert.Equal(t, ips[0], malformedInput) c.Assert(ips[0], check.Equals, malformedInput)
} }
func TestGenerateWithMalformedInputAtRangeEnd(t *testing.T) { func (s *DiscoverySuite) TestGenerateWithMalformedInputAtRangeEnd(c *check.C) {
malformedInput := "127.0.0.[1:x]:2375" malformedInput := "127.0.0.[1:x]:2375"
ips := Generate(malformedInput) ips := Generate(malformedInput)
assert.Equal(t, len(ips), 1) c.Assert(len(ips), check.Equals, 1)
assert.Equal(t, ips[0], malformedInput) c.Assert(ips[0], check.Equals, malformedInput)
} }

View file

@ -9,58 +9,66 @@ import (
"github.com/docker/docker/pkg/discovery" "github.com/docker/docker/pkg/discovery"
"github.com/docker/libkv/store" "github.com/docker/libkv/store"
libkvmock "github.com/docker/libkv/store/mock" libkvmock "github.com/docker/libkv/store/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
"github.com/go-check/check"
) )
func TestInitialize(t *testing.T) { // Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { check.TestingT(t) }
type DiscoverySuite struct{}
var _ = check.Suite(&DiscoverySuite{})
func (ds *DiscoverySuite) TestInitialize(c *check.C) {
storeMock, err := libkvmock.New([]string{"127.0.0.1"}, nil) storeMock, err := libkvmock.New([]string{"127.0.0.1"}, nil)
assert.NotNil(t, storeMock) c.Assert(storeMock, check.NotNil)
assert.NoError(t, err) c.Assert(err, check.IsNil)
d := &Discovery{backend: store.CONSUL} d := &Discovery{backend: store.CONSUL}
d.Initialize("127.0.0.1", 0, 0) d.Initialize("127.0.0.1", 0, 0)
d.store = storeMock d.store = storeMock
s := d.store.(*libkvmock.Mock) s := d.store.(*libkvmock.Mock)
assert.Len(t, s.Endpoints, 1) c.Assert(s.Endpoints, check.HasLen, 1)
assert.Equal(t, s.Endpoints[0], "127.0.0.1") c.Assert(s.Endpoints[0], check.Equals, "127.0.0.1")
assert.Equal(t, d.path, discoveryPath) c.Assert(d.path, check.Equals, discoveryPath)
storeMock, err = libkvmock.New([]string{"127.0.0.1:1234"}, nil) storeMock, err = libkvmock.New([]string{"127.0.0.1:1234"}, nil)
assert.NotNil(t, storeMock) c.Assert(storeMock, check.NotNil)
assert.NoError(t, err) c.Assert(err, check.IsNil)
d = &Discovery{backend: store.CONSUL} d = &Discovery{backend: store.CONSUL}
d.Initialize("127.0.0.1:1234/path", 0, 0) d.Initialize("127.0.0.1:1234/path", 0, 0)
d.store = storeMock d.store = storeMock
s = d.store.(*libkvmock.Mock) s = d.store.(*libkvmock.Mock)
assert.Len(t, s.Endpoints, 1) c.Assert(s.Endpoints, check.HasLen, 1)
assert.Equal(t, s.Endpoints[0], "127.0.0.1:1234") c.Assert(s.Endpoints[0], check.Equals, "127.0.0.1:1234")
assert.Equal(t, d.path, "path/"+discoveryPath) c.Assert(d.path, check.Equals, "path/"+discoveryPath)
storeMock, err = libkvmock.New([]string{"127.0.0.1:1234", "127.0.0.2:1234", "127.0.0.3:1234"}, nil) storeMock, err = libkvmock.New([]string{"127.0.0.1:1234", "127.0.0.2:1234", "127.0.0.3:1234"}, nil)
assert.NotNil(t, storeMock) c.Assert(storeMock, check.NotNil)
assert.NoError(t, err) c.Assert(err, check.IsNil)
d = &Discovery{backend: store.CONSUL} d = &Discovery{backend: store.CONSUL}
d.Initialize("127.0.0.1:1234,127.0.0.2:1234,127.0.0.3:1234/path", 0, 0) d.Initialize("127.0.0.1:1234,127.0.0.2:1234,127.0.0.3:1234/path", 0, 0)
d.store = storeMock d.store = storeMock
s = d.store.(*libkvmock.Mock) s = d.store.(*libkvmock.Mock)
if assert.Len(t, s.Endpoints, 3) { c.Assert(s.Endpoints, check.HasLen, 3)
assert.Equal(t, s.Endpoints[0], "127.0.0.1:1234") c.Assert(s.Endpoints[0], check.Equals, "127.0.0.1:1234")
assert.Equal(t, s.Endpoints[1], "127.0.0.2:1234") c.Assert(s.Endpoints[1], check.Equals, "127.0.0.2:1234")
assert.Equal(t, s.Endpoints[2], "127.0.0.3:1234") c.Assert(s.Endpoints[2], check.Equals, "127.0.0.3:1234")
}
assert.Equal(t, d.path, "path/"+discoveryPath) c.Assert(d.path, check.Equals, "path/"+discoveryPath)
} }
func TestWatch(t *testing.T) { func (ds *DiscoverySuite) TestWatch(c *check.C) {
storeMock, err := libkvmock.New([]string{"127.0.0.1:1234"}, nil) storeMock, err := libkvmock.New([]string{"127.0.0.1:1234"}, nil)
assert.NotNil(t, storeMock) c.Assert(storeMock, check.NotNil)
assert.NoError(t, err) c.Assert(err, check.IsNil)
d := &Discovery{backend: store.CONSUL} d := &Discovery{backend: store.CONSUL}
d.Initialize("127.0.0.1:1234/path", 0, 0) d.Initialize("127.0.0.1:1234/path", 0, 0)
@ -86,7 +94,7 @@ func TestWatch(t *testing.T) {
ch, errCh := d.Watch(stopCh) ch, errCh := d.Watch(stopCh)
// It should fire an error since the first WatchTree call failed. // It should fire an error since the first WatchTree call failed.
assert.EqualError(t, <-errCh, "test error") c.Assert(<-errCh, check.ErrorMatches, "test error")
// We have to drain the error channel otherwise Watch will get stuck. // We have to drain the error channel otherwise Watch will get stuck.
go func() { go func() {
for range errCh { for range errCh {
@ -95,13 +103,13 @@ func TestWatch(t *testing.T) {
// Push the entries into the store channel and make sure discovery emits. // Push the entries into the store channel and make sure discovery emits.
mockCh <- kvs mockCh <- kvs
assert.Equal(t, <-ch, expected) c.Assert(<-ch, check.DeepEquals, expected)
// Add a new entry. // Add a new entry.
expected = append(expected, &discovery.Entry{Host: "3.3.3.3", Port: "3333"}) expected = append(expected, &discovery.Entry{Host: "3.3.3.3", Port: "3333"})
kvs = append(kvs, &store.KVPair{Key: path.Join("path", discoveryPath, "3.3.3.3"), Value: []byte("3.3.3.3:3333")}) kvs = append(kvs, &store.KVPair{Key: path.Join("path", discoveryPath, "3.3.3.3"), Value: []byte("3.3.3.3:3333")})
mockCh <- kvs mockCh <- kvs
assert.Equal(t, <-ch, expected) c.Assert(<-ch, check.DeepEquals, expected)
// Make sure that if an error occurs it retries. // Make sure that if an error occurs it retries.
// This third call to WatchTree will be checked later by AssertExpectations. // This third call to WatchTree will be checked later by AssertExpectations.
@ -112,8 +120,8 @@ func TestWatch(t *testing.T) {
// Stop and make sure it closes all channels. // Stop and make sure it closes all channels.
close(stopCh) close(stopCh)
assert.Nil(t, <-ch) c.Assert(<-ch, check.IsNil)
assert.Nil(t, <-errCh) c.Assert(<-errCh, check.IsNil)
s.AssertExpectations(t) s.AssertExpectations(c)
} }

View file

@ -4,29 +4,37 @@ import (
"testing" "testing"
"github.com/docker/docker/pkg/discovery" "github.com/docker/docker/pkg/discovery"
"github.com/stretchr/testify/assert"
"github.com/go-check/check"
) )
func TestInitialize(t *testing.T) { // 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) TestInitialize(c *check.C) {
d := &Discovery{} d := &Discovery{}
d.Initialize("1.1.1.1:1111,2.2.2.2:2222", 0, 0) d.Initialize("1.1.1.1:1111,2.2.2.2:2222", 0, 0)
assert.Equal(t, len(d.entries), 2) c.Assert(len(d.entries), check.Equals, 2)
assert.Equal(t, d.entries[0].String(), "1.1.1.1:1111") c.Assert(d.entries[0].String(), check.Equals, "1.1.1.1:1111")
assert.Equal(t, d.entries[1].String(), "2.2.2.2:2222") c.Assert(d.entries[1].String(), check.Equals, "2.2.2.2:2222")
} }
func TestInitializeWithPattern(t *testing.T) { func (s *DiscoverySuite) TestInitializeWithPattern(c *check.C) {
d := &Discovery{} d := &Discovery{}
d.Initialize("1.1.1.[1:2]:1111,2.2.2.[2:4]:2222", 0, 0) d.Initialize("1.1.1.[1:2]:1111,2.2.2.[2:4]:2222", 0, 0)
assert.Equal(t, len(d.entries), 5) c.Assert(len(d.entries), check.Equals, 5)
assert.Equal(t, d.entries[0].String(), "1.1.1.1:1111") c.Assert(d.entries[0].String(), check.Equals, "1.1.1.1:1111")
assert.Equal(t, d.entries[1].String(), "1.1.1.2:1111") c.Assert(d.entries[1].String(), check.Equals, "1.1.1.2:1111")
assert.Equal(t, d.entries[2].String(), "2.2.2.2:2222") c.Assert(d.entries[2].String(), check.Equals, "2.2.2.2:2222")
assert.Equal(t, d.entries[3].String(), "2.2.2.3:2222") c.Assert(d.entries[3].String(), check.Equals, "2.2.2.3:2222")
assert.Equal(t, d.entries[4].String(), "2.2.2.4:2222") c.Assert(d.entries[4].String(), check.Equals, "2.2.2.4:2222")
} }
func TestWatch(t *testing.T) { func (s *DiscoverySuite) TestWatch(c *check.C) {
d := &Discovery{} d := &Discovery{}
d.Initialize("1.1.1.1:1111,2.2.2.2:2222", 0, 0) d.Initialize("1.1.1.1:1111,2.2.2.2:2222", 0, 0)
expected := discovery.Entries{ expected := discovery.Entries{
@ -34,10 +42,10 @@ func TestWatch(t *testing.T) {
&discovery.Entry{Host: "2.2.2.2", Port: "2222"}, &discovery.Entry{Host: "2.2.2.2", Port: "2222"},
} }
ch, _ := d.Watch(nil) ch, _ := d.Watch(nil)
assert.True(t, expected.Equals(<-ch)) c.Assert(expected.Equals(<-ch), check.Equals, true)
} }
func TestRegister(t *testing.T) { func (s *DiscoverySuite) TestRegister(c *check.C) {
d := &Discovery{} d := &Discovery{}
assert.Error(t, d.Register("0.0.0.0")) c.Assert(d.Register("0.0.0.0"), check.NotNil)
} }