From 735a57ffaf33305ad710c3e1406fe975b23c1a6c Mon Sep 17 00:00:00 2001 From: Felipe Gasper Date: Mon, 24 Mar 2025 14:54:53 -0400 Subject: [PATCH 1/5] add test --- mongorestore/mongorestore_test.go | 61 +++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/mongorestore/mongorestore_test.go b/mongorestore/mongorestore_test.go index d7e81b831..10b4a020a 100644 --- a/mongorestore/mongorestore_test.go +++ b/mongorestore/mongorestore_test.go @@ -2188,6 +2188,67 @@ func setupTimeseriesWithMixedSchema(dbName string, collName string) error { return nil } +func TestRestoreTimeseriesCollectionsWithTTL(t *testing.T) { + testtype.SkipUnlessTestType(t, testtype.IntegrationTestType) + ctx := context.Background() + + sessionProvider, _, err := testutil.GetBareSessionProvider() + require.NoError(t, err, "should find cluster") + + defer sessionProvider.Close() + + session, err := sessionProvider.GetSession() + require.NoError(t, err, "should find session") + + fcv := testutil.GetFCV(session) + if cmp, err := testutil.CompareFCV(fcv, "5.0"); err != nil || cmp < 0 { + t.Skip("Requires server with FCV 5.0 or later") + } + + dbName := t.Name() + + db := session.Database(dbName) + require.NoError(t, db.Drop(ctx), "should drop db") + + err = db.CreateCollection( + ctx, + "coll", + mopt.CreateCollection(). + SetTimeSeriesOptions( + mopt.TimeSeries(). + SetTimeField("time"). + SetMetaField("meta"), + ), + ) + require.NoError(t, err, "should create collection") + + _, err = db.Collection("coll").Indexes().CreateOne( + ctx, + mongo.IndexModel{ + Keys: bson.D{{"time", 1}}, + Options: mopt.Index().SetPartialFilterExpression( + bson.D{{"meta", 123123}}, + ), + }, + ) + require.NoError(t, err, "should create index") + + withArchiveMongodump(t, func(file string) { + args := []string{ + DropOption, + ArchiveOption + "=" + file, + } + + restore, err := getRestoreWithArgs(args...) + require.NoError(t, err, "restore should run") + defer restore.Close() + + result := restore.Restore() + require.NoError(t, result.Err, "can run mongorestore (result: %+v)", result) + require.EqualValues(t, 0, result.Failures, "mongorestore reports 0 failures") + }) +} + func TestRestoreTimeseriesCollections(t *testing.T) { testtype.SkipUnlessTestType(t, testtype.IntegrationTestType) ctx := context.Background() From cba80d891df79fb957ad7266c753d15cfb692d93 Mon Sep 17 00:00:00 2001 From: Felipe Gasper Date: Mon, 24 Mar 2025 14:56:39 -0400 Subject: [PATCH 2/5] drop --- mongorestore/mongorestore_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mongorestore/mongorestore_test.go b/mongorestore/mongorestore_test.go index 10b4a020a..00f559b48 100644 --- a/mongorestore/mongorestore_test.go +++ b/mongorestore/mongorestore_test.go @@ -2209,6 +2209,9 @@ func TestRestoreTimeseriesCollectionsWithTTL(t *testing.T) { db := session.Database(dbName) require.NoError(t, db.Drop(ctx), "should drop db") + defer func() { + _ = db.Drop(ctx) + }() err = db.CreateCollection( ctx, From cde2316a3c2be05b9b8eb3ac03b06fb40d87bf9b Mon Sep 17 00:00:00 2001 From: Felipe Gasper Date: Mon, 24 Mar 2025 15:01:33 -0400 Subject: [PATCH 3/5] panic if fail to get FCV --- common/testutil/testutil.go | 8 ++++++-- mongorestore/mongorestore_test.go | 12 ++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/common/testutil/testutil.go b/common/testutil/testutil.go index 8e2bffc1b..a3f073c61 100644 --- a/common/testutil/testutil.go +++ b/common/testutil/testutil.go @@ -99,8 +99,12 @@ func GetFCV(s *mongo.Client) string { Version string } res := coll.FindOne(context.TODO(), bson.M{"_id": "featureCompatibilityVersion"}) - //nolint:errcheck - res.Decode(&result) + + err := res.Decode(&result) + if err != nil { + panic(fmt.Sprintf("failed to get FCV: %v", err)) + } + return result.Version } diff --git a/mongorestore/mongorestore_test.go b/mongorestore/mongorestore_test.go index 00f559b48..1fee3cfd5 100644 --- a/mongorestore/mongorestore_test.go +++ b/mongorestore/mongorestore_test.go @@ -2202,7 +2202,7 @@ func TestRestoreTimeseriesCollectionsWithTTL(t *testing.T) { fcv := testutil.GetFCV(session) if cmp, err := testutil.CompareFCV(fcv, "5.0"); err != nil || cmp < 0 { - t.Skip("Requires server with FCV 5.0 or later") + t.Skipf("Found FCV %s; this test requires 5.0 or later", fcv) } dbName := t.Name() @@ -2229,8 +2229,11 @@ func TestRestoreTimeseriesCollectionsWithTTL(t *testing.T) { ctx, mongo.IndexModel{ Keys: bson.D{{"time", 1}}, - Options: mopt.Index().SetPartialFilterExpression( - bson.D{{"meta", 123123}}, + Options: mopt.Index(). + SetPartialFilterExpression( + bson.D{{"meta", 123123}}, + ).SetExpireAfterSeconds( + 123123, ), }, ) @@ -2250,6 +2253,7 @@ func TestRestoreTimeseriesCollectionsWithTTL(t *testing.T) { require.NoError(t, result.Err, "can run mongorestore (result: %+v)", result) require.EqualValues(t, 0, result.Failures, "mongorestore reports 0 failures") }) + } func TestRestoreTimeseriesCollections(t *testing.T) { @@ -2271,7 +2275,7 @@ func TestRestoreTimeseriesCollections(t *testing.T) { fcv := testutil.GetFCV(session) if cmp, err := testutil.CompareFCV(fcv, "5.0"); err != nil || cmp < 0 { - t.Skip("Requires server with FCV 5.0 or later") + t.Skipf("Found FCV %s; this test requires 5.0 or later", fcv) } Convey("With a test MongoRestore instance", t, func() { From b8ded99207ed5ea2d771260a9457b9c3d93559d5 Mon Sep 17 00:00:00 2001 From: Felipe Gasper Date: Mon, 24 Mar 2025 15:13:50 -0400 Subject: [PATCH 4/5] 7.0 only --- common/testutil/testutil.go | 38 +++++++++++++++++++++++++++---- mongorestore/mongorestore_test.go | 10 ++------ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/common/testutil/testutil.go b/common/testutil/testutil.go index a3f073c61..186db2574 100644 --- a/common/testutil/testutil.go +++ b/common/testutil/testutil.go @@ -18,6 +18,7 @@ import ( "github.com/mongodb/mongo-tools/common/db" "github.com/mongodb/mongo-tools/common/options" + "github.com/pkg/errors" "github.com/stretchr/testify/require" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" @@ -91,9 +92,11 @@ func GetBareArgs() []string { return args } -// GetFCV returns the featureCompatibilityVersion string for an mgo Session +// GetFCVErr returns the featureCompatibilityVersion string for an mgo Session // or the empty string if it can't be found. -func GetFCV(s *mongo.Client) string { +// +// See SkipUnlessFCVAtLeast() to simplify FCV checks. +func GetFCVErr(s *mongo.Client) (string, error) { coll := s.Database("admin").Collection("system.version") var result struct { Version string @@ -102,13 +105,40 @@ func GetFCV(s *mongo.Client) string { err := res.Decode(&result) if err != nil { - panic(fmt.Sprintf("failed to get FCV: %v", err)) + return "", errors.Wrap(err, "failed to get FCV") + } + + return result.Version, nil +} + +// GetFCV is like GetFCVErr but panicks instead of returning an error. +// Avoid this function in new code. +func GetFCV(s *mongo.Client) string { + version, err := GetFCVErr(s) + if err != nil { + panic(err) } - return result.Version + return version +} + +// SkipUnlessFCVAtLeast skips the present test unless the server’s FCV +// is at least minFCV. +func SkipUnlessFCVAtLeast(t *testing.T, s *mongo.Client, minFCV string) { + fcv, err := GetFCVErr(s) + require.NoError(t, err, "should get FCV") + + cmp, err := CompareFCV(fcv, minFCV) + require.NoError(t, err, "should compare FCVs (got %#q; want %#q)", fcv, minFCV) + + if cmp < 0 { + t.Skipf("This test requires a server with FCV %#q or later", minFCV) + } } // CompareFCV compares two strings as dot-delimited tuples of integers. +// +// See SkipUnlessFCVAtLeast() to simplify FCV checks. func CompareFCV(x, y string) (int, error) { left, err := dottedStringToSlice(x) if err != nil { diff --git a/mongorestore/mongorestore_test.go b/mongorestore/mongorestore_test.go index 1fee3cfd5..05854035e 100644 --- a/mongorestore/mongorestore_test.go +++ b/mongorestore/mongorestore_test.go @@ -404,6 +404,7 @@ func TestMongorestoreLongCollectionName(t *testing.T) { if err != nil { t.Fatalf("No server available") } + fcv := testutil.GetFCV(session) if cmp, err := testutil.CompareFCV(fcv, "4.4"); err != nil || cmp < 0 { t.Skip("Requires server with FCV 4.4 or later") @@ -2200,18 +2201,12 @@ func TestRestoreTimeseriesCollectionsWithTTL(t *testing.T) { session, err := sessionProvider.GetSession() require.NoError(t, err, "should find session") - fcv := testutil.GetFCV(session) - if cmp, err := testutil.CompareFCV(fcv, "5.0"); err != nil || cmp < 0 { - t.Skipf("Found FCV %s; this test requires 5.0 or later", fcv) - } + testutil.SkipUnlessFCVAtLeast(t, session, "7.0") dbName := t.Name() db := session.Database(dbName) require.NoError(t, db.Drop(ctx), "should drop db") - defer func() { - _ = db.Drop(ctx) - }() err = db.CreateCollection( ctx, @@ -2253,7 +2248,6 @@ func TestRestoreTimeseriesCollectionsWithTTL(t *testing.T) { require.NoError(t, result.Err, "can run mongorestore (result: %+v)", result) require.EqualValues(t, 0, result.Failures, "mongorestore reports 0 failures") }) - } func TestRestoreTimeseriesCollections(t *testing.T) { From 3464ed3cb742252115169855c07dc691da74812d Mon Sep 17 00:00:00 2001 From: Felipe Gasper Date: Mon, 24 Mar 2025 15:14:41 -0400 Subject: [PATCH 5/5] save --- mongorestore/mongorestore_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mongorestore/mongorestore_test.go b/mongorestore/mongorestore_test.go index 05854035e..0818f136c 100644 --- a/mongorestore/mongorestore_test.go +++ b/mongorestore/mongorestore_test.go @@ -404,7 +404,6 @@ func TestMongorestoreLongCollectionName(t *testing.T) { if err != nil { t.Fatalf("No server available") } - fcv := testutil.GetFCV(session) if cmp, err := testutil.CompareFCV(fcv, "4.4"); err != nil || cmp < 0 { t.Skip("Requires server with FCV 4.4 or later") @@ -2269,7 +2268,7 @@ func TestRestoreTimeseriesCollections(t *testing.T) { fcv := testutil.GetFCV(session) if cmp, err := testutil.CompareFCV(fcv, "5.0"); err != nil || cmp < 0 { - t.Skipf("Found FCV %s; this test requires 5.0 or later", fcv) + t.Skip("Requires server with FCV 5.0 or later") } Convey("With a test MongoRestore instance", t, func() {