2021-07-13 03:43:59 -04:00
|
|
|
import { exec } from 'child_process'
|
|
|
|
import { copy, ensureDir, readFile, remove } from 'fs-extra'
|
2021-07-23 05:20:00 -04:00
|
|
|
import { basename, join } from 'path'
|
2021-12-17 05:58:15 -05:00
|
|
|
import { isGithubCI, root, wait } from '@shared/core-utils'
|
|
|
|
import { getFileSize } from '@shared/extra-utils'
|
2021-07-16 04:42:24 -04:00
|
|
|
import { HttpStatusCode } from '@shared/models'
|
2021-07-13 03:43:59 -04:00
|
|
|
import { AbstractCommand, OverrideCommandOptions } from '../shared'
|
|
|
|
|
|
|
|
export class ServersCommand extends AbstractCommand {
|
|
|
|
|
|
|
|
static flushTests (internalServerNumber: number) {
|
|
|
|
return new Promise<void>((res, rej) => {
|
|
|
|
const suffix = ` -- ${internalServerNumber}`
|
|
|
|
|
|
|
|
return exec('npm run clean:server:test' + suffix, (err, _stdout, stderr) => {
|
|
|
|
if (err || stderr) return rej(err || new Error(stderr))
|
|
|
|
|
|
|
|
return res()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ping (options: OverrideCommandOptions = {}) {
|
|
|
|
return this.getRequestBody({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path: '/api/v1/ping',
|
|
|
|
implicitToken: false,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-28 09:51:41 -05:00
|
|
|
cleanupTests () {
|
|
|
|
const promises: Promise<any>[] = []
|
|
|
|
|
|
|
|
const saveGithubLogsIfNeeded = async () => {
|
|
|
|
if (!isGithubCI()) return
|
2021-07-13 03:43:59 -04:00
|
|
|
|
|
|
|
await ensureDir('artifacts')
|
|
|
|
|
2021-07-16 03:47:51 -04:00
|
|
|
const origin = this.buildDirectory('logs/peertube.log')
|
2021-07-13 03:43:59 -04:00
|
|
|
const destname = `peertube-${this.server.internalServerNumber}.log`
|
|
|
|
console.log('Saving logs %s.', destname)
|
|
|
|
|
|
|
|
await copy(origin, join('artifacts', destname))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.server.parallel) {
|
2022-02-28 09:51:41 -05:00
|
|
|
const promise = saveGithubLogsIfNeeded()
|
|
|
|
.then(() => ServersCommand.flushTests(this.server.internalServerNumber))
|
|
|
|
|
|
|
|
promises.push(promise)
|
2021-07-13 03:43:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.server.customConfigFile) {
|
2022-02-28 09:51:41 -05:00
|
|
|
promises.push(remove(this.server.customConfigFile))
|
2021-07-13 03:43:59 -04:00
|
|
|
}
|
|
|
|
|
2022-02-28 09:51:41 -05:00
|
|
|
return promises
|
2021-07-13 03:43:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async waitUntilLog (str: string, count = 1, strictCount = true) {
|
2021-08-18 03:14:51 -04:00
|
|
|
const logfile = this.buildDirectory('logs/peertube.log')
|
2021-07-13 03:43:59 -04:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const buf = await readFile(logfile)
|
|
|
|
|
|
|
|
const matches = buf.toString().match(new RegExp(str, 'g'))
|
|
|
|
if (matches && matches.length === count) return
|
|
|
|
if (matches && strictCount === false && matches.length >= count) return
|
|
|
|
|
|
|
|
await wait(1000)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buildDirectory (directory: string) {
|
|
|
|
return join(root(), 'test' + this.server.internalServerNumber, directory)
|
|
|
|
}
|
|
|
|
|
2021-07-22 08:28:03 -04:00
|
|
|
buildWebTorrentFilePath (fileUrl: string) {
|
|
|
|
return this.buildDirectory(join('videos', basename(fileUrl)))
|
|
|
|
}
|
|
|
|
|
|
|
|
buildFragmentedFilePath (videoUUID: string, fileUrl: string) {
|
|
|
|
return this.buildDirectory(join('streaming-playlists', 'hls', videoUUID, basename(fileUrl)))
|
|
|
|
}
|
|
|
|
|
2021-08-18 03:14:51 -04:00
|
|
|
getLogContent () {
|
|
|
|
return readFile(this.buildDirectory('logs/peertube.log'))
|
|
|
|
}
|
|
|
|
|
2021-07-13 03:43:59 -04:00
|
|
|
async getServerFileSize (subPath: string) {
|
2021-07-16 03:04:35 -04:00
|
|
|
const path = this.server.servers.buildDirectory(subPath)
|
2021-07-13 03:43:59 -04:00
|
|
|
|
|
|
|
return getFileSize(path)
|
|
|
|
}
|
|
|
|
}
|