]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/server-commands/server/servers-command.ts
f174a2aa02c30146d00e5eee98a4f21968c52371
[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 console.log(this.server.parallel)
48
49 if (this.server.parallel) {
50 const promise = saveGithubLogsIfNeeded()
51 .then(() => ServersCommand.flushTests(this.server.internalServerNumber))
52
53 promises.push(promise)
54 }
55
56 if (this.server.customConfigFile) {
57 promises.push(remove(this.server.customConfigFile))
58 }
59
60 return promises
61 }
62
63 async waitUntilLog (str: string, count = 1, strictCount = true) {
64 const logfile = this.buildDirectory('logs/peertube.log')
65
66 while (true) {
67 const buf = await readFile(logfile)
68
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
72
73 await wait(1000)
74 }
75 }
76
77 buildDirectory (directory: string) {
78 return join(root(), 'test' + this.server.internalServerNumber, directory)
79 }
80
81 buildWebTorrentFilePath (fileUrl: string) {
82 return this.buildDirectory(join('videos', basename(fileUrl)))
83 }
84
85 buildFragmentedFilePath (videoUUID: string, fileUrl: string) {
86 return this.buildDirectory(join('streaming-playlists', 'hls', videoUUID, basename(fileUrl)))
87 }
88
89 getLogContent () {
90 return readFile(this.buildDirectory('logs/peertube.log'))
91 }
92
93 async getServerFileSize (subPath: string) {
94 const path = this.server.servers.buildDirectory(subPath)
95
96 return getFileSize(path)
97 }
98 }