image:
  name: nexus.engageska-portugal.pt/ska-docker/tango-dsconfig

# The following variables are automatically passed down to the tangodb container
# as well as the tangodatabaseds container and available within each.
variables:
  MYSQL_ROOT_PASSWORD: "secret"
  MYSQL_DATABASE: "tango"
  MYSQL_USER: "tango"
  MYSQL_PASSWORD: "tango"
  MYSQL_HOST: "tangodb"
  TANGO_HOST: "localhost:10000"

stages:
  - build
  - test

#services:
#  - name: nexus.engageska-portugal.pt/ska-docker/tango-db
#    alias: tangodb
#  - name: nexus.engageska-portugal.pt/ska-docker/tango-cpp
#    alias: tangodatabaseds
#    entrypoint: ["/usr/local/bin/DataBaseds"]
#    command: ["2","-ORBendPoint giop:tcp::10000"]

build_job:
  stage: build
  before_script:
    #TODO: remove procps use for ps
    - sudo apt update && sudo apt -y --no-install-recommends install build-essential cmake pkg-config libboost-thread-dev procps
  script:
    #- make
    - mkdir build && cd build
    - cmake -DBUILD_TESTS=ON ..
    - make
  artifacts:
    paths:
      - build/alarm-handler-srv
      - build/bin/testdevice-srv
    expire_in: 1 week
  #    # depending on your build setup it's most likely a good idea to cache outputs to reduce the build time
  #    cache:
  #      paths:
  #        - build/CMakeFiles/

test_load_job:
  stage: test
  before_script:
    #TODO: remove procps use for ps
    - sudo apt update && sudo apt -y --no-install-recommends install libboost-thread-dev procps
  script:
    - sleep 10
    - /usr/local/bin/DataBaseds 2 -ORBendPoint giop:tcp::10000 &
    - sleep 10
    - exit_code=2
    - json2tango -w -a -u ./test/ah_config.json || exit_code=$?
    # json2tango returns 2 if values written to DB
    - if [ ${exit_code} -ne 2 ]; then echo "Tango DB configuration failed!" ; else echo "Tango DB configuration succedeed!"; fi
    - sleep 5
    - ./build/alarm-handler-srv 01 &
    - ./build/bin/testdevice-srv 01 &
    - sleep 10
    - ps -ef | grep alarm-handler-srv | grep -v grep
    - ps -ef | grep testdevice-srv | grep -v grep
    - sleep 5
    - python ./test/load-alarm-conf.py --device=alarm/handler/01 --load="tag=test0;formula=(alarm/test/01/condition == 1);on_delay=0;off_delay=0;priority=high;shlvd_time=0;group=gr_test;message=Test alarm;url=;on_command=;off_command=;enabled=1"
    - sleep 1
    - python ./test/check-alarm-conf.py --device=alarm/handler/01 --alarm="tag=test0;formula=(alarm/test/01/condition == 1);on_delay=0;off_delay=0;priority=high;shlvd_time=0;group=gr_test;message=Test alarm;url=;on_command=;off_command=;enabled=1"
  #artifacts:
  #  paths:
  #    - build/alarm-handler-srv
  #  expire_in: 1 week
  needs: ["build_job"]
  services:
    - name: nexus.engageska-portugal.pt/ska-docker/tango-db
      alias: tangodb


