chore: fix oplog tests

This commit is contained in:
garethgeorge
2024-09-09 00:40:14 -07:00
parent 6894128d90
commit ca678d9495
4 changed files with 51 additions and 23 deletions
+11 -13
View File
@@ -3,8 +3,8 @@ package main
import (
"bytes"
"compress/gzip"
"errors"
"flag"
"log"
"os"
"path"
@@ -12,8 +12,6 @@ import (
"github.com/garethgeorge/backrest/internal/env"
"github.com/garethgeorge/backrest/internal/oplog"
"github.com/garethgeorge/backrest/internal/oplog/bboltstore"
"go.etcd.io/bbolt"
"go.uber.org/zap"
"google.golang.org/protobuf/encoding/prototext"
)
@@ -32,34 +30,34 @@ func main() {
oplogFile := path.Join(env.DataDir(), "oplog.boltdb")
opstore, err := bboltstore.NewBboltStore(oplogFile)
if err != nil {
if !errors.Is(err, bbolt.ErrTimeout) {
zap.S().Fatalf("timeout while waiting to open database, is the database open elsewhere?")
}
zap.S().Warnf("operation log may be corrupted, if errors recur delete the file %q and restart. Your backups stored in your repos are safe.", oplogFile)
zap.S().Fatalf("error creating oplog : %v", err)
log.Fatalf("error creating oplog : %v", err)
}
defer opstore.Close()
output := &v1.OperationList{}
log := oplog.NewOpLog(opstore)
log.Query(oplog.Query{}, func(op *v1.Operation) error {
l, err := oplog.NewOpLog(opstore)
if err != nil {
log.Fatalf("error creating oplog: %v", err)
}
l.Query(oplog.Query{}, func(op *v1.Operation) error {
output.Operations = append(output.Operations, op)
return nil
})
log.Printf("exporting %d operations", len(output.Operations))
bytes, err := prototext.MarshalOptions{Multiline: true}.Marshal(output)
if err != nil {
zap.S().Fatalf("error marshalling operations: %v", err)
log.Fatalf("error marshalling operations: %v", err)
}
bytes, err = compress(bytes)
if err != nil {
zap.S().Fatalf("error compressing operations: %v", err)
log.Fatalf("error compressing operations: %v", err)
}
if err := os.WriteFile(*outpath, bytes, 0644); err != nil {
zap.S().Fatalf("error writing to file: %v", err)
log.Fatalf("error writing to file: %v", err)
}
}
+4 -1
View File
@@ -790,7 +790,10 @@ func createSystemUnderTest(t *testing.T, config config.ConfigStore) systemUnderT
t.Fatalf("Failed to create oplog store: %v", err)
}
t.Cleanup(func() { opstore.Close() })
oplog := oplog.NewOpLog(opstore)
oplog, err := oplog.NewOpLog(opstore)
if err != nil {
t.Fatalf("Failed to create oplog: %v", err)
}
logStore, err := logwriter.NewLogManager(dir+"/log", 10)
if err != nil {
t.Fatalf("Failed to create log store: %v", err)
@@ -35,7 +35,10 @@ func TestCreate(t *testing.T) {
// t.Parallel()
for name, store := range StoresForTest(t) {
t.Run(name, func(t *testing.T) {
_ = oplog.NewOpLog(store)
_, err := oplog.NewOpLog(store)
if err != nil {
t.Fatalf("error creating oplog: %v", err)
}
})
}
}
@@ -127,7 +130,10 @@ func TestAddOperation(t *testing.T) {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
log := oplog.NewOpLog(store)
log, err := oplog.NewOpLog(store)
if err != nil {
t.Fatalf("error creating oplog: %v", err)
}
op := proto.Clone(tc.op).(*v1.Operation)
if err := log.Add(op); (err != nil) != tc.wantErr {
t.Errorf("Add() error = %v, wantErr %v", err, tc.wantErr)
@@ -226,7 +232,10 @@ func TestListOperation(t *testing.T) {
for name, store := range StoresForTest(t) {
t.Run(name, func(t *testing.T) {
log := oplog.NewOpLog(store)
log, err := oplog.NewOpLog(store)
if err != nil {
t.Fatalf("error creating oplog: %v", err)
}
for _, op := range ops {
if err := log.Add(proto.Clone(op).(*v1.Operation)); err != nil {
t.Fatalf("error adding operation: %s", err)
@@ -266,7 +275,10 @@ func TestBigIO(t *testing.T) {
store := store
t.Run(name, func(t *testing.T) {
t.Parallel()
log := oplog.NewOpLog(store)
log, err := oplog.NewOpLog(store)
if err != nil {
t.Fatalf("error creating oplog: %v", err)
}
for i := 0; i < count; i++ {
if err := log.Add(&v1.Operation{
UnixTimeStartMs: 1234,
@@ -301,7 +313,10 @@ func TestIndexSnapshot(t *testing.T) {
store := store
t.Run(name, func(t *testing.T) {
t.Parallel()
log := oplog.NewOpLog(store)
log, err := oplog.NewOpLog(store)
if err != nil {
t.Fatalf("error creating oplog: %v", err)
}
op := proto.Clone(op).(*v1.Operation)
if err := log.Add(op); err != nil {
@@ -341,7 +356,10 @@ func TestUpdateOperation(t *testing.T) {
store := store
t.Run(name, func(t *testing.T) {
t.Parallel()
log := oplog.NewOpLog(store)
log, err := oplog.NewOpLog(store)
if err != nil {
t.Fatalf("error creating oplog: %v", err)
}
op := proto.Clone(op).(*v1.Operation)
if err := log.Add(op); err != nil {
@@ -432,7 +450,10 @@ func countBySnapshotIdHelper(t *testing.T, log *oplog.OpLog, snapshotId string,
func BenchmarkAdd(b *testing.B) {
for name, store := range StoresForTest(b) {
b.Run(name, func(b *testing.B) {
log := oplog.NewOpLog(store)
log, err := oplog.NewOpLog(store)
if err != nil {
b.Fatalf("error creating oplog: %v", err)
}
for i := 0; i < b.N; i++ {
_ = log.Add(&v1.Operation{
UnixTimeStartMs: 1234,
@@ -450,7 +471,10 @@ func BenchmarkList(b *testing.B) {
for _, count := range []int{100, 1000, 10000} {
b.Run(fmt.Sprintf("%d", count), func(b *testing.B) {
for name, store := range StoresForTest(b) {
log := oplog.NewOpLog(store)
log, err := oplog.NewOpLog(store)
if err != nil {
b.Fatalf("error creating oplog: %v", err)
}
for i := 0; i < count; i++ {
_ = log.Add(&v1.Operation{
UnixTimeStartMs: 1234,
@@ -378,7 +378,10 @@ func TestScheduling(t *testing.T) {
}
}
log := oplog.NewOpLog(opstore)
log, err := oplog.NewOpLog(opstore)
if err != nil {
t.Fatalf("failed to create oplog: %v", err)
}
runner := newTestTaskRunner(t, cfg, log)