################################################################################
## 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: bmastc/golang-glide:latest
  # 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: ${REPOS_API}server/jobs/artifacts/$SERVER_ID/raw/release/server.linux64?job=build
  CLIENT_URL: ${REPOS_API}client/jobs/artifacts/$CLIENT_ID/raw/release/client.linux64?job=build
  REGISTRATION_URL: ${REPOS_API}registration/jobs/artifacts/$REGISTRATION_ID/raw/release/registration.linux64?job=build
  UDB_URL: ${REPOS_API}user-discovery-bot/jobs/artifacts/$UDB_ID/raw/release/udb.linux64?job=build
  GATEWAY_URL: ${REPOS_API}gateway/jobs/artifacts/$GATEWAY_ID/raw/release/gateway.linux64?job=build
  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
  script:
    - git clean -ffdx
    - mkdir -p ~/.elixxir
    - mkdir -p bin
    - cd bin
    - "DOWNLOADS=\"$SERVER_URL $CLIENT_URL $REGISTRATION_URL $UDB_URL $GATEWAY_URL\""
    - >
      if [ "$CI_BUILD_REF_NAME" == "master" ]; then
        DOWNLOADS=$(echo $DOWNLOADS | sed "s/artifacts\/$DEFAULTBRANCH\/raw/artifacts\/master\/raw/g")
      fi
    - >
      for D in $DOWNLOADS; do
        echo $D
        curl -f -O -s -L -H "PRIVATE-TOKEN: $PATKEY" $D
      done
    - ls -lha
    - mv server.linux64?job=build server
    - mv client.linux64?job=build client
    - mv udb.linux64?job=build udb
    - mv registration.linux64?job=build permissioning
    - mv gateway.linux64?job=build gateway
    - "chmod +x *"
    - cd ..
    - bin/server version
    - bin/client --version || bin/client version
    - bin/udb --version || bin/udb version
    - bin/gateway --version || bin/gateway version
  artifacts:
    when: always
    expire_in: '1 day'
    paths:
     - bin/

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


# NOTE: This was removed because of port conflicts with the base test. Instead
# we run in TLS mode by default and disable this version
# basice2eTLS:
#   stage: tests
#   image: $DOCKER_IMAGE
#   script:
#     - mkdir -p ~/.elixxir
#     - echo $PWD
#     - cd basice2e
#     - for CONFIG in $(echo *.yaml); do echo $CONFIG; sed -i 's/cert\:\ \"\"/cert\:\ \"\.\.\/keys\/cmix\.rip\.crt\"/g'  $CONFIG; sed -i 's/key\:\ \ \"\"/key\:\ \ \"\.\.\/keys\/cmix\.rip\.key\"/g'  $CONFIG; done
#     - for CONFIG in $(echo *.yaml); do echo $CONFIG; sed -i 's/ertPath\:\ \"\"/ertPath\:\ \"\.\.\/keys\/cmix\.rip\.crt\"/g'  $CONFIG; sed -i 's/keyPath\:\ \"\"/keyPath\:\ \"\.\.\/keys\/cmix\.rip\.key\"/g'  $CONFIG; done
#     - sed -i "s/\"Tls\_certificate\"\:\ \"\"/\"Tls\_certificate\"\:\ \"$(awk -v ORS='\\n' '1' ../keys/cmix.rip.crt | sed -e 's/[\/&]/\\&/g')\"/g" ndf.json
#     - sed -i 's/\-\-noTLS//g' run.sh
#     - ./run.sh
#     - 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' *.yaml
    - ./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
