Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 13 additions & 39 deletions server/storage/backend/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ func TestBackendClose(t *testing.T) {
// check close could work
done := make(chan struct{}, 1)
go func() {
err := b.Close()
if err != nil {
t.Errorf("close error = %v, want nil", err)
}
assert.NoErrorf(t, b.Close(), "close error")
done <- struct{}{}
}()
select {
Expand All @@ -63,14 +60,11 @@ func TestBackendSnapshot(t *testing.T) {

// write snapshot to a new file
f, err := os.CreateTemp(t.TempDir(), "etcd_backend_test")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
snap := b.Snapshot()
defer func() { assert.NoError(t, snap.Close()) }()
if _, err := snap.WriteTo(f); err != nil {
t.Fatal(err)
}
_, err = snap.WriteTo(f)
require.NoError(t, err)
require.NoError(t, f.Close())

// bootstrap new backend from the snapshot
Expand All @@ -82,9 +76,7 @@ func TestBackendSnapshot(t *testing.T) {
newTx := nb.BatchTx()
newTx.Lock()
ks, _ := newTx.UnsafeRange(schema.Test, []byte("foo"), []byte("goo"), 0)
if len(ks) != 1 {
t.Errorf("len(kvs) = %d, want 1", len(ks))
}
assert.Lenf(t, ks, 1, "len(kvs) = %d, want 1", len(ks))
newTx.Unlock()
}

Expand Down Expand Up @@ -116,10 +108,7 @@ func TestBackendBatchIntervalCommit(t *testing.T) {
t.Errorf("bucket test does not exit")
return nil
}
v := bucket.Get([]byte("foo"))
if v == nil {
t.Errorf("foo key failed to written in backend")
}
assert.NotNilf(t, bucket.Get([]byte("foo")), "foo key failed to written in backend")
return nil
}))
}
Expand Down Expand Up @@ -160,31 +149,18 @@ func TestBackendDefrag(t *testing.T) {

// shrink and check hash
oh, err := b.Hash(nil)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

err = b.Defrag()
if err != nil {
t.Fatal(err)
}
require.NoError(t, b.Defrag())

nh, err := b.Hash(nil)
if err != nil {
t.Fatal(err)
}
if oh != nh {
t.Errorf("hash = %v, want %v", nh, oh)
}
require.NoError(t, err)
assert.Equalf(t, oh, nh, "hash = %v, want %v", nh, oh)

nsize := b.Size()
if nsize >= size {
t.Errorf("new size = %v, want < %d", nsize, size)
}
assert.Lessf(t, nsize, size, "new size = %v, want < %d", nsize, size)
db := backend.DbFromBackendForTest(b)
if db.FreelistType != bcfg.BackendFreelistType {
t.Errorf("db FreelistType = [%v], want [%v]", db.FreelistType, bcfg.BackendFreelistType)
}
assert.Equalf(t, db.FreelistType, bcfg.BackendFreelistType, "db FreelistType = [%v], want [%v]", db.FreelistType, bcfg.BackendFreelistType)

// try put more keys after shrink.
tx = b.BatchTx()
Expand Down Expand Up @@ -345,7 +321,5 @@ func TestBackendWritebackForEach(t *testing.T) {
require.NoError(t, tx.UnsafeForEach(schema.Key, getSeq))
tx.Unlock()

if seq != partialSeq {
t.Fatalf("expected %q, got %q", seq, partialSeq)
}
require.Equalf(t, seq, partialSeq, "expected %q, got %q", seq, partialSeq)
}
27 changes: 7 additions & 20 deletions server/storage/backend/batch_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"

bolt "go.etcd.io/bbolt"
"go.etcd.io/etcd/server/v3/storage/backend"
Expand Down Expand Up @@ -51,9 +52,7 @@ func TestBatchTxPut(t *testing.T) {
tx.Lock()
_, gv := tx.UnsafeRange(schema.Test, []byte("foo"), nil, 0)
tx.Unlock()
if !reflect.DeepEqual(gv[0], v) {
t.Errorf("v = %s, want %s", gv[0], v)
}
assert.Truef(t, reflect.DeepEqual(gv[0], v), "v = %s, want %s", gv[0], v)
tx.Commit()
}
}
Expand Down Expand Up @@ -120,12 +119,8 @@ func TestBatchTxRange(t *testing.T) {
}
for i, tt := range tests {
keys, vals := tx.UnsafeRange(schema.Test, tt.key, tt.endKey, tt.limit)
if !reflect.DeepEqual(keys, tt.wkeys) {
t.Errorf("#%d: keys = %+v, want %+v", i, keys, tt.wkeys)
}
if !reflect.DeepEqual(vals, tt.wvals) {
t.Errorf("#%d: vals = %+v, want %+v", i, vals, tt.wvals)
}
assert.Truef(t, reflect.DeepEqual(keys, tt.wkeys), "#%d: keys = %+v, want %+v", i, keys, tt.wkeys)
assert.Truef(t, reflect.DeepEqual(vals, tt.wvals), "#%d: vals = %+v, want %+v", i, vals, tt.wvals)
}
}

Expand All @@ -148,9 +143,7 @@ func TestBatchTxDelete(t *testing.T) {
tx.Lock()
ks, _ := tx.UnsafeRange(schema.Test, []byte("foo"), nil, 0)
tx.Unlock()
if len(ks) != 0 {
t.Errorf("keys on foo = %v, want nil", ks)
}
assert.Emptyf(t, ks, "keys on foo = %v, want nil", ks)
tx.Commit()
}
}
Expand All @@ -174,10 +167,7 @@ func TestBatchTxCommit(t *testing.T) {
t.Errorf("bucket test does not exit")
return nil
}
v := bucket.Get([]byte("foo"))
if v == nil {
t.Errorf("foo key failed to written in backend")
}
assert.NotNilf(t, bucket.Get([]byte("foo")), "foo key failed to written in backend")
return nil
})
}
Expand All @@ -202,10 +192,7 @@ func TestBatchTxBatchLimitCommit(t *testing.T) {
t.Errorf("bucket test does not exit")
return nil
}
v := bucket.Get([]byte("foo"))
if v == nil {
t.Errorf("foo key failed to written in backend")
}
assert.NotNilf(t, bucket.Get([]byte("foo")), "foo key failed to written in backend")
return nil
})
}
Expand Down
6 changes: 3 additions & 3 deletions server/storage/backend/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"

"go.etcd.io/etcd/client/pkg/v3/verify"
"go.etcd.io/etcd/server/v3/storage/backend"
betesting "go.etcd.io/etcd/server/v3/storage/backend/testing"
Expand Down Expand Up @@ -83,9 +85,7 @@ func TestLockVerify(t *testing.T) {
tc.lock(be.BatchTx())
}
}) != nil
if hasPaniced != tc.expectPanic {
t.Errorf("%v != %v", hasPaniced, tc.expectPanic)
}
assert.Equalf(t, hasPaniced, tc.expectPanic, "%v != %v", hasPaniced, tc.expectPanic)
})
}
}
Expand Down
24 changes: 6 additions & 18 deletions server/storage/mvcc/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,10 @@ func TestHasherStore(t *testing.T) {

for _, want := range hashes {
got, _, err := s.HashByRev(want.Revision)
if err != nil {
t.Fatal(err)
}
if want.Hash != got.Hash {
t.Errorf("Expected stored hash to match, got: %d, expected: %d", want.Hash, got.Hash)
}
if want.Revision != got.Revision {
t.Errorf("Expected stored revision to match, got: %d, expected: %d", want.Revision, got.Revision)
}
if want.CompactRevision != got.CompactRevision {
t.Errorf("Expected stored compact revision to match, got: %d, expected: %d", want.CompactRevision, got.CompactRevision)
}
require.NoError(t, err)
assert.Equalf(t, want.Hash, got.Hash, "Expected stored hash to match, got: %d, expected: %d", want.Hash, got.Hash)
assert.Equalf(t, want.Revision, got.Revision, "Expected stored revision to match, got: %d, expected: %d", want.Revision, got.Revision)
assert.Equalf(t, want.CompactRevision, got.CompactRevision, "Expected stored compact revision to match, got: %d, expected: %d", want.CompactRevision, got.CompactRevision)
}
}

Expand All @@ -222,13 +214,9 @@ func TestHasherStoreFull(t *testing.T) {
// Hash for old revision should be discarded as storage is already full
s.Store(KeyValueHash{Revision: minRevision - 1})
hash, _, err := s.HashByRev(minRevision - 1)
if err == nil {
t.Errorf("Expected an error as old revision should be discarded, got: %v", hash)
}
require.Errorf(t, err, "Expected an error as old revision should be discarded, got: %v", hash)
// Hash for new revision should be stored even when storage is full
s.Store(KeyValueHash{Revision: maxRevision + 1})
_, _, err = s.HashByRev(maxRevision + 1)
if err != nil {
t.Errorf("Didn't expect error for new revision, err: %v", err)
}
assert.NoErrorf(t, err, "Didn't expect error for new revision, err: %v", err)
}
50 changes: 13 additions & 37 deletions server/storage/mvcc/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
package mvcc

import (
"errors"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
Expand Down Expand Up @@ -47,18 +47,10 @@ func TestIndexGet(t *testing.T) {
}
for i, tt := range tests {
rev, created, ver, err := ti.Get([]byte("foo"), tt.rev)
if !errors.Is(err, tt.werr) {
t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
}
if rev != tt.wrev {
t.Errorf("#%d: rev = %+v, want %+v", i, rev, tt.wrev)
}
if created != tt.wcreated {
t.Errorf("#%d: created = %+v, want %+v", i, created, tt.wcreated)
}
if ver != tt.wver {
t.Errorf("#%d: ver = %d, want %d", i, ver, tt.wver)
}
require.ErrorIsf(t, err, tt.werr, "#%d: err = %v, want %v", i, err, tt.werr)
assert.Equalf(t, rev, tt.wrev, "#%d: rev = %+v, want %+v", i, rev, tt.wrev)
assert.Equalf(t, created, tt.wcreated, "#%d: created = %+v, want %+v", i, created, tt.wcreated)
assert.Equalf(t, ver, tt.wver, "#%d: ver = %d, want %d", i, ver, tt.wver)
}
}

Expand Down Expand Up @@ -112,32 +104,20 @@ func TestIndexRange(t *testing.T) {
}
for i, tt := range tests {
keys, revs := ti.Range(tt.key, tt.end, atRev)
if !reflect.DeepEqual(keys, tt.wkeys) {
t.Errorf("#%d: keys = %+v, want %+v", i, keys, tt.wkeys)
}
if !reflect.DeepEqual(revs, tt.wrevs) {
t.Errorf("#%d: revs = %+v, want %+v", i, revs, tt.wrevs)
}
assert.Truef(t, reflect.DeepEqual(keys, tt.wkeys), "#%d: keys = %+v, want %+v", i, keys, tt.wkeys)
assert.Truef(t, reflect.DeepEqual(revs, tt.wrevs), "#%d: revs = %+v, want %+v", i, revs, tt.wrevs)
}
}

func TestIndexTombstone(t *testing.T) {
ti := newTreeIndex(zaptest.NewLogger(t))
ti.Put([]byte("foo"), Revision{Main: 1})

err := ti.Tombstone([]byte("foo"), Revision{Main: 2})
if err != nil {
t.Errorf("tombstone error = %v, want nil", err)
}
require.NoErrorf(t, ti.Tombstone([]byte("foo"), Revision{Main: 2}), "tombstone error")

_, _, _, err = ti.Get([]byte("foo"), 2)
if !errors.Is(err, ErrRevisionNotFound) {
t.Errorf("get error = %v, want ErrRevisionNotFound", err)
}
err = ti.Tombstone([]byte("foo"), Revision{Main: 3})
if !errors.Is(err, ErrRevisionNotFound) {
t.Errorf("tombstone error = %v, want %v", err, ErrRevisionNotFound)
}
_, _, _, err := ti.Get([]byte("foo"), 2)
require.ErrorIsf(t, err, ErrRevisionNotFound, "get error = %v, want ErrRevisionNotFound", err)
assert.ErrorIsf(t, ti.Tombstone([]byte("foo"), Revision{Main: 3}), ErrRevisionNotFound, "tombstone error")
}

func TestIndexRevision(t *testing.T) {
Expand Down Expand Up @@ -224,13 +204,9 @@ func TestIndexRevision(t *testing.T) {
}
for i, tt := range tests {
revs, _ := ti.Revisions(tt.key, tt.end, tt.atRev, tt.limit)
if !reflect.DeepEqual(revs, tt.wrevs) {
t.Errorf("#%d limit %d: revs = %+v, want %+v", i, tt.limit, revs, tt.wrevs)
}
assert.Truef(t, reflect.DeepEqual(revs, tt.wrevs), "#%d limit %d: revs = %+v, want %+v", i, tt.limit, revs, tt.wrevs)
count := ti.CountRevisions(tt.key, tt.end, tt.atRev)
if count != tt.wcounts {
t.Errorf("#%d: count = %d, want %v", i, count, tt.wcounts)
}
assert.Equalf(t, count, tt.wcounts, "#%d: count = %d, want %v", i, count, tt.wcounts)
}
}

Expand Down
Loading