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

@ -6,41 +6,49 @@ import (
"testing"
"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.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)
assert.NoError(t, err)
assert.Equal(t, d.(*Discovery).path, "/path/to/file")
c.Assert(err, check.IsNil)
c.Assert(d.(*Discovery).path, check.Equals, "/path/to/file")
}
func TestContent(t *testing.T) {
func (s *DiscoverySuite) TestContent(c *check.C) {
data := `
1.1.1.[1:2]:1111
2.2.2.[2:4]:2222
`
ips := parseFileContent([]byte(data))
assert.Len(t, ips, 5)
assert.Equal(t, ips[0], "1.1.1.1:1111")
assert.Equal(t, ips[1], "1.1.1.2:1111")
assert.Equal(t, ips[2], "2.2.2.2:2222")
assert.Equal(t, ips[3], "2.2.2.3:2222")
assert.Equal(t, ips[4], "2.2.2.4:2222")
c.Assert(ips, check.HasLen, 5)
c.Assert(ips[0], check.Equals, "1.1.1.1:1111")
c.Assert(ips[1], check.Equals, "1.1.1.2:1111")
c.Assert(ips[2], check.Equals, "2.2.2.2:2222")
c.Assert(ips[3], check.Equals, "2.2.2.3: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"}
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 := `
### test ###
1.1.1.1:1111 # inline comment
@ -50,12 +58,12 @@ func TestParsingContentsWithComments(t *testing.T) {
### test ###
`
ips := parseFileContent([]byte(data))
assert.Len(t, ips, 2)
assert.Equal(t, "1.1.1.1:1111", ips[0])
assert.Equal(t, "3.3.3.3:3333", ips[1])
c.Assert(ips, check.HasLen, 2)
c.Assert("1.1.1.1:1111", check.Equals, ips[0])
c.Assert("3.3.3.3:3333", check.Equals, ips[1])
}
func TestWatch(t *testing.T) {
func (s *DiscoverySuite) TestWatch(c *check.C) {
data := `
1.1.1.1:1111
2.2.2.2:2222
@ -67,9 +75,9 @@ func TestWatch(t *testing.T) {
// Create a temporary file and remove it.
tmp, err := ioutil.TempFile(os.TempDir(), "discovery-file-test")
assert.NoError(t, err)
assert.NoError(t, tmp.Close())
assert.NoError(t, os.Remove(tmp.Name()))
c.Assert(err, check.IsNil)
c.Assert(tmp.Close(), check.IsNil)
c.Assert(os.Remove(tmp.Name()), check.IsNil)
// Set up file discovery.
d := &Discovery{}
@ -78,7 +86,7 @@ func TestWatch(t *testing.T) {
ch, errCh := d.Watch(stopCh)
// 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.
go func() {
for range errCh {
@ -86,21 +94,21 @@ func TestWatch(t *testing.T) {
}()
// Write the file and make sure we get the expected value back.
assert.NoError(t, ioutil.WriteFile(tmp.Name(), []byte(data), 0600))
assert.Equal(t, expected, <-ch)
c.Assert(ioutil.WriteFile(tmp.Name(), []byte(data), 0600), check.IsNil)
c.Assert(<-ch, check.DeepEquals, expected)
// Add a new entry and look it up.
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)
assert.NoError(t, err)
assert.NotNil(t, f)
c.Assert(err, check.IsNil)
c.Assert(f, check.NotNil)
_, err = f.WriteString("\n3.3.3.3:3333\n")
assert.NoError(t, err)
c.Assert(err, check.IsNil)
f.Close()
assert.Equal(t, expected, <-ch)
c.Assert(<-ch, check.DeepEquals, expected)
// Stop and make sure it closes all channels.
close(stopCh)
assert.Nil(t, <-ch)
assert.Nil(t, <-errCh)
c.Assert(<-ch, check.IsNil)
c.Assert(<-errCh, check.IsNil)
}