From 7577e43dd7c43f80ed084bf6624b0cd22fe6514c Mon Sep 17 00:00:00 2001 From: Jemiah Westerman Date: Tue, 19 Aug 2025 14:36:42 -0700 Subject: [PATCH 1/2] Upgrade to MongoDB client 2.3.0 --- api/api_gomux.go | 14 ++++---- api/api_test.go | 8 ++--- cdc/changestream/server.go | 4 +-- cdc/changestream/server_test.go | 4 +-- cdc/store.go | 12 ++++--- cdc/store_test.go | 8 ++--- db/db.go | 22 +++--------- entity/entity.go | 13 ++++---- entity/store.go | 36 ++++++++++++-------- entity/store_test.go | 59 +++++++++++++++++++++------------ entity/v09_test.go | 31 +++++++++-------- go.mod | 7 ++-- go.sum | 14 ++++---- server/server.go | 2 +- server/server_test.go | 2 +- test/db.go | 13 ++------ test/mock/changestream.go | 15 ++++----- test/mock/db.go | 2 +- 18 files changed, 133 insertions(+), 133 deletions(-) diff --git a/api/api_gomux.go b/api/api_gomux.go index d678d0f..584b377 100644 --- a/api/api_gomux.go +++ b/api/api_gomux.go @@ -18,7 +18,7 @@ import ( "github.com/gorilla/websocket" httpSwagger "github.com/swaggo/http-swagger" - "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/v2/bson" "github.com/square/etre" "github.com/square/etre/app" @@ -494,13 +494,13 @@ func (api *API) id(next http.Handler) http.Handler { rc := ctx.Value(reqKey).(*req) var err error - var entityId primitive.ObjectID + var entityId bson.ObjectID id := r.PathValue("id") // 1. from URL if id == "" { err = ErrMissingParam.New("missing id param") } else { - entityId, err = primitive.ObjectIDFromHex(id) // 2. convert to/validate as ObjectID + entityId, err = bson.ObjectIDFromHex(id) // 2. convert to/validate as ObjectID if err != nil { err = ErrInvalidParam.New("id '%s' is not a valid ObjectID: %v", id, err) } @@ -1309,8 +1309,8 @@ func (api *API) WriteResult(rc *req, w http.ResponseWriter, ids interface{}, err diffs := ids.([]etre.Entity) writes = make([]etre.Write, len(diffs)) for i, diff := range diffs { - // _id from db is primitive.ObjectID, convert to string - id := diff["_id"].(primitive.ObjectID).Hex() + // _id from db is bson.ObjectID, convert to string + id := diff["_id"].(bson.ObjectID).Hex() writes[i] = etre.Write{ EntityId: id, URI: api.addr + etre.API_ROOT + "/entity/" + id, @@ -1336,9 +1336,9 @@ func (api *API) WriteResult(rc *req, w http.ResponseWriter, ids interface{}, err // Entity from DeleteLabel diff := ids.(etre.Entity) - // _id from db is primitive.ObjectID, convert to string + // _id from db is bson.ObjectID, convert to string if diff != nil && diff["_id"] != nil { - id = diff["_id"].(primitive.ObjectID).Hex() + id = diff["_id"].(bson.ObjectID).Hex() } writes = []etre.Write{ { diff --git a/api/api_test.go b/api/api_test.go index b3665e1..0b1dbd1 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -16,7 +16,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/v2/bson" "github.com/square/etre" "github.com/square/etre/api" @@ -60,9 +60,9 @@ var testEntities = []etre.Entity{ var testEntityIds = []string{"59f10d2a5669fc79103a0000", "59f10d2a5669fc79103a1111", "59f10d2a5669fc79103a2222"} var ( - testEntityId0, _ = primitive.ObjectIDFromHex(testEntityIds[0]) - testEntityId1, _ = primitive.ObjectIDFromHex(testEntityIds[1]) - testEntityId2, _ = primitive.ObjectIDFromHex(testEntityIds[2]) + testEntityId0, _ = bson.ObjectIDFromHex(testEntityIds[0]) + testEntityId1, _ = bson.ObjectIDFromHex(testEntityIds[1]) + testEntityId2, _ = bson.ObjectIDFromHex(testEntityIds[2]) ) var testEntitiesWithObjectIDs = []etre.Entity{ diff --git a/cdc/changestream/server.go b/cdc/changestream/server.go index 562eac4..9260e1b 100644 --- a/cdc/changestream/server.go +++ b/cdc/changestream/server.go @@ -11,8 +11,8 @@ import ( "github.com/square/etre" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" ) var ( diff --git a/cdc/changestream/server_test.go b/cdc/changestream/server_test.go index 80ec494..2cacdf3 100644 --- a/cdc/changestream/server_test.go +++ b/cdc/changestream/server_test.go @@ -9,8 +9,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" "github.com/square/etre" "github.com/square/etre/cdc" diff --git a/cdc/store.go b/cdc/store.go index ffdcf8c..986cac7 100644 --- a/cdc/store.go +++ b/cdc/store.go @@ -11,9 +11,9 @@ import ( "sort" "time" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" "github.com/square/etre" ) @@ -107,8 +107,10 @@ func (s *store) Read(f Filter) ([]etre.CDCEvent, error) { // etre.CDC to match so, below, cursor.All() doesn't have to realloc the // slice. For small fetches, this is overkill, but it makes large fetchs // (>100k events) very quick and efficient. - opts := options.Count().SetMaxTime(5 * time.Second) - count, err := s.coll.CountDocuments(context.TODO(), q, opts) + opts := options.Count() + cctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + count, err := s.coll.CountDocuments(cctx, q, opts) if err != nil { return nil, err } diff --git a/cdc/store_test.go b/cdc/store_test.go index 8dd5490..bbde0a3 100644 --- a/cdc/store_test.go +++ b/cdc/store_test.go @@ -12,9 +12,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" "github.com/square/etre" "github.com/square/etre/cdc" @@ -47,7 +47,7 @@ func setup(t *testing.T, fallbackFile string, wrp cdc.RetryPolicy) cdc.Store { // First time, create unique index on "x" if coll == nil { iv := cdcColl.Indexes() - if _, err := iv.DropAll(context.TODO()); err != nil { + if err := iv.DropAll(context.TODO()); err != nil { t.Fatal(err) } idx := mongo.IndexModel{ diff --git a/db/db.go b/db/db.go index 50d8c88..ad4d516 100644 --- a/db/db.go +++ b/db/db.go @@ -3,15 +3,14 @@ package db import ( - "context" "crypto/tls" "crypto/x509" "io/ioutil" "log" "time" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" "github.com/square/etre/config" ) @@ -68,24 +67,11 @@ func (d Default) Connect(cfg config.DatasourceConfig) (*mongo.Client, error) { log.Printf("WARNING: No database username for %s specified in config. Authentication will fail unless MongoDB access control is disabled.", cfg.URL) } - client, err := mongo.NewClient(opts) - if err != nil { - return nil, err - } - - // mongo.Connect() does not actually connect: - // The Client.Connect method starts background goroutines to monitor the - // state of the deployment and does not do any I/O in the main goroutine to - // prevent the main goroutine from blocking. Therefore, it will not error if - // the deployment is down. - // https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo?tab=doc#Connect + // mongo.Connect() does not actually connect to the database. // The caller must call client.Ping() to actually connect. Consequently, // we don't need a context here. As long as there's not a bug in the mongo // driver, this won't block. - if err := client.Connect(context.Background()); err != nil { - return nil, err - } - return client, nil + return mongo.Connect(opts) } func loadTLS(cfg config.DatasourceConfig) (*tls.Config, error) { diff --git a/entity/entity.go b/entity/entity.go index c7960b1..b551eff 100644 --- a/entity/entity.go +++ b/entity/entity.go @@ -8,9 +8,8 @@ import ( "github.com/square/etre" "github.com/square/etre/query" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" ) type DbError struct { @@ -66,16 +65,16 @@ func Filter(q query.Query) bson.M { if p.Label == etre.META_LABEL_ID { switch p.Value.(type) { case string: - id, _ := primitive.ObjectIDFromHex(p.Value.(string)) + id, _ := bson.ObjectIDFromHex(p.Value.(string)) filter[p.Label] = bson.M{operatorMap[p.Operator]: id} case []string: vals := p.Value.([]string) - oids := make([]primitive.ObjectID, len(vals)) + oids := make([]bson.ObjectID, len(vals)) for i, v := range vals { - oids[i], _ = primitive.ObjectIDFromHex(v) + oids[i], _ = bson.ObjectIDFromHex(v) } filter[p.Label] = bson.M{operatorMap[p.Operator]: oids} - case primitive.ObjectID: + case bson.ObjectID: filter[p.Label] = bson.M{operatorMap[p.Operator]: p.Value} default: panic(fmt.Sprintf("invalid _id value type: %T", p.Value)) diff --git a/entity/store.go b/entity/store.go index 18e80ca..b4bace7 100644 --- a/entity/store.go +++ b/entity/store.go @@ -4,12 +4,12 @@ package entity import ( "context" + "errors" "time" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" "github.com/square/etre" "github.com/square/etre/cdc" @@ -58,7 +58,17 @@ func (s store) ReadEntities(ctx context.Context, entityType string, q query.Quer // "es -u node.metacluster zone=pd" returns a list of unique metacluster names. // This is 10x faster than "es node.metacluster zone=pd | sort -u". if len(f.ReturnLabels) == 1 && f.Distinct { - values, err := c.Distinct(ctx, f.ReturnLabels[0], Filter(q)) + dr := c.Distinct(ctx, f.ReturnLabels[0], Filter(q)) + if err := dr.Err(); err != nil { + nfe := mongo.ErrNoDocuments + if errors.Is(err, nfe) { + // No documents found, return empty slice + return []etre.Entity{}, nil + } + return nil, s.dbError(ctx, err, "db-read-distinct") + } + var values []string + err := dr.Decode(&values) if err != nil { return nil, s.dbError(ctx, err, "db-read-distinct") } @@ -122,7 +132,7 @@ func (s store) CreateEntities(ctx context.Context, wo WriteOp, entities []etre.E now := time.Now().UnixNano() for i := range entities { - entities[i]["_id"] = primitive.NewObjectID() + entities[i]["_id"] = bson.NewObjectID() entities[i]["_type"] = wo.EntityType entities[i]["_rev"] = int64(0) entities[i]["_created"] = now @@ -132,7 +142,7 @@ func (s store) CreateEntities(ctx context.Context, wo WriteOp, entities []etre.E if err != nil { return newIds, s.dbError(ctx, err, "db-insert") } - id := res.InsertedID.(primitive.ObjectID) + id := res.InsertedID.(bson.ObjectID) newIds = append(newIds, id.Hex()) // Create a CDC event. @@ -195,7 +205,7 @@ func (s store) UpdateEntities(ctx context.Context, wo WriteOp, q query.Query, pa } opts := options.FindOneAndUpdate().SetProjection(p) - nextId := map[string]primitive.ObjectID{} + nextId := map[string]bson.ObjectID{} for cursor.Next(ctx) { if err := cursor.Decode(&nextId); err != nil { return diffs, s.dbError(ctx, err, "db-cursor-decode") @@ -222,7 +232,7 @@ func (s store) UpdateEntities(ctx context.Context, wo WriteOp, q query.Query, pa cp := cdcPartial{ op: "u", - id: orig["_id"].(primitive.ObjectID), + id: orig["_id"].(bson.ObjectID), rev: orig.Rev() + 1, old: &old, new: &patch, @@ -266,7 +276,7 @@ func (s store) DeleteEntities(ctx context.Context, wo WriteOp, q query.Query) ([ deleted = append(deleted, old) ce := cdcPartial{ op: "d", - id: old["_id"].(primitive.ObjectID), + id: old["_id"].(bson.ObjectID), old: &old, new: nil, rev: old.Rev() + 1, @@ -286,7 +296,7 @@ func (s store) DeleteLabel(ctx context.Context, wo WriteOp, label string) (etre. panic("invalid entity type passed to DeleteLabel: " + wo.EntityType) } - id, _ := primitive.ObjectIDFromHex(wo.EntityId) + id, _ := bson.ObjectIDFromHex(wo.EntityId) filter := bson.M{"_id": id} update := bson.M{ "$unset": bson.M{label: ""}, // removes label, Mongo expects "" (see $unset docs) @@ -322,7 +332,7 @@ func (s store) DeleteLabel(ctx context.Context, wo WriteOp, label string) (etre. cp := cdcPartial{ op: "u", - id: old["_id"].(primitive.ObjectID), + id: old["_id"].(bson.ObjectID), new: &new, old: &old, rev: old.Rev() + 1, @@ -355,7 +365,7 @@ func (s store) dbError(ctx context.Context, err error, errType string) error { // which makes a complete CDCEvent from the partial and a WriteOp. type cdcPartial struct { op string - id primitive.ObjectID + id bson.ObjectID old *etre.Entity new *etre.Entity rev int64 diff --git a/entity/store_test.go b/entity/store_test.go index 1b1a0a4..84389a8 100644 --- a/entity/store_test.go +++ b/entity/store_test.go @@ -9,10 +9,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" "github.com/square/etre" "github.com/square/etre/config" @@ -51,7 +50,7 @@ func setup(t *testing.T, cdcm *mock.CDCStore) entity.Store { // Create unique index on "x" iv := nodesColl.Indexes() - if _, err := iv.DropAll(context.TODO()); err != nil { + if err := iv.DropAll(context.TODO()); err != nil { // Don't error; Mongo returns an error if the namespace doesn't exist, // which is what we want, so it's not an error in this context t.Log(err) @@ -73,7 +72,7 @@ func setup(t *testing.T, cdcm *mock.CDCStore) entity.Store { require.NoError(t, err) require.Len(t, res.InsertedIDs, len(testNodes), "mongo-driver returned %d doc ids, expected %d", len(res.InsertedIDs), len(testNodes)) for i, id := range res.InsertedIDs { - testNodes[i]["_id"] = id.(primitive.ObjectID) + testNodes[i]["_id"] = id.(bson.ObjectID) } testConfig := config.EntityConfig{ @@ -186,6 +185,22 @@ func TestReadEntitiesFilterDistinct(t *testing.T) { assert.Equal(t, expect, got) } +func TestReadEntitiesFilterDistinctNoResult(t *testing.T) { + // Test that etre.QueryFilter{Distinct: true} returns an empty list + // if no rows match the query. + store := setup(t, &mock.CDCStore{}) + q, err := query.Translate("BOGUS") // test nodes do not have the label + require.NoError(t, err) + + f := etre.QueryFilter{ + ReturnLabels: []string{"BOGUS"}, // only works with 1 return label + Distinct: true, + } + got, err := store.ReadEntities(context.Background(), entityType, q, f) + require.NoError(t, err) + assert.Empty(t, got) +} + func TestReadEntitiesFilterReturnLabels(t *testing.T) { // Test that etre.QueryFilter{ReturnLabels: []string{x}} returns only that // label and not the others (y, z, bar, foo). We'll select/match by label y @@ -248,9 +263,9 @@ func TestCreateEntitiesMultiple(t *testing.T) { assert.Len(t, ids, len(testData)) // Verify that the last CDC event we create is as expected. - id1, _ := primitive.ObjectIDFromHex(ids[0]) - id2, _ := primitive.ObjectIDFromHex(ids[1]) - id3, _ := primitive.ObjectIDFromHex(ids[2]) + id1, _ := bson.ObjectIDFromHex(ids[0]) + id2, _ := bson.ObjectIDFromHex(ids[1]) + id3, _ := bson.ObjectIDFromHex(ids[2]) createTime, ok := (*gotEvents[0].New)["_created"].(int64) require.True(t, ok, "expected _created to be int64, got %T", (*gotEvents[0].New)["_created"]) @@ -322,7 +337,7 @@ func TestCreateEntitiesMultiplePartialSuccess(t *testing.T) { assert.Len(t, ids, 1) // Only x=5 written/inserted, so only a CDC event for it - id1, _ := primitive.ObjectIDFromHex(ids[0]) + id1, _ := bson.ObjectIDFromHex(ids[0]) upd1 := (*gotEvents[0].New)["_updated"].(int64) expectEvents := []etre.CDCEvent{ { @@ -425,9 +440,9 @@ func TestUpdateEntities(t *testing.T) { gotEvents[i].Id = "" gotEvents[i].Ts = 0 } - id1, _ := testNodes[0]["_id"].(primitive.ObjectID) - id2, _ := testNodes[1]["_id"].(primitive.ObjectID) - id3, _ := testNodes[2]["_id"].(primitive.ObjectID) + id1, _ := testNodes[0]["_id"].(bson.ObjectID) + id2, _ := testNodes[1]["_id"].(bson.ObjectID) + id3, _ := testNodes[2]["_id"].(bson.ObjectID) upd1 := (*gotEvents[0].New)["_updated"].(int64) upd2 := (*gotEvents[1].New)["_updated"].(int64) upd3 := (*gotEvents[2].New)["_updated"].(int64) @@ -492,7 +507,7 @@ func TestUpdateEntitiesById(t *testing.T) { // ---------------------------------------------------------------------- // This matches first test node - q, _ := query.Translate("_id=" + testNodes[0]["_id"].(primitive.ObjectID).Hex()) + q, _ := query.Translate("_id=" + testNodes[0]["_id"].(bson.ObjectID).Hex()) patch := etre.Entity{"y": "y"} // y=a -> y=y wo1 := entity.WriteOp{ EntityType: entityType, @@ -553,9 +568,9 @@ func TestUpdateEntitiesById(t *testing.T) { gotEvents[i].Id = "" gotEvents[i].Ts = 0 } - id1, _ := testNodes[0]["_id"].(primitive.ObjectID) - id2, _ := testNodes[1]["_id"].(primitive.ObjectID) - id3, _ := testNodes[2]["_id"].(primitive.ObjectID) + id1, _ := testNodes[0]["_id"].(bson.ObjectID) + id2, _ := testNodes[1]["_id"].(bson.ObjectID) + id3, _ := testNodes[2]["_id"].(bson.ObjectID) upd1 := (*gotEvents[0].New)["_updated"].(int64) upd2 := (*gotEvents[1].New)["_updated"].(int64) upd3 := (*gotEvents[2].New)["_updated"].(int64) @@ -666,9 +681,9 @@ func TestDeleteEntities(t *testing.T) { gotEvents[i].Id = "" gotEvents[i].Ts = 0 } - id1, _ := testNodes[0]["_id"].(primitive.ObjectID) - id2, _ := testNodes[1]["_id"].(primitive.ObjectID) - id3, _ := testNodes[2]["_id"].(primitive.ObjectID) + id1, _ := testNodes[0]["_id"].(bson.ObjectID) + id2, _ := testNodes[1]["_id"].(bson.ObjectID) + id3, _ := testNodes[2]["_id"].(bson.ObjectID) expectEvent := []etre.CDCEvent{ { EntityId: id1.Hex(), @@ -714,7 +729,7 @@ func TestDeleteLabel(t *testing.T) { wo := entity.WriteOp{ EntityType: entityType, - EntityId: testNodes[0]["_id"].(primitive.ObjectID).Hex(), + EntityId: testNodes[0]["_id"].(bson.ObjectID).Hex(), Caller: username, } gotOld, err := store.DeleteLabel(context.Background(), wo, "foo") @@ -746,7 +761,7 @@ func TestDeleteLabel(t *testing.T) { gotEvents[i].Id = "" gotEvents[i].Ts = 0 } - id1, _ := testNodes[0]["_id"].(primitive.ObjectID) + id1, _ := testNodes[0]["_id"].(bson.ObjectID) expectedEventNew := etre.Entity{ "_id": testNodes[0]["_id"], "_type": testNodes[0]["_type"], diff --git a/entity/v09_test.go b/entity/v09_test.go index 76e1f3f..1f8a9e6 100644 --- a/entity/v09_test.go +++ b/entity/v09_test.go @@ -9,10 +9,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" "github.com/square/etre" "github.com/square/etre/config" @@ -41,7 +40,7 @@ func setupV09(t *testing.T, cdcm *mock.CDCStore) entity.Store { // First time, create unique index on "x" if coll == nil { iv := nodesColl.Indexes() - _, err = iv.DropAll(context.TODO()) + err = iv.DropAll(context.TODO()) require.NoError(t, err) idx := mongo.IndexModel{ @@ -66,8 +65,8 @@ func setupV09(t *testing.T, cdcm *mock.CDCStore) entity.Store { require.NoError(t, err) assert.Len(t, res.InsertedIDs, len(v09testNodes)) for i, id := range res.InsertedIDs { - v09testNodes[i]["_id"] = id.(primitive.ObjectID) - v09testNodes_int32[i]["_id"] = id.(primitive.ObjectID) + v09testNodes[i]["_id"] = id.(bson.ObjectID) + v09testNodes_int32[i]["_id"] = id.(bson.ObjectID) } testConfig := config.EntityConfig{ @@ -99,9 +98,9 @@ func TestV09CreateEntitiesMultiple(t *testing.T) { assert.Len(t, ids, len(testData)) // Verify that the last CDC event we create is as expected. - id1, _ := primitive.ObjectIDFromHex(ids[0]) - id2, _ := primitive.ObjectIDFromHex(ids[1]) - id3, _ := primitive.ObjectIDFromHex(ids[2]) + id1, _ := bson.ObjectIDFromHex(ids[0]) + id2, _ := bson.ObjectIDFromHex(ids[1]) + id3, _ := bson.ObjectIDFromHex(ids[2]) upd := (*gotEvents[0].New)["_updated"].(int64) expectEvents := []etre.CDCEvent{ { @@ -184,7 +183,7 @@ func TestV09UpdateEntities(t *testing.T) { gotEvents[i].Id = "" gotEvents[i].Ts = 0 } - id1, _ := v09testNodes[0]["_id"].(primitive.ObjectID) + id1, _ := v09testNodes[0]["_id"].(bson.ObjectID) upd := (*gotEvents[0].New)["_updated"].(int64) expectEvent := []etre.CDCEvent{ { @@ -234,9 +233,9 @@ func TestV09DeleteEntities(t *testing.T) { gotEvents[i].Id = "" gotEvents[i].Ts = 0 } - id1, _ := v09testNodes[0]["_id"].(primitive.ObjectID) - id2, _ := v09testNodes[1]["_id"].(primitive.ObjectID) - id3, _ := v09testNodes[2]["_id"].(primitive.ObjectID) + id1, _ := v09testNodes[0]["_id"].(bson.ObjectID) + id2, _ := v09testNodes[1]["_id"].(bson.ObjectID) + id3, _ := v09testNodes[2]["_id"].(bson.ObjectID) expectEvent := []etre.CDCEvent{ { EntityId: id1.Hex(), @@ -278,7 +277,7 @@ func TestV09DeleteLabel(t *testing.T) { wo := entity.WriteOp{ EntityType: entityType, - EntityId: v09testNodes[0]["_id"].(primitive.ObjectID).Hex(), + EntityId: v09testNodes[0]["_id"].(bson.ObjectID).Hex(), Caller: username, } gotOld, err := store.DeleteLabel(context.Background(), wo, "y") @@ -312,7 +311,7 @@ func TestV09DeleteLabel(t *testing.T) { gotEvents[i].Id = "" gotEvents[i].Ts = 0 } - id1, _ := v09testNodes[0]["_id"].(primitive.ObjectID) + id1, _ := v09testNodes[0]["_id"].(bson.ObjectID) expectedEventNew := etre.Entity{ "_id": v09testNodes[0]["_id"], "_type": v09testNodes[0]["_type"], diff --git a/go.mod b/go.mod index 67c9e2f..add862a 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/stretchr/testify v1.9.0 github.com/swaggo/http-swagger v1.3.4 github.com/swaggo/swag v1.16.4 - go.mongodb.org/mongo-driver v1.17.1 + go.mongodb.org/mongo-driver/v2 v2.3.0 gopkg.in/yaml.v2 v2.4.0 ) @@ -25,11 +25,10 @@ require ( github.com/go-openapi/jsonreference v0.21.0 // indirect github.com/go-openapi/spec v0.21.0 // indirect github.com/go-openapi/swag v0.23.0 // indirect - github.com/golang/snappy v0.0.4 // indirect + github.com/golang/snappy v1.0.0 // indirect github.com/josharian/intern v1.0.0 // indirect - github.com/klauspost/compress v1.13.6 // indirect + github.com/klauspost/compress v1.16.7 // indirect github.com/mailru/easyjson v0.7.7 // indirect - github.com/montanaflynn/stats v0.7.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/swaggo/files v1.0.1 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect diff --git a/go.sum b/go.sum index 4c10712..82116d4 100644 --- a/go.sum +++ b/go.sum @@ -20,24 +20,22 @@ github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U= github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY= github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= -github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/golang/snappy v1.0.0 h1:Oy607GVXHs7RtbggtPBnr2RmDArIsAefDwvrdWvRhGs= +github.com/golang/snappy v1.0.0/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= -github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= -github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= +github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= -github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE= -github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= @@ -60,8 +58,8 @@ github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gi github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM= github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.mongodb.org/mongo-driver v1.17.1 h1:Wic5cJIwJgSpBhe3lx3+/RybR5PiYRMpVFgO7cOHyIM= -go.mongodb.org/mongo-driver v1.17.1/go.mod h1:wwWm/+BuOddhcq3n68LKRmgk2wXzmF6s0SFOa0GINL4= +go.mongodb.org/mongo-driver/v2 v2.3.0 h1:sh55yOXA2vUjW1QYw/2tRlHSQViwDyPnW61AwpZ4rtU= +go.mongodb.org/mongo-driver/v2 v2.3.0/go.mod h1:jHeEDJHJq7tm6ZF45Issun9dbogjfnPySb1vXA7EeAI= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34= diff --git a/server/server.go b/server/server.go index 5a2922a..2a0fe00 100644 --- a/server/server.go +++ b/server/server.go @@ -8,7 +8,7 @@ import ( "log" "time" - "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/v2/mongo" "github.com/square/etre" "github.com/square/etre/api" diff --git a/server/server_test.go b/server/server_test.go index 8d8a413..468bfdf 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/v2/mongo" "github.com/square/etre/app" "github.com/square/etre/auth" diff --git a/test/db.go b/test/db.go index c42f00f..f5cc523 100644 --- a/test/db.go +++ b/test/db.go @@ -3,12 +3,10 @@ package test import ( - "context" "log" - "time" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" ) var ( @@ -20,15 +18,10 @@ var ( func DbCollections(entityTypes []string) (*mongo.Client, map[string]*mongo.Collection, error) { url := "mongodb://" + url log.Printf("Connecting to %s", url) - client, err := mongo.NewClient(options.Client().ApplyURI(url)) + client, err := mongo.Connect(options.Client().ApplyURI(url)) if err != nil { return nil, nil, err } - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - if err := client.Connect(ctx); err != nil { - return nil, nil, err - } coll := map[string]*mongo.Collection{} for _, t := range entityTypes { coll[t] = client.Database(database).Collection(t) diff --git a/test/mock/changestream.go b/test/mock/changestream.go index a9aea4c..3979928 100644 --- a/test/mock/changestream.go +++ b/test/mock/changestream.go @@ -3,8 +3,7 @@ package mock import ( - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/v2/bson" "github.com/square/etre" "github.com/square/etre/cdc/changestream" @@ -106,10 +105,10 @@ func (s Stream) Error() error { // -------------------------------------------------------------------------- var RawInsertEvents = []bson.M{ - primitive.M{ - "_id": primitive.M{"_data": primitive.Binary{Data: []uint8{0x82, 0x5e, 0x8f, 0x5c, 0xf7, 0x0, 0x0, 0x0, 0x26, 0x46, 0x3c, 0x5f, 0x69, 0x64, 0x0, 0x3c, 0x61, 0x62, 0x63, 0x0, 0x0, 0x5a, 0x10, 0x4, 0x4d, 0x5e, 0xa3, 0x2c, 0x5c, 0x27, 0x4c, 0xf8, 0xb1, 0xad, 0x80, 0x30, 0x3a, 0x44, 0xbc, 0x82, 0x4}}}, - "documentKey": primitive.M{"_id": "abc"}, - "fullDocument": primitive.M{ // etre.CDCEvent + { + "_id": bson.M{"_data": bson.Binary{Data: []uint8{0x82, 0x5e, 0x8f, 0x5c, 0xf7, 0x0, 0x0, 0x0, 0x26, 0x46, 0x3c, 0x5f, 0x69, 0x64, 0x0, 0x3c, 0x61, 0x62, 0x63, 0x0, 0x0, 0x5a, 0x10, 0x4, 0x4d, 0x5e, 0xa3, 0x2c, 0x5c, 0x27, 0x4c, 0xf8, 0xb1, 0xad, 0x80, 0x30, 0x3a, 0x44, 0xbc, 0x82, 0x4}}}, + "documentKey": bson.M{"_id": "abc"}, + "fullDocument": bson.M{ // etre.CDCEvent "_id": "abc", "entityId": "e13", "entityType": "node", @@ -117,9 +116,9 @@ var RawInsertEvents = []bson.M{ "rev": int64(7), "ts": 54, "user": "mike", - "new": primitive.M{"_id": "e13", "_rev": int64(7), "_type": "node", "foo": "bar"}, + "new": bson.M{"_id": "e13", "_rev": int64(7), "_type": "node", "foo": "bar"}, }, - "ns": primitive.M{"coll": "cdc", "db": "etre_test"}, + "ns": bson.M{"coll": "cdc", "db": "etre_test"}, "operationType": "insert", }, } diff --git a/test/mock/db.go b/test/mock/db.go index ea22de9..4fb9525 100644 --- a/test/mock/db.go +++ b/test/mock/db.go @@ -1,7 +1,7 @@ package mock import ( - "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/v2/mongo" "github.com/square/etre/config" "github.com/square/etre/db" From 332fd05f798bdaacc22e7e2f98605b2390c890c1 Mon Sep 17 00:00:00 2001 From: Jemiah Westerman Date: Fri, 5 Sep 2025 10:43:22 -0700 Subject: [PATCH 2/2] Update dependency minor versions --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index add862a..291c773 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/alexflint/go-arg v1.5.1 github.com/daniel-nichter/go-metrics v1.0.1 github.com/go-test/deep v1.1.1 - github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e + github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 github.com/gorilla/websocket v1.5.3 github.com/stretchr/testify v1.9.0 github.com/swaggo/http-swagger v1.3.4 diff --git a/go.sum b/go.sum index 82116d4..3570107 100644 --- a/go.sum +++ b/go.sum @@ -18,8 +18,8 @@ github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+Gr github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ= github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U= github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= -github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY= -github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8JmEHVZIycC7hBoQxHH9pNKQORJNozsQ= +github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw= github.com/golang/snappy v1.0.0 h1:Oy607GVXHs7RtbggtPBnr2RmDArIsAefDwvrdWvRhGs= github.com/golang/snappy v1.0.0/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=