Refactoring code to remove duplicate code test.

Signed-off-by: Samuele Resca <sr7@ad.datcon.co.uk>
Signed-off-by: Samuele Resca <samuele.resca@gmail.com>
This commit is contained in:
Samuele Resca 2022-10-16 19:15:11 +01:00 committed by Samuele Resca
parent 3d9c5c6166
commit b58f9c27e4

View File

@ -12,7 +12,7 @@ import (
"go.uber.org/zap/zaptest" "go.uber.org/zap/zaptest"
) )
func FuzzRangeRequest(f *testing.F) { func FuzzTxnRangeRequest(f *testing.F) {
testcases := []pb.RangeRequest{ testcases := []pb.RangeRequest{
{ {
Key: []byte{2}, Key: []byte{2},
@ -27,8 +27,9 @@ func FuzzRangeRequest(f *testing.F) {
for _, tc := range testcases { for _, tc := range testcases {
soValue := pb.RangeRequest_SortOrder_value[tc.SortOrder.String()] soValue := pb.RangeRequest_SortOrder_value[tc.SortOrder.String()]
soTarget := pb.RangeRequest_SortTarget_value[tc.SortTarget.String()] soTarget := pb.RangeRequest_SortTarget_value[tc.SortTarget.String()]
f.Add(tc.Key, tc.RangeEnd, tc.Limit, tc.Revision, soValue, soTarget) // Use f.Add to provide a seed corpus f.Add(tc.Key, tc.RangeEnd, tc.Limit, tc.Revision, soValue, soTarget)
} }
f.Fuzz(func(t *testing.T, f.Fuzz(func(t *testing.T,
key []byte, key []byte,
rangeEnd []byte, rangeEnd []byte,
@ -37,54 +38,27 @@ func FuzzRangeRequest(f *testing.F) {
sortOrder int32, sortOrder int32,
sortTarget int32, sortTarget int32,
) { ) {
b, _ := betesting.NewDefaultTmpBackend(t) fuzzRequest := &pb.RangeRequest{
defer betesting.Close(t, b) Key: key,
s := mvcc.NewStore(zaptest.NewLogger(t), b, &lease.FakeLessor{}, mvcc.StoreConfig{}) RangeEnd: rangeEnd,
defer s.Close() Limit: limit,
SortOrder: pb.RangeRequest_SortOrder(sortOrder),
SortTarget: pb.RangeRequest_SortTarget(sortTarget),
}
// setup cancelled context verifyCheck(t, func() error {
ctx, cancel := context.WithCancel(context.TODO()) return checkRangeRequest(fuzzRequest)
cancel() })
// put some data to prevent early termination in rangeKeys
// we are expecting failure on cancelled context check
s.Put(key, []byte("bar"), lease.NoLease)
request := &pb.TxnRequest{ execTransaction(t, &pb.RequestOp{
Success: []*pb.RequestOp{
{
Request: &pb.RequestOp_RequestRange{ Request: &pb.RequestOp_RequestRange{
RequestRange: &pb.RangeRequest{ RequestRange: fuzzRequest,
Key: key,
RangeEnd: rangeEnd,
Limit: limit,
SortOrder: pb.RangeRequest_SortOrder(sortOrder),
SortTarget: pb.RangeRequest_SortTarget(sortTarget),
}, },
},
},
},
}
errCheck := checkRangeRequest(&pb.RangeRequest{
Key: key,
RangeEnd: rangeEnd,
Limit: limit,
SortOrder: pb.RangeRequest_SortOrder(sortOrder),
SortTarget: pb.RangeRequest_SortTarget(sortTarget),
}) })
if errCheck != nil {
t.Skip("Validation not passing. Skipping the apply.")
}
_, _, err := txn.Txn(ctx, zaptest.NewLogger(t), request, false, s, &lease.FakeLessor{})
if err != nil {
t.Logf("Check: %s | Apply: %s", errCheck, err)
t.Skip("Application erroring.")
}
}) })
} }
func FuzzPutRequest(f *testing.F) { func FuzzTxnPutRequest(f *testing.F) {
testcases := []pb.PutRequest{ testcases := []pb.PutRequest{
{ {
Key: []byte{2}, Key: []byte{2},
@ -97,7 +71,7 @@ func FuzzPutRequest(f *testing.F) {
} }
for _, tc := range testcases { for _, tc := range testcases {
f.Add(tc.Key, tc.Value, tc.Lease, tc.PrevKv, tc.IgnoreValue, tc.IgnoreLease) // Use f.Add to provide a seed corpus f.Add(tc.Key, tc.Value, tc.Lease, tc.PrevKv, tc.IgnoreValue, tc.IgnoreLease)
} }
f.Fuzz(func(t *testing.T, f.Fuzz(func(t *testing.T,
@ -108,56 +82,28 @@ func FuzzPutRequest(f *testing.F) {
ignoreValue bool, ignoreValue bool,
IgnoreLease bool, IgnoreLease bool,
) { ) {
b, _ := betesting.NewDefaultTmpBackend(t) fuzzRequest := &pb.PutRequest{
defer betesting.Close(t, b) Key: key,
s := mvcc.NewStore(zaptest.NewLogger(t), b, &lease.FakeLessor{}, mvcc.StoreConfig{}) Value: value,
defer s.Close() Lease: leaseValue,
PrevKv: prevKv,
IgnoreValue: ignoreValue,
IgnoreLease: IgnoreLease,
}
// setup cancelled context verifyCheck(t, func() error {
ctx, cancel := context.WithCancel(context.TODO()) return checkPutRequest(fuzzRequest)
cancel() })
// put some data to prevent early termination in rangeKeys
// we are expecting failure on cancelled context check
s.Put(key, []byte("bar"), lease.NoLease)
request := &pb.TxnRequest{ execTransaction(t, &pb.RequestOp{
Success: []*pb.RequestOp{
{
Request: &pb.RequestOp_RequestPut{ Request: &pb.RequestOp_RequestPut{
RequestPut: &pb.PutRequest{ RequestPut: fuzzRequest,
Key: key,
Value: value,
Lease: leaseValue,
PrevKv: prevKv,
IgnoreValue: ignoreValue,
IgnoreLease: IgnoreLease,
}, },
},
},
},
}
errCheck := checkPutRequest(&pb.PutRequest{
Key: key,
Value: value,
Lease: leaseValue,
PrevKv: prevKv,
IgnoreValue: ignoreValue,
IgnoreLease: IgnoreLease,
}) })
if errCheck != nil {
t.Skip("Validation not passing. Skipping the apply.")
}
_, _, err := txn.Txn(ctx, zaptest.NewLogger(t), request, false, s, &lease.FakeLessor{})
if err != nil {
t.Logf("Check: %s | Apply: %s", errCheck, err)
t.Skip("Application erroring.")
}
}) })
} }
func FuzzDeleteRangeRequest(f *testing.F) { func FuzzTxnDeleteRangeRequest(f *testing.F) {
testcases := []pb.DeleteRangeRequest{ testcases := []pb.DeleteRangeRequest{
{ {
Key: []byte{2}, Key: []byte{2},
@ -167,7 +113,7 @@ func FuzzDeleteRangeRequest(f *testing.F) {
} }
for _, tc := range testcases { for _, tc := range testcases {
f.Add(tc.Key, tc.RangeEnd, tc.PrevKv) // Use f.Add to provide a seed corpus f.Add(tc.Key, tc.RangeEnd, tc.PrevKv)
} }
f.Fuzz(func(t *testing.T, f.Fuzz(func(t *testing.T,
@ -175,6 +121,32 @@ func FuzzDeleteRangeRequest(f *testing.F) {
rangeEnd []byte, rangeEnd []byte,
prevKv bool, prevKv bool,
) { ) {
fuzzRequest := &pb.DeleteRangeRequest{
Key: key,
RangeEnd: rangeEnd,
PrevKv: prevKv,
}
verifyCheck(t, func() error {
return checkDeleteRequest(fuzzRequest)
})
execTransaction(t, &pb.RequestOp{
Request: &pb.RequestOp_RequestDeleteRange{
RequestDeleteRange: fuzzRequest,
},
})
})
}
func verifyCheck(t *testing.T, check func() error) {
errCheck := check()
if errCheck != nil {
t.Skip("Validation not passing. Skipping the apply.")
}
}
func execTransaction(t *testing.T, req *pb.RequestOp) {
b, _ := betesting.NewDefaultTmpBackend(t) b, _ := betesting.NewDefaultTmpBackend(t)
defer betesting.Close(t, b) defer betesting.Close(t, b)
s := mvcc.NewStore(zaptest.NewLogger(t), b, &lease.FakeLessor{}, mvcc.StoreConfig{}) s := mvcc.NewStore(zaptest.NewLogger(t), b, &lease.FakeLessor{}, mvcc.StoreConfig{})
@ -183,37 +155,13 @@ func FuzzDeleteRangeRequest(f *testing.F) {
// setup cancelled context // setup cancelled context
ctx, cancel := context.WithCancel(context.TODO()) ctx, cancel := context.WithCancel(context.TODO())
cancel() cancel()
// put some data to prevent early termination in rangeKeys
// we are expecting failure on cancelled context check
s.Put(key, []byte("bar"), lease.NoLease)
request := &pb.TxnRequest{ request := &pb.TxnRequest{
Success: []*pb.RequestOp{ Success: []*pb.RequestOp{req},
{
Request: &pb.RequestOp_RequestDeleteRange{
RequestDeleteRange: &pb.DeleteRangeRequest{
Key: key,
RangeEnd: rangeEnd,
PrevKv: prevKv,
},
},
},
},
}
errCheck := checkDeleteRequest(&pb.DeleteRangeRequest{
Key: key,
RangeEnd: rangeEnd,
PrevKv: prevKv,
})
if errCheck != nil {
t.Skip("Validation not passing. Skipping the apply.")
} }
_, _, err := txn.Txn(ctx, zaptest.NewLogger(t), request, false, s, &lease.FakeLessor{}) _, _, err := txn.Txn(ctx, zaptest.NewLogger(t), request, false, s, &lease.FakeLessor{})
if err != nil { if err != nil {
t.Logf("Check: %s | Apply: %s", errCheck, err) t.Skipf("Application erroring. %s", err.Error())
t.Skip("Application erroring.")
} }
})
} }