tests: Move CorruptBBolt to testutil

Signed-off-by: Marek Siarkowicz <siarkowicz@google.com>
This commit is contained in:
Marek Siarkowicz 2022-07-25 12:27:22 +02:00
parent 99b982e71a
commit 264498258b
3 changed files with 44 additions and 43 deletions

View File

@ -16,10 +16,14 @@ package testutil
import (
"context"
"errors"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
"go.etcd.io/bbolt"
"go.etcd.io/etcd/api/v3/mvccpb"
)
const (
@ -103,3 +107,40 @@ func PickKey(i int64) string {
panic("Can't count")
}
}
func CorruptBBolt(fpath string) error {
db, derr := bbolt.Open(fpath, os.ModePerm, &bbolt.Options{})
if derr != nil {
return derr
}
defer db.Close()
return db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte("key"))
if b == nil {
return errors.New("got nil bucket for 'key'")
}
keys, vals := [][]byte{}, [][]byte{}
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
keys = append(keys, k)
var kv mvccpb.KeyValue
if uerr := kv.Unmarshal(v); uerr != nil {
return uerr
}
kv.Key[0]++
kv.Value[0]++
v2, v2err := kv.Marshal()
if v2err != nil {
return v2err
}
vals = append(vals, v2)
}
for i := range keys {
if perr := b.Put(keys[i], vals[i]); perr != nil {
return perr
}
}
return nil
})
}

View File

@ -16,16 +16,13 @@ package e2e
import (
"context"
"errors"
"fmt"
"os"
"testing"
"time"
bolt "go.etcd.io/bbolt"
"go.etcd.io/etcd/api/v3/mvccpb"
"go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/server/v3/storage/datadir"
"go.etcd.io/etcd/server/v3/storage/mvcc/testutil"
"go.etcd.io/etcd/tests/v3/framework/e2e"
)
@ -44,7 +41,7 @@ func TestEtcdCorruptHash(t *testing.T) {
testCtl(t, corruptTest, withQuorum(),
withCfg(*cfg),
withInitialCorruptCheck(),
withCorruptFunc(CorruptBBolt),
withCorruptFunc(testutil.CorruptBBolt),
)
}
@ -98,40 +95,3 @@ func corruptTest(cx ctlCtx) {
// restarting corrupted member should fail
e2e.WaitReadyExpectProc(proc, []string{fmt.Sprintf("etcdmain: %016x found data inconsistency with peers", id0)})
}
func CorruptBBolt(fpath string) error {
db, derr := bolt.Open(fpath, os.ModePerm, &bolt.Options{})
if derr != nil {
return derr
}
defer db.Close()
return db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("key"))
if b == nil {
return errors.New("got nil bucket for 'key'")
}
keys, vals := [][]byte{}, [][]byte{}
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
keys = append(keys, k)
var kv mvccpb.KeyValue
if uerr := kv.Unmarshal(v); uerr != nil {
return uerr
}
kv.Key[0]++
kv.Value[0]++
v2, v2err := kv.Marshal()
if v2err != nil {
return v2err
}
vals = append(vals, v2)
}
for i := range keys {
if perr := b.Put(keys[i], vals[i]); perr != nil {
return perr
}
}
return nil
})
}

View File

@ -28,7 +28,6 @@ require (
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.2
go.etcd.io/bbolt v1.3.6
go.etcd.io/etcd/api/v3 v3.6.0-alpha.0
go.etcd.io/etcd/client/pkg/v3 v3.6.0-alpha.0
go.etcd.io/etcd/client/v2 v2.306.0-alpha.0
@ -80,6 +79,7 @@ require (
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 // indirect
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.32.0 // indirect
go.opentelemetry.io/otel v1.7.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.7.0 // indirect