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"
@@ -37,6 +39,12 @@ func TestChangeStreamFilter(t *testing.T) {
3739// terminates that verifier, updates the source cluster, starts a new
3840// verifier with change stream, and confirms that things look as they should.
3941func (suite * IntegrationTestSuite ) TestChangeStreamResumability () {
42+ suite .Require ().NoError (
43+ suite .srcMongoClient .
44+ Database (suite .DBNameForTest ()).
45+ CreateCollection (suite .Context (), "testColl" ),
46+ )
47+
4048 func () {
4149 verifier1 := suite .BuildVerifier ()
4250 ctx , cancel := context .WithCancel (context .Background ())
@@ -45,7 +53,7 @@ func (suite *IntegrationTestSuite) TestChangeStreamResumability() {
4553 suite .Require ().NoError (err )
4654 }()
4755
48- ctx , cancel := context .WithCancel (context . Background ())
56+ ctx , cancel := context .WithCancel (suite . Context ())
4957 defer cancel ()
5058
5159 _ , err := suite .srcMongoClient .
@@ -221,19 +229,26 @@ func (suite *IntegrationTestSuite) TestNoStartAtTime() {
221229}
222230
223231func (suite * IntegrationTestSuite ) TestWithChangeEventsBatching () {
224- verifier := suite .BuildVerifier ()
232+ ctx := suite .Context ()
225233
226- ctx , cancel := context .WithCancel (context .Background ())
227- defer cancel ()
234+ db := suite .srcMongoClient .Database (suite .DBNameForTest ())
235+ coll1 := db .Collection ("testColl1" )
236+ coll2 := db .Collection ("testColl2" )
237+
238+ for _ , coll := range mslices .Of (coll1 , coll2 ) {
239+ suite .Require ().NoError (db .CreateCollection (ctx , coll .Name ()))
240+ }
241+
242+ verifier := suite .BuildVerifier ()
228243
229244 suite .Require ().NoError (verifier .StartChangeStream (ctx ))
230245
231- _ , err := suite . srcMongoClient . Database ( "testDb" ). Collection ( "testColl1" ) .InsertOne (ctx , bson.D {{"_id" , 1 }})
246+ _ , err := coll1 .InsertOne (ctx , bson.D {{"_id" , 1 }})
232247 suite .Require ().NoError (err )
233- _ , err = suite . srcMongoClient . Database ( "testDb" ). Collection ( "testColl1" ) .InsertOne (ctx , bson.D {{"_id" , 2 }})
248+ _ , err = coll1 .InsertOne (ctx , bson.D {{"_id" , 2 }})
234249 suite .Require ().NoError (err )
235250
236- _ , err = suite . srcMongoClient . Database ( "testDb" ). Collection ( "testColl2" ) .InsertOne (ctx , bson.D {{"_id" , 1 }})
251+ _ , err = coll2 .InsertOne (ctx , bson.D {{"_id" , 1 }})
237252 suite .Require ().NoError (err )
238253
239254 var rechecks []bson.M
@@ -247,6 +262,7 @@ func (suite *IntegrationTestSuite) TestWithChangeEventsBatching() {
247262 500 * time .Millisecond ,
248263 "the verifier should flush a recheck doc after a batch" ,
249264 )
265+
250266}
251267
252268func (suite * IntegrationTestSuite ) TestCursorKilledResilience () {
@@ -392,3 +408,40 @@ func (suite *IntegrationTestSuite) testInsertsBeforeWritesOff(docsCount int) {
392408
393409 suite .Assert ().Equal (docsCount , totalFailed , "all source docs should be missing" )
394410}
411+
412+ func (suite * IntegrationTestSuite ) TestCreateForbidden () {
413+ ctx := suite .Context ()
414+ buildInfo , err := util .GetBuildInfo (ctx , suite .srcMongoClient )
415+ suite .Require ().NoError (err )
416+
417+ if buildInfo .VersionArray [0 ] < 6 {
418+ suite .T ().Skipf ("This test requires server v6+. (Found: %v)" , buildInfo .VersionArray )
419+ }
420+
421+ verifier := suite .BuildVerifier ()
422+
423+ // start verifier
424+ verifierRunner := RunVerifierCheck (suite .Context (), suite .T (), verifier )
425+
426+ // wait for generation 0 to end
427+ verifierRunner .AwaitGenerationEnd ()
428+
429+ db := suite .srcMongoClient .Database (suite .DBNameForTest ())
430+ coll := db .Collection ("mycoll" )
431+ suite .Require ().NoError (
432+ db .CreateCollection (ctx , coll .Name ()),
433+ )
434+
435+ // The error from the create event will come either at WritesOff
436+ // or when we finalize the change stream.
437+ err = verifier .WritesOff (ctx )
438+ if err == nil {
439+ err = verifierRunner .Await ()
440+ }
441+
442+ suite .Require ().Error (err , "should detect forbidden create event" )
443+
444+ eventErr := UnknownEventError {}
445+ suite .Require ().ErrorAs (err , & eventErr )
446+ suite .Assert ().Equal ("create" , eventErr .Event .OpType )
447+ }
0 commit comments