]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/server/servers-command.ts
Generate random uuid for video files
[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'
83903cb6 4import { basename } from 'path/posix'
6c5065a0 5import { root } from '@server/helpers/core-utils'
c0e8b12e 6import { HttpStatusCode } from '@shared/models'
7a397c7f 7import { getFileSize, isGithubCI, wait } from '../miscs'
6c5065a0
C
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
254d3579 40 const origin = this.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
83903cb6
C
76 buildWebTorrentFilePath (fileUrl: string) {
77 return this.buildDirectory(join('videos', basename(fileUrl)))
78 }
79
80 buildFragmentedFilePath (videoUUID: string, fileUrl: string) {
81 return this.buildDirectory(join('streaming-playlists', 'hls', videoUUID, basename(fileUrl)))
82 }
83
6c5065a0 84 async getServerFileSize (subPath: string) {
89d241a7 85 const path = this.server.servers.buildDirectory(subPath)
6c5065a0
C
86
87 return getFileSize(path)
88 }
89}