diff options
Diffstat (limited to 'shared/extra-utils/server/servers-command.ts')
-rw-r--r-- | shared/extra-utils/server/servers-command.ts | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/shared/extra-utils/server/servers-command.ts b/shared/extra-utils/server/servers-command.ts new file mode 100644 index 000000000..9ef68fede --- /dev/null +++ b/shared/extra-utils/server/servers-command.ts | |||
@@ -0,0 +1,81 @@ | |||
1 | import { exec } from 'child_process' | ||
2 | import { copy, ensureDir, readFile, remove } from 'fs-extra' | ||
3 | import { join } from 'path' | ||
4 | import { root } from '@server/helpers/core-utils' | ||
5 | import { HttpStatusCode } from '@shared/core-utils' | ||
6 | import { getFileSize } from '@uploadx/core' | ||
7 | import { isGithubCI, wait } from '../miscs' | ||
8 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
9 | |||
10 | export 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 | |||
40 | const origin = this.server.serversCommand.buildDirectory('logs/peertube.log') | ||
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) { | ||
59 | const logfile = this.server.serversCommand.buildDirectory('logs/peertube.log') | ||
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) { | ||
77 | const path = this.server.serversCommand.buildDirectory(subPath) | ||
78 | |||
79 | return getFileSize(path) | ||
80 | } | ||
81 | } | ||