-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-e2e.sh
More file actions
executable file
·158 lines (135 loc) · 4.58 KB
/
Copy pathtest-e2e.sh
File metadata and controls
executable file
·158 lines (135 loc) · 4.58 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env bash
# Usage: test-e2e.sh path-to-obelisk.toml <truncate>
# If the second parameter is "truncate", database tables will be wiped.
# This script starts Obelisk,
# sends a "star-added" HTTP request to the webhook endpoint,
# waits for the scheduled execution to complete,
# and verifies that the user is stored in the database along with the generated description.
set -exo pipefail
cd "$(dirname "$0")/.."
OBELISK_TOML="$1"
TRUNCATE="${2:-}"
STAR_ACCOUNT="someghaccount"
STAR_REPO="someghrepo"
MOCK_OPENAI_PORT=18080
MOCK_PID=""
MOCK_OPENAI_API_BASE_URL="http://127.0.0.1:$MOCK_OPENAI_PORT"
export OBELISK__API__TOKEN=$(obelisk generate token --json | jq -r .token)
export GITHUB_WEBHOOK_SECRET="It's a Secret to Everybody"
# Use mock server unless OPENAI_API_KEY is already set
if [[ -z "${OPENAI_API_KEY:-}" ]]; then
export OPENAI_API_KEY="mock-api-key-for-testing"
export OPENAI_API_BASE_URL="$MOCK_OPENAI_API_BASE_URL"
fi
obelisk server verify --deployment $OBELISK_TOML
obelisk server run --deployment $OBELISK_TOML &
PID=$!
# Start the mock server. Starting after `obelisk verify` to avoid two cleanup functions.
if [[ "$OPENAI_API_BASE_URL" == "$MOCK_OPENAI_API_BASE_URL" ]]; then
echo "OPENAI_API_KEY was not set, using mock OpenAI server"
# Start mock OpenAI server
python3 ./scripts/mock-openai-server.py $MOCK_OPENAI_PORT &
MOCK_PID=$!
echo "Started mock OpenAI server with PID $MOCK_PID"
# Wait for mock server to be ready
SECONDS=0
while ! curl -s http://127.0.0.1:$MOCK_OPENAI_PORT/v1/chat/completions -X POST -d '{}' > /dev/null 2>&1; do
if [[ $SECONDS -ge 5 ]]; then
echo "Mock OpenAI server failed to start"
exit 1
fi
sleep 0.5
done
echo "Mock OpenAI server is ready"
fi
cleanup() {
echo "Sending SIGINT to obelisk process $PID..."
kill -SIGINT $PID 2>/dev/null || true
# Wait up to 5 seconds for the process to exit
SECONDS=0
while kill -0 $PID 2>/dev/null; do
if [[ $SECONDS -ge 5 ]]; then
echo "Cleanup timeout reached. Sending SIGKILL to process $PID..."
kill -SIGKILL $PID 2>/dev/null || true
break
fi
sleep 1
done
# Kill mock OpenAI server if it was started
if [[ -n "$MOCK_PID" ]]; then
echo "Stopping mock OpenAI server (PID $MOCK_PID)..."
kill $MOCK_PID 2>/dev/null || true
fi
}
trap cleanup EXIT
delete_from() {
TABLE="$1"
curl --fail -X POST "https://${TURSO_LOCATION}/v2/pipeline" \
-H "Authorization: Bearer ${TURSO_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"requests": [
{ "type": "execute", "stmt": { "sql": "DELETE FROM '${TABLE}'" } },
{ "type": "close" }
]
}'
}
delete_from_all() {
delete_from "stars"
delete_from "users"
delete_from "repos"
}
# If TRUNCATE is set to "truncate", delete data
if [[ "$TRUNCATE" == "truncate" ]]; then
delete_from_all
fi
# Wait for obelisk to start responding
SECONDS=0
while ! obelisk component list 2>/dev/null; do
if [[ $SECONDS -ge 10 ]]; then
echo "Timeout reached"
exit 1
fi
sleep 1
done
# Make sure this repo has no stars
JSON=$(curl --fail "http://127.0.0.1:9090?repo=${STAR_ACCOUNT}/${STAR_REPO}&ordering=asc&limit=1")
if [[ "$JSON" != "[]" ]]; then
echo "The repo ${STAR_ACCOUNT}/${STAR_REPO} already has star gazers"
exit 1
fi
PAYLOAD='{
"action": "created",
"sender": {
"login": "'${TEST_GITHUB_LOGIN}'"
},
"repository": {
"owner": {
"login": "'${STAR_ACCOUNT}'"
},
"name": "'${STAR_REPO}'"
}
}'
SIGNATURE=$(echo -n "$PAYLOAD" | openssl dgst -sha256 -hmac "$GITHUB_WEBHOOK_SECRET" | cut -d ' ' -f2)
SIGNATURE="sha256=$SIGNATURE"
# Send the webhook event
EXECUTION_ID=$(curl --fail -X POST http://127.0.0.1:9090 \
-H "X-Hub-Signature-256:$SIGNATURE" \
-d "$PAYLOAD" -i | grep -i "execution-id" | cut -d ' ' -f2- | tr -d '\r')
# Wait until the scheduled execution of the workflow finishes.
obelisk execution get --follow $EXECUTION_ID
# Get the first and only user back from the database.
JSON=$(curl --fail "http://127.0.0.1:9090?repo=${STAR_ACCOUNT}/${STAR_REPO}&ordering=asc&limit=1")
LOGIN=$(echo $JSON | jq .[0].login -r)
if [[ "$LOGIN" != ${TEST_GITHUB_LOGIN} ]]; then
echo "Error: First stargazer should be '${TEST_GITHUB_LOGIN}', got '$LOGIN'" >&2
exit 1
fi
DESCRIPTION=$(echo $JSON | jq .[0].description -r)
if [ "$DESCRIPTION" == "null" ]; then
echo "Error: description is null" >&2
exit 1
else
echo "Description: $DESCRIPTION"
fi
echo "End to end test succeeded."