#!/usr/bin/env bash
# Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e

# For the test step concourse will set the following environment variables:
# SOURCE_PATH - path to component repository root directory.

if [[ -z "${SOURCE_PATH}" ]]; then
  export SOURCE_PATH="$(readlink -f "$(dirname ${0})/..")"
else
  export SOURCE_PATH="$(readlink -f "${SOURCE_PATH}")"
fi

VCS="github.com"
ORGANIZATION="gardener"
PROJECT="hvpa-controller"
REPOSITORY=${VCS}/${ORGANIZATION}/${PROJECT}
VERSION_FILE="$(readlink  -f "${SOURCE_PATH}/VERSION")"
VERSION="$(cat "${VERSION_FILE}")"

# The `go <cmd>` commands requires to see the target repository to be part of a
# Go workspace. Thus, if we are not yet in a Go workspace, let's create one
# temporarily by using symbolic links.
if [[ "${SOURCE_PATH}" != *"src/${REPOSITORY}" ]]; then
  SOURCE_SYMLINK_PATH="${SOURCE_PATH}/tmp/src/${REPOSITORY}"
  if [[ -d "${SOURCE_PATH}/tmp" ]]; then
    rm -rf "${SOURCE_PATH}/tmp"
  fi
  mkdir -p "${SOURCE_PATH}/tmp/src/${VCS}/${ORGANIZATION}"
  ln -s "${SOURCE_PATH}" "${SOURCE_SYMLINK_PATH}"
  cd "${SOURCE_SYMLINK_PATH}"

  export GOPATH="${SOURCE_PATH}/tmp"
  export GOBIN="${SOURCE_PATH}/tmp/bin"
  export PATH="${GOBIN}:${PATH}"
fi

##############################################################################

# Declare global variables
TEST_ID=

function setup_test_enviornment() {
  setup_ginkgo
  setup_env
  setup_hvpacontroller
}

function setup_ginkgo(){
    echo "Installing Ginkgo..."
    go get -u github.com/onsi/ginkgo/ginkgo
    echo "Successfully installed Ginkgo."
}

function setup_env(){
    echo "Downloading and installing kubebuilder..."
    os=$(go env GOOS)
    arch=$(go env GOARCH)

    # download kubebuilder and extract it to tmp
    curl -sL https://go.kubebuilder.io/dl/2.0.0-beta.0/${os}/${arch} | tar -xz -C /tmp/

    # move to a long-term location and put it on your path
    # (you'll need to set the KUBEBUILDER_ASSETS env var if you put it somewhere else)
    mv /tmp/kubebuilder_2.0.0-beta.0_${os}_${arch} /usr/local/kubebuilder
    export PATH=$PATH:/usr/local/kubebuilder/bin
    echo "Successfully installed kubebuilder."
}

function setup_hvpacontroller(){
    echo "Installing hvpa-controller..."
    go build \
    -v \
    -o ${GOBIN}/manager \
    github.com/gardener/hvpa-controller/
    chmod +x ${GOBIN}/manager
    echo "Successfully installed hvpa-controller."
}

function get_test_id() {
  git_commit=`git show -s --format="%H"`
  export TEST_ID=hvpa-test-${git_commit}
  echo "Test id: ${TEST_ID}"
}

##############################################################################
function setup_test_cluster() {
  get_test_id
}

###############################################################################

setup_test_enviornment
echo "Setting up test cluster..."
setup_test_cluster

echo "Starting integration tests..."

set +e
ginkgo -r -cover utils api controllers
TEST_RESULT=$?
set -e

echo "Done with integration tests."

echo "Successfully completed all tests."

exit $TEST_RESULT