#!/usr/bin/env bash

set -eux

start-bosh

source /tmp/local-bosh/director/env

bosh upload-stemcell bosh-stemcell/*.tgz

bosh create-release \
  --dir "$PWD/bpm-release/"

bosh upload-release \
  --dir "$PWD/bpm-release/"

export BOSH_DEPLOYMENT=bpm
export BOSH_NON_INTERACTIVE=true

bosh deploy \
  "$PWD/bpm-release/manifests/bosh-lite-ci.yml" \
  -v "stemcell_name=${STEMCELL_NAME}"

export RELEASE_PATH="$PWD/bpm-release"
export GOBIN="$RELEASE_PATH/bin"
export PATH="$PATH:$GOBIN"

pushd "${RELEASE_PATH}/src/bpm" > /dev/null
  go install -mod=vendor github.com/onsi/ginkgo/ginkgo
popd

AGENT_HOST="$(bosh -d bpm instances | grep running | awk '{ print $4 }')"
agent_port=1337

pushd "${RELEASE_PATH}/src/bpm/acceptance" > /dev/null
  ginkgo -mod=vendor -r -p -race -randomizeAllSpecs -- --agent-uri="http://${AGENT_HOST}:${agent_port}"
popd > /dev/null
