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
34 const promises: Promise<any>[] = []
36 const saveGithubLogsIfNeeded = async () => {
37 if (!isGithubCI()) return
39 await ensureDir('artifacts')
41 const origin = this.buildDirectory('logs/peertube.log')
42 const destname = `peertube-${this.server.internalServerNumber}.log`
43 console.log('Saving logs %s.', destname)
45 await copy(origin, join('artifacts', destname))
47 console.log(this.server.parallel)
49 if (this.server.parallel) {
50 const promise = saveGithubLogsIfNeeded()
51 .then(() => ServersCommand.flushTests(this.server.internalServerNumber))
53 promises.push(promise)
56 if (this.server.customConfigFile) {
57 promises.push(remove(this.server.customConfigFile))
63 async waitUntilLog (str: string, count = 1, strictCount = true) {
64 const logfile = this.buildDirectory('logs/peertube.log')
67 const buf = await readFile(logfile)
69 const matches = buf.toString().match(new RegExp(str, 'g'))
70 if (matches && matches.length === count) return
71 if (matches && strictCount === false && matches.length >= count) return
77 buildDirectory (directory: string) {
78 return join(root(), 'test' + this.server.internalServerNumber, directory)
81 buildWebTorrentFilePath (fileUrl: string) {
82 return this.buildDirectory(join('videos', basename(fileUrl)))
85 buildFragmentedFilePath (videoUUID: string, fileUrl: string) {
86 return this.buildDirectory(join('streaming-playlists', 'hls', videoUUID, basename(fileUrl)))
90 return readFile(this.buildDirectory('logs/peertube.log'))
93 async getServerFileSize (subPath: string) {
94 const path = this.server.servers.buildDirectory(subPath)
96 return getFileSize(path)