functions:
  # Assume ec2 role
  assume ec2 role:
    - command: ec2.assume_role
      params:
        role_arn: ${aws_test_secrets_role}
        duration_seconds: 3600

  # Attach benchmark test results
  attach benchmark test results:
    - command: attach.results
      params:
        file_location: src/report.json

  # Cleanup
  cleanup:
    - command: subprocess.exec
      params:
        binary: bash
        args:
          - .evergreen/scripts/cleanup.sh
        working_dir: src
      type: test

  # Download and merge coverage
  download and merge coverage:
    - command: ec2.assume_role
      params:
        role_arn: ${assume_role_arn}
      type: setup
    - command: subprocess.exec
      params:
        binary: bash
        args:
          - .evergreen/scripts/download-and-merge-coverage.sh
          - ${bucket_name}
          - ${revision}
          - ${version_id}
        working_dir: src
        silent: true
        include_expansions_in_env:
          - AWS_ACCESS_KEY_ID
          - AWS_SECRET_ACCESS_KEY
          - AWS_SESSION_TOKEN
      type: test
    - command: subprocess.exec
      params:
        binary: bash
        args:
          - .evergreen/combine-coverage.sh
        working_dir: src
      type: test
    - command: subprocess.exec
      params:
        binary: bash
        args:
          - .evergreen/scripts/upload-coverage-report.sh
          - ${bucket_name}
          - ${revision}
          - ${version_id}
        working_dir: src
        silent: true
        include_expansions_in_env:
          - AWS_ACCESS_KEY_ID
          - AWS_SECRET_ACCESS_KEY
          - AWS_SESSION_TOKEN
      type: test
    - command: s3.put
      params:
        remote_file: coverage/${revision}/${version_id}/htmlcov/index.html
        aws_key: ${AWS_ACCESS_KEY_ID}
        aws_secret: ${AWS_SECRET_ACCESS_KEY}
        aws_session_token: ${AWS_SESSION_TOKEN}
        bucket: ${bucket_name}
        local_file: src/htmlcov/index.html
        permissions: public-read
        content_type: text/html
        display_name: Coverage Report HTML
        optional: "true"
      type: setup

  # Fetch source
  fetch source:
    - command: git.get_project
      params:
        directory: src

  # Run server
  run server:
    - command: subprocess.exec
      params:
        binary: bash
        args:
          - .evergreen/just.sh
          - run-server
          - ${TEST_NAME}
        working_dir: src
        include_expansions_in_env:
          - VERSION
          - TOPOLOGY
          - AUTH
          - SSL
          - ORCHESTRATION_FILE
          - PYTHON_BINARY
          - PYTHON_VERSION
          - STORAGE_ENGINE
          - REQUIRE_API_VERSION
          - DRIVERS_TOOLS
          - TEST_CRYPT_SHARED
          - AUTH_AWS
          - LOAD_BALANCER
          - LOCAL_ATLAS
          - NO_EXT
      type: test
    - command: expansions.update
      params:
        file: ${DRIVERS_TOOLS}/mo-expansion.yml

  # Run tests
  run tests:
    - command: subprocess.exec
      params:
        binary: bash
        args:
          - .evergreen/just.sh
          - setup-tests
          - ${TEST_NAME}
          - ${SUB_TEST_NAME}
        working_dir: src
        include_expansions_in_env:
          - AUTH
          - SSL
          - AWS_ACCESS_KEY_ID
          - AWS_SECRET_ACCESS_KEY
          - AWS_SESSION_TOKEN
          - COVERAGE
          - PYTHON_BINARY
          - LIBMONGOCRYPT_URL
          - MONGODB_URI
          - PYTHON_VERSION
          - DISABLE_TEST_COMMANDS
          - GREEN_FRAMEWORK
          - NO_EXT
          - COMPRESSORS
          - MONGODB_API_VERSION
          - REQUIRE_API_VERSION
          - DEBUG_LOG
          - DISABLE_FLAKY
          - ORCHESTRATION_FILE
          - OCSP_SERVER_TYPE
          - VERSION
          - IS_WIN32
          - REQUIRE_FIPS
      type: test
    - command: subprocess.exec
      params:
        binary: bash
        args:
          - .evergreen/just.sh
          - run-tests
        working_dir: src
      type: test

  # Send dashboard data
  send dashboard data:
    - command: subprocess.exec
      params:
        binary: bash
        args:
          - .evergreen/scripts/perf-submission-setup.sh
        working_dir: src
        include_expansions_in_env:
          - requester
          - revision_order_id
          - project_id
          - version_id
          - build_variant
          - parsed_order_id
          - task_name
          - task_id
          - execution
          - is_mainline
      type: test
    - command: expansions.update
      params:
        file: src/expansion.yml
    - command: subprocess.exec
      params:
        binary: bash
        args:
          - .evergreen/scripts/perf-submission.sh
        working_dir: src
        include_expansions_in_env:
          - requester
          - revision_order_id
          - project_id
          - version_id
          - build_variant
          - parsed_order_id
          - task_name
          - task_id
          - execution
          - is_mainline
      type: test

  # Setup system
  setup system:
    - command: subprocess.exec
      params:
        binary: bash
        args:
          - .evergreen/scripts/setup-system.sh
        working_dir: src
        include_expansions_in_env:
          - is_patch
          - project
          - version_id
      type: test
    - command: expansions.update
      params:
        file: src/expansion.yml

  # Teardown system
  teardown system:
    - command: subprocess.exec
      params:
        binary: bash
        args:
          - .evergreen/just.sh
          - teardown-tests
        working_dir: src
      type: test
    - command: subprocess.exec
      params:
        binary: bash
        args:
          - ${DRIVERS_TOOLS}/.evergreen/teardown.sh
        working_dir: src
      type: test

  # Upload coverage
  upload coverage:
    - command: ec2.assume_role
      params:
        role_arn: ${assume_role_arn}
      type: setup
    - command: s3.put
      params:
        remote_file: coverage/${revision}/${version_id}/coverage/coverage.${build_variant}.${task_name}
        aws_key: ${AWS_ACCESS_KEY_ID}
        aws_secret: ${AWS_SECRET_ACCESS_KEY}
        aws_session_token: ${AWS_SESSION_TOKEN}
        bucket: ${bucket_name}
        local_file: src/.coverage
        permissions: public-read
        content_type: text/html
        display_name: Raw Coverage Report
        optional: "true"
      type: setup

  # Upload mo artifacts
  upload mo artifacts:
    - command: ec2.assume_role
      params:
        role_arn: ${assume_role_arn}
      type: setup
    - command: archive.targz_pack
      params:
        target: mongo-coredumps.tgz
        source_dir: ./
        include:
          - ./**.core
          - ./**.mdmp
    - command: s3.put
      params:
        remote_file: ${build_variant}/${revision}/${version_id}/${build_id}/coredumps/${task_id}-${execution}-mongodb-coredumps.tar.gz
        aws_key: ${AWS_ACCESS_KEY_ID}
        aws_secret: ${AWS_SECRET_ACCESS_KEY}
        aws_session_token: ${AWS_SESSION_TOKEN}
        bucket: ${bucket_name}
        local_file: mongo-coredumps.tgz
        permissions: public-read
        content_type: ${content_type|application/x-gzip}
        display_name: Core Dumps - Execution
        optional: "true"
      type: setup
    - command: s3.put
      params:
        remote_file: ${build_variant}/${revision}/${version_id}/${build_id}/logs/${task_id}-${execution}-drivers-tools-logs.tar.gz
        aws_key: ${AWS_ACCESS_KEY_ID}
        aws_secret: ${AWS_SECRET_ACCESS_KEY}
        aws_session_token: ${AWS_SESSION_TOKEN}
        bucket: ${bucket_name}
        local_file: ${DRIVERS_TOOLS}/.evergreen/test_logs.tar.gz
        permissions: public-read
        content_type: ${content_type|application/x-gzip}
        display_name: drivers-tools-logs.tar.gz
        optional: "true"
      type: setup

  # Upload test results
  upload test results:
    - command: attach.results
      params:
        file_location: ${DRIVERS_TOOLS}/results.json
    - command: attach.xunit_results
      params:
        file: src/xunit-results/TEST-*.xml
