Skip to content

Commit

Permalink
Merge pull request #1082 from percona/fix-oplog-flaky-test
Browse files Browse the repository at this point in the history
Fix flaky test in oplog package
  • Loading branch information
boris-ilijic authored Jan 17, 2025
2 parents d46e925 + 0f31540 commit a0ed86d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
30 changes: 16 additions & 14 deletions pbm/oplog/db_tc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package oplog
import (
"context"
"fmt"
"log"
"os"
"testing"

Expand All @@ -17,29 +18,30 @@ var mClient *mongo.Client

func TestMain(m *testing.M) {
ctx := context.Background()
mongodbContainer, err := mongodb.Run(ctx, "perconalab/percona-server-mongodb:7.0")
mongodbContainer, err := mongodb.Run(ctx, "perconalab/percona-server-mongodb:8.0.4-multi")
if err != nil {
fmt.Fprintf(os.Stderr, "error while creating mongo test container: %v", err)
return
log.Fatalf("error while creating mongo test container: %v", err)
}
defer func() {
if err := testcontainers.TerminateContainer(mongodbContainer); err != nil {
fmt.Fprintf(os.Stderr, "failed to terminate container: %s", err)
}
}()

connStr, err := mongodbContainer.ConnectionString(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "conn string error: %v", err)
return
log.Fatalf("conn string error: %v", err)
}
mClient, err = mongo.Connect(ctx, options.Client().ApplyURI(connStr))
if err != nil {
fmt.Fprintf(os.Stderr, "mongo client connect error: %v", err)
return
log.Fatalf("mongo client connect error: %v", err)
}

code := m.Run()

err = mClient.Disconnect(ctx)
if err != nil {
log.Fatalf("mongo client disconnect error: %v", err)
}
if err := testcontainers.TerminateContainer(mongodbContainer); err != nil {
log.Fatalf("failed to terminate container: %s", err)
}

m.Run()
os.Exit(code)
}

func TestGetUUIDForNSv2(t *testing.T) {
Expand Down
16 changes: 8 additions & 8 deletions pbm/oplog/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ func TestApply(t *testing.T) {
oplogFile string
db string
coll string
idxs bson.D
idxs bson.M
}{
{
desc: "index: dropIndexes-createIndexes",
oplogFile: "ops_cmd_createIndexes_dropIndexes",
db: "mydb",
coll: "c1",
idxs: bson.D{{"fieldX", -1}, {"fieldZ", -1}},
idxs: bson.M{"fieldX": -1, "fieldZ": -1},
},
// todo: add more cases
}
Expand All @@ -237,13 +237,13 @@ func TestApply(t *testing.T) {
t.Fatalf("error while applying oplog: %v", err)
}

idxs := oRestore.indexCatalog.GetIndexes(tC.db, tC.coll)
if len(idxs) != len(tC.idxs) {
t.Errorf("wrong number of indexes: want=%d, got=%d", len(tC.idxs), len(idxs))
idxDocs := oRestore.indexCatalog.GetIndexes(tC.db, tC.coll)
if len(idxDocs) != len(tC.idxs) {
t.Errorf("wrong number of indexes: want=%d, got=%d", len(tC.idxs), len(idxDocs))
}
for i, idx := range idxs {
if idx.Key[0].Key != tC.idxs[i].Key {
t.Errorf("wrong key: want=%v, got=%v", tC.idxs[i], idx.Key[0])
for _, idxDoc := range idxDocs {
if _, ok := tC.idxs[idxDoc.Key[0].Key]; !ok {
t.Errorf("wrong key: %v", idxDoc.Key[0])
}
}
})
Expand Down

0 comments on commit a0ed86d

Please sign in to comment.