Skip to content

Commit a89eb5c

Browse files
committed
cocalc-api: fix the CI test
1 parent 5faae38 commit a89eb5c

File tree

3 files changed

+62
-27
lines changed

3 files changed

+62
-27
lines changed

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

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -128,28 +128,13 @@ 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-
151131
- name: Start CoCalc Hub
152132
run: |
133+
# Create conat password for hub internal authentication
134+
mkdir -p src/data/secrets
135+
echo "test-conat-password-$(date +%s)" > src/data/secrets/conat-password
136+
chmod 600 src/data/secrets/conat-password
137+
153138
cd src/packages/hub
154139
pnpm run hub-project-dev-nobuild > hub.log 2>&1 &
155140
HUB_PID=$!
@@ -168,13 +153,14 @@ jobs:
168153
PGUSER: smc
169154
PGHOST: localhost
170155
COCALC_MODE: single-user
156+
COCALC_TEST_MODE: yes
171157

172158
- name: Wait for hub readiness
173159
run: |
174160
MAX_ATTEMPTS=30
175161
READY=false
176162
for i in $(seq 1 $MAX_ATTEMPTS); do
177-
if curl -f --max-time 3 http://localhost:5000/healthcheck; then
163+
if curl -sf --max-time 3 http://localhost:5000/healthcheck > /dev/null; then
178164
echo "Hub is ready"
179165
READY=true
180166
break
@@ -189,9 +175,54 @@ jobs:
189175
exit 1
190176
fi
191177
178+
- name: Create CI admin user and API key
179+
run: |
180+
cd src/packages/hub
181+
node dist/run/test-create-admin.js > ../../api_key.txt
182+
echo "Contents of api_key.txt:"
183+
cat ../../api_key.txt
184+
echo ""
185+
# Validate API key was created
186+
if [ ! -s ../../api_key.txt ]; then
187+
echo "Error: API key file is empty or missing"
188+
exit 1
189+
fi
190+
API_KEY=$(cat ../../api_key.txt)
191+
if ! echo "$API_KEY" | grep -qE '^sk-[A-Za-z0-9]+$'; then
192+
echo "Error: Invalid API key format: $API_KEY"
193+
exit 1
194+
fi
195+
echo "API key created successfully"
196+
env:
197+
PGDATABASE: smc
198+
PGUSER: smc
199+
PGHOST: localhost
200+
192201
- name: Install uv for cocalc-api tests
193202
run: curl -LsSf https://astral.sh/uv/install.sh | sh && echo "$HOME/.local/bin" >> $GITHUB_PATH
194203

204+
- name: Verify API endpoint is responding
205+
run: |
206+
API_KEY=$(cat src/api_key.txt)
207+
echo "Testing API endpoint with system.ping..."
208+
MAX_ATTEMPTS=30
209+
for i in $(seq 1 $MAX_ATTEMPTS); do
210+
if curl -sf --max-time 5 -u "$API_KEY:" -H 'Content-Type: application/json' \
211+
-d '{"name":"system.ping", "args":[]}' \
212+
http://localhost:5000/api/conat/hub > /dev/null; then
213+
echo "API endpoint is responding"
214+
break
215+
fi
216+
if [ $i -eq $MAX_ATTEMPTS ]; then
217+
echo "API endpoint failed to respond after $MAX_ATTEMPTS attempts"
218+
echo "Hub log tail:"
219+
tail -100 src/packages/hub/hub.log || echo "No log file found"
220+
exit 1
221+
fi
222+
echo "Waiting for API endpoint... ($i/$MAX_ATTEMPTS)"
223+
sleep 3
224+
done
225+
195226
- name: Run cocalc-api tests
196227
run: |
197228
export COCALC_API_KEY=$(cat src/api_key.txt)

src/packages/hub/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@
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",
100-
"cocalc-hub-test-create-admin": "./run/test-create-admin.js"
99+
"cocalc-hub-stripe-sync": "./run/stripe-sync.js"
101100
}
102101
}

src/packages/hub/run/test-create-admin.js renamed to src/packages/hub/run/test-create-admin.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import { v4 as uuidv4 } from "uuid";
9+
910
import createAccount from "@cocalc/server/accounts/create-account";
1011
import manageApiKeys from "@cocalc/server/api/manage";
1112
import getPool from "@cocalc/database/pool";
@@ -17,7 +18,7 @@ async function main() {
1718
const firstName = "CI";
1819
const lastName = "Admin";
1920

20-
console.log(`Creating admin account ${account_id}...`);
21+
console.error(`Creating admin account ${account_id}...`);
2122

2223
// Create the account
2324
await createAccount({
@@ -38,7 +39,7 @@ async function main() {
3839
account_id,
3940
]);
4041

41-
console.log("Creating API key...");
42+
console.error("Creating API key...");
4243

4344
// Create API key
4445
const keys = await manageApiKeys({
@@ -52,7 +53,11 @@ async function main() {
5253
}
5354

5455
const apiKey = keys[0];
55-
console.log(`API key created: ${apiKey.secret}`);
56+
if (!apiKey.secret) {
57+
throw new Error("API key secret is missing");
58+
}
59+
console.error(`API key created with id=${apiKey.id}: ${apiKey.secret}`);
60+
console.error(`Last 6 chars: ${apiKey.secret.slice(-6)}`);
5661

5762
// Output the key for CI
5863
process.stdout.write(apiKey.secret);
@@ -61,4 +66,4 @@ async function main() {
6166
main().catch((err) => {
6267
console.error("Error:", err);
6368
process.exit(1);
64-
});
69+
});

0 commit comments

Comments
 (0)