From 13a7c038a32911e39c6987a94e5527433549b1e9 Mon Sep 17 00:00:00 2001 From: Qingyang Hu Date: Wed, 9 Aug 2023 19:22:11 -0400 Subject: [PATCH] e2e wip --- .evergreen/config.yml | 38 +++++------ Makefile | 4 ++ internal/test/searchindex/mongodb/go.mod | 1 - internal/test/searchindex/mongodb/go.sum | 5 -- internal/test/searchindex/mongodb/main.go | 81 +++++++---------------- mongo/search_index_prose_test.go | 66 ++++++++++++++++++ 6 files changed, 111 insertions(+), 84 deletions(-) create mode 100644 mongo/search_index_prose_test.go diff --git a/.evergreen/config.yml b/.evergreen/config.yml index 6981c48646..d7bbc3e663 100644 --- a/.evergreen/config.yml +++ b/.evergreen/config.yml @@ -706,6 +706,18 @@ functions: params: file: lb-expansion.yml + run-search-index-tests: + - command: shell.exec + type: test + params: + shell: "bash" + working_dir: src/go.mongodb.org/mongo-driver + script: | + ${PREPARE_SHELL} + SEARCH_INDEX_TEST=1 \ + MONGODB_URI="${MONGODB_URI}" \ + make evg-test-search-index + stop-load-balancer: - command: shell.exec params: @@ -2273,22 +2285,11 @@ tasks: - name: "test-aws-searchindex-deployed" commands: - - command: ec2.assume_role - params: - role_arn: ${LAMBDA_AWS_ROLE_ARN} - duration_seconds: 3600 - - command: shell.exec - params: - working_dir: src/go.mongodb.org/mongo-driver - shell: bash - add_expansions_to_env: true - env: - TEST_LAMBDA_DIRECTORY: ${PROJECT_DIRECTORY}/internal/test/searchindex - LAMBDA_STACK_NAME: dbx-go-lambda - AWS_REGION: us-east-1 - script: | - ${PREPARE_SHELL} - ./.evergreen/run-deployed-lambda-aws-tests.sh + - func: "bootstrap-mongo-orchestration" + vars: + VERSION: "latest" + TOPOLOGY: "replica_set" + - func: "run-search-index-tests" axes: - id: version @@ -2657,8 +2658,6 @@ task_groups: binary: bash add_expansions_to_env: true env: - LAMBDA_STACK_NAME: dbx-go-lambda - AWS_REGION: us-east-1 MONGODB_VERSION: "7.0" args: - ${DRIVERS_TOOLS}/.evergreen/atlas/setup-atlas-cluster.sh @@ -2671,9 +2670,6 @@ task_groups: working_dir: src/go.mongodb.org/mongo-driver binary: bash add_expansions_to_env: true - env: - LAMBDA_STACK_NAME: dbx-go-lambda - AWS_REGION: us-east-1 args: - ${DRIVERS_TOOLS}/.evergreen/atlas/teardown-atlas-cluster.sh setup_group_can_fail_task: true diff --git a/Makefile b/Makefile index f3d3b88292..8c7b494e93 100644 --- a/Makefile +++ b/Makefile @@ -160,6 +160,10 @@ evg-test-load-balancers: go test $(BUILD_TAGS) ./mongo/integration -run TestLoadBalancerSupport -v -timeout $(TEST_TIMEOUT)s >> test.suite go test $(BUILD_TAGS) ./mongo/integration/unified -run TestUnifiedSpec -v -timeout $(TEST_TIMEOUT)s >> test.suite +.PHONY: evg-test-search-index +evg-test-search-index: + go test ./mongo -run TestSearchIndexProse -v -timeout $(TEST_TIMEOUT)s + .PHONY: evg-test-ocsp evg-test-ocsp: go test -v ./mongo -run TestOCSP $(OCSP_TLS_SHOULD_SUCCEED) >> test.suite diff --git a/internal/test/searchindex/mongodb/go.mod b/internal/test/searchindex/mongodb/go.mod index 7ad90aad0b..24febc65e0 100644 --- a/internal/test/searchindex/mongodb/go.mod +++ b/internal/test/searchindex/mongodb/go.mod @@ -5,7 +5,6 @@ go 1.20 replace go.mongodb.org/mongo-driver => ../../../../ require ( - github.com/aws/aws-lambda-go v1.41.0 github.com/google/uuid v1.3.0 // Note that the Go driver version is replaced with the local Go driver code // by the replace directive above. diff --git a/internal/test/searchindex/mongodb/go.sum b/internal/test/searchindex/mongodb/go.sum index 96fdb1799b..42c3a85d34 100644 --- a/internal/test/searchindex/mongodb/go.sum +++ b/internal/test/searchindex/mongodb/go.sum @@ -1,5 +1,3 @@ -github.com/aws/aws-lambda-go v1.41.0 h1:l/5fyVb6Ud9uYd411xdHZzSf2n86TakxzpvIoz7l+3Y= -github.com/aws/aws-lambda-go v1.41.0/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= @@ -12,8 +10,6 @@ github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQ github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= @@ -56,4 +52,3 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/internal/test/searchindex/mongodb/main.go b/internal/test/searchindex/mongodb/main.go index 6fe7047d2c..ba3cb5a69c 100644 --- a/internal/test/searchindex/mongodb/main.go +++ b/internal/test/searchindex/mongodb/main.go @@ -8,14 +8,10 @@ package main import ( "context" - "encoding/json" "fmt" - "net/http" "os" "time" - "github.com/aws/aws-lambda-go/events" - "github.com/aws/aws-lambda-go/lambda" "github.com/google/uuid" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" @@ -24,83 +20,54 @@ import ( const timeout = 5 * time.Minute -// response is the data we return in the body of the API Gateway response. -type response struct { - Index string `json:"index"` - Cursor string `json:"cursor"` -} - -// gateway500 is a convenience function for constructing a gateway response with -// a 500 status code, indicating an internal server error. -func gateway500() events.APIGatewayProxyResponse { - return events.APIGatewayProxyResponse{ - StatusCode: http.StatusInternalServerError, - Body: http.StatusText(http.StatusInternalServerError), - } - -} - -// handler is the AWS Lambda handler, executing at runtime. -func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { - ctx, cancel := context.WithTimeout(ctx, timeout) - defer cancel() +func main() { + ctx := context.Background() - clientOptions := options.Client().ApplyURI(os.Getenv("MONGODB_URI")). + uri := os.Getenv("MONGODB_URI") + clientOptions := options.Client().ApplyURI(uri). SetTimeout(timeout) // Create a MongoClient that points to MONGODB_URI and listens to the // ComandMonitor, ServerMonitor, and PoolMonitor events. client, err := mongo.Connect(ctx, clientOptions) if err != nil { - return gateway500(), fmt.Errorf("failed to create client: %w", err) + panic(fmt.Errorf("failed to connect %s: %w", uri, err)) } defer client.Disconnect(ctx) - db := client.Database("test") collName := fmt.Sprintf("col%s", uuid.New().String()) - err = db.CreateCollection(ctx, collName) - if err != nil { - return gateway500(), fmt.Errorf("failed to create collection: %w", err) - } - collection := client.Database("test").Collection(collName) defer collection.Drop(ctx) + _, err = collection.InsertOne(ctx, bson.D{}) + if err != nil { + panic(fmt.Errorf("failed to insert: %w", err)) + } + view := collection.SearchIndexes() + + definition := bson.D{{"mappings", bson.D{{"dynamic", false}}}} searchName := "test-search-index" index, err := view.CreateOne(ctx, mongo.SearchIndexModel{ - Definition: bson.D{{"mappings", bson.D{{"dynamic", false}}}}, + Definition: definition, Name: &searchName, }) if err != nil { - return gateway500(), fmt.Errorf("failed to create index: %w", err) + panic(fmt.Errorf("failed to create index: %w", err)) } - time.Sleep(20 * time.Second) - - cursor, err := view.List(ctx, nil, nil) + cursor, err := view.List(ctx, &index, nil) if err != nil { - return gateway500(), fmt.Errorf("failed to list: %w", err) + panic(fmt.Errorf("failed to list: %w", err)) } - rsp := &response{ - Index: index, - Cursor: cursor.Current.String(), - } - - body, err := json.Marshal(rsp) - if err != nil { - return gateway500(), fmt.Errorf("failed to marshal: %w", err) + for i := 0; i < 3; i++ { + if !cursor.Next(ctx) { + panic(fmt.Errorf("expected aggregate to return a document, but got none")) + } + if cursor.Current.Lookup("name").StringValue() == searchName && cursor.Current.Lookup("queryable").Boolean() { + break + } + time.Sleep(5 * time.Second) } - - return events.APIGatewayProxyResponse{ - Body: string(body), - StatusCode: http.StatusOK, - }, nil -} - -func main() { - ctx := context.Background() - - lambda.StartWithOptions(handler, lambda.WithContext(ctx)) } diff --git a/mongo/search_index_prose_test.go b/mongo/search_index_prose_test.go new file mode 100644 index 0000000000..f0dde86597 --- /dev/null +++ b/mongo/search_index_prose_test.go @@ -0,0 +1,66 @@ +package mongo + +import ( + "context" + "fmt" + "os" + "testing" + "time" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/internal/assert" + "go.mongodb.org/mongo-driver/internal/uuid" + "go.mongodb.org/mongo-driver/mongo/options" +) + +func TestSearchIndexProse(t *testing.T) { + if os.Getenv("SEARCH_INDEX_TEST") == "" { + t.Skip("skipping") + } + + const timeout = 5 * time.Minute + + ctx := context.Background() + + uri := os.Getenv("MONGODB_URI") + t.Logf("connecting to %s...", uri) + clientOptions := options.Client().ApplyURI(uri).SetTimeout(timeout) + + // Create a MongoClient that points to MONGODB_URI and listens to the + // ComandMonitor, ServerMonitor, and PoolMonitor events. + client, err := Connect(ctx, clientOptions) + assert.NoError(t, err, "failed to connect %s", uri) + defer client.Disconnect(ctx) + + id, err := uuid.New() + assert.NoError(t, err) + collName := fmt.Sprintf("col%s", id.String()) + collection := client.Database("test").Collection(collName) + defer collection.Drop(ctx) + + _, err = collection.InsertOne(ctx, bson.D{}) + assert.NoError(t, err, "failed to insert") + + view := collection.SearchIndexes() + + definition := bson.D{{"mappings", bson.D{{"dynamic", false}}}} + searchName := "test-search-index" + index, err := view.CreateOne(ctx, SearchIndexModel{ + Definition: definition, + Name: &searchName, + }) + assert.NoError(t, err, "failed to create index") + + cursor, err := view.List(ctx, &index, nil) + assert.NoError(t, err, "failed to list") + + for i := 0; i < 3; i++ { + if !cursor.Next(ctx) { + assert.Fail(t, "expected aggregate to return a document, but got none") + } + if cursor.Current.Lookup("name").StringValue() == searchName && cursor.Current.Lookup("queryable").Boolean() { + break + } + time.Sleep(5 * time.Second) + } +}