Skip to content

Commit

Permalink
add test-setup and test teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
sonyafenge committed Jul 8, 2022
1 parent 57f657b commit 3f725da
Show file tree
Hide file tree
Showing 3 changed files with 263 additions and 0 deletions.
48 changes: 48 additions & 0 deletions hack/test-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash

# gcloud multiplexing for shared GCE/GKE tests.
GRS_ROOT=$(dirname "${BASH_SOURCE[0]}")

SIM_NUM=${SIM_NUM:-2}
CLIENT_NUM=${CLIENT_NUM:-4}
SERVER_NUM=${SERVER_NUM:-1}
ZONE=${GRS_GCE_ZONE:-"us-central1-a"}
REGION=${ZONE%-*}
PROJECT=${GRS_GCE_project:-"workload-controller-manager"}
INSTANCE_PREFIX=${GRS_INSTANCE_PREFIX:-grs}
SIM_INSTANCE_PREFIX=${SIM_INSTANCE_PREFIX:-"${INSTANCE_PREFIX}-sim"}
CLIENT_INSTANCE_PREFIX=${CLIENT_INSTANCE_PREFIX:-"${INSTANCE_PREFIX}-client"}
SERVER_INSTANCE_PREFIX=${SERVER_INSTANCE_PREFIX:-"${INSTANCE_PREFIX}-server"}
SIM_RESERVED_IP=${SIM_RESERVED_IP:-"35.188.145.60,35.193.92.26"}

SOURCE_INSTANCE=${SOURCE_INSTANCE:-"sonya-grs-test-template"}
SOURCE_DISK=${SOURCE_DISK:-"sonya-grs-test-template"}
SOURCE_DISK_ZONE=${SOURCE_DISK_ZONE:-"${ZONE}"}
IMAGE_NAME=${IMAGE_NAME:-"grs-test-image"}

# simulator instance parameter
SIM_SOURCE_INSTANCE=${SIM_SOURCE_INSTANCE:-"${SOURCE_INSTANCE}"}
SIM_SOURCE_DISK=${SIM_SOURCE_DISK:-"${SOURCE_DISK}"}
SIM_SOURCE_DISK_ZONE=${SIM_SOURCE_DISK_ZONE:-"${SOURCE_DISK_ZONE}"}
SIM_IMAGE_NAME=${SIM_IMAGE_NAME:-"${IMAGE_NAME}"}


# client instance parameter
CLIENT_SOURCE_INSTANCE=${CLIENT_SOURCE_INSTANCE:-"${SOURCE_INSTANCE}"}
CLIENT_SOURCE_DISK=${CLIENT_SOURCE_DISK:-"${SOURCE_DISK}"}
CLIENT_SOURCE_DISK_ZONE=${CLIENT_SOURCE_DISK_ZONE:-"${SOURCE_DISK_ZONE}"}
CLIENT_IMAGE_NAME=${CLIENT_IMAGE_NAME:-"${IMAGE_NAME}"}

# server instance parameter
SERVER_SOURCE_INSTANCE=${SERVER_SOURCE_INSTANCE:-"sonya-grs-resourcemanagement"}
SERVER_SOURCE_DISK=${SERVER_SOURCE_DISK:-"sonya-grs-resourcemanagement"}
SERVER_SOURCE_DISK_ZONE=${SERVER_SOURCE_DISK_ZONE:-"${ZONE}"}
SERVER_IMAGE_NAME=${SERVER_IMAGE_NAME:-"grs-server-image"}

#teardown parameter
SERVER_AUTO_DELETE=${SERVER_AUTO_DELETE:-true}
CLIENT_AUTO_DELETE=${CLIENT_AUTO_DELETE:-true}
SIM_AUTO_DELETE=${SIM_AUTO_DELETE:-true}
SERVERIMAGE_AUTO_DELETE=${SERVERIMAGE_AUTO_DELETE:-false}
CLIENTIMAGE_AUTO_DELETE=${CLIENTIMAGE_AUTO_DELETE:-false}
SIMIMAGE_AUTO_DELETE=${SIMIMAGE_AUTO_DELETE:-false}
134 changes: 134 additions & 0 deletions hack/test-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#!/usr/bin/env bash

### Only support gcloud
### Please ensure gcloud is installed before run this script

GRS_ROOT=$(dirname "${BASH_SOURCE[0]}")

source "${GRS_ROOT}/test-config.sh"

function create-image {
local image_name="$1"
local source_disk="$2"
local source_disk_zone="$3"
echo "Check and create images with image_name: ${image_name}, source_disk: ${source_disk}."
if gcloud compute images describe "${image_name}" --project "${PROJECT}" &>/dev/null; then
echo "Image name: ${image_name} already exist, using existing one."
else
gcloud compute images create \
"${image_name}" \
--project "${PROJECT}" \
--source-disk "${source_disk}" \
--source-disk-zone "${source_disk_zone}" \
--quiet
fi
}

function create-template {
local template_name="$1"
local source_instance="$2"
local source_disk="$3"
local image_name="$4"
local source_instance_zone="$5"
echo "Check and create instance templates"
if gcloud compute instance-templates describe "${template_name}" --project "${PROJECT}" &>/dev/null; then
gcloud compute instance-templates delete \
"${template_name}" \
--project "${PROJECT}" \
--quiet
fi
gcloud compute instance-templates create \
"${template_name}" \
--project "${PROJECT}" \
--source-instance "${source_instance}" \
--source-instance-zone "${source_instance_zone}" \
--configure-disk=device-name="${source_disk}",instantiate-from=custom-image,custom-image="projects/${PROJECT}/global/images/${image_name}" \
--quiet

}

function create-instance-group {
local group_name="$1"
local template_name="$2"
local instance_num="$3"
echo "Check and create instance groups"
if gcloud compute instance-groups managed describe "${group_name}" --project "${PROJECT}" &>/dev/null; then
echo "Try to Delete existing instance group: ${group_name}"
gcloud compute instance-groups managed delete \
"${group_name}" \
--project "${PROJECT}" \
--quiet
fi
gcloud compute instance-groups managed create \
"${group_name}" \
--project "${PROJECT}" \
--zone "${ZONE}" \
--template "${template_name}" \
--size "${instance_num}" \
--quiet
}

function attach-ipaddress {
local group_name="$1"
echo "Attaching reserved ip address to instances"
instance_names=()
instance_names=($(gcloud compute instance-groups managed list-instances \
"${group_name}" --zone "${ZONE}" --project "${PROJECT}" \
--format='value(instance)'))

ip_index=0
for name in "${instance_names[@]}"; do
echo $name
gcloud compute instances delete-access-config \
$name \
--zone ${ZONE} \
--project "${PROJECT}" \
--access-config-name="External NAT"

gcloud compute instances add-access-config \
$name \
--zone ${ZONE} \
--project "${PROJECT}" \
--access-config-name "External NAT" \
--address "${IP_ADDRESS[$ip_index]}"
echo "$ip_index,${IP_ADDRESS[$ip_index]}"
ip_index=$(($ip_index + 1))
done
}


###############
# main function
###############

#verify reserved ip address number is greater then simulator vm number
IFS=','; IP_ADDRESS=($SIM_RESERVED_IP); unset IFS;

if [ ${#IP_ADDRESS[@]} -lt ${SIM_NUM} ]; then
echo "Reserved IP address number less then simulator vm number, Please double check."
exit 1
fi

if [ ${SIM_NUM} -gt 0 ]; then
echo "starting region simulator... "
create-image "${SIM_IMAGE_NAME}" "${SIM_SOURCE_DISK}" "${SIM_SOURCE_DISK_ZONE}"
create-template "${SIM_INSTANCE_PREFIX}-template" "${SIM_SOURCE_INSTANCE}" "${SIM_SOURCE_DISK}" "${SIM_IMAGE_NAME}" "${SIM_SOURCE_DISK_ZONE}"
create-instance-group "${SIM_INSTANCE_PREFIX}-instance-group" "${SIM_INSTANCE_PREFIX}-template" "${SIM_NUM}"
echo "attach reserved IP to region simulator"
attach-ipaddress "${SIM_INSTANCE_PREFIX}-instance-group"
fi

if [ ${CLIENT_NUM} -gt 0 ]; then
echo "starting client scheduler... "
create-image "${CLIENT_IMAGE_NAME}" "${CLIENT_SOURCE_DISK}" "${CLIENT_SOURCE_DISK_ZONE}"
create-template "${CLIENT_INSTANCE_PREFIX}-template" "${CLIENT_SOURCE_INSTANCE}" "${CLIENT_SOURCE_DISK}" "${CLIENT_IMAGE_NAME}" "${CLIENT_SOURCE_DISK_ZONE}"
create-instance-group "${CLIENT_INSTANCE_PREFIX}-instance-group" "${CLIENT_INSTANCE_PREFIX}-template" "${CLIENT_NUM}"
fi

if [ ${SERVER_NUM} -gt 0 ]; then
echo "starting resource management service... "
create-image "${SERVER_IMAGE_NAME}" "${SERVER_SOURCE_DISK}" "${SERVER_SOURCE_DISK_ZONE}"
create-template "${SERVER_INSTANCE_PREFIX}-template" "${SERVER_SOURCE_INSTANCE}" "${SERVER_SOURCE_DISK}" "${SERVER_IMAGE_NAME}" "${SERVER_SOURCE_DISK_ZONE}"
create-instance-group "${SERVER_INSTANCE_PREFIX}-instance-group" "${SERVER_INSTANCE_PREFIX}-template" "${SERVER_NUM}"
fi

81 changes: 81 additions & 0 deletions hack/test-teardown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env bash

### Only support gcloud
### Please ensure gcloud is installed before run this script
GRS_ROOT=$(dirname "${BASH_SOURCE[0]}")

source "${GRS_ROOT}/test-config.sh"

echo "${GRS_ROOT}/test-config.sh"

echo "Tear down region simulator and clients... "

function delete-image {
local image_name="$1"
if gcloud compute images describe "${image_name}" --project "${PROJECT}" &>/dev/null; then
echo "Deleting existing instance image: ${image_name}"
gcloud compute images delete \
"${image_name}" \
--project "${PROJECT}" \
--quiet
fi
}

function delete-instance-template {
local template_name="$1"
if gcloud compute instance-templates describe "${template_name}" --project "${PROJECT}" &>/dev/null; then
echo "Try to Delete existing instance template: ${template_name}, if failed, Please manully delete instance group first!"
gcloud compute instance-templates delete \
"${template_name}" \
--project "${PROJECT}" \
--quiet
fi
}

function delete-instance-groups {
local group_name="$1"
if gcloud compute instance-groups managed describe "${group_name}" --project "${PROJECT}" --zone "${ZONE}" &>/dev/null; then
echo "Try to Delete existing instance groups: ${group_name}"
gcloud compute instance-groups managed delete \
"${group_name}" \
--project "${PROJECT}" \
--zone "${ZONE}" \
--quiet
fi
}



###############
# main function
###############

if [ "${SIM_AUTO_DELETE}" == "true" ]; then
echo "Deleting simulator resources"
delete-instance-groups "${SIM_INSTANCE_PREFIX}-instance-group"
delete-instance-template "${SIM_INSTANCE_PREFIX}-template"
if [ "${SIMIMAGE_AUTO_DELETE}" == "true" ]; then
delete-image "${SIM_IMAGE_NAME}"
fi
fi

if [ "${CLIENT_AUTO_DELETE}" == "true" ]; then
echo "Deleting client scheduler resources"
delete-instance-groups "${CLIENT_INSTANCE_PREFIX}-instance-group"
delete-instance-template "${CLIENT_INSTANCE_PREFIX}-template"
if [ "${CLIENTIMAGE_AUTO_DELETE}" == "true" ]; then
delete-image "${CLIENT_IMAGE_NAME}"
fi
fi

if [ "${SERVER_AUTO_DELETE}" == "true" ]; then
echo "Deleting server resources"
delete-instance-groups "${SERVER_INSTANCE_PREFIX}-instance-group"
delete-instance-template "${SERVER_INSTANCE_PREFIX}-template"
if [ "${SERVERIMAGE_AUTO_DELETE}" == "true" ]; then
delete-image "${SERVER_IMAGE_NAME}"
fi
fi

echo "Done. All resources deleted successfully"

0 comments on commit 3f725da

Please sign in to comment.