Commit a934fd1f authored by 金戟's avatar 金戟
Browse files

feat: add provide command integration test

parent bd0aba59
Showing with 76 additions and 42 deletions
+76 -42
......@@ -6,13 +6,17 @@ MODE="vpn"
# Log
function log() {
printf ">> ${*}\n"
printf "\e[34m>>\e[0m ${*}\n"
}
# Clean everything up
function cleanup() {
log "cleaning up ..."
for i in `jobs -l | grep '^\[[0-9]\]' | cut -c 1-30 | grep -o ' [0-9]\+ ' | awk {'print $1'}`; do sudo kill $i; done
for i in `jobs -l | grep '^\[[0-9]\]' | cut -c 1-30 | grep -o ' [0-9]\+ ' | awk {'print $1'}`; do
log "killing process ${i}"
sudo kill -15 ${i};
done
docker rm -f tomcat
kubectl -n ${NS} delete deployment tomcat
kubectl -n ${NS} delete service tomcat
kubectl delete namespace ${NS}
......@@ -20,18 +24,23 @@ function cleanup() {
# Exit with error
function fail() {
log "'\e[31m${*} !!!\e[0m"
log "\e[31m${*} !!!\e[0m"
log "check logs for detail: \e[33m`ls -t /tmp/kt-it-*.log`\e[0m"
cleanup
exit -1
}
# Test passed
function success() {
log "\e[32m${*} !!!\e[0m"
}
# Wait pod ready
function wait_for_pod() {
for i in `seq 10`; do
log "checking pod ${1}, ${i} times"
exist=`kubectl -n ${NS} get pod | grep "^${1}-" | grep "${2}/${2}"`
if [ "$exist" != "" ]; then
sleep 2
return
fi
sleep 3
......@@ -39,6 +48,36 @@ function wait_for_pod() {
fail "failed to start pod ${1}"
}
# Check if background job running
function check_job() {
declare -i count=`jobs | grep "${1}" | wc -l`
if [ ${count} -ne 1 ]; then fail "failed to setup ${1} job"; fi
}
# Check if ktctl pid file exists
function check_pid_file() {
pidFile=`ls -t ~/.ktctl/${1}-*.pid | less -1`
pid=`cat ${pidFile}`
log "ktctl ${1} pid: ${pid}"
}
# Verify access specified url with result
function verify() {
target=${1}
url=${2}
shift 2
log "accessing ${url}"
for c in `seq 5`; do
res=`curl --connect-timeout 2 -s ${url}`
if [ "$res" = "${*}" ]; then
return
fi
log "retry times: ${c}"
sleep 2
done
fail "failed to access ${target}, got: ${res}"
}
# Require for root access
sudo true
if [ ${?} -ne 0 ]; then fail "failed to require root access"; fi
......@@ -47,7 +86,8 @@ if [ ${?} -ne 0 ]; then fail "failed to require root access"; fi
existPid=`ps aux | grep "ktctl" | grep -v "grep" | awk '{print $2}' | sort -n | head -1`
if [ "${existPid}" != "" ]; then fail "ktctl already running before test start (pid: ${existPid})"; fi
ktctl clean >/dev/null 2>&1
rm -f /tmp/kt-it-*.log
ktctl -n ${NS} clean >/dev/null 2>&1
# Prepare test resources
kubectl create namespace ${NS}
......@@ -55,7 +95,7 @@ kubectl create namespace ${NS}
kubectl -n ${NS} create deployment tomcat --image=tomcat:9 --port=8080
kubectl -n ${NS} expose deployment tomcat --port=8080 --target-port=8080
wait_for_pod tomcat 1
kubectl -n ${NS} exec deployment/tomcat -c tomcat -- /bin/bash -c 'mkdir webapps/ROOT; echo "kt-connect demo v1" > webapps/ROOT/index.html'
kubectl -n ${NS} exec deployment/tomcat -c tomcat -- /bin/bash -c 'mkdir -p webapps/ROOT; echo "kt-connect demo v1" > webapps/ROOT/index.html'
podIp=`kubectl -n ${NS} get pod --selector app=tomcat -o jsonpath='{.items[0].status.podIP}'`
log "tomcat pod-ip: ${podIp}"
......@@ -65,52 +105,46 @@ log "tomcat cluster-ip: ${clusterIP}"
if [ ${clusterIP} = "" ]; then fail "failed to setup test service"; fi
# Test connect
sudo ktctl -n ${NS} -i ${IMAGE} -f connect --method ${MODE} >/tmp/kt-ci-connect.log 2>&1 &
sleep 1
declare -i count=`jobs | grep 'connect' | wc -l`
if [ ${count} -ne 1 ]; then fail "failed to setup ktctl connect"; fi
pidFile=`ls -t ~/.ktctl/connect-*.pid | less -1`
connectPid=`cat ${pidFile}`
log "ktctl connect pid: ${connectPid}"
res=`curl -s "http://${podIp}:8080"`
if [ "${res}" != "kt-connect demo v1" ]; then fail "failed to access via pod-ip, got: ${res}"; fi
res=`curl -s "http://${clusterIP}:8080"`
if [ "${res}" != "kt-connect demo v1" ]; then fail "failed to access via cluster-ip, got: ${res}"; fi
res=`curl -s "http://tomcat.${NS}.svc.cluster.local:8080"`
if [ "${res}" != "kt-connect demo v1" ]; then fail "failed to access via service-domain, got: ${res}"; fi
sudo ktctl -n ${NS} -i ${IMAGE} -f connect --method ${MODE} >/tmp/kt-it-connect.log 2>&1 &
wait_for_pod kt-connect 1
check_job connect
check_pid_file connect
verify "pod-ip" "http://${podIp}:8080" "kt-connect demo v1"
verify "cluster-ip" "http://${clusterIP}:8080" "kt-connect demo v1"
verify "service-domain" "http://tomcat:8080" "kt-connect demo v1"
verify "service-domain-with-namespace" "http://tomcat.${NS}:8080" "kt-connect demo v1"
verify "service-domain-full-qualified" "http://tomcat.${NS}.svc.cluster.local:8080" "kt-connect demo v1"
success "ktctl connect test passed"
# Prepare local service
while true; do
printf "HTTP/1.1 200 OK\nContent-Length: 19\nContent-Type: text/plain\n\nkt-connect local v2" | nc -l 8080
sleep 1
done >/tmp/kt-ci-nc.log 2>&1 &
docker run -d --name tomcat -p 8080:8080 tomcat:9
sleep 1
res=`curl -s "http://127.0.0.1:8080"`
if [ "${res}" != "kt-connect local v2" ]; then fail "failed to setup local service, got: ${res}"; fi
exist=`docker ps -a | grep ' tomcat$' | grep -i ' Up '`
if [ "${exist}" = "" ]; then fail "failed to start up local tomcat container"; fi
docker exec tomcat /bin/bash -c 'mkdir webapps/ROOT; echo "kt-connect local v2" > webapps/ROOT/index.html'
ncPid=`jobs -l | grep 'while true' | grep -o ' [0-9]\+ ' | awk {'print $1'}`
log "local server pid: ${ncPid}"
verify "local-service" "http://127.0.0.1:8080" "kt-connect local v2"
# Test exchange
ktctl -n ${NS} -i ${IMAGE} -f exchange tomcat --expose 8080 >/tmp/kt-ci-exchange.log 2>&1 &
sleep 1
declare -i count=`jobs | grep 'exchange' | wc -l`
if [ ${count} -ne 1 ]; then fail "failed to setup ktctl exchange"; fi
pidFile=`ls -t ~/.ktctl/exchange-*.pid | less -1`
exchangePid=`cat ${pidFile}`
log "ktctl exchange pid: ${exchangePid}"
ktctl -n ${NS} -i ${IMAGE} -f exchange tomcat --expose 8080 >/tmp/kt-it-exchange.log 2>&1 &
wait_for_pod tomcat-kt 1
check_job exchange
check_pid_file exchange
verify "service-domain" "http://tomcat.${NS}.svc.cluster.local:8080" "kt-connect local v2"
success "ktctl exchange test passed"
res=`curl -s "http://tomcat.${NS}.svc.cluster.local:8080"`
if [ "${res}" != "kt-connect local v2" ]; then fail "failed to exchange test service, got: ${res}"; fi
# Test provide
ktctl -n ${NS} -i ${IMAGE} -f provide tomcat-preview --expose 8080 >/tmp/kt-it-provide.log 2>&1 &
wait_for_pod tomcat-preview-kt 1
check_job provide
check_pid_file provide
log "\e[32mall tests done !!!\e[0m"
verify "service-domain" "http://tomcat-preview.${NS}.svc.cluster.local:8080" "kt-connect local v2"
success "ktctl provide test passed"
# Clean up
cleanup
success "all tests done"
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment