overleaf/server-ce/test/helpers/hostAdminClient.ts
Jakob Ackermann ce9b531892 Merge pull request #19098 from overleaf/jpa-graceful-shutdown-tests
[server-ce] add test for graceful shutdown

GitOrigin-RevId: 5e72e0073169009d2e3ece5a79cbd62051f6ad5b
2024-06-25 11:12:43 +00:00

85 lines
1.8 KiB
TypeScript

const hostAdminUrl = Cypress.env('ADMIN_CLIENT_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 mongoInit() {
return await fetchJSON(`${hostAdminUrl}/mongo/init`, {
method: 'POST',
})
}
export async function resetData() {
return await fetchJSON(`${hostAdminUrl}/reset/data`, {
method: 'POST',
})
}
export async function reconfigure({
pro = false,
version = 'latest',
vars = {},
withDataDir = false,
}) {
return await fetchJSON(`${hostAdminUrl}/reconfigure`, {
method: 'POST',
body: JSON.stringify({
pro,
version,
vars,
withDataDir,
}),
})
}
async function fetchJSON(
input: RequestInfo,
init?: RequestInit
): Promise<{ stdout: string; stderr: string }> {
if (init?.body) {
init.headers = { 'Content-Type': 'application/json' }
}
const res = await fetch(input, init)
const { error, stdout, stderr } = 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 }
}
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')
}