From d46f29c34e9ff3968a5c00dcdea8fb41a20a2b2f Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Wed, 20 Dec 2023 06:27:08 -0600 Subject: [PATCH] GODRIVER-3061 Support Load Balancing in Docker Container (#1494) Co-authored-by: Qingyang Hu <103950869+qingyang-hu@users.noreply.github.com> --- .evergreen/config.yml | 5 ++- .evergreen/run-tests.sh | 1 - Dockerfile | 12 ++++-- docs/CONTRIBUTING.md | 41 ++++++++++++++++++++- etc/docker_entry.sh | 7 +--- etc/run_docker.sh | 24 ++---------- mongo/client_test.go | 3 ++ mongo/integration/mtest/opmsg_deployment.go | 2 +- mongo/with_transactions_test.go | 4 ++ x/mongo/driver/operation/hello_test.go | 9 +++++ x/mongo/driver/topology/server_test.go | 3 ++ 11 files changed, 77 insertions(+), 34 deletions(-) diff --git a/.evergreen/config.yml b/.evergreen/config.yml index 490cf91311..54ad119ac9 100644 --- a/.evergreen/config.yml +++ b/.evergreen/config.yml @@ -27,7 +27,7 @@ timeout: ls -la functions: fetch-source: - # Executes git clone and applies the submitted patch, if any + # Executes clone and applies the submitted patch, if any - command: git.get_project type: system params: @@ -1526,8 +1526,8 @@ tasks: - func: run-atlas-data-lake-test - name: test-docker-runner - tags: ["pullrequest"] commands: + - func: bootstrap-mongo-orchestration - func: run-docker-test - name: test-load-balancer-noauth-nossl @@ -2585,6 +2585,7 @@ buildvariants: - name: "test-atlas-data-lake" - name: docker-runner-test + tags: ["pullrequest"] display_name: "Docker Runner Test" run_on: - ubuntu2204-large diff --git a/.evergreen/run-tests.sh b/.evergreen/run-tests.sh index d4abde9b3b..4b558bcd7d 100644 --- a/.evergreen/run-tests.sh +++ b/.evergreen/run-tests.sh @@ -18,7 +18,6 @@ fi export GOROOT="${GOROOT}" export PATH="${GOROOT}/bin:${GCC_PATH}:$GOPATH/bin:$PATH" -export PATH="${MONGODB_BINARIES:-$DRIVERS_TOOLS/mongodb/bin}:$PATH" export PROJECT="${project}" export PKG_CONFIG_PATH=$(pwd)/install/libmongocrypt/lib64/pkgconfig:$(pwd)/install/mongo-c-driver/lib/pkgconfig export LD_LIBRARY_PATH=$(pwd)/install/libmongocrypt/lib64 diff --git a/Dockerfile b/Dockerfile index d49c7ca1a7..cd56254a5e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,15 +16,19 @@ COPY etc/install-libmongocrypt.sh /root/install-libmongocrypt.sh RUN cd /root && bash ./install-libmongocrypt.sh -# Inherit from the drivers-evergreen-tools image and copy in the files -# from the libmongocrypt build stage. -FROM drivers-evergreen-tools +# Copy in the files from the libmongocrypt build stage. +FROM ubuntu:20.04 # Install common deps. RUN export DEBIAN_FRONTEND=noninteractive && \ export TZ=Etc/UTC && \ apt-get -qq update && \ apt-get -qqy install --reinstall --no-install-recommends \ + git \ + ca-certificates \ + curl \ + wget \ + sudo \ tzdata \ ca-certificates \ pkg-config \ @@ -50,4 +54,6 @@ COPY ./etc/docker_entry.sh /root/docker_entry.sh COPY --from=libmongocrypt /root/install /root/install +ENV DOCKER_RUNNING=true + ENTRYPOINT ["/bin/bash", "/root/docker_entry.sh"] diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md index ec628ae0b2..f6d629c6d2 100644 --- a/docs/CONTRIBUTING.md +++ b/docs/CONTRIBUTING.md @@ -152,19 +152,56 @@ The usage of host.docker.internal comes from the [Docker networking documentatio There is currently no arm64 support for the go1.x runtime, see [here](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html). Known issues running on linux/arm64 include the inability to network with the localhost from the public.ecr.aws/lambda/go Docker image. +### Load Balancer + +To launch the load balancer on MacOS, run the following. + +- `brew install haproxy` +- Clone drivers-evergreen-tools and save the path as `DRIVERS_TOOLS`. +- Start the servers using (or use the docker-based method below): + +```bash +LOAD_BALANCER=true TOPOLOGY=sharded_cluster AUTH=noauth SSL=nossl MONGODB_VERSION=6.0 DRIVERS_TOOLS=$PWD/drivers-evergreen-tools MONGO_ORCHESTRATION_HOME=$PWD/drivers-evergreen-tools/.evergreen/orchestration $PWD/drivers-evergreen-tools/.evergreen/run-orchestration.sh +``` + +- Start the load balancer using: + +```bash +MONGODB_URI='mongodb://localhost:27017,localhost:27018/' $PWD/drivers-evergreen-tools/.evergreen/run-load-balancer.sh start +``` + +- Run the load balancer tests (or use the docker runner below with `evg-test-load-balancers`): + +```bash +make evg-test-load-balancers +``` + ### Testing in Docker We support local testing in Docker. To test using docker, you will need to set the `DRIVERS_TOOLs` environment variable to point to a local clone of the drivers-evergreen-tools repository. This is essential for running the testing matrix in a container. You can set the `DRIVERS_TOOLS` variable in your shell profile or in your project-specific environment. +1. First, start the drivers-tools server docker container, as: + +```bash +bash $DRIVERS_TOOLS/.evergreen/docker/start-server.sh +``` + +See the readme in `$DRIVERS_TOOLS/.evergreen/docker` for more information on usage. + +2. Next, start any other required services in another terminal, like a load balancer. + +1. Finally, run the Go Driver tests using the following script in this repo: + ```bash bash etc/run_docker.sh ``` The script takes an optional argument for the `MAKEFILE_TARGET` and allows for some environment variable overrides. The docker container has the required binaries, including libmongocrypt. -The entry script starts a MongoDB topology, and then executes the desired `MAKEFILE_TARGET`. +The entry script executes the desired `MAKEFILE_TARGET`. -For example, to test against a sharded cluster, using enterprise auth, run: +For example, to test against a sharded cluster (make sure you started the server with a sharded_cluster), +using enterprise auth, run: ```bash TOPOLOGY=sharded_cluster bash etc/run_docker.sh evg-test-enterprise-auth diff --git a/etc/docker_entry.sh b/etc/docker_entry.sh index 51f31217f1..4e3e325970 100644 --- a/etc/docker_entry.sh +++ b/etc/docker_entry.sh @@ -1,17 +1,14 @@ #!/usr/bin/env bash # -# Entry point for Dockerfile for launching a server and running a go test. +# Entry point for Dockerfile for running a go test. # set -eux -# Start the server. -bash /root/base-entrypoint.sh -source $DRIVERS_TOOLS/.evergreen/mo-expansion.sh - # Prep files. cd /src rm -f test.suite cp -r $HOME/install ./install + export PATH="$MONGODB_BINARIES:$PATH" # Run the test. diff --git a/etc/run_docker.sh b/etc/run_docker.sh index a4b400035b..b6d0fc9ae5 100644 --- a/etc/run_docker.sh +++ b/etc/run_docker.sh @@ -8,33 +8,17 @@ if [ -z "$DRIVERS_TOOLS" ]; then exit 1 fi PLATFORM=${DOCKER_PLATFORM:-} - -pushd $DRIVERS_TOOLS/.evergreen/docker/ubuntu20.04 -docker build $PLATFORM -t drivers-evergreen-tools . -popd docker build $PLATFORM -t go-test . # Handle environment variables and optional positional arg for the makefile target. - MAKEFILE_TARGET=${1:-evg-test-versioned-api} -MONGODB_VERSION=${MONGODB_VERSION:-latest} -TOPOLOGY=${TOPOLOGY:-replica_set} -ORCHESTRATION_FILE=${ORCHESTRATION_FILE:-basic.json} -AUTH=${AUTH:-""} -SSL=${SSL:=""} GO_BUILD_TAGS=${GO_BUILD_TAGS:-""} -ENV="-e MONGODB_VERSION=$MONGODB_VERSION -e TOPOLOGY=$TOPOLOGY" -ENV="$ENV -e MAKEFILE_TARGET=$MAKEFILE_TARGET -e AUTH=$AUTH" -ENV="$ENV -e ORCHESTRATION_FILE=$ORCHESTRATION_FILE -e SSL=$SSL" -ENV="$ENV -e GO_BUILD_TAGS=$GO_BUILD_TAGS" - -VOL="-v `pwd`:/src" -VOL="$VOL -v $DRIVERS_TOOLS:/root/drivers-evergreen-tools" -USE_TTY="" -test -t 1 && USE_TTY="-t" +ARGS=" -e MAKEFILE_TARGET=$MAKEFILE_TARGET" +ARGS="$ARGS -e GO_BUILD_TAGS=$GO_BUILD_TAGS" +ARGS="$ARGS go-test" -docker run $PLATFORM --rm $VOL $ENV -i $USE_TTY go-test +$DRIVERS_TOOLS/.evergreen/docker/run-client.sh $ARGS if [ -f "test.suite" ]; then tail test.suite fi diff --git a/mongo/client_test.go b/mongo/client_test.go index d4aeaeacd3..013c1ae6bb 100644 --- a/mongo/client_test.go +++ b/mongo/client_test.go @@ -337,6 +337,9 @@ func TestClient(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } + if os.Getenv("DOCKER_RUNNING") != "" { + t.Skip("skipping test in docker environment") + } t.Run(tc.name, func(t *testing.T) { // Setup a client and skip the test based on server version. diff --git a/mongo/integration/mtest/opmsg_deployment.go b/mongo/integration/mtest/opmsg_deployment.go index ae4e359380..2215f84b38 100644 --- a/mongo/integration/mtest/opmsg_deployment.go +++ b/mongo/integration/mtest/opmsg_deployment.go @@ -21,7 +21,7 @@ import ( ) const ( - serverAddress = address.Address("localhost:27017") + serverAddress = address.Address("127.0.0.1:27017") maxDocumentSize uint32 = 16777216 maxMessageSize uint32 = 48000000 maxBatchCount uint32 = 100000 diff --git a/mongo/with_transactions_test.go b/mongo/with_transactions_test.go index 9a387264f9..af7ce98b0c 100644 --- a/mongo/with_transactions_test.go +++ b/mongo/with_transactions_test.go @@ -11,6 +11,7 @@ import ( "errors" "fmt" "math" + "os" "strconv" "strings" "testing" @@ -37,6 +38,9 @@ func TestConvenientTransactions(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } + if os.Getenv("DOCKER_RUNNING") != "" { + t.Skip("skipping test in docker environment") + } client := setupConvenientTransactions(t) db := client.Database("TestConvenientTransactions") diff --git a/x/mongo/driver/operation/hello_test.go b/x/mongo/driver/operation/hello_test.go index 114f53b617..6bfaf00f6d 100644 --- a/x/mongo/driver/operation/hello_test.go +++ b/x/mongo/driver/operation/hello_test.go @@ -8,6 +8,7 @@ package operation import ( "fmt" + "os" "runtime" "testing" @@ -142,6 +143,10 @@ func TestAppendClientDriver(t *testing.T) { func TestAppendClientEnv(t *testing.T) { clearTestEnv(t) + if os.Getenv("DOCKER_RUNNING") != "" { + t.Skip("These tests gives different results when run in Docker due to extra environment data.") + } + tests := []struct { name string omitEnvFields bool @@ -377,6 +382,10 @@ func TestAppendClientPlatform(t *testing.T) { func TestEncodeClientMetadata(t *testing.T) { clearTestEnv(t) + if os.Getenv("DOCKER_RUNNING") != "" { + t.Skip("These tests gives different results when run in Docker due to extra environment data.") + } + type application struct { Name string `bson:"name"` } diff --git a/x/mongo/driver/topology/server_test.go b/x/mongo/driver/topology/server_test.go index 4a9ffb10a1..a04f5ed7c0 100644 --- a/x/mongo/driver/topology/server_test.go +++ b/x/mongo/driver/topology/server_test.go @@ -131,6 +131,9 @@ func TestServerHeartbeatTimeout(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } + if os.Getenv("DOCKER_RUNNING") != "" { + t.Skip("Skipping this test in docker.") + } networkTimeoutError := &net.DNSError{ IsTimeout: true,