Skip to content

Commit fb96004

Browse files
committed
chore: use testify instead of testing in server/storage
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
1 parent f07e2ae commit fb96004

22 files changed

+455
-1198
lines changed

server/storage/backend/backend_test.go

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ func TestBackendClose(t *testing.T) {
3737
// check close could work
3838
done := make(chan struct{}, 1)
3939
go func() {
40-
err := b.Close()
41-
if err != nil {
42-
t.Errorf("close error = %v, want nil", err)
43-
}
40+
assert.NoErrorf(t, b.Close(), "close error")
4441
done <- struct{}{}
4542
}()
4643
select {
@@ -63,14 +60,11 @@ func TestBackendSnapshot(t *testing.T) {
6360

6461
// write snapshot to a new file
6562
f, err := os.CreateTemp(t.TempDir(), "etcd_backend_test")
66-
if err != nil {
67-
t.Fatal(err)
68-
}
63+
require.NoError(t, err)
6964
snap := b.Snapshot()
7065
defer func() { assert.NoError(t, snap.Close()) }()
71-
if _, err := snap.WriteTo(f); err != nil {
72-
t.Fatal(err)
73-
}
66+
_, err = snap.WriteTo(f)
67+
require.NoError(t, err)
7468
require.NoError(t, f.Close())
7569

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

@@ -116,10 +108,7 @@ func TestBackendBatchIntervalCommit(t *testing.T) {
116108
t.Errorf("bucket test does not exit")
117109
return nil
118110
}
119-
v := bucket.Get([]byte("foo"))
120-
if v == nil {
121-
t.Errorf("foo key failed to written in backend")
122-
}
111+
assert.NotNilf(t, bucket.Get([]byte("foo")), "foo key failed to written in backend")
123112
return nil
124113
}))
125114
}
@@ -160,31 +149,18 @@ func TestBackendDefrag(t *testing.T) {
160149

161150
// shrink and check hash
162151
oh, err := b.Hash(nil)
163-
if err != nil {
164-
t.Fatal(err)
165-
}
152+
require.NoError(t, err)
166153

167-
err = b.Defrag()
168-
if err != nil {
169-
t.Fatal(err)
170-
}
154+
require.NoError(t, b.Defrag())
171155

172156
nh, err := b.Hash(nil)
173-
if err != nil {
174-
t.Fatal(err)
175-
}
176-
if oh != nh {
177-
t.Errorf("hash = %v, want %v", nh, oh)
178-
}
157+
require.NoError(t, err)
158+
assert.Equalf(t, oh, nh, "hash = %v, want %v", nh, oh)
179159

180160
nsize := b.Size()
181-
if nsize >= size {
182-
t.Errorf("new size = %v, want < %d", nsize, size)
183-
}
161+
assert.Lessf(t, nsize, size, "new size = %v, want < %d", nsize, size)
184162
db := backend.DbFromBackendForTest(b)
185-
if db.FreelistType != bcfg.BackendFreelistType {
186-
t.Errorf("db FreelistType = [%v], want [%v]", db.FreelistType, bcfg.BackendFreelistType)
187-
}
163+
assert.Equalf(t, db.FreelistType, bcfg.BackendFreelistType, "db FreelistType = [%v], want [%v]", db.FreelistType, bcfg.BackendFreelistType)
188164

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

348-
if seq != partialSeq {
349-
t.Fatalf("expected %q, got %q", seq, partialSeq)
350-
}
324+
require.Equalf(t, seq, partialSeq, "expected %q, got %q", seq, partialSeq)
351325
}

server/storage/backend/batch_tx_test.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"github.com/google/go-cmp/cmp"
25+
"github.com/stretchr/testify/assert"
2526

2627
bolt "go.etcd.io/bbolt"
2728
"go.etcd.io/etcd/server/v3/storage/backend"
@@ -51,9 +52,7 @@ func TestBatchTxPut(t *testing.T) {
5152
tx.Lock()
5253
_, gv := tx.UnsafeRange(schema.Test, []byte("foo"), nil, 0)
5354
tx.Unlock()
54-
if !reflect.DeepEqual(gv[0], v) {
55-
t.Errorf("v = %s, want %s", gv[0], v)
56-
}
55+
assert.Truef(t, reflect.DeepEqual(gv[0], v), "v = %s, want %s", gv[0], v)
5756
tx.Commit()
5857
}
5958
}
@@ -120,12 +119,8 @@ func TestBatchTxRange(t *testing.T) {
120119
}
121120
for i, tt := range tests {
122121
keys, vals := tx.UnsafeRange(schema.Test, tt.key, tt.endKey, tt.limit)
123-
if !reflect.DeepEqual(keys, tt.wkeys) {
124-
t.Errorf("#%d: keys = %+v, want %+v", i, keys, tt.wkeys)
125-
}
126-
if !reflect.DeepEqual(vals, tt.wvals) {
127-
t.Errorf("#%d: vals = %+v, want %+v", i, vals, tt.wvals)
128-
}
122+
assert.Truef(t, reflect.DeepEqual(keys, tt.wkeys), "#%d: keys = %+v, want %+v", i, keys, tt.wkeys)
123+
assert.Truef(t, reflect.DeepEqual(vals, tt.wvals), "#%d: vals = %+v, want %+v", i, vals, tt.wvals)
129124
}
130125
}
131126

@@ -148,9 +143,7 @@ func TestBatchTxDelete(t *testing.T) {
148143
tx.Lock()
149144
ks, _ := tx.UnsafeRange(schema.Test, []byte("foo"), nil, 0)
150145
tx.Unlock()
151-
if len(ks) != 0 {
152-
t.Errorf("keys on foo = %v, want nil", ks)
153-
}
146+
assert.Emptyf(t, ks, "keys on foo = %v, want nil", ks)
154147
tx.Commit()
155148
}
156149
}
@@ -174,10 +167,7 @@ func TestBatchTxCommit(t *testing.T) {
174167
t.Errorf("bucket test does not exit")
175168
return nil
176169
}
177-
v := bucket.Get([]byte("foo"))
178-
if v == nil {
179-
t.Errorf("foo key failed to written in backend")
180-
}
170+
assert.NotNilf(t, bucket.Get([]byte("foo")), "foo key failed to written in backend")
181171
return nil
182172
})
183173
}
@@ -202,10 +192,7 @@ func TestBatchTxBatchLimitCommit(t *testing.T) {
202192
t.Errorf("bucket test does not exit")
203193
return nil
204194
}
205-
v := bucket.Get([]byte("foo"))
206-
if v == nil {
207-
t.Errorf("foo key failed to written in backend")
208-
}
195+
assert.NotNilf(t, bucket.Get([]byte("foo")), "foo key failed to written in backend")
209196
return nil
210197
})
211198
}

server/storage/backend/verify_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"testing"
1919
"time"
2020

21+
"github.com/stretchr/testify/assert"
22+
2123
"go.etcd.io/etcd/client/pkg/v3/verify"
2224
"go.etcd.io/etcd/server/v3/storage/backend"
2325
betesting "go.etcd.io/etcd/server/v3/storage/backend/testing"
@@ -83,9 +85,7 @@ func TestLockVerify(t *testing.T) {
8385
tc.lock(be.BatchTx())
8486
}
8587
}) != nil
86-
if hasPaniced != tc.expectPanic {
87-
t.Errorf("%v != %v", hasPaniced, tc.expectPanic)
88-
}
88+
assert.Equalf(t, hasPaniced, tc.expectPanic, "%v != %v", hasPaniced, tc.expectPanic)
8989
})
9090
}
9191
}

server/storage/mvcc/hash_test.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,10 @@ func TestHasherStore(t *testing.T) {
192192

193193
for _, want := range hashes {
194194
got, _, err := s.HashByRev(want.Revision)
195-
if err != nil {
196-
t.Fatal(err)
197-
}
198-
if want.Hash != got.Hash {
199-
t.Errorf("Expected stored hash to match, got: %d, expected: %d", want.Hash, got.Hash)
200-
}
201-
if want.Revision != got.Revision {
202-
t.Errorf("Expected stored revision to match, got: %d, expected: %d", want.Revision, got.Revision)
203-
}
204-
if want.CompactRevision != got.CompactRevision {
205-
t.Errorf("Expected stored compact revision to match, got: %d, expected: %d", want.CompactRevision, got.CompactRevision)
206-
}
195+
require.NoError(t, err)
196+
assert.Equalf(t, want.Hash, got.Hash, "Expected stored hash to match, got: %d, expected: %d", want.Hash, got.Hash)
197+
assert.Equalf(t, want.Revision, got.Revision, "Expected stored revision to match, got: %d, expected: %d", want.Revision, got.Revision)
198+
assert.Equalf(t, want.CompactRevision, got.CompactRevision, "Expected stored compact revision to match, got: %d, expected: %d", want.CompactRevision, got.CompactRevision)
207199
}
208200
}
209201

@@ -222,13 +214,9 @@ func TestHasherStoreFull(t *testing.T) {
222214
// Hash for old revision should be discarded as storage is already full
223215
s.Store(KeyValueHash{Revision: minRevision - 1})
224216
hash, _, err := s.HashByRev(minRevision - 1)
225-
if err == nil {
226-
t.Errorf("Expected an error as old revision should be discarded, got: %v", hash)
227-
}
217+
require.Errorf(t, err, "Expected an error as old revision should be discarded, got: %v", hash)
228218
// Hash for new revision should be stored even when storage is full
229219
s.Store(KeyValueHash{Revision: maxRevision + 1})
230220
_, _, err = s.HashByRev(maxRevision + 1)
231-
if err != nil {
232-
t.Errorf("Didn't expect error for new revision, err: %v", err)
233-
}
221+
assert.NoErrorf(t, err, "Didn't expect error for new revision, err: %v", err)
234222
}

server/storage/mvcc/index_test.go

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
package mvcc
1616

1717
import (
18-
"errors"
1918
"reflect"
2019
"testing"
2120

21+
"github.com/stretchr/testify/assert"
2222
"github.com/stretchr/testify/require"
2323
"go.uber.org/zap/zaptest"
2424
)
@@ -47,18 +47,10 @@ func TestIndexGet(t *testing.T) {
4747
}
4848
for i, tt := range tests {
4949
rev, created, ver, err := ti.Get([]byte("foo"), tt.rev)
50-
if !errors.Is(err, tt.werr) {
51-
t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
52-
}
53-
if rev != tt.wrev {
54-
t.Errorf("#%d: rev = %+v, want %+v", i, rev, tt.wrev)
55-
}
56-
if created != tt.wcreated {
57-
t.Errorf("#%d: created = %+v, want %+v", i, created, tt.wcreated)
58-
}
59-
if ver != tt.wver {
60-
t.Errorf("#%d: ver = %d, want %d", i, ver, tt.wver)
61-
}
50+
require.ErrorIsf(t, err, tt.werr, "#%d: err = %v, want %v", i, err, tt.werr)
51+
assert.Equalf(t, rev, tt.wrev, "#%d: rev = %+v, want %+v", i, rev, tt.wrev)
52+
assert.Equalf(t, created, tt.wcreated, "#%d: created = %+v, want %+v", i, created, tt.wcreated)
53+
assert.Equalf(t, ver, tt.wver, "#%d: ver = %d, want %d", i, ver, tt.wver)
6254
}
6355
}
6456

@@ -112,32 +104,20 @@ func TestIndexRange(t *testing.T) {
112104
}
113105
for i, tt := range tests {
114106
keys, revs := ti.Range(tt.key, tt.end, atRev)
115-
if !reflect.DeepEqual(keys, tt.wkeys) {
116-
t.Errorf("#%d: keys = %+v, want %+v", i, keys, tt.wkeys)
117-
}
118-
if !reflect.DeepEqual(revs, tt.wrevs) {
119-
t.Errorf("#%d: revs = %+v, want %+v", i, revs, tt.wrevs)
120-
}
107+
assert.Truef(t, reflect.DeepEqual(keys, tt.wkeys), "#%d: keys = %+v, want %+v", i, keys, tt.wkeys)
108+
assert.Truef(t, reflect.DeepEqual(revs, tt.wrevs), "#%d: revs = %+v, want %+v", i, revs, tt.wrevs)
121109
}
122110
}
123111

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

128-
err := ti.Tombstone([]byte("foo"), Revision{Main: 2})
129-
if err != nil {
130-
t.Errorf("tombstone error = %v, want nil", err)
131-
}
116+
require.NoErrorf(t, ti.Tombstone([]byte("foo"), Revision{Main: 2}), "tombstone error")
132117

133-
_, _, _, err = ti.Get([]byte("foo"), 2)
134-
if !errors.Is(err, ErrRevisionNotFound) {
135-
t.Errorf("get error = %v, want ErrRevisionNotFound", err)
136-
}
137-
err = ti.Tombstone([]byte("foo"), Revision{Main: 3})
138-
if !errors.Is(err, ErrRevisionNotFound) {
139-
t.Errorf("tombstone error = %v, want %v", err, ErrRevisionNotFound)
140-
}
118+
_, _, _, err := ti.Get([]byte("foo"), 2)
119+
require.ErrorIsf(t, err, ErrRevisionNotFound, "get error = %v, want ErrRevisionNotFound", err)
120+
assert.ErrorIsf(t, ti.Tombstone([]byte("foo"), Revision{Main: 3}), ErrRevisionNotFound, "tombstone error")
141121
}
142122

143123
func TestIndexRevision(t *testing.T) {
@@ -224,13 +204,9 @@ func TestIndexRevision(t *testing.T) {
224204
}
225205
for i, tt := range tests {
226206
revs, _ := ti.Revisions(tt.key, tt.end, tt.atRev, tt.limit)
227-
if !reflect.DeepEqual(revs, tt.wrevs) {
228-
t.Errorf("#%d limit %d: revs = %+v, want %+v", i, tt.limit, revs, tt.wrevs)
229-
}
207+
assert.Truef(t, reflect.DeepEqual(revs, tt.wrevs), "#%d limit %d: revs = %+v, want %+v", i, tt.limit, revs, tt.wrevs)
230208
count := ti.CountRevisions(tt.key, tt.end, tt.atRev)
231-
if count != tt.wcounts {
232-
t.Errorf("#%d: count = %d, want %v", i, count, tt.wcounts)
233-
}
209+
assert.Equalf(t, count, tt.wcounts, "#%d: count = %d, want %v", i, count, tt.wcounts)
234210
}
235211
}
236212

0 commit comments

Comments
 (0)