2020-12-09 05:12:45 -05:00
|
|
|
const fs = require('fs')
|
|
|
|
const Path = require('path')
|
|
|
|
|
2021-07-07 05:38:56 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-12-09 05:12:45 -05:00
|
|
|
const { getCsrfTokenForFactory } = require('./support/Csrf')
|
|
|
|
const { SmokeTestFailure } = require('./support/Errors')
|
|
|
|
const {
|
|
|
|
requestFactory,
|
2021-04-27 03:52:58 -04:00
|
|
|
assertHasStatusCode,
|
2020-12-09 05:12:45 -05:00
|
|
|
} = require('./support/requestHelper')
|
|
|
|
const { processWithTimeout } = require('./support/timeoutHelper')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-12-09 05:12:45 -05:00
|
|
|
const STEP_TIMEOUT = Settings.smokeTest.stepTimeout
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-12-09 05:12:45 -05:00
|
|
|
const PATH_STEPS = Path.join(__dirname, './steps')
|
|
|
|
const STEPS = fs
|
|
|
|
.readdirSync(PATH_STEPS)
|
|
|
|
.sort()
|
|
|
|
.map(name => {
|
|
|
|
const step = require(Path.join(PATH_STEPS, name))
|
|
|
|
step.name = Path.basename(name, '.js')
|
|
|
|
return step
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-12-09 05:12:45 -05:00
|
|
|
async function runSmokeTests({ isAborted, stats }) {
|
|
|
|
let lastStep = stats.start
|
|
|
|
function completeStep(key) {
|
|
|
|
const step = Date.now()
|
|
|
|
stats.steps.push({ [key]: step - lastStep })
|
|
|
|
lastStep = step
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-12-09 05:12:45 -05:00
|
|
|
const request = requestFactory({ timeout: STEP_TIMEOUT })
|
|
|
|
const getCsrfTokenFor = getCsrfTokenForFactory({ request })
|
|
|
|
const ctx = {
|
|
|
|
assertHasStatusCode,
|
|
|
|
getCsrfTokenFor,
|
|
|
|
processWithTimeout,
|
|
|
|
request,
|
|
|
|
stats,
|
2021-04-27 03:52:58 -04:00
|
|
|
timeout: STEP_TIMEOUT,
|
2020-12-09 05:12:45 -05:00
|
|
|
}
|
|
|
|
const cleanupSteps = []
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-12-09 05:12:45 -05:00
|
|
|
async function runAndTrack(id, fn) {
|
|
|
|
let result
|
|
|
|
try {
|
|
|
|
result = await fn(ctx)
|
|
|
|
} catch (e) {
|
|
|
|
throw new SmokeTestFailure(`${id} failed`, {}, e)
|
|
|
|
} finally {
|
|
|
|
completeStep(id)
|
|
|
|
}
|
|
|
|
Object.assign(ctx, result)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-12-09 05:12:45 -05:00
|
|
|
completeStep('init')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-12-09 05:12:45 -05:00
|
|
|
let err
|
|
|
|
try {
|
|
|
|
for (const step of STEPS) {
|
|
|
|
if (isAborted()) break
|
|
|
|
|
|
|
|
const { name, run, cleanup } = step
|
|
|
|
if (cleanup) cleanupSteps.unshift({ name, cleanup })
|
|
|
|
|
|
|
|
await runAndTrack(`run.${name}`, run)
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
|
|
|
|
const cleanupErrors = []
|
|
|
|
for (const step of cleanupSteps) {
|
|
|
|
const { name, cleanup } = step
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-12-09 05:12:45 -05:00
|
|
|
try {
|
|
|
|
await runAndTrack(`cleanup.${name}`, cleanup)
|
|
|
|
} catch (e) {
|
|
|
|
// keep going with cleanup
|
|
|
|
cleanupErrors.push(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) throw err
|
|
|
|
if (cleanupErrors.length) {
|
|
|
|
if (cleanupErrors.length === 1) throw cleanupErrors[0]
|
|
|
|
throw new SmokeTestFailure('multiple cleanup steps failed', {
|
|
|
|
stats,
|
2021-04-27 03:52:58 -04:00
|
|
|
cleanupErrors,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2020-12-09 05:12:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { runSmokeTests, SmokeTestFailure }
|