]>
Commit | Line | Data |
---|---|---|
6c5065a0 C |
1 | import { exec } from 'child_process' |
2 | import { copy, ensureDir, readFile, remove } from 'fs-extra' | |
764b1a14 | 3 | import { basename, join } from 'path' |
c55e3d72 C |
4 | import { isGithubCI, root, wait } from '@shared/core-utils' |
5 | import { getFileSize } from '@shared/extra-utils' | |
c0e8b12e | 6 | import { HttpStatusCode } from '@shared/models' |
6c5065a0 C |
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 | ||
cba79775 C |
33 | cleanupTests () { |
34 | const promises: Promise<any>[] = [] | |
35 | ||
36 | const saveGithubLogsIfNeeded = async () => { | |
37 | if (!isGithubCI()) return | |
6c5065a0 | 38 | |
6c5065a0 C |
39 | await ensureDir('artifacts') |
40 | ||
254d3579 | 41 | const origin = this.buildDirectory('logs/peertube.log') |
6c5065a0 C |
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) { | |
cba79775 C |
49 | const promise = saveGithubLogsIfNeeded() |
50 | .then(() => ServersCommand.flushTests(this.server.internalServerNumber)) | |
51 | ||
52 | promises.push(promise) | |
6c5065a0 C |
53 | } |
54 | ||
55 | if (this.server.customConfigFile) { | |
cba79775 | 56 | promises.push(remove(this.server.customConfigFile)) |
6c5065a0 C |
57 | } |
58 | ||
cba79775 | 59 | return promises |
6c5065a0 C |
60 | } |
61 | ||
62 | async waitUntilLog (str: string, count = 1, strictCount = true) { | |
1f6125be | 63 | const logfile = this.buildDirectory('logs/peertube.log') |
6c5065a0 C |
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 | ||
83903cb6 C |
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 | ||
1f6125be C |
88 | getLogContent () { |
89 | return readFile(this.buildDirectory('logs/peertube.log')) | |
90 | } | |
91 | ||
6c5065a0 | 92 | async getServerFileSize (subPath: string) { |
89d241a7 | 93 | const path = this.server.servers.buildDirectory(subPath) |
6c5065a0 C |
94 | |
95 | return getFileSize(path) | |
96 | } | |
97 | } |