servers-command.ts 2.21 KB
Newer Older
Chocobozzz's avatar
Chocobozzz committed
1
2
3
4
import { exec } from 'child_process'
import { copy, ensureDir, readFile, remove } from 'fs-extra'
import { join } from 'path'
import { root } from '@server/helpers/core-utils'
Chocobozzz's avatar
Chocobozzz committed
5
import { HttpStatusCode } from '@shared/models'
Chocobozzz's avatar
Chocobozzz committed
6
import { getFileSize, isGithubCI, wait } from '../miscs'
Chocobozzz's avatar
Chocobozzz committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { AbstractCommand, OverrideCommandOptions } from '../shared'

export class ServersCommand extends AbstractCommand {

  static flushTests (internalServerNumber: number) {
    return new Promise<void>((res, rej) => {
      const suffix = ` -- ${internalServerNumber}`

      return exec('npm run clean:server:test' + suffix, (err, _stdout, stderr) => {
        if (err || stderr) return rej(err || new Error(stderr))

        return res()
      })
    })
  }

  ping (options: OverrideCommandOptions = {}) {
    return this.getRequestBody({
      ...options,

      path: '/api/v1/ping',
      implicitToken: false,
      defaultExpectedStatus: HttpStatusCode.OK_200
    })
  }

  async cleanupTests () {
    const p: Promise<any>[] = []

    if (isGithubCI()) {
      await ensureDir('artifacts')

39
      const origin = this.buildDirectory('logs/peertube.log')
Chocobozzz's avatar
Chocobozzz committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
      const destname = `peertube-${this.server.internalServerNumber}.log`
      console.log('Saving logs %s.', destname)

      await copy(origin, join('artifacts', destname))
    }

    if (this.server.parallel) {
      p.push(ServersCommand.flushTests(this.server.internalServerNumber))
    }

    if (this.server.customConfigFile) {
      p.push(remove(this.server.customConfigFile))
    }

    return p
  }

  async waitUntilLog (str: string, count = 1, strictCount = true) {
Chocobozzz's avatar
Chocobozzz committed
58
    const logfile = this.server.servers.buildDirectory('logs/peertube.log')
Chocobozzz's avatar
Chocobozzz committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

    while (true) {
      const buf = await readFile(logfile)

      const matches = buf.toString().match(new RegExp(str, 'g'))
      if (matches && matches.length === count) return
      if (matches && strictCount === false && matches.length >= count) return

      await wait(1000)
    }
  }

  buildDirectory (directory: string) {
    return join(root(), 'test' + this.server.internalServerNumber, directory)
  }

  async getServerFileSize (subPath: string) {
Chocobozzz's avatar
Chocobozzz committed
76
    const path = this.server.servers.buildDirectory(subPath)
Chocobozzz's avatar
Chocobozzz committed
77
78
79
80

    return getFileSize(path)
  }
}