-
Notifications
You must be signed in to change notification settings - Fork 26
/
smoke_test.sh
executable file
·126 lines (99 loc) · 4.01 KB
/
smoke_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
set -euo pipefail
DIR_NAME="$(dirname $0)"
source "${DIR_NAME}/utils/common.sh"
if [ $(type -P "kcat") ]; then
KCAT=$(which kcat)
else
KCAT=''
fi
source ${DIR_NAME}/kas-installer.env
MK_EXISTING_ID=${1:-}
function cleanup()
{
if [[ "${CREATED_MK_ID:-}" ]] ; then
${DIR_NAME}/managed_kafka.sh --delete ${CREATED_MK_ID}
elif [[ "${SMOKE_TOPIC:-}" ]] ; then
curl -skXDELETE -H "Authorization: Bearer ${ACCESS_TOKEN}" "${ADMIN_SERVER_URL}/api/v1/topics/${SMOKE_TOPIC}" || true
curl -skXDELETE -H "Authorization: Bearer ${OWNER_TOKEN}" "${ADMIN_SERVER_URL}/api/v1/acls?principal=User:${SA_CLIENT_ID}" || true
fi
if [[ "${SA_ID:-}" ]] ; then
${DIR_NAME}/service_account.sh --delete "${SA_ID}" --access-token ${OWNER_TOKEN}
fi
}
trap cleanup EXIT
echo "Obtaining owner token"
OWNER_TOKEN=$(${DIR_NAME}/get_access_token.sh --owner 2>/dev/null)
echo "Creating service account"
SERVICE_ACCOUNT_RESOURCE=$(${DIR_NAME}/service_account.sh --create --access-token ${OWNER_TOKEN})
SA_ID=$(echo ${SERVICE_ACCOUNT_RESOURCE} | jq -r .id)
echo "Service account created: ${SA_ID}"
# MAS SSO (via KFM API) use different properties for client ID/secret than SSO directly. Support both forms here
SA_CLIENT_ID=$(echo ${SERVICE_ACCOUNT_RESOURCE} | jq -r '.client_id // .clientId')
SA_CLIENT_SECRET=$(echo ${SERVICE_ACCOUNT_RESOURCE} | jq -r '.client_secret // .secret')
echo "Obtaining access token for service account"
ACCESS_TOKEN=$(${DIR_NAME}/get_access_token.sh ${SA_CLIENT_ID} ${SA_CLIENT_SECRET} 2>/dev/null)
if [ "${MK_EXISTING_ID:-}" != "" ] ; then
MK_SMOKE=$(${DIR_NAME}/managed_kafka.sh --get ${MK_EXISTING_ID} --access-token ${OWNER_TOKEN})
else
MK_SMOKE=$(${DIR_NAME}/managed_kafka.sh --create 'kafka-smoke' --access-token ${OWNER_TOKEN})
CREATED_MK_ID=$(echo ${MK_SMOKE} | jq -r .id)
fi
BOOTSTRAP_HOST=$(echo ${MK_SMOKE} | jq -r .bootstrap_server_host)
ADMIN_SERVER_URL=$(echo ${MK_SMOKE} | jq -r .admin_api_server_url)
if [ "${ADMIN_SERVER_URL}" == "null" ] ; then
echo "Admin server url not yet available, unable to complete smoke test"
exit 1
fi
SMOKE_TOPIC="smoke_topic-$(${DATE} +%Y%j%H%M)"
curl -f -skXPOST -H'Content-type: application/json' \
-H "Authorization: Bearer ${OWNER_TOKEN}" \
--data '{"resourceType":"TOPIC", "resourceName":"'${SMOKE_TOPIC}'", "patternType":"LITERAL", "principal":"User:'${SA_CLIENT_ID}'", "operation":"ALL", "permission":"ALLOW"}' \
"${ADMIN_SERVER_URL}/api/v1/acls"
echo "Granted service account access to topic '${SMOKE_TOPIC}'"
SMOKE_TOPIC_INFO=$(curl -skXPOST -H'Content-type: application/json' \
-H "Authorization: Bearer $ACCESS_TOKEN" \
--data '{ "name":"'${SMOKE_TOPIC}'", "settings": { "numPartitions":3, "config": [] } }' \
"${ADMIN_SERVER_URL}/api/v1/topics")
MSG_FILE=$(mktemp)
SMOKE_MESSAGE="Smoke message from $(date)"
echo "${SMOKE_MESSAGE}" > ${MSG_FILE}
PARGS=()
CARGS=()
if [ "${KCAT}" ]; then
CMD=${KCAT}
PARGS=()
CARGS=()
else
CMD=docker
PARGS=(run --rm --mount type=bind,source=${MSG_FILE},target=${MSG_FILE} --network=host edenhill/kcat:1.7.0)
CARGS=(run --rm --network=host edenhill/kcat:1.7.0)
fi
${CMD} \
${PARGS[@]+"${PARGS[@]}"} \
-t "${SMOKE_TOPIC}" \
-b "${BOOTSTRAP_HOST}" \
-X security.protocol=SASL_SSL \
-X sasl.mechanisms=PLAIN \
-X sasl.username="${SA_CLIENT_ID}" \
-X sasl.password="${SA_CLIENT_SECRET}" \
-X enable.ssl.certificate.verification=false \
-P -l ${MSG_FILE}
rm ${MSG_FILE}
echo "Smoke message sent to topic: [${SMOKE_MESSAGE}]"
SMOKE_MESSAGE_OUT=$(${CMD} \
${CARGS[@]+"${CARGS[@]}"} \
-t "${SMOKE_TOPIC}" \
-b "${BOOTSTRAP_HOST}" \
-X security.protocol=SASL_SSL \
-X sasl.mechanisms=PLAIN \
-X sasl.username="${SA_CLIENT_ID}" \
-X sasl.password="${SA_CLIENT_SECRET}" \
-X enable.ssl.certificate.verification=false \
-C -c1)
echo "Smoke message read from topic: [${SMOKE_MESSAGE_OUT}]"
if [ "${SMOKE_MESSAGE_OUT}" = "${SMOKE_MESSAGE}" ] ; then
echo "Smoke test successful"
else
echo "Failed smoke test: '${SMOKE_MESSAGE_OUT}' != '${SMOKE_MESSAGE}'"
fi