diff options
author | Chocobozzz <me@florianbigard.com> | 2021-12-17 09:29:23 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-12-17 09:29:23 +0100 |
commit | bf54587a3e2ad9c2c186828f2a5682b91ee2cc00 (patch) | |
tree | 54b40aaf01bae210632473285c3c7571d51e4f89 /shared/server-commands/server/servers-command.ts | |
parent | 6b5f72beda96d8b7e4d6329c4001827334de27dd (diff) | |
download | PeerTube-bf54587a3e2ad9c2c186828f2a5682b91ee2cc00.tar.gz PeerTube-bf54587a3e2ad9c2c186828f2a5682b91ee2cc00.tar.zst PeerTube-bf54587a3e2ad9c2c186828f2a5682b91ee2cc00.zip |
shared/ typescript types dir server-commands
Diffstat (limited to 'shared/server-commands/server/servers-command.ts')
-rw-r--r-- | shared/server-commands/server/servers-command.ts | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/shared/server-commands/server/servers-command.ts b/shared/server-commands/server/servers-command.ts new file mode 100644 index 000000000..47420c95f --- /dev/null +++ b/shared/server-commands/server/servers-command.ts | |||
@@ -0,0 +1,92 @@ | |||
1 | import { exec } from 'child_process' | ||
2 | import { copy, ensureDir, readFile, remove } from 'fs-extra' | ||
3 | import { basename, join } from 'path' | ||
4 | import { root } from '@shared/core-utils' | ||
5 | import { HttpStatusCode } from '@shared/models' | ||
6 | import { getFileSize, isGithubCI, wait } from '../miscs' | ||
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 | async cleanupTests () { | ||
34 | const p: Promise<any>[] = [] | ||
35 | |||
36 | if (isGithubCI()) { | ||
37 | await ensureDir('artifacts') | ||
38 | |||
39 | const origin = this.buildDirectory('logs/peertube.log') | ||
40 | const destname = `peertube-${this.server.internalServerNumber}.log` | ||
41 | console.log('Saving logs %s.', destname) | ||
42 | |||
43 | await copy(origin, join('artifacts', destname)) | ||
44 | } | ||
45 | |||
46 | if (this.server.parallel) { | ||
47 | p.push(ServersCommand.flushTests(this.server.internalServerNumber)) | ||
48 | } | ||
49 | |||
50 | if (this.server.customConfigFile) { | ||
51 | p.push(remove(this.server.customConfigFile)) | ||
52 | } | ||
53 | |||
54 | return p | ||
55 | } | ||
56 | |||
57 | async waitUntilLog (str: string, count = 1, strictCount = true) { | ||
58 | const logfile = this.buildDirectory('logs/peertube.log') | ||
59 | |||
60 | while (true) { | ||
61 | const buf = await readFile(logfile) | ||
62 | |||
63 | const matches = buf.toString().match(new RegExp(str, 'g')) | ||
64 | if (matches && matches.length === count) return | ||
65 | if (matches && strictCount === false && matches.length >= count) return | ||
66 | |||
67 | await wait(1000) | ||
68 | } | ||
69 | } | ||
70 | |||
71 | buildDirectory (directory: string) { | ||
72 | return join(root(), 'test' + this.server.internalServerNumber, directory) | ||
73 | } | ||
74 | |||
75 | buildWebTorrentFilePath (fileUrl: string) { | ||
76 | return this.buildDirectory(join('videos', basename(fileUrl))) | ||
77 | } | ||
78 | |||
79 | buildFragmentedFilePath (videoUUID: string, fileUrl: string) { | ||
80 | return this.buildDirectory(join('streaming-playlists', 'hls', videoUUID, basename(fileUrl))) | ||
81 | } | ||
82 | |||
83 | getLogContent () { | ||
84 | return readFile(this.buildDirectory('logs/peertube.log')) | ||
85 | } | ||
86 | |||
87 | async getServerFileSize (subPath: string) { | ||
88 | const path = this.server.servers.buildDirectory(subPath) | ||
89 | |||
90 | return getFileSize(path) | ||
91 | } | ||
92 | } | ||