1 import { exec } from 'child_process'
2 import { copy, ensureDir, readFile, remove } from 'fs-extra'
3 import { basename, join } from 'path'
4 import { isGithubCI, root, wait } from '@shared/core-utils'
5 import { getFileSize } from '@shared/extra-utils'
6 import { HttpStatusCode } from '@shared/models'
7 import { AbstractCommand, OverrideCommandOptions } from '../shared'
9 export class ServersCommand extends AbstractCommand {
11 static flushTests (internalServerNumber: number) {
12 return new Promise<void>((res, rej) => {
13 const suffix = ` -- ${internalServerNumber}`
15 return exec('npm run clean:server:test' + suffix, (err, _stdout, stderr) => {
16 if (err || stderr) return rej(err || new Error(stderr))
23 ping (options: OverrideCommandOptions = {}) {
24 return this.getRequestBody({
29 defaultExpectedStatus: HttpStatusCode.OK_200
33 async cleanupTests () {
34 const p: Promise<any>[] = []
37 await ensureDir('artifacts')
39 const origin = this.buildDirectory('logs/peertube.log')
40 const destname = `peertube-${this.server.internalServerNumber}.log`
41 console.log('Saving logs %s.', destname)
43 await copy(origin, join('artifacts', destname))
46 if (this.server.parallel) {
47 p.push(ServersCommand.flushTests(this.server.internalServerNumber))
50 if (this.server.customConfigFile) {
51 p.push(remove(this.server.customConfigFile))
57 async waitUntilLog (str: string, count = 1, strictCount = true) {
58 const logfile = this.buildDirectory('logs/peertube.log')
61 const buf = await readFile(logfile)
63 const matches = buf.toString().match(new RegExp(str, 'g'))
64 if (matches && matches.length === count) return
65 if (matches && strictCount === false && matches.length >= count) return
71 buildDirectory (directory: string) {
72 return join(root(), 'test' + this.server.internalServerNumber, directory)
75 buildWebTorrentFilePath (fileUrl: string) {
76 return this.buildDirectory(join('videos', basename(fileUrl)))
79 buildFragmentedFilePath (videoUUID: string, fileUrl: string) {
80 return this.buildDirectory(join('streaming-playlists', 'hls', videoUUID, basename(fileUrl)))
84 return readFile(this.buildDirectory('logs/peertube.log'))
87 async getServerFileSize (subPath: string) {
88 const path = this.server.servers.buildDirectory(subPath)
90 return getFileSize(path)