55 "testing"
66 "time"
77
8+ "github.com/10gen/migration-verifier/internal/util"
9+ "github.com/10gen/migration-verifier/mslices"
810 "github.com/pkg/errors"
911 "github.com/samber/lo"
1012 "github.com/stretchr/testify/require"
@@ -35,6 +37,12 @@ func TestChangeStreamFilter(t *testing.T) {
3537// terminates that verifier, updates the source cluster, starts a new
3638// verifier with change stream, and confirms that things look as they should.
3739func (suite * IntegrationTestSuite ) TestChangeStreamResumability () {
40+ suite .Require ().NoError (
41+ suite .srcMongoClient .
42+ Database (suite .DBNameForTest ()).
43+ CreateCollection (suite .Context (), "testColl" ),
44+ )
45+
3846 func () {
3947 verifier1 := suite .BuildVerifier ()
4048 ctx , cancel := context .WithCancel (context .Background ())
@@ -43,7 +51,7 @@ func (suite *IntegrationTestSuite) TestChangeStreamResumability() {
4351 suite .Require ().NoError (err )
4452 }()
4553
46- ctx , cancel := context .WithCancel (context . Background ())
54+ ctx , cancel := context .WithCancel (suite . Context ())
4755 defer cancel ()
4856
4957 _ , err := suite .srcMongoClient .
@@ -219,19 +227,26 @@ func (suite *IntegrationTestSuite) TestNoStartAtTime() {
219227}
220228
221229func (suite * IntegrationTestSuite ) TestWithChangeEventsBatching () {
222- verifier := suite .BuildVerifier ()
230+ ctx := suite .Context ()
223231
224- ctx , cancel := context .WithCancel (context .Background ())
225- defer cancel ()
232+ db := suite .srcMongoClient .Database (suite .DBNameForTest ())
233+ coll1 := db .Collection ("testColl1" )
234+ coll2 := db .Collection ("testColl2" )
235+
236+ for _ , coll := range mslices .Of (coll1 , coll2 ) {
237+ suite .Require ().NoError (db .CreateCollection (ctx , coll .Name ()))
238+ }
239+
240+ verifier := suite .BuildVerifier ()
226241
227242 suite .Require ().NoError (verifier .StartChangeStream (ctx ))
228243
229- _ , err := suite . srcMongoClient . Database ( "testDb" ). Collection ( "testColl1" ) .InsertOne (ctx , bson.D {{"_id" , 1 }})
244+ _ , err := coll1 .InsertOne (ctx , bson.D {{"_id" , 1 }})
230245 suite .Require ().NoError (err )
231- _ , err = suite . srcMongoClient . Database ( "testDb" ). Collection ( "testColl1" ) .InsertOne (ctx , bson.D {{"_id" , 2 }})
246+ _ , err = coll1 .InsertOne (ctx , bson.D {{"_id" , 2 }})
232247 suite .Require ().NoError (err )
233248
234- _ , err = suite . srcMongoClient . Database ( "testDb" ). Collection ( "testColl2" ) .InsertOne (ctx , bson.D {{"_id" , 1 }})
249+ _ , err = coll2 .InsertOne (ctx , bson.D {{"_id" , 1 }})
235250 suite .Require ().NoError (err )
236251
237252 var rechecks []bson.M
@@ -245,6 +260,7 @@ func (suite *IntegrationTestSuite) TestWithChangeEventsBatching() {
245260 500 * time .Millisecond ,
246261 "the verifier should flush a recheck doc after a batch" ,
247262 )
263+
248264}
249265
250266func (suite * IntegrationTestSuite ) TestManyInsertsBeforeWritesOff () {
@@ -304,3 +320,40 @@ func (suite *IntegrationTestSuite) testInsertsBeforeWritesOff(docsCount int) {
304320
305321 suite .Assert ().Equal (docsCount , totalFailed , "all source docs should be missing" )
306322}
323+
324+ func (suite * IntegrationTestSuite ) TestCreateForbidden () {
325+ ctx := suite .Context ()
326+ buildInfo , err := util .GetBuildInfo (ctx , suite .srcMongoClient )
327+ suite .Require ().NoError (err )
328+
329+ if buildInfo .VersionArray [0 ] < 6 {
330+ suite .T ().Skipf ("This test requires server v6+. (Found: %v)" , buildInfo .VersionArray )
331+ }
332+
333+ verifier := suite .BuildVerifier ()
334+
335+ // start verifier
336+ verifierRunner := RunVerifierCheck (suite .Context (), suite .T (), verifier )
337+
338+ // wait for generation 0 to end
339+ verifierRunner .AwaitGenerationEnd ()
340+
341+ db := suite .srcMongoClient .Database (suite .DBNameForTest ())
342+ coll := db .Collection ("mycoll" )
343+ suite .Require ().NoError (
344+ db .CreateCollection (ctx , coll .Name ()),
345+ )
346+
347+ // The error from the create event will come either at WritesOff
348+ // or when we finalize the change stream.
349+ err = verifier .WritesOff (ctx )
350+ if err == nil {
351+ err = verifierRunner .Await ()
352+ }
353+
354+ suite .Require ().Error (err , "should detect forbidden create event" )
355+
356+ eventErr := UnknownEventError {}
357+ suite .Require ().ErrorAs (err , & eventErr )
358+ suite .Assert ().Equal ("create" , eventErr .Event .OpType )
359+ }
0 commit comments