################################################################################
## Copyright © 2018 Privategrity Corporation                                   #
##                                                                             #
## All rights reserved.                                                        #
################################################################################

# This drives all the integration tests. When you create a new test, add it as a
# new stage to this file. The stage should have a very clear report and may need
# to run a couple commands to publish.

cache:
  untracked: true
  key: "$CI_BUILD_REF_NAME"
#  paths:
#    - vendor/

variables:
  REPO_DIR: gitlab.com/elixxir
  REPO_NAME: server
  DOCKER_IMAGE: elixxirlabs/cuda-go:go1.16-cuda11.1
  # See https://docs.gitlab.com/ee/api/jobs.html#download-a-single-artifact-file-from-specific-tag-or-branch
  REPOS_API: https://gitlab.com/api/v4/projects/elixxir%2F
  SERVER_URL: ""
  SERVER_GPU_URL: ""
  GPULIB_URL: ""
  CLIENT_URL: ""
  REGISTRATION_URL: ""
  UDB_URL: ""
  GATEWAY_URL: ""
  DEFAULTBRANCH: "release"

before_script:
  ##
  ## Go Setup
  ##
  ##  - go version || echo "Go executable not found."
  ##  - mkdir -p $GOPATH/src/$REPO_DIR
  ##  - ln -s $CI_PROJECT_DIR $GOPATH/src/$REPO_DIR/$REPO_NAME
  ##  - cd $GOPATH/src/$REPO_DIR/$REPO_NAME
  - echo $CI_BUILD_REF
  - echo $CI_PROJECT_DIR
  - echo $CI_BUILD_REF_NAME

stages:
  - installbinaries
  - tests
  - benchmark

installbinaries:
  stage: installbinaries
  image: $DOCKER_IMAGE
  tags:
    - gpu
  script:
    - git clean -ffdx
    - mkdir -p ~/.elixxir
    - ./download_cmix_binaries.sh l d
    - mkdir -p /opt/xxnetwork/lib
    - cp bin/libpowmosm75.so /opt/xxnetwork/lib/
    - cp bin/libpow.fatbin /opt/xxnetwork/lib/
    - "chmod +x *"
    - bin/server version
    - bin/server-cuda version
    - bin/client version
    - bin/udb version
    - bin/gateway version
  artifacts:
    when: always
    expire_in: '1 day'
    paths:
     - bin/

basice2e:
  stage: tests
  tags:
    - gpu
  image: $DOCKER_IMAGE
  script:
    - mkdir -p ~/.elixxir
    - echo $PWD
    - cd basice2e
    - rm -fr results
    - rm -fr results-cpu
    - rm -fr results-gpu
    # - ls -lha
    # - ./run.sh
    # - mv results results-cpu
    # - sleep 60
    - mkdir -p /opt/xxnetwork/lib
    - cp ../bin/libpowmosm75.so /opt/xxnetwork/lib/
    - cp ../bin/libpow.fatbin /opt/xxnetwork/lib/
    - sed -i 's/bin\/server/bin\/server-cuda/g' run.sh
    - sed -i 's/useGPU\:\ false/useGPU\:\ true/g' server-[12345].yaml
    - NSYSENABLED=TRUE ./run.sh
    - mv results results-gpu
    - cd ..
  artifacts:
    when: always
    expire_in: '1 day'
    paths:
     - basice2e/
     - bin/

smokeinfra:
  stage: tests
  image: $DOCKER_IMAGE
  script:
    - mkdir -p ~/.elixxir
    - echo $PWD
    - cd smokeinfra
    - ./run.sh
    - cd ..
  artifacts:
    when: always
    expire_in: '1 day'
    paths:
     - smokeinfra/results/
     - bin/


smokeinfraBatch4:
  stage: tests
  image: $DOCKER_IMAGE
  script:
    - mkdir -p ~/.elixxir
    - echo $PWD
    - cd smokeinfra
    - git checkout -- .
    - sed -i 's/\ 42,/\ 4,/g' registration.json
    - ./run.sh
    - cd ..
  artifacts:
    when: always
    expire_in: '1 day'
    paths:
     - smokeinfra/results/
     - bin/

# smokeinfraBatch1:
#   stage: tests
#   image: $DOCKER_IMAGE
#   script:
#     - mkdir -p ~/.elixxir
#     - echo $PWD
#     - cd smokeinfra
#     - git checkout -- .
#     - sed -i 's/\ 42/\ 1/g' *.yaml
#     - ./run.sh
#     - cd ..
#   artifacts:
#     when: always
#     expire_in: '1 day'
#     paths:
#      - smokeinfra/results/
#      - bin/


benchmark:
  stage: benchmark
  image: $DOCKER_IMAGE
  script:
    # These take about 2 minutes each right now, so 16 total
    - ./bin/server benchmark -b 1024 -n 1 -i 4
    - ./bin/server benchmark -b 1024 -n 5 -i 1
  artifacts:
    when: always
    expire_in: '1 day'
    paths:
      - bin/
  only:
    - benchmark
