]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/real-world/populate-database.ts
Add populate database script
[github/Chocobozzz/PeerTube.git] / server / tests / real-world / populate-database.ts
1 import { VideoRateType } from '../../../shared'
2 import {
3 addVideoChannel,
4 createUser,
5 flushTests,
6 getVideosList,
7 killallServers,
8 rateVideo,
9 runServer,
10 ServerInfo,
11 setAccessTokensToServers,
12 uploadVideo
13 } from '../utils'
14 import * as Bluebird from 'bluebird'
15
16 start()
17 .catch(err => console.error(err))
18
19 // ----------------------------------------------------------------------------
20
21 async function start () {
22 process.on('exit', async () => {
23 killallServers([ server ])
24 return
25 })
26 process.on('SIGINT', goodbye)
27 process.on('SIGTERM', goodbye)
28
29 await flushTests()
30
31 console.log('Flushed tests.')
32
33 const server = await runServer(6)
34 await setAccessTokensToServers([ server ])
35
36 console.log('Servers ran.')
37
38 // Forever
39 const fakeTab = Array.from(Array(1000000).keys())
40 await Bluebird.map(fakeTab, () => {
41 return Promise.all([
42 uploadCustom(server),
43 likeCustom(server),
44 dislikeCustom(server),
45 createUserCustom(server),
46 createCustomChannel(server)
47 ]).catch(err => console.error(err))
48 }, { concurrency: 5 })
49 }
50
51 function getRandomInt (min, max) {
52 return Math.floor(Math.random() * (max - min)) + min
53 }
54
55 function createCustomChannel (server: ServerInfo) {
56 const videoChannel = {
57 displayName: Date.now().toString(),
58 description: Date.now().toString()
59 }
60
61 return addVideoChannel(server.url, server.accessToken, videoChannel)
62 }
63
64 function createUserCustom (server: ServerInfo) {
65 const username = Date.now().toString() + getRandomInt(0, 100000)
66 console.log('Creating user %s.', username)
67
68 return createUser(server.url, server.accessToken, username, 'coucou')
69 }
70
71 function uploadCustom (server: ServerInfo) {
72 console.log('Uploading video.')
73
74 const videoAttributes = {
75 name: Date.now() + ' name',
76 category: 4,
77 nsfw: false,
78 licence: 2,
79 language: 'en',
80 description: Date.now() + ' description',
81 tags: [ Date.now().toString().substring(0, 5) + 't1', Date.now().toString().substring(0, 5) + 't2' ],
82 fixture: 'video_short.mp4'
83 }
84
85 return uploadVideo(server.url, server.accessToken, videoAttributes)
86 }
87
88 function likeCustom (server: ServerInfo) {
89 return rateCustom(server, 'like')
90 }
91
92 function dislikeCustom (server: ServerInfo) {
93 return rateCustom(server, 'dislike')
94 }
95
96 async function rateCustom (server: ServerInfo, rating: VideoRateType) {
97 const res = await getVideosList(server.url)
98
99 const videos = res.body.data
100 if (videos.length === 0) return undefined
101
102 const videoToRate = videos[getRandomInt(0, videos.length)]
103
104 console.log('Rating (%s) video.', rating)
105 return rateVideo(server.url, server.accessToken, videoToRate.id, rating)
106 }
107
108 function goodbye () {
109 return process.exit(-1)
110 }