forked from Jigsaw-Code/outline-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_integration.action.sh
executable file
·99 lines (90 loc) · 4.12 KB
/
test_integration.action.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
#!/bin/bash -eu
#
# Copyright 2020 The Outline Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Metrics server integration test. Posts metrics to the development environment and queries BigQuery
# to ensure the rows have been inserted to the features and connections tables.
readonly BIGQUERY_PROJECT='uproxysite'
readonly BIGQUERY_DATASET='uproxy_metrics_dev'
readonly CONNECTIONS_TABLE='connections_v1'
readonly FEATURES_TABLE='feature_metrics'
readonly METRICS_URL='https://dev.metrics.getoutline.org'
TMPDIR="$(mktemp -d)"
readonly TMPDIR
readonly CONNECTIONS_REQUEST="${TMPDIR}/connections.json"
readonly CONNECTIONS_RESPONSE="${TMPDIR}/connections_res.json"
readonly CONNECTIONS_EXPECTED_RESPONSE="${TMPDIR}/connections_expected_res.json"
readonly FEATURES_REQUEST="${TMPDIR}/features_req.json"
readonly FEATURES_RESPONSE="${TMPDIR}/features_res.json"
readonly FEATURES_EXPECTED_RESPONSE="${TMPDIR}/features_expected_res.json"
TIMESTAMP="$(date +%s%3N)"
SERVER_ID="$(uuidgen)"
SERVER_VERSION="$(uuidgen)"
USER_ID1="$(uuidgen)"
USER_ID2="$(uuidgen)"
readonly TIMESTAMP SERVER_ID SERVER_VERSION USER_ID1 USER_ID2
# BYTES_TRANSFERRED2 < BYTES_TRANSFERRED1 so we can order the records before comparing them.
BYTES_TRANSFERRED1=$((2 + RANDOM % 100))
BYTES_TRANSFERRED2=$((BYTES_TRANSFERRED1 - 1))
PER_KEY_LIMIT_COUNT=$((RANDOM))
declare -ir BYTES_TRANSFERRED1 BYTES_TRANSFERRED2 PER_KEY_LIMIT_COUNT
echo "Using tmp directory ${TMPDIR}"
# Write the request data to temporary files.
cat << EOF > "${CONNECTIONS_REQUEST}"
{
"serverId": "${SERVER_ID}",
"startUtcMs": ${TIMESTAMP},
"endUtcMs": $((TIMESTAMP+1)),
"userReports": [{
"userId": "${USER_ID1}",
"bytesTransferred": ${BYTES_TRANSFERRED1},
"countries": ["US", "NL"]
}, {
"userId": "${USER_ID2}",
"bytesTransferred": ${BYTES_TRANSFERRED2},
"countries": ["UK"]
}]
}
EOF
cat << EOF > "${FEATURES_REQUEST}"
{
"serverId": "${SERVER_ID}",
"serverVersion": "${SERVER_VERSION}",
"timestampUtcMs": ${TIMESTAMP},
"dataLimit": {
"enabled": false,
"perKeyLimitCount": ${PER_KEY_LIMIT_COUNT}
}
}
EOF
# Write the expected responses to temporary files.
# Ignore the ISO formatted timestamps to ease the comparison.
cat << EOF > "${CONNECTIONS_EXPECTED_RESPONSE}"
[{"bytesTransferred":"${BYTES_TRANSFERRED1}","countries":["US","NL"],"serverId":"${SERVER_ID}","userId":"${USER_ID1}"},{"bytesTransferred":"${BYTES_TRANSFERRED2}","countries":["UK"],"serverId":"${SERVER_ID}","userId":"${USER_ID2}"}]
EOF
cat << EOF > "${FEATURES_EXPECTED_RESPONSE}"
[{"dataLimit":{"enabled":"false","perKeyLimitCount":"${PER_KEY_LIMIT_COUNT}"},"serverId":"${SERVER_ID}","serverVersion":"${SERVER_VERSION}"}]
EOF
echo "Connections request:"
cat "${CONNECTIONS_REQUEST}"
curl -X POST -H "Content-Type: application/json" -d "@${CONNECTIONS_REQUEST}" "${METRICS_URL}/connections" && echo
sleep 5
bq --project_id "${BIGQUERY_PROJECT}" --format json query --nouse_legacy_sql "SELECT serverId, userId, bytesTransferred, countries FROM \`${BIGQUERY_DATASET}.${CONNECTIONS_TABLE}\` WHERE serverId = \"${SERVER_ID}\" ORDER BY bytesTransferred DESC LIMIT 2" > "${CONNECTIONS_RESPONSE}"
diff "${CONNECTIONS_RESPONSE}" "${CONNECTIONS_EXPECTED_RESPONSE}"
echo "Features request:"
cat "${FEATURES_REQUEST}"
curl -X POST -H "Content-Type: application/json" -d "@${FEATURES_REQUEST}" "${METRICS_URL}/features" && echo
sleep 5
bq --project_id "${BIGQUERY_PROJECT}" --format json query --nouse_legacy_sql "SELECT serverId, serverVersion, dataLimit FROM \`${BIGQUERY_DATASET}.${FEATURES_TABLE}\` WHERE serverId = \"${SERVER_ID}\" ORDER BY timestamp DESC LIMIT 1" > "${FEATURES_RESPONSE}"
diff "${FEATURES_RESPONSE}" "${FEATURES_EXPECTED_RESPONSE}"