-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest.sh
executable file
·81 lines (71 loc) · 3.21 KB
/
test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/bash
# shellcheck disable=SC2086,SC2181
set -o errexit
set -o pipefail
set -o nounset
# set -x # debug
cleanup() {
echo "container/user info test complete!"
}
trap cleanup EXIT
TESTING_CHANNEL=${TESTING_CHANNEL:-C034FNXS3FA}
main() {
local ns=${1:-bashbot}
local dn=${2:-bashbot}
# Retry loop (20/$i with 3 second delay between loops)
for i in {30..1}; do
# Get the expected number of replicas for this deployment
expectedReplicas=$(kubectl --namespace ${ns} -o jsonpath='{.status.replicas}' get deployment ${dn})
# If the '.status.replicas' value is empty/not-set, set the default number of replicas to '1'
[ -z "$expectedReplicas" ] && expectedReplicas=1
# Get the number of replicas that are ready for this deployment
readyReplicas=$(kubectl --namespace ${ns} -o jsonpath='{.status.readyReplicas}' get deployment ${dn})
# If the .status.readyReplicas value is empty/not-set, set the default number of "ready" replicas to '0'
[ -z "$readyReplicas" ] && readyReplicas=0
# Test that the number of "ready" replicas match the number of expected replicas for this deployment
test $readyReplicas -eq $expectedReplicas \
&& test 1 -eq 1
if [ $? -eq 0 ]; then
# echo "Bashbot deployment confirmed!"
# kubectl --namespace ${ns} get deployments
found_user=0
for j in {3..1}; do
bashbot_pod=$(kubectl -n ${ns} get pods -o jsonpath='{.items[0].metadata.name}')
# Send `!bashbot info` via bashbot binary within bashbot pod
kubectl --namespace ${ns} exec $bashbot_pod -- bash -c \
'bashbot send-message --channel '${TESTING_CHANNEL}' --msg "!bashbot info"'
sleep 5
last_log_line=$(kubectl -n ${ns} logs --tail 10 $bashbot_pod)
# Tail the last line of the bashbot pod's log looking
# for the string 'whoami: bb' to prove the info script
# is showing the correct value for whoami
if [[ $last_log_line =~ "whoami: bb" ]]; then
echo "container/user info test successful!"
found_user=1
kubectl --namespace ${ns} exec $bashbot_pod -- bash -c \
'bashbot send-message --channel '${TESTING_CHANNEL}' --msg ":large_green_circle: container/user info test successful!\n> Saw \"whoami: bb\" in bashbot logs"'
exit 0
fi
echo "Bashbot container/user info test failed. $j more attempts..."
sleep 5
done
# Don't require container/user info tests to pass for the whole test to pass
[ $found_user -eq 1 ] && exit 0 || exit 1
fi
# Since the deployment was not ready, try again $i more times
echo "Deployment not found or not ready. $i more attempts..."
sleep 5
done
# The retry loop has exited without finding a stable deployment
echo "Bashbot deployment failed :("
# Display some debug information and fail test
kubectl --namespace ${ns} get deployments
kubectl --namespace ${ns} get pods -o wide
kubectl --namespace ${ns} exec $bashbot_pod -- bash -c \
'bashbot send-message --channel '${TESTING_CHANNEL}' --msg ":red_circle: container/user info test failed!"'
exit 1
}
# Usage: ./test.sh [namespace] [deployment]
namespace=${1:-bashbot}
deploymentName=${2:-bashbot}
main $namespace $deploymentName