]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/server/servers-command.ts
Shorter server command names
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / server / servers-command.ts
CommitLineData
6c5065a0
C
1import { exec } from 'child_process'
2import { copy, ensureDir, readFile, remove } from 'fs-extra'
3import { join } from 'path'
4import { root } from '@server/helpers/core-utils'
5import { HttpStatusCode } from '@shared/core-utils'
6import { getFileSize } from '@uploadx/core'
7import { isGithubCI, wait } from '../miscs'
8import { AbstractCommand, OverrideCommandOptions } from '../shared'
9
10export class ServersCommand extends AbstractCommand {
11
12 static flushTests (internalServerNumber: number) {
13 return new Promise<void>((res, rej) => {
14 const suffix = ` -- ${internalServerNumber}`
15
16 return exec('npm run clean:server:test' + suffix, (err, _stdout, stderr) => {
17 if (err || stderr) return rej(err || new Error(stderr))
18
19 return res()
20 })
21 })
22 }
23
24 ping (options: OverrideCommandOptions = {}) {
25 return this.getRequestBody({
26 ...options,
27
28 path: '/api/v1/ping',
29 implicitToken: false,
30 defaultExpectedStatus: HttpStatusCode.OK_200
31 })
32 }
33
34 async cleanupTests () {
35 const p: Promise<any>[] = []
36
37 if (isGithubCI()) {
38 await ensureDir('artifacts')
39
89d241a7 40 const origin = this.server.servers.buildDirectory('logs/peertube.log')
6c5065a0
C
41 const destname = `peertube-${this.server.internalServerNumber}.log`
42 console.log('Saving logs %s.', destname)
43
44 await copy(origin, join('artifacts', destname))
45 }
46
47 if (this.server.parallel) {
48 p.push(ServersCommand.flushTests(this.server.internalServerNumber))
49 }
50
51 if (this.server.customConfigFile) {
52 p.push(remove(this.server.customConfigFile))
53 }
54
55 return p
56 }
57
58 async waitUntilLog (str: string, count = 1, strictCount = true) {
89d241a7 59 const logfile = this.server.servers.buildDirectory('logs/peertube.log')
6c5065a0
C
60
61 while (true) {
62 const buf = await readFile(logfile)
63
64 const matches = buf.toString().match(new RegExp(str, 'g'))
65 if (matches && matches.length === count) return
66 if (matches && strictCount === false && matches.length >= count) return
67
68 await wait(1000)
69 }
70 }
71
72 buildDirectory (directory: string) {
73 return join(root(), 'test' + this.server.internalServerNumber, directory)
74 }
75
76 async getServerFileSize (subPath: string) {
89d241a7 77 const path = this.server.servers.buildDirectory(subPath)
6c5065a0
C
78
79 return getFileSize(path)
80 }
81}