Skip to content

Commit b824e3f

Browse files
committed
cocalc-api: attempt to integrate into CI
1 parent 3eb7997 commit b824e3f

File tree

3 files changed

+163
-1
lines changed

3 files changed

+163
-1
lines changed

.github/workflows/make-and-test.yml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,103 @@ jobs:
128128
name: "test-results-node-${{ matrix.node-version }}-pg-${{ matrix.pg-version }}"
129129
path: 'src/packages/*/junit.xml'
130130

131+
- name: Create CI admin user and API key
132+
run: |
133+
cd src/packages/hub
134+
node run/test-create-admin.js > ../../api_key.txt
135+
# Validate API key was created
136+
if [ ! -s ../../api_key.txt ]; then
137+
echo "Error: API key file is empty or missing"
138+
exit 1
139+
fi
140+
API_KEY=$(cat ../../api_key.txt)
141+
if ! echo "$API_KEY" | grep -qE '^sk-[A-Za-z0-9]+$'; then
142+
echo "Error: Invalid API key format: $API_KEY"
143+
exit 1
144+
fi
145+
echo "API key created successfully"
146+
env:
147+
PGDATABASE: smc
148+
PGUSER: smc
149+
PGHOST: localhost
150+
151+
- name: Start CoCalc Hub
152+
run: |
153+
cd src/packages/hub
154+
pnpm run hub-project-dev-nobuild > hub.log 2>&1 &
155+
HUB_PID=$!
156+
echo $HUB_PID > hub.pid
157+
echo "Hub started with PID $HUB_PID"
158+
# Check if process is still running after a moment
159+
sleep 2
160+
if ! kill -0 $HUB_PID 2>/dev/null; then
161+
echo "Error: Hub process died immediately after starting"
162+
echo "Hub log:"
163+
cat hub.log
164+
exit 1
165+
fi
166+
env:
167+
PGDATABASE: smc
168+
PGUSER: smc
169+
PGHOST: localhost
170+
COCALC_MODE: single-user
171+
172+
- name: Wait for hub readiness
173+
run: |
174+
MAX_ATTEMPTS=30
175+
READY=false
176+
for i in $(seq 1 $MAX_ATTEMPTS); do
177+
if curl -f --max-time 3 http://localhost:5000/healthcheck; then
178+
echo "Hub is ready"
179+
READY=true
180+
break
181+
fi
182+
echo "Waiting for hub... ($i/$MAX_ATTEMPTS)"
183+
sleep 3
184+
done
185+
if [ "$READY" = "false" ]; then
186+
echo "Hub failed to become ready after $MAX_ATTEMPTS attempts"
187+
echo "Hub log:"
188+
cat src/packages/hub/hub.log || echo "No log file found"
189+
exit 1
190+
fi
191+
192+
- name: Install uv for cocalc-api tests
193+
run: curl -LsSf https://astral.sh/uv/install.sh | sh && echo "$HOME/.local/bin" >> $GITHUB_PATH
194+
195+
- name: Run cocalc-api tests
196+
run: |
197+
export COCALC_API_KEY=$(cat src/api_key.txt)
198+
export COCALC_HOST=http://localhost:5000
199+
cd src/python/cocalc-api && uv run pytest --junitxml=test-results.xml
200+
201+
- name: Stop CoCalc Hub
202+
if: always()
203+
run: |
204+
if [ -f src/packages/hub/hub.pid ]; then
205+
HUB_PID=$(cat src/packages/hub/hub.pid)
206+
echo "Stopping hub with PID $HUB_PID"
207+
kill $HUB_PID || true
208+
# Wait a bit for graceful shutdown
209+
sleep 2
210+
# Force kill if still running
211+
kill -9 $HUB_PID 2>/dev/null || true
212+
fi
213+
214+
- name: Upload hub logs
215+
uses: actions/upload-artifact@v4
216+
if: always()
217+
with:
218+
name: "hub-logs-node-${{ matrix.node-version }}-pg-${{ matrix.pg-version }}"
219+
path: 'src/packages/hub/hub.log'
220+
221+
- name: Upload cocalc-api test results
222+
uses: actions/upload-artifact@v4
223+
if: always()
224+
with:
225+
name: "cocalc-api-test-results-node-${{ matrix.node-version }}-pg-${{ matrix.pg-version }}"
226+
path: 'src/python/cocalc-api/test-results.xml'
227+
131228
report:
132229
runs-on: ubuntu-latest
133230

src/packages/hub/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
"cocalc-hub-maintenance-expired": "./run/maintenance-expired.js",
9797
"cocalc-hub-maintenance-syncstrings": "./run/maintenance-syncstrings.js",
9898
"cocalc-hub-maintenance-blobs": "./run/maintenance-blobs.js",
99-
"cocalc-hub-stripe-sync": "./run/stripe-sync.js"
99+
"cocalc-hub-stripe-sync": "./run/stripe-sync.js",
100+
"cocalc-hub-test-create-admin": "./run/test-create-admin.js"
100101
}
101102
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env node
2+
3+
/*
4+
* Script to create a test admin account and API key for CI testing.
5+
* This is used in GitHub Actions to set up cocalc-api tests.
6+
*/
7+
8+
import { v4 as uuidv4 } from "uuid";
9+
import createAccount from "@cocalc/server/accounts/create-account";
10+
import manageApiKeys from "@cocalc/server/api/manage";
11+
import getPool from "@cocalc/database/pool";
12+
13+
async function main() {
14+
const account_id = uuidv4();
15+
const email = "[email protected]";
16+
const password = "testpassword"; // dummy password
17+
const firstName = "CI";
18+
const lastName = "Admin";
19+
20+
console.log(`Creating admin account ${account_id}...`);
21+
22+
// Create the account
23+
await createAccount({
24+
email,
25+
password,
26+
firstName,
27+
lastName,
28+
account_id,
29+
tags: [],
30+
signupReason: "CI testing",
31+
noFirstProject: true,
32+
});
33+
34+
// Set as admin
35+
const pool = getPool();
36+
await pool.query("UPDATE accounts SET groups=$1 WHERE account_id=$2", [
37+
["admin"],
38+
account_id,
39+
]);
40+
41+
console.log("Creating API key...");
42+
43+
// Create API key
44+
const keys = await manageApiKeys({
45+
account_id,
46+
action: "create",
47+
name: "ci-testing",
48+
});
49+
50+
if (!keys || keys.length === 0) {
51+
throw new Error("Failed to create API key");
52+
}
53+
54+
const apiKey = keys[0];
55+
console.log(`API key created: ${apiKey.secret}`);
56+
57+
// Output the key for CI
58+
process.stdout.write(apiKey.secret);
59+
}
60+
61+
main().catch((err) => {
62+
console.error("Error:", err);
63+
process.exit(1);
64+
});

0 commit comments

Comments
 (0)