Skip to content

Commit

Permalink
Allow sourcing smoke test script
Browse files Browse the repository at this point in the history
  • Loading branch information
ldx committed Aug 26, 2020
1 parent fe3ad50 commit 6f9bdac
Showing 1 changed file with 89 additions and 38 deletions.
127 changes: 89 additions & 38 deletions scripts/run_smoke_test.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,88 @@
#!/bin/bash

set -e
#!/usr/bin/env bash

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ROOT_DIR=$SCRIPT_DIR/..
cd $ROOT_DIR

SSH_KEY_FILE=$(mktemp)
chmod 0600 $SSH_KEY_FILE

PORTFW_PID=""

BUILD=${TRAVIS_BUILD_NUMBER:-local}
CLUSTER_NAME="build-$BUILD"
USE_REGION=${USE_REGION:-us-east-1}
STATE_BUCKET=${STATE_BUCKET:-elotl-tf-state}
STATE_PATH=${STATE_PATH:-build/terraform-${BUILD}.tfstate}

export KUBECONFIG=$(mktemp)
setup() {
SSH_KEY_FILE=$(mktemp)
chmod 0600 $SSH_KEY_FILE
export KUBECONFIG=$(mktemp)
TFDIR=$(mktemp -d)
PORTFW_PID=""
}

handle_error() {
trap - EXIT
show_kube_info || true
cleanup || true
}

cleanup() {
kubectl get pods -A || true
kubectl get nodes || true
kubectl -n kube-system describe pod -l app=kip-provider || true
kubectl logs -n kube-system -l app=kip-provider -c init-cert --tail=-1 || true
kubectl logs -n kube-system -l app=kip-provider -c kip --tail=-1 || true
kubectl delete pod nginx > /dev/null 2>&1 || true
kubectl delete svc nginx > /dev/null 2>&1 || true
kubectl delete pod test > /dev/null 2>&1 || true
rm -rf $SSH_KEY_FILE
rm -rf $KUBECONFIG
if [[ -n "$PORTFW_PID" ]]; then
kill -9 $PORTFW_PID
fi
cd $ROOT_DIR/deploy/terraform-aws
terraform destroy -var cluster_name=${CLUSTER_NAME} -auto-approve
delete_kube_resources
destroy_cluster
delete_temp_files
stop_port_forwarding
}

stop_port_forwarding() {
[[ -n "$PORTFW_PID" ]] && kill -9 $PORTFW_PID || true
}

delete_temp_files() {
[[ -n "$SSH_KEY_FILE" ]] && rm -rf $SSH_KEY_FILE
[[ -n "$KUBECONFIG" ]] && rm -rf $KUBECONFIG
[[ -n "$TFDIR" ]] && rm -rf $TFDIR
}

delete_kube_resources() {
kubectl delete pod nginx
kubectl delete svc nginx
kubectl delete pod test
}

show_kube_info() {
kubectl get pods -A
kubectl get nodes
kubectl -n kube-system describe pod -l app=kip-provider
kubectl logs -n kube-system -l app=kip-provider -c init-cert --tail=-1
kubectl logs -n kube-system -l app=kip-provider -c kip --tail=-1
}

create_cluster() {
# Create a test cluster.
pushd $TFDIR
cat > main.tf <<EOF
terraform {
required_version = "~> 0.12"
backend "s3" {
region = "${USE_REGION}"
bucket = "${STATE_BUCKET}"
key = "${STATE_PATH}"
encrypt = "true"
}
}
module "kip-aws" {
source = "${ROOT_DIR}/deploy/terraform-aws"
cluster_name = "${CLUSTER_NAME}"
}
EOF
terraform init
terraform apply -auto-approve
terraform show -json | jq -r '.values.root_module.child_modules[] | select(.address=="module.kip-aws") | .resources[] | select(.address=="tls_private_key.ssh_key") | .values.private_key_pem' > $SSH_KEY_FILE
SSH_HOST=$(terraform show -json | jq -r '.values.root_module.child_modules[] | select(.address=="module.kip-aws") | .resources[] | select(.address=="aws_instance.k8s_node") | .values.public_ip')
popd
}

destroy_cluster() {
pushd $TFDIR
terraform destroy -auto-approve
popd
}

update_vk() {
Expand Down Expand Up @@ -61,17 +112,17 @@ fetch_kubeconfig() {
timeout 60s sh -c "while true; do kubectl get sa default > /dev/null 2>&1 && exit 0; done"
}

trap cleanup EXIT

# Create a test cluster.
cd $ROOT_DIR/deploy/terraform-aws
terraform init
terraform apply -var cluster_name=${CLUSTER_NAME} -auto-approve
terraform show -json | jq -r '.values.root_module.resources | .[] | select(.address=="tls_private_key.ssh_key") | .values.private_key_pem' > $SSH_KEY_FILE
SSH_HOST=$(terraform show -json | jq -r '.values.root_module.resources | .[] | select(.address=="aws_instance.k8s_node") | .values.public_ip')

fetch_kubeconfig

update_vk
test_once() {
set -euxo pipefail
trap handle_error EXIT
setup
create_cluster
fetch_kubeconfig
update_vk
run_smoke_test
cleanup
trap - EXIT
}

run_smoke_test
# Run test_once() if running as a shell script, and return if sourced.
(return 0 2>/dev/null) || test_once

0 comments on commit 6f9bdac

Please sign in to comment.