#!/bin/bash

trap 'rm_fd' EXIT

set -x 

log_file_path="amccorrelator_tests.log"

exec 3> >(tee -a $log_file_path)
exec 1>&3
exec 2>&3


rm_fd() {
  exec 1>&-
  exec 2>&-
  exec 3>&-
  wait
}


timeout=10

keys="GOPROXY=http://nexus.iwarma.ru/repository/proxy-go/ \
      GOPRIVATE=https://gitlab.iwarma.ru"
echo $keys

export $keys

function check { 
    if [ "$1" == "$timeout" ]; then
        echo "timeout. connection to $2 failed"
        exit 1
    else 
        echo "connection to $2 established"
    fi
}


response=0
counter=0

while [ "$response" != 200 ] && [ "$counter" != "$timeout" ]; do
    response=$(curl -s -o /dev/null -w "%{http_code}" -u elastic:changeme -X GET http://elasticsearch:9200/)
    sleep 5s
    counter=$(( $counter + 1 ))
done

check $counter "elasticsearch"


set -ex # fail on any error

cp cmd/correlator/config_example.json cmd/correlator/config.json

echo "tests starting..."

/bin/bash ./cicd/test.sh
