Merge pull request #665 from cyli/changelist-assert-require

Change assert to require in client/changelist
This commit is contained in:
Ying Li 2016-04-05 15:59:05 -07:00
commit 6e84b596ca
3 changed files with 76 additions and 76 deletions

View File

@ -5,13 +5,13 @@ import (
"github.com/docker/notary/tuf/data" "github.com/docker/notary/tuf/data"
"github.com/docker/notary/tuf/signed" "github.com/docker/notary/tuf/signed"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
) )
func TestTufDelegation(t *testing.T) { func TestTufDelegation(t *testing.T) {
cs := signed.NewEd25519() cs := signed.NewEd25519()
key, err := cs.Create("targets/new_name", "gun", data.ED25519Key) key, err := cs.Create("targets/new_name", "gun", data.ED25519Key)
assert.NoError(t, err) require.NoError(t, err)
kl := data.KeyList{key} kl := data.KeyList{key}
td := TufDelegation{ td := TufDelegation{
NewName: "targets/new_name", NewName: "targets/new_name",
@ -21,9 +21,9 @@ func TestTufDelegation(t *testing.T) {
} }
r, err := td.ToNewRole("targets/old_name") r, err := td.ToNewRole("targets/old_name")
assert.NoError(t, err) require.NoError(t, err)
assert.Equal(t, td.NewName, r.Name) require.Equal(t, td.NewName, r.Name)
assert.Len(t, r.KeyIDs, 1) require.Len(t, r.KeyIDs, 1)
assert.Equal(t, kl[0].ID(), r.KeyIDs[0]) require.Equal(t, kl[0].ID(), r.KeyIDs[0])
assert.Len(t, r.Paths, 1) require.Len(t, r.Paths, 1)
} }

View File

@ -3,7 +3,7 @@ package changelist
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
) )
func TestMemChangelist(t *testing.T) { func TestMemChangelist(t *testing.T) {
@ -12,29 +12,29 @@ func TestMemChangelist(t *testing.T) {
c := NewTufChange(ActionCreate, "targets", "target", "test/targ", []byte{1}) c := NewTufChange(ActionCreate, "targets", "target", "test/targ", []byte{1})
err := cl.Add(c) err := cl.Add(c)
assert.Nil(t, err, "Non-nil error while adding change") require.Nil(t, err, "Non-nil error while adding change")
cs := cl.List() cs := cl.List()
assert.Equal(t, 1, len(cs), "List should have returned exactly one item") require.Equal(t, 1, len(cs), "List should have returned exactly one item")
assert.Equal(t, c.Action(), cs[0].Action(), "Action mismatch") require.Equal(t, c.Action(), cs[0].Action(), "Action mismatch")
assert.Equal(t, c.Scope(), cs[0].Scope(), "Scope mismatch") require.Equal(t, c.Scope(), cs[0].Scope(), "Scope mismatch")
assert.Equal(t, c.Type(), cs[0].Type(), "Type mismatch") require.Equal(t, c.Type(), cs[0].Type(), "Type mismatch")
assert.Equal(t, c.Path(), cs[0].Path(), "Path mismatch") require.Equal(t, c.Path(), cs[0].Path(), "Path mismatch")
assert.Equal(t, c.Content(), cs[0].Content(), "Content mismatch") require.Equal(t, c.Content(), cs[0].Content(), "Content mismatch")
err = cl.Clear("") err = cl.Clear("")
assert.Nil(t, err, "Non-nil error while clearing") require.Nil(t, err, "Non-nil error while clearing")
cs = cl.List() cs = cl.List()
assert.Equal(t, 0, len(cs), "List should be empty") require.Equal(t, 0, len(cs), "List should be empty")
} }
func TestMemChangeIterator(t *testing.T) { func TestMemChangeIterator(t *testing.T) {
cl := memChangelist{} cl := memChangelist{}
it, err := cl.NewIterator() it, err := cl.NewIterator()
assert.Nil(t, err, "Non-nil error from NewIterator") require.Nil(t, err, "Non-nil error from NewIterator")
assert.False(t, it.HasNext(), "HasNext returns false for empty ChangeList") require.False(t, it.HasNext(), "HasNext returns false for empty ChangeList")
c1 := NewTufChange(ActionCreate, "t1", "target1", "test/targ1", []byte{1}) c1 := NewTufChange(ActionCreate, "t1", "target1", "test/targ1", []byte{1})
cl.Add(c1) cl.Add(c1)
@ -50,17 +50,17 @@ func TestMemChangeIterator(t *testing.T) {
it, _ = cl.NewIterator() it, _ = cl.NewIterator()
for it.HasNext() { for it.HasNext() {
c, err := it.Next() c, err := it.Next()
assert.Nil(t, err, "Next err should be false") require.Nil(t, err, "Next err should be false")
assert.Equal(t, c.Action(), cs[index].Action(), "Action mismatch") require.Equal(t, c.Action(), cs[index].Action(), "Action mismatch")
assert.Equal(t, c.Scope(), cs[index].Scope(), "Scope mismatch") require.Equal(t, c.Scope(), cs[index].Scope(), "Scope mismatch")
assert.Equal(t, c.Type(), cs[index].Type(), "Type mismatch") require.Equal(t, c.Type(), cs[index].Type(), "Type mismatch")
assert.Equal(t, c.Path(), cs[index].Path(), "Path mismatch") require.Equal(t, c.Path(), cs[index].Path(), "Path mismatch")
assert.Equal(t, c.Content(), cs[index].Content(), "Content mismatch") require.Equal(t, c.Content(), cs[index].Content(), "Content mismatch")
index++ index++
} }
assert.Equal(t, index, len(cs), "Iterator produced all data in ChangeList") require.Equal(t, index, len(cs), "Iterator produced all data in ChangeList")
_, err = it.Next() _, err = it.Next()
assert.NotNil(t, err, "Next errors gracefully when exhausted") require.NotNil(t, err, "Next errors gracefully when exhausted")
var iterError IteratorBoundsError var iterError IteratorBoundsError
assert.IsType(t, iterError, err, "IteratorBoundsError type") require.IsType(t, iterError, err, "IteratorBoundsError type")
} }

View File

@ -6,7 +6,7 @@ import (
"path" "path"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
) )
func TestAdd(t *testing.T) { func TestAdd(t *testing.T) {
@ -17,29 +17,29 @@ func TestAdd(t *testing.T) {
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
cl, err := NewFileChangelist(tmpDir) cl, err := NewFileChangelist(tmpDir)
assert.Nil(t, err, "Error initializing fileChangelist") require.Nil(t, err, "Error initializing fileChangelist")
c := NewTufChange(ActionCreate, "targets", "target", "test/targ", []byte{1}) c := NewTufChange(ActionCreate, "targets", "target", "test/targ", []byte{1})
err = cl.Add(c) err = cl.Add(c)
assert.Nil(t, err, "Non-nil error while adding change") require.Nil(t, err, "Non-nil error while adding change")
cs := cl.List() cs := cl.List()
assert.Equal(t, 1, len(cs), "List should have returned exactly one item") require.Equal(t, 1, len(cs), "List should have returned exactly one item")
assert.Equal(t, c.Action(), cs[0].Action(), "Action mismatch") require.Equal(t, c.Action(), cs[0].Action(), "Action mismatch")
assert.Equal(t, c.Scope(), cs[0].Scope(), "Scope mismatch") require.Equal(t, c.Scope(), cs[0].Scope(), "Scope mismatch")
assert.Equal(t, c.Type(), cs[0].Type(), "Type mismatch") require.Equal(t, c.Type(), cs[0].Type(), "Type mismatch")
assert.Equal(t, c.Path(), cs[0].Path(), "Path mismatch") require.Equal(t, c.Path(), cs[0].Path(), "Path mismatch")
assert.Equal(t, c.Content(), cs[0].Content(), "Content mismatch") require.Equal(t, c.Content(), cs[0].Content(), "Content mismatch")
err = cl.Clear("") err = cl.Clear("")
assert.Nil(t, err, "Non-nil error while clearing") require.Nil(t, err, "Non-nil error while clearing")
cs = cl.List() cs = cl.List()
assert.Equal(t, 0, len(cs), "List should be empty") require.Equal(t, 0, len(cs), "List should be empty")
err = os.Remove(tmpDir) // will error if anything left in dir err = os.Remove(tmpDir) // will error if anything left in dir
assert.Nil(t, err, "Clear should have left the tmpDir empty") require.Nil(t, err, "Clear should have left the tmpDir empty")
} }
func TestErrorConditions(t *testing.T) { func TestErrorConditions(t *testing.T) {
@ -53,14 +53,14 @@ func TestErrorConditions(t *testing.T) {
// Attempt to unmarshall a bad JSON file. Note: causes a WARN on the console. // Attempt to unmarshall a bad JSON file. Note: causes a WARN on the console.
ioutil.WriteFile(path.Join(tmpDir, "broken_file.change"), []byte{5}, 0644) ioutil.WriteFile(path.Join(tmpDir, "broken_file.change"), []byte{5}, 0644)
noItems := cl.List() noItems := cl.List()
assert.Len(t, noItems, 0, "List returns zero items on bad JSON file error") require.Len(t, noItems, 0, "List returns zero items on bad JSON file error")
os.RemoveAll(tmpDir) os.RemoveAll(tmpDir)
err = cl.Clear("") err = cl.Clear("")
assert.Error(t, err, "Clear on missing change list should return err") require.Error(t, err, "Clear on missing change list should return err")
noItems = cl.List() noItems = cl.List()
assert.Len(t, noItems, 0, "List returns zero items on directory read error") require.Len(t, noItems, 0, "List returns zero items on directory read error")
} }
func TestListOrder(t *testing.T) { func TestListOrder(t *testing.T) {
@ -71,30 +71,30 @@ func TestListOrder(t *testing.T) {
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
cl, err := NewFileChangelist(tmpDir) cl, err := NewFileChangelist(tmpDir)
assert.Nil(t, err, "Error initializing fileChangelist") require.Nil(t, err, "Error initializing fileChangelist")
c1 := NewTufChange(ActionCreate, "targets", "target", "test/targ1", []byte{1}) c1 := NewTufChange(ActionCreate, "targets", "target", "test/targ1", []byte{1})
err = cl.Add(c1) err = cl.Add(c1)
assert.Nil(t, err, "Non-nil error while adding change") require.Nil(t, err, "Non-nil error while adding change")
c2 := NewTufChange(ActionCreate, "targets", "target", "test/targ2", []byte{1}) c2 := NewTufChange(ActionCreate, "targets", "target", "test/targ2", []byte{1})
err = cl.Add(c2) err = cl.Add(c2)
assert.Nil(t, err, "Non-nil error while adding change") require.Nil(t, err, "Non-nil error while adding change")
cs := cl.List() cs := cl.List()
assert.Equal(t, 2, len(cs), "List should have returned exactly one item") require.Equal(t, 2, len(cs), "List should have returned exactly one item")
assert.Equal(t, c1.Action(), cs[0].Action(), "Action mismatch") require.Equal(t, c1.Action(), cs[0].Action(), "Action mismatch")
assert.Equal(t, c1.Scope(), cs[0].Scope(), "Scope mismatch") require.Equal(t, c1.Scope(), cs[0].Scope(), "Scope mismatch")
assert.Equal(t, c1.Type(), cs[0].Type(), "Type mismatch") require.Equal(t, c1.Type(), cs[0].Type(), "Type mismatch")
assert.Equal(t, c1.Path(), cs[0].Path(), "Path mismatch") require.Equal(t, c1.Path(), cs[0].Path(), "Path mismatch")
assert.Equal(t, c1.Content(), cs[0].Content(), "Content mismatch") require.Equal(t, c1.Content(), cs[0].Content(), "Content mismatch")
assert.Equal(t, c2.Action(), cs[1].Action(), "Action 2 mismatch") require.Equal(t, c2.Action(), cs[1].Action(), "Action 2 mismatch")
assert.Equal(t, c2.Scope(), cs[1].Scope(), "Scope 2 mismatch") require.Equal(t, c2.Scope(), cs[1].Scope(), "Scope 2 mismatch")
assert.Equal(t, c2.Type(), cs[1].Type(), "Type 2 mismatch") require.Equal(t, c2.Type(), cs[1].Type(), "Type 2 mismatch")
assert.Equal(t, c2.Path(), cs[1].Path(), "Path 2 mismatch") require.Equal(t, c2.Path(), cs[1].Path(), "Path 2 mismatch")
assert.Equal(t, c2.Content(), cs[1].Content(), "Content 2 mismatch") require.Equal(t, c2.Content(), cs[1].Content(), "Content 2 mismatch")
} }
func TestFileChangeIterator(t *testing.T) { func TestFileChangeIterator(t *testing.T) {
@ -105,11 +105,11 @@ func TestFileChangeIterator(t *testing.T) {
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
cl, err := NewFileChangelist(tmpDir) cl, err := NewFileChangelist(tmpDir)
assert.Nil(t, err, "Error initializing fileChangelist") require.Nil(t, err, "Error initializing fileChangelist")
it, err := cl.NewIterator() it, err := cl.NewIterator()
assert.Nil(t, err, "Error initializing iterator") require.Nil(t, err, "Error initializing iterator")
assert.False(t, it.HasNext(), "HasNext returns false for empty ChangeList") require.False(t, it.HasNext(), "HasNext returns false for empty ChangeList")
c1 := NewTufChange(ActionCreate, "t1", "target1", "test/targ1", []byte{1}) c1 := NewTufChange(ActionCreate, "t1", "target1", "test/targ1", []byte{1})
cl.Add(c1) cl.Add(c1)
@ -123,47 +123,47 @@ func TestFileChangeIterator(t *testing.T) {
cs := cl.List() cs := cl.List()
index := 0 index := 0
it, err = cl.NewIterator() it, err = cl.NewIterator()
assert.Nil(t, err, "Error initializing iterator") require.Nil(t, err, "Error initializing iterator")
for it.HasNext() { for it.HasNext() {
c, err := it.Next() c, err := it.Next()
assert.Nil(t, err, "Next err should be false") require.Nil(t, err, "Next err should be false")
assert.Equal(t, c.Action(), cs[index].Action(), "Action mismatch") require.Equal(t, c.Action(), cs[index].Action(), "Action mismatch")
assert.Equal(t, c.Scope(), cs[index].Scope(), "Scope mismatch") require.Equal(t, c.Scope(), cs[index].Scope(), "Scope mismatch")
assert.Equal(t, c.Type(), cs[index].Type(), "Type mismatch") require.Equal(t, c.Type(), cs[index].Type(), "Type mismatch")
assert.Equal(t, c.Path(), cs[index].Path(), "Path mismatch") require.Equal(t, c.Path(), cs[index].Path(), "Path mismatch")
assert.Equal(t, c.Content(), cs[index].Content(), "Content mismatch") require.Equal(t, c.Content(), cs[index].Content(), "Content mismatch")
index++ index++
} }
assert.Equal(t, index, len(cs), "Iterator produced all data in ChangeList") require.Equal(t, index, len(cs), "Iterator produced all data in ChangeList")
// negative test case: index out of range // negative test case: index out of range
_, err = it.Next() _, err = it.Next()
assert.Error(t, err, "Next errors gracefully when exhausted") require.Error(t, err, "Next errors gracefully when exhausted")
var iterError IteratorBoundsError var iterError IteratorBoundsError
assert.IsType(t, iterError, err, "IteratorBoundsError type") require.IsType(t, iterError, err, "IteratorBoundsError type")
assert.Regexp(t, "out of bounds", err, "Message for iterator bounds error") require.Regexp(t, "out of bounds", err, "Message for iterator bounds error")
// negative test case: changelist files missing // negative test case: changelist files missing
it, err = cl.NewIterator() it, err = cl.NewIterator()
assert.Nil(t, err, "Error initializing iterator") require.Nil(t, err, "Error initializing iterator")
for it.HasNext() { for it.HasNext() {
cl.Clear("") cl.Clear("")
_, err := it.Next() _, err := it.Next()
assert.Error(t, err, "Next() error for missing changelist files") require.Error(t, err, "Next() error for missing changelist files")
} }
// negative test case: bad JSON file to unmarshall via Next() // negative test case: bad JSON file to unmarshall via Next()
cl.Clear("") cl.Clear("")
ioutil.WriteFile(path.Join(tmpDir, "broken_file.change"), []byte{5}, 0644) ioutil.WriteFile(path.Join(tmpDir, "broken_file.change"), []byte{5}, 0644)
it, err = cl.NewIterator() it, err = cl.NewIterator()
assert.Nil(t, err, "Error initializing iterator") require.Nil(t, err, "Error initializing iterator")
for it.HasNext() { for it.HasNext() {
_, err := it.Next() _, err := it.Next()
assert.Error(t, err, "Next should indicate error for bad JSON file") require.Error(t, err, "Next should indicate error for bad JSON file")
} }
// negative test case: changelist directory does not exist // negative test case: changelist directory does not exist
os.RemoveAll(tmpDir) os.RemoveAll(tmpDir)
it, err = cl.NewIterator() it, err = cl.NewIterator()
assert.Error(t, err, "Initializing iterator without underlying file store") require.Error(t, err, "Initializing iterator without underlying file store")
} }