overleaf-cep/server-ce/test/helpers/hostAdminClient.ts
Jakob Ackermann 81f0807fc6 [web] prepare filestore migration for Server Pro/CE (#27230)
* [web] prepare filestore migration for Server Pro/CE

* [history-v1] remove unused USER_FILES_BUCKET_NAME env var from script

* [server-ce] tests: write default docker-compose.override.yml on startup

* [server-ce] tests: extend access logging of host-admin for response

* [server-ce] tests: test text and binary file upload

* [server-ce] tests: add tests for filestore migration

* [web] simplify feature gate for filestore/project-history-blobs logic

Co-authored-by: Brian Gough <brian.gough@overleaf.com>

* [server-ce] test: fix flaky test helper

---------

Co-authored-by: Brian Gough <brian.gough@overleaf.com>
GitOrigin-RevId: f89bdab2749e2b7a49d609e2eac6bf621c727966
2025-07-22 08:06:58 +00:00

98 lines
2 KiB
TypeScript

const hostAdminURL = Cypress.env('HOST_ADMIN_URL') || 'http://host-admin'
export async function dockerCompose(cmd: string, ...args: string[]) {
return await fetchJSON(`${hostAdminURL}/docker/compose/${cmd}`, {
method: 'POST',
body: JSON.stringify({
args,
}),
})
}
export async function reconfigure({
pro = false,
version = 'latest',
vars = {},
withDataDir = false,
resetData = false,
}): Promise<{ previousConfigServer: string }> {
return await fetchJSON(`${hostAdminURL}/reconfigure`, {
method: 'POST',
body: JSON.stringify({
pro,
version,
vars,
withDataDir,
resetData,
}),
})
}
async function fetchJSON<T = { stdout: string; stderr: string }>(
input: RequestInfo,
init?: RequestInit
): Promise<T> {
if (init?.body) {
init.headers = { 'Content-Type': 'application/json' }
}
let res
for (let attempt = 0; attempt < 5; attempt++) {
try {
res = await fetch(input, init)
break
} catch {
await sleep(3_000)
}
}
if (!res) {
res = await fetch(input, init)
}
const { error, stdout, stderr, ...rest } = await res.json()
if (error) {
console.error(input, init, 'failed:', error)
if (stdout) console.log(stdout)
if (stderr) console.warn(stderr)
const err = new Error(error.message)
Object.assign(err, error)
throw err
}
return { stdout, stderr, ...rest }
}
export async function runScript({
cwd,
script,
args = [],
}: {
cwd: string
script: string
args?: string[]
}) {
return await fetchJSON(`${hostAdminURL}/run/script`, {
method: 'POST',
body: JSON.stringify({
cwd,
script,
args,
}),
})
}
export async function getRedisKeys() {
const { stdout } = await fetchJSON(`${hostAdminURL}/redis/keys`, {
method: 'GET',
})
return stdout.split('\n')
}
export async function purgeFilestoreData() {
await fetchJSON(`${hostAdminURL}/data/user_files`, {
method: 'DELETE',
})
}
async function sleep(ms: number) {
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}