########################################
# Evergreen Template for MongoDB Drivers
########################################

# 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
# 12 minutes is the longest we'll ever run
exec_timeout_secs: 3600 # 12 minutes is the longest we'll ever run

# 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"
    # Applies the subitted patch, if any
    # Deprecated. Should be removed. But still needed for certain agents (ZAP)
    - command: git.apply_patch
    # Make an evergreen exapanstion 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"

           # Python has cygwin path problems on Windows. Detect prospective mongo-orchestration home directory
           if [ "Windows_NT" == "$OS" ]; then # Magic variable in cygwin
              export DRIVERS_TOOLS=$(cygpath -m $DRIVERS_TOOLS)
           fi

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

           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}
          rm -rf $DRIVERS_TOOLS
          if [ "${project}" = "drivers-tools" ]; then
            # If this was a patch build, doing a fresh clone would not actually test the patch
            cp -R ${PROJECT_DIRECTORY}/ $DRIVERS_TOOLS
          else
            git clone git://github.com/mongodb-labs/drivers-evergreen-tools.git $DRIVERS_TOOLS
          fi
          echo "{ \"releases\": { \"default\": \"$MONGODB_BINARIES\" }}" > $MONGO_ORCHESTRATION_HOME/orchestration.config

  # Upload build artifacts that other tasks may depend on
  # Note this URL needs to be totally unique, while predictable for the next task
  # so it can automatically download the artifacts
  "upload build":
    # Compress and upload the entire build directory
    - command: archive.targz_pack
      params:
        # Example: mongo_c_driver_releng_9dfb7d741efbca16faa7859b9349d7a942273e43_16_11_08_19_29_52.tar.gz
        target: "${build_id}.tar.gz"
        source_dir: ${PROJECT_DIRECTORY}/
        include:
          - "./**"
    - command: s3.put
      params:
        aws_key: ${aws_key}
        aws_secret: ${aws_secret}
        local_file: ${build_id}.tar.gz
        # Example: /mciuploads/${UPLOAD_BUCKET}/gcc49/9dfb7d741efbca16faa7859b9349d7a942273e43/debug-compile-nosasl-nossl/mongo_c_driver_releng_9dfb7d741efbca16faa7859b9349d7a942273e43_16_11_08_19_29_52.tar.gz
        remote_file: ${UPLOAD_BUCKET}/${build_variant}/${revision}/${task_name}/${build_id}.tar.gz
        bucket: mciuploads
        permissions: public-read
        content_type: ${content_type|application/x-gzip}

  "exec script" :
    - command: shell.exec
      type: test
      params:
        working_dir: "src"
        script: |
          ${PREPARE_SHELL}
          ${PROJECT_DIRECTORY}/${file}

  "upload mo artifacts":
    - command: shell.exec
      params:
        script: |
          ${PREPARE_SHELL}
          find $MONGO_ORCHESTRATION_HOME -name \*.log | xargs tar czf mongodb-logs.tar.gz
    - command: s3.put
      params:
        aws_key: ${aws_key}
        aws_secret: ${aws_secret}
        local_file: mongodb-logs.tar.gz
        remote_file: ${UPLOAD_BUCKET}/${build_variant}/${revision}/${version_id}/${build_id}/logs/${task_id}-${execution}-mongodb-logs.tar.gz
        bucket: mciuploads
        permissions: public-read
        content_type: ${content_type|application/x-gzip}
        display_name: "mongodb-logs.tar.gz"
    - command: s3.put
      params:
        aws_key: ${aws_key}
        aws_secret: ${aws_secret}
        local_file: drivers-tools/.evergreen/orchestration/server.log
        remote_file: ${UPLOAD_BUCKET}/${build_variant}/${revision}/${version_id}/${build_id}/logs/${task_id}-${execution}-orchestration.log
        bucket: mciuploads
        permissions: public-read
        content_type: ${content_type|text/plain}
        display_name: "orchestration.log"

  "upload working dir":
    - command: archive.targz_pack
      params:
        target: "working-dir.tar.gz"
        source_dir: ${PROJECT_DIRECTORY}/
        include:
          - "./**"
    - command: s3.put
      params:
        aws_key: ${aws_key}
        aws_secret: ${aws_secret}
        local_file: working-dir.tar.gz
        remote_file: ${UPLOAD_BUCKET}/${build_variant}/${revision}/${version_id}/${build_id}/artifacts/${task_id}-${execution}-working-dir.tar.gz
        bucket: mciuploads
        permissions: public-read
        content_type: ${content_type|application/x-gzip}
        display_name: "working-dir.tar.gz"
    - command: archive.targz_pack
      params:
        target: "drivers-dir.tar.gz"
        source_dir: ${DRIVERS_TOOLS}
        include:
          - "./**"
    - command: s3.put
      params:
        aws_key: ${aws_key}
        aws_secret: ${aws_secret}
        local_file: drivers-dir.tar.gz
        remote_file: ${UPLOAD_BUCKET}/${build_variant}/${revision}/${version_id}/${build_id}/artifacts/${task_id}-${execution}-drivers-dir.tar.gz
        bucket: mciuploads
        permissions: public-read
        content_type: ${content_type|application/x-gzip}
        display_name: "drivers-dir.tar.gz"

  "upload test results":
    - command: attach.xunit_results
      params:
        file: ./src/*/build/test-results/TEST-*.xml

  "bootstrap mongo-orchestration":
    - command: shell.exec
      params:
        script: |
          ${PREPARE_SHELL}
          MONGODB_VERSION=${VERSION} TOPOLOGY=${TOPOLOGY} AUTH=${AUTH} SSL=${SSL} sh ${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:
        working_dir: "src"
        script: |
          ${PREPARE_SHELL}
          AUTH="${AUTH}" SSL="${SSL}" MONGODB_URI="${MONGODB_URI}" TOPOLOGY="${TOPOLOGY}" COMPRESSOR="${COMPRESSOR}" JDK="${JDK}" .evergreen/run-tests.sh

  "run plain auth test":
    - command: shell.exec
      type: test
      params:
        silent: true
        working_dir: "src"
        script: |
          # DO NOT ECHO WITH XTRACE (which PREPARE_SHELL does)
          JDK="jdk8" MONGODB_URI="${plain_auth_mongodb_uri}" .evergreen/run-plain-auth-test.sh

  "run gssapi auth test":
    - command: shell.exec
      type: test
      params:
        silent: true
        working_dir: "src"
        script: |
          # DO NOT ECHO WITH XTRACE (which PREPARE_SHELL does)
          PROJECT_DIRECTORY=${PROJECT_DIRECTORY} JDK=${JDK} MONGODB_URI=${gssapi_auth_mongodb_uri} KDC=${gssapi_auth_kdc} REALM=${gssapi_auth_realm} KEYTAB_BASE64=${gssapi_auth_keytab_base64} .evergreen/run-gssapi-auth-test.sh

  "publish snapshot":
    - command: shell.exec
      type: test
      params:
        silent: true
        working_dir: "src"
        script: |
          # DO NOT ECHO WITH XTRACE (which PREPARE_SHELL does)
          PROJECT_DIRECTORY=${PROJECT_DIRECTORY} NEXUS_USERNAME=${nexus_username} NEXUS_PASSWORD=${nexus_password} SIGNING_PASSWORD=${signing_password} SIGNING_KEY_ID=${signing_keyId} RING_FILE_GPG_BASE64=${ring_file_gpg_base64} .evergreen/publish.sh

  "cleanup":
    - command: shell.exec
      params:
        script: |
          ${PREPARE_SHELL}
          cd "$MONGO_ORCHESTRATION_HOME"
          # source the mongo-orchestration virtualenv if it exists
          if [ -f venv/bin/activate ]; then
            . venv/bin/activate
          elif [ -f venv/Scripts/activate ]; then
            . venv/Scripts/activate
          fi
          mongo-orchestration stop
          cd -
          rm -rf $DRIVERS_TOOLS || true

  "fix absolute paths":
    - command: shell.exec
      params:
        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:
        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

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

  "init test-results":
    - command: shell.exec
      params:
        script: |
          ${PREPARE_SHELL}
          echo '{"results": [{ "status": "FAIL", "test_file": "Build", "log_raw": "No test-results.json found was created"  } ]}' > ${PROJECT_DIRECTORY}/test-results.json

  "install dependencies":
    type: test
    params:
      working_dir: "src"
      script: |
        ${PREPARE_SHELL}
        file="${PROJECT_DIRECTORY}/.evergreen/install-dependencies.sh"
        [ -f ${file} ] && sh ${file} || echo "${file} not available, skipping"

# Anchors

hosts: &hosts
 - rhel62-small
 - rhel70-small

pre:
  - func: "fetch source"
  - func: "prepare resources"
  - func: "windows fix"
  - func: "fix absolute paths"
  - func: "init test-results"
  - func: "make files executable"
  - func: "install dependencies"

post:
  # Removed, causing timeouts
  # - func: "upload working dir"
  - func: "upload mo artifacts"
  - func: "upload test results"
  - func: "cleanup"

tasks:

    # Compile / check build variant
    - name: static-analysis
      commands:
        - func: "exec script"
          vars:
            file: ".evergreen/compile.sh"
        - func: "upload build"

    - name: "test"
      depends_on:
        - variant: "static-checks"
          name: "static-analysis"
      commands:
        - func: "bootstrap mongo-orchestration"
        - func: "run tests"

    - name: "plain-auth-test"
      depends_on:
        - variant: "static-checks"
          name: "static-analysis"
      commands:
        - func: "run plain auth test"

    - name: "gssapi-auth-test"
      depends_on:
        - variant: "static-checks"
          name: "static-analysis"
      commands:
        - func: "run gssapi auth test"

    - name: publish-snapshot
      depends_on:
        - variant: ".tests-variant"
          name: "test"
        - variant: "plain-auth-test"
          name: "plain-auth-test"
      commands:
        - func: "publish snapshot"

axes:
  - id: version
    display_name: MongoDB Version
    values:
      - id: "3.6"
        display_name: "3.6"
        variables:
           VERSION: "3.6"
      - id: "3.4"
        display_name: "3.4"
        variables:
           VERSION: "3.4"
      - id: "3.2"
        display_name: "3.2"
        variables:
           VERSION: "3.2"
      - id: "3.0"
        display_name: "3.0"
        variables:
           VERSION: "3.0"
      - id: "2.6"
        display_name: "2.6"
        variables:
           VERSION: "2.6"
  - id: os
    display_name: OS
    values:
      - id: "linux"
        display_name: "Linux"
        run_on: *hosts

  - id: topology
    display_name: Topology
    values:
      - id: "standalone"
        display_name: Standalone
        variables:
           TOPOLOGY: "server"
      - id: "replicaset"
        display_name: Replica Set
        variables:
           TOPOLOGY: "replica_set"
      - id: "sharded-cluster"
        display_name: Sharded Cluster
        variables:
           TOPOLOGY: "sharded_cluster"
  - id: auth
    display_name: Authentication
    values:
      - id: "auth"
        display_name: Auth
        variables:
           AUTH: "auth"
      - id: "noauth"
        display_name: NoAuth
        variables:
           AUTH: "noauth"
  - id: ssl
    display_name: SSL
    values:
      - id: "ssl"
        display_name: SSL
        variables:
           SSL: "ssl"
      - id: "nossl"
        display_name: NoSSL
        variables:
           SSL: "nossl"
  - id: compressor
    display_name: Compressor
    values:
      - id: "snappy"
        display_name: Snappy
        variables:
           COMPRESSOR: "snappy"
      - id: "zlib"
        display_name: Zlib
        variables:
           COMPRESSOR: "zlib"
  - id: jdk
    display_name: JDK
    values:
      - id: "jdk8"
        display_name: JDK8 
        variables:
           JDK: "jdk8"
      - id: "jdk7"
        display_name: JDK7 
        variables:
           JDK: "jdk7"
      - id: "jdk6"
        display_name: JDK6
        variables:
           JDK: "jdk6"

buildvariants:

# Test packaging and other release related routines
- name: static-checks
  display_name: "Static Checks"
  run_on: *hosts
  tasks:
    - name: "static-analysis"

- matrix_name: "tests-zlib-compression"
  matrix_spec: { compressor : "zlib", auth: "noauth", ssl: "nossl", jdk: "jdk6", version: ["3.6"], topology: "standalone", os:
  "linux" }
  display_name: "${version} ${compressor} ${topology} ${auth} ${ssl} ${jdk} ${os} "
  tags: ["tests-variant"]
  tasks:
     - name: "test"

- matrix_name: "tests-snappy-compression"
  matrix_spec: { compressor : "snappy", auth: "noauth", ssl: "nossl", jdk: "jdk7", version: ["3.4", "3.6"], topology: "standalone", os: "linux" }
  display_name: "${version} ${compressor} ${topology} ${auth} ${ssl} ${jdk} ${os} "
  tags: ["tests-variant"]
  tasks:
     - name: "test"

- matrix_name: "tests-jdk6-unsecure"
  matrix_spec: { auth: "noauth", ssl: "nossl", jdk: "jdk6", version: "*", topology: "*", os: "*" }
  display_name: "${version} ${topology} ${auth} ${ssl} ${jdk} ${os} "
  tags: ["tests-variant"]
  tasks:
     - name: "test"

- matrix_name: "tests-jdk6-secure"
  matrix_spec: { auth: "auth", ssl: "ssl", jdk: "jdk6", version: "*", topology: "*", os: "*" }
  display_name: "${version} ${topology} ${auth} ${ssl} ${jdk} ${os} "
  tags: ["tests-variant"]
  tasks:
     - name: "test"

- matrix_name: "tests-jdk-other"
  matrix_spec: { auth: "auth", ssl: "ssl", jdk: "*", version: "3.6", topology: "standalone", os: "linux" }
  exclude_spec: { jdk: "jdk6", auth: "*", ssl: "*", version: "*", topology: "*", os: "*" }
  display_name: "${version} ${topology} ${auth} ${ssl} ${jdk} ${os} "
  tags: ["tests-variant"]
  tasks:
     - name: "test"

- name: plain-auth-test
  display_name: "PLAIN (LDAP) Auth test"
  run_on: *hosts
  tasks:
    - name: "plain-auth-test"

- name: publish-snapshot
  display_name: "Publish Snapshot"
  run_on: *hosts
  tasks:
    - name: "publish-snapshot"
