#!/bin/bash

# Copyright 2019 The Kubernetes Authors.
#
# 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.

# Hard-coded version is needed in case GitHub API rate limit is exceeded.
# TODO: When aws-k8s-tester becomes a full release (https://developer.github.com/v3/repos/releases/#get-the-latest-release), use:
# $(shell curl -s --request GET --url https://api.github.com/repos/aws/aws-k8s-tester/releases/latest | jq -r '.tag_name? // "<current version number>"')
AWS_K8S_TESTER_VERSION=$(curl -s --request GET --url https://api.github.com/repos/aws/aws-k8s-tester/tags | jq -r '.[0]?.name // "0.2.5"')
AWS_K8S_TESTER_OS_ARCH=$(go env GOOS)-amd64
AWS_K8S_TESTER_DOWNLOAD_URL=https://github.com/aws/aws-k8s-tester/releases/download/${AWS_K8S_TESTER_VERSION}/aws-k8s-tester-${AWS_K8S_TESTER_VERSION}-${AWS_K8S_TESTER_OS_ARCH}
AWS_K8S_TESTER_PATH=/tmp/aws-k8s-tester

VPC_ID_FLAG=""
if [ -z "$AWS_K8S_TESTER_VPC_ID" ]; then
    VPC_ID_FLAG=--vpc-id=${AWS_K8S_TESTER_VPC_ID}
fi

PR_NUM_FLAG=""
if [ -z "$PULL_NUMBER" ]; then
    PR_NUM_FLAG=--pr-num=${PULL_NUMBER}
fi

curl -L ${AWS_K8S_TESTER_DOWNLOAD_URL} -o ${AWS_K8S_TESTER_PATH}
chmod +x ${AWS_K8S_TESTER_PATH}
${AWS_K8S_TESTER_PATH} csi test integration --terminate-on-exit=true --timeout=20m ${PR_NUM_FLAG} ${VPC_ID_FLAG}
