#!/usr/bin/env bash

_common_setup() {
  bats_load_library bats-support
  bats_load_library bats-assert
  bats_load_library bats-file

  load "$PWD/tests/integration/helpers/file"
  load "$PWD/tests/integration/helpers/app"
  load "$PWD/tests/integration/helpers/git"
  load "$PWD/tests/integration/helpers/recipe"
  load "$PWD/tests/integration/helpers/server"
  load "$PWD/tests/integration/helpers/docker"

  export ABRA="$PWD/abra"

  export TEST_APP_NAME="$(basename "${BATS_TEST_FILENAME//./_}")"
  export TEST_APP_DOMAIN="$TEST_APP_NAME.$TEST_SERVER"
  export TEST_MOVE_SERVER="default2"
  export TEST_RECIPE="abra-test-recipe"

  _ensure_swarm
  _ensure_ssh_agent
}


_ensure_ssh_agent() {
  if ! command -v ssh-agent >/dev/null 2>&1
  then
    echo "ssh-agent is missing, please install it"
    exit 1
  fi

  export SSH_AUTH_SOCK="$HOME/.ssh/ssh_auth_sock"
  if [ ! -S ~/.ssh/ssh_auth_sock ]; then
    eval `ssh-agent`
    ln -sf "$SSH_AUTH_SOCK" ~/.ssh/ssh_auth_sock
  fi
}
