]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/server-commands/server/servers-command.ts
Prevent invalid end watch section warnings
[github/Chocobozzz/PeerTube.git] / shared / server-commands / server / servers-command.ts
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'
8
9 export class ServersCommand extends AbstractCommand {
10
11 static flushTests (internalServerNumber: number) {
12 return new Promise<void>((res, rej) => {
13 const suffix = ` -- ${internalServerNumber}`
14
15 return exec('npm run clean:server:test' + suffix, (err, _stdout, stderr) => {
16 if (err || stderr) return rej(err || new Error(stderr))
17
18 return res()
19 })
20 })
21 }
22
23 ping (options: OverrideCommandOptions = {}) {
24 return this.getRequestBody({
25 ...options,
26
27 path: '/api/v1/ping',
28 implicitToken: false,
29 defaultExpectedStatus: HttpStatusCode.OK_200
30 })
31 }
32
33 cleanupTests () {
34 const promises: Promise<any>[] = []
35
36 const saveGithubLogsIfNeeded = async () => {
37 if (!isGithubCI()) return
38
39 await ensureDir('artifacts')
40
41 const origin = this.buildDirectory('logs/peertube.log')
42 const destname = `peertube-${this.server.internalServerNumber}.log`
43 console.log('Saving logs %s.', destname)
44
45 await copy(origin, join('artifacts', destname))
46 }
47
48 if (this.server.parallel) {
49 const promise = saveGithubLogsIfNeeded()
50 .then(() => ServersCommand.flushTests(this.server.internalServerNumber))
51
52 promises.push(promise)
53 }
54
55 if (this.server.customConfigFile) {
56 promises.push(remove(this.server.customConfigFile))
57 }
58
59 return promises
60 }
61
62 async waitUntilLog (str: string, count = 1, strictCount = true) {
63 const logfile = this.buildDirectory('logs/peertube.log')
64
65 while (true) {
66 const buf = await readFile(logfile)
67
68 const matches = buf.toString().match(new RegExp(str, 'g'))
69 if (matches && matches.length === count) return
70 if (matches && strictCount === false && matches.length >= count) return
71
72 await wait(1000)
73 }
74 }
75
76 buildDirectory (directory: string) {
77 return join(root(), 'test' + this.server.internalServerNumber, directory)
78 }
79
80 buildWebTorrentFilePath (fileUrl: string) {
81 return this.buildDirectory(join('videos', basename(fileUrl)))
82 }
83
84 buildFragmentedFilePath (videoUUID: string, fileUrl: string) {
85 return this.buildDirectory(join('streaming-playlists', 'hls', videoUUID, basename(fileUrl)))
86 }
87
88 getLogContent () {
89 return readFile(this.buildDirectory('logs/peertube.log'))
90 }
91
92 async getServerFileSize (subPath: string) {
93 const path = this.server.servers.buildDirectory(subPath)
94
95 return getFileSize(path)
96 }
97 }