# When a task that used to pass starts to fail
# Go through all versions that may have been skipped to detect
# when the task started failing
stepback: true

# Mark a failure as a system/bootstrap failure (purple box) rather then a task
# failure by default.
# Actual testing tasks are marked with `type: test`
command_type: system

# Protect ourself against rogue test case, or curl gone wild, that runs forever
# Good rule of thumb: the averageish length a task takes, times 5
# That roughly accounts for variable system performance for various buildvariants
exec_timeout_secs: 1200

# What to do when evergreen hits the timeout (`post:` tasks are run automatically)
timeout:
  - command: shell.exec
    params:
      script: |
        ls -la

functions:
  "fetch source":
    # Executes git clone and applies the submitted patch, if any
    - command: git.get_project
      params:
        directory: "src"
    # Make an evergreen expansion file with dynamic values
    - command: shell.exec
      params:
        working_dir: "src"
        script: |
           # Get the current unique version of this checkout
           if [ "${is_patch}" = "true" ]; then
              CURRENT_VERSION=$(git describe)-patch-${version_id}
           else
              CURRENT_VERSION=latest
           fi

           export DRIVERS_TOOLS="$(pwd)/../drivers-tools"
           export PROJECT_DIRECTORY="$(pwd)"
           export MONGO_ORCHESTRATION_HOME="$DRIVERS_TOOLS/.evergreen/orchestration"
           export MONGODB_BINARIES="$DRIVERS_TOOLS/mongodb/bin"
           export UPLOAD_BUCKET="${project}"

           # fix paths on windows
           if [ "Windows_NT" = "$OS" ]; then
             export DRIVERS_TOOLS=$(cygpath -m -a $DRIVERS_TOOLS)
             export MONGO_ORCHESTRATION_HOME=$(cygpath -m -a $MONGO_ORCHESTRATION_HOME)
             export MONGODB_BINARIES=$(cygpath -m -a $MONGODB_BINARIES)
             export PROJECT_DIRECTORY=$(cygpath -m -a $PROJECT_DIRECTORY)
           fi

           cat <<EOT > expansion.yml
           CURRENT_VERSION: "$CURRENT_VERSION"
           DRIVERS_TOOLS: "$DRIVERS_TOOLS"
           MONGO_ORCHESTRATION_HOME: "$MONGO_ORCHESTRATION_HOME"
           MONGODB_BINARIES: "$MONGODB_BINARIES"
           UPLOAD_BUCKET: "$UPLOAD_BUCKET"
           PROJECT_DIRECTORY: "$PROJECT_DIRECTORY"
           PREPARE_SHELL: |
              set -o errexit
              set -o xtrace
              export DRIVERS_TOOLS="$DRIVERS_TOOLS"
              export MONGO_ORCHESTRATION_HOME="$MONGO_ORCHESTRATION_HOME"
              export MONGODB_BINARIES="$MONGODB_BINARIES"
              export UPLOAD_BUCKET="$UPLOAD_BUCKET"
              export PROJECT_DIRECTORY="$PROJECT_DIRECTORY"

              export TMPDIR="$MONGO_ORCHESTRATION_HOME/db"
              export PATH="$MONGODB_BINARIES:$PATH"
              export PROJECT="${project}"
           EOT
           # See what we've done
           cat expansion.yml

    # Load the expansion file to make an evergreen variable with the current unique version
    - command: expansions.update
      params:
        file: src/expansion.yml

  "prepare resources":
    - command: shell.exec
      params:
        script: |
          ${PREPARE_SHELL}
          git clone git://github.com/mongodb-labs/drivers-evergreen-tools.git $DRIVERS_TOOLS
          echo "{ \"releases\": { \"default\": \"$MONGODB_BINARIES\" }}" > $MONGO_ORCHESTRATION_HOME/orchestration.config

  "bootstrap mongo-orchestration":
    - command: shell.exec
      params:
        script: |
          ${PREPARE_SHELL}
          MONGODB_VERSION=${VERSION} TOPOLOGY=${TOPOLOGY} \
          AUTH=${AUTH} SSL=${SSL} \
          ORCHESTRATION_FILE=${ORCHESTRATION_FILE} \
          bash ${DRIVERS_TOOLS}/.evergreen/run-orchestration.sh
    # run-orchestration generates expansion file with the MONGODB_URI for the cluster
    - command: expansions.update
      params:
        file: mo-expansion.yml

  "run tests":
    - command: shell.exec
      type: test
      params:
        silent: true
        working_dir: "src"
        script: |
          if [ -n "${CLIENT_ENCRYPTION}" ]; then
            cat <<EOT > prepare_client_encryption.sh
            export CLIENT_ENCRYPTION=${CLIENT_ENCRYPTION}
            export CSFLE_KMS_PROVIDERS='${CSFLE_KMS_PROVIDERS}'
          EOT
          fi
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        timeout_secs: 60
        script: |
          ${PREPARE_SHELL}

          if [ -n "${CLIENT_ENCRYPTION}" ]; then
            # Disable xtrace (just in case it was accidentally set).
            set +x
            . ./prepare_client_encryption.sh
            rm -f ./prepare_client_encryption.sh
          fi

          AUTH=${AUTH} SSL=${SSL} UNIFIED=${UNIFIED} MONGODB_URI="${MONGODB_URI}" \
          NODE_VERSION=${NODE_VERSION} SKIP_DEPS=1 NO_EXIT=1 \
            bash ${PROJECT_DIRECTORY}/.evergreen/run-tests.sh

  "run checks":
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        script: |
          ${PREPARE_SHELL}
          bash ${PROJECT_DIRECTORY}/.evergreen/run-checks.sh

  "cleanup":
    - command: shell.exec
      params:
        silent: true
        script: |
          ${PREPARE_SHELL}
          rm -rf $DRIVERS_TOOLS || true

  "fix absolute paths":
    - command: shell.exec
      params:
        silent: true
        script: |
          ${PREPARE_SHELL}
          for filename in $(find ${DRIVERS_TOOLS} -name \*.json); do
            perl -p -i -e "s|ABSOLUTE_PATH_REPLACEMENT_TOKEN|${DRIVERS_TOOLS}|g" $filename
          done

  "windows fix":
    - command: shell.exec
      params:
        silent: true
        script: |
          ${PREPARE_SHELL}
          # for i in $(find ${DRIVERS_TOOLS}/.evergreen ${PROJECT_DIRECTORY}/.evergreen -name \*.sh); do
          #   cat $i | tr -d '\r' > $i.new
          #   mv $i.new $i
          # done
          # Copy client certificate because symlinks do not work on Windows.
          cp ${DRIVERS_TOOLS}/.evergreen/x509gen/client.pem ${MONGO_ORCHESTRATION_HOME}/lib/client.pem

  "make files executable":
    - command: shell.exec
      params:
        silent: true
        script: |
          ${PREPARE_SHELL}
          for i in $(find ${DRIVERS_TOOLS}/.evergreen ${PROJECT_DIRECTORY}/.evergreen -name \*.sh); do
            chmod +x $i
          done

  "install dependencies":
    - command: shell.exec
      type: setup
      params:
        working_dir: "src"
        script: |
          ${PREPARE_SHELL}
          NODE_LTS_NAME=${NODE_LTS_NAME} MSVS_VERSION=${MSVS_VERSION} \
            bash ${PROJECT_DIRECTORY}/.evergreen/install-dependencies.sh
    - command: expansions.update
      params:
        file: src/deps-expansion.yml          

  "run atlas tests":
    - command: shell.exec
      type: test
      params:
        silent: true
        working_dir: "src"
        script: |
          cat <<EOT > prepare_atlas_connectivity.sh
          export ATLAS_CONNECTIVITY='${ATLAS_CONNECTIVITY}'
          EOT
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        script: |
          # Disable xtrace (just in case it was accidentally set).
          set +x
          . ./prepare_atlas_connectivity.sh
          rm -f ./prepare_atlas_connectivity.sh

          NODE_LTS_NAME='${NODE_LTS_NAME}' bash ${PROJECT_DIRECTORY}/.evergreen/run-atlas-tests.sh

  "run kerberos tests":
    - command: shell.exec
      type: test
      params:
        working_dir: src
        script: |
          KRB5_KEYTAB='${gssapi_auth_keytab_base64}' KRB5_PRINCIPAL='${gssapi_auth_principal}' \
          MONGODB_URI='${gssapi_auth_mongodb_uri}' UNIFIED=${UNIFIED} \
          NODE_LTS_NAME='${NODE_LTS_NAME}' bash ${PROJECT_DIRECTORY}/.evergreen/run-kerberos-tests.sh

  "run ldap tests":
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        script: |
          MONGODB_URI='${plain_auth_mongodb_uri}' NODE_LTS_NAME='${NODE_LTS_NAME}' \
          bash ${PROJECT_DIRECTORY}/.evergreen/run-ldap-tests.sh

  "run tls tests":
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        script: |
          NODE_LTS_NAME=${NODE_LTS_NAME} DRIVERS_TOOLS="${DRIVERS_TOOLS}" \
          SSL_CA_FILE="${SSL_CA_FILE}" SSL_KEY_FILE="${SSL_KEY_FILE}" \
          MONGODB_URI="${MONGODB_URI}" bash ${PROJECT_DIRECTORY}/.evergreen/run-tls-tests.sh

  "add aws auth variables to file":
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        silent: true
        script: |
          cat <<EOF > ${DRIVERS_TOOLS}/.evergreen/auth_aws/aws_e2e_setup.json
          {
              "iam_auth_ecs_account" : "${iam_auth_ecs_account}",
              "iam_auth_ecs_secret_access_key" : "${iam_auth_ecs_secret_access_key}",
              "iam_auth_ecs_account_arn": "arn:aws:iam::557821124784:user/authtest_fargate_user",
              "iam_auth_ecs_cluster": "${iam_auth_ecs_cluster}",
              "iam_auth_ecs_task_definition": "${iam_auth_ecs_task_definition}",
              "iam_auth_ecs_subnet_a": "${iam_auth_ecs_subnet_a}",
              "iam_auth_ecs_subnet_b": "${iam_auth_ecs_subnet_b}",
              "iam_auth_ecs_security_group": "${iam_auth_ecs_security_group}",
              "iam_auth_assume_aws_account" : "${iam_auth_assume_aws_account}",
              "iam_auth_assume_aws_secret_access_key" : "${iam_auth_assume_aws_secret_access_key}",
              "iam_auth_assume_role_name" : "${iam_auth_assume_role_name}",
              "iam_auth_ec2_instance_account" : "${iam_auth_ec2_instance_account}",
              "iam_auth_ec2_instance_secret_access_key" : "${iam_auth_ec2_instance_secret_access_key}",
              "iam_auth_ec2_instance_profile" : "${iam_auth_ec2_instance_profile}"
          }
          EOF

  "run aws auth test with regular aws credentials":
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        script: |
          ${PREPARE_SHELL}
          cd ${DRIVERS_TOOLS}/.evergreen/auth_aws
          . ./activate_venv.sh
          ${MONGODB_BINARIES}/mongo aws_e2e_regular_aws.js
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        silent: true
        script: |
          cat <<'EOF' > "${PROJECT_DIRECTORY}/prepare_mongodb_aws.sh"
            alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'
            USER=$(urlencode ${iam_auth_ecs_account})
            PASS=$(urlencode ${iam_auth_ecs_secret_access_key})
            export MONGODB_URI="mongodb://$USER:$PASS@localhost:27017/aws?authMechanism=MONGODB-AWS"
          EOF
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        script: |
          ${PREPARE_SHELL}
          ${PROJECT_DIRECTORY}/.evergreen/run-mongodb-aws-test.sh

  "run aws auth test with assume role credentials":
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        script: |
          ${PREPARE_SHELL}
          cd ${DRIVERS_TOOLS}/.evergreen/auth_aws
          . ./activate_venv.sh
          ${MONGODB_BINARIES}/mongo aws_e2e_assume_role.js
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        silent: true
        script: |
          cat <<'EOF' > "${PROJECT_DIRECTORY}/prepare_mongodb_aws.sh"
            alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'
            USER=$(jq -r '.AccessKeyId' ${DRIVERS_TOOLS}/.evergreen/auth_aws/creds.json)
            USER=$(urlencode $USER)
            PASS=$(jq -r '.SecretAccessKey' ${DRIVERS_TOOLS}/.evergreen/auth_aws/creds.json)
            PASS=$(urlencode $PASS)
            SESSION_TOKEN=$(jq -r '.SessionToken' ${DRIVERS_TOOLS}/.evergreen/auth_aws/creds.json)
            SESSION_TOKEN=$(urlencode $SESSION_TOKEN)
            export MONGODB_URI="mongodb://$USER:$PASS@localhost:27017/aws?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:$SESSION_TOKEN"
          EOF
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        script: |
          ${PREPARE_SHELL}
          ${PROJECT_DIRECTORY}/.evergreen/run-mongodb-aws-test.sh

  "run aws auth test with aws EC2 credentials":
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        script: |
          ${PREPARE_SHELL}
          cd ${DRIVERS_TOOLS}/.evergreen/auth_aws
          . ./activate_venv.sh
          ${MONGODB_BINARIES}/mongo aws_e2e_ec2.js
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        script: |
          ${PREPARE_SHELL}
          ${PROJECT_DIRECTORY}/.evergreen/run-mongodb-aws-test.sh

  "run aws auth test with aws credentials as environment variables":
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        silent: true
        script: |
          cat <<EOF > "${PROJECT_DIRECTORY}/prepare_mongodb_aws.sh"
            export AWS_ACCESS_KEY_ID=${iam_auth_ecs_account}
            export AWS_SECRET_ACCESS_KEY=${iam_auth_ecs_secret_access_key}
            export MONGODB_URI="mongodb://localhost:27017/aws?authMechanism=MONGODB-AWS"
          EOF
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        script: |
          ${PREPARE_SHELL}
          ${PROJECT_DIRECTORY}/.evergreen/run-mongodb-aws-test.sh

  "run aws auth test with aws credentials and session token as environment variables":
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        silent: true
        script: |
          cat <<'EOF' > "${PROJECT_DIRECTORY}/prepare_mongodb_aws.sh"
            export AWS_ACCESS_KEY_ID=$(jq -r '.AccessKeyId' ${DRIVERS_TOOLS}/.evergreen/auth_aws/creds.json)
            export AWS_SECRET_ACCESS_KEY=$(jq -r '.SecretAccessKey' ${DRIVERS_TOOLS}/.evergreen/auth_aws/creds.json)
            export AWS_SESSION_TOKEN=$(jq -r '.SessionToken' ${DRIVERS_TOOLS}/.evergreen/auth_aws/creds.json)
            export MONGODB_URI="mongodb://localhost:27017/aws?authMechanism=MONGODB-AWS"
          EOF
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        script: |
          ${PREPARE_SHELL}
          ${PROJECT_DIRECTORY}/.evergreen/run-mongodb-aws-test.sh

  "run aws ECS auth test":
    - command: shell.exec
      type: test
      params:
        working_dir: src
        script: |
          ${PREPARE_SHELL}
          AUTH_AWS_DIR=${DRIVERS_TOOLS}/.evergreen/auth_aws
          ECS_SRC_DIR=$AUTH_AWS_DIR/src

          # fix issue with `TestData` in SERVER-46340
          sed -i '1s+^+TestData = {};\n+' $AUTH_AWS_DIR/lib/ecs_hosted_test.js

          # pack up project directory to ssh it to the container
          mkdir -p $ECS_SRC_DIR/.evergreen
          cp $PROJECT_DIRECTORY/.evergreen/run-mongodb-aws-ecs-test.sh $ECS_SRC_DIR/.evergreen
          tar -czf $ECS_SRC_DIR/src.tgz -C $PROJECT_DIRECTORY .

          cd $AUTH_AWS_DIR
          cat <<EOF > setup.js
            const mongo_binaries = "$MONGODB_BINARIES";
            const project_dir = "$ECS_SRC_DIR";
          EOF

          cat setup.js
          . ./activate_venv.sh
          mongo --nodb setup.js aws_e2e_ecs.js

  "run-ocsp-test":
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        script: |
          ${PREPARE_SHELL}

          UNIFIED=${UNIFIED} \
          CA_FILE="$DRIVERS_TOOLS/.evergreen/ocsp/rsa/ca.pem" \
          OCSP_TLS_SHOULD_SUCCEED="${OCSP_TLS_SHOULD_SUCCEED}" \
          bash ${PROJECT_DIRECTORY}/.evergreen/run-ocsp-tests.sh

  "run-valid-ocsp-server":
    - command: shell.exec
      params:
        script: |
          cd ${DRIVERS_TOOLS}/.evergreen/ocsp
          /opt/mongodbtoolchain/v3/bin/python3 -m venv ./venv
          ./venv/bin/pip3 install asn1crypto oscrypto bottle
    - command: shell.exec
      params:
        background: true
        script: |
          cd ${DRIVERS_TOOLS}/.evergreen/ocsp
          nohup ./venv/bin/python3 ocsp_mock.py \
            --ca_file rsa/ca.pem \
            --ocsp_responder_cert rsa/ca.crt \
            --ocsp_responder_key rsa/ca.key \
            -p 8100 -v

  "run-revoked-ocsp-server":
    - command: shell.exec
      params:
        script: |
          cd ${DRIVERS_TOOLS}/.evergreen/ocsp
          /opt/mongodbtoolchain/v3/bin/python3 -m venv ./venv
          ./venv/bin/pip3 install asn1crypto oscrypto bottle
    - command: shell.exec
      params:
        background: true
        script: |
          cd ${DRIVERS_TOOLS}/.evergreen/ocsp
          nohup ./venv/bin/python3 ocsp_mock.py \
            --ca_file rsa/ca.pem \
            --ocsp_responder_cert rsa/ca.crt \
            --ocsp_responder_key rsa/ca.key \
            -p 8100 \
            -v \
            --fault revoked

  "upload test results":
    - command: attach.xunit_results
      params:
        file: src/xunit.xml
pre:
  - func: "fetch source"
  - func: "prepare resources"
  - func: "windows fix"
  - func: "fix absolute paths"
  - func: "make files executable"

post:
  - func: "upload test results"
  - func: "cleanup"

ignore:
  - '*.md'
