aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/real-world
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-07-17 18:17:50 +0200
committerChocobozzz <me@florianbigard.com>2018-07-17 18:17:50 +0200
commitb54431049b7b50fc18788bbe600de6adf3f4eb2c (patch)
tree94a3ab78d1e7f18649925ce6439ef0f4c0004bc3 /server/tests/real-world
parent3b492bff49bbab71e826cc4fe93db8c6a74a3391 (diff)
downloadPeerTube-b54431049b7b50fc18788bbe600de6adf3f4eb2c.tar.gz
PeerTube-b54431049b7b50fc18788bbe600de6adf3f4eb2c.tar.zst
PeerTube-b54431049b7b50fc18788bbe600de6adf3f4eb2c.zip
Add populate database script
Diffstat (limited to 'server/tests/real-world')
-rw-r--r--server/tests/real-world/populate-database.ts110
1 files changed, 110 insertions, 0 deletions
diff --git a/server/tests/real-world/populate-database.ts b/server/tests/real-world/populate-database.ts
new file mode 100644
index 000000000..5f93d09db
--- /dev/null
+++ b/server/tests/real-world/populate-database.ts
@@ -0,0 +1,110 @@
1import { VideoRateType } from '../../../shared'
2import {
3 addVideoChannel,
4 createUser,
5 flushTests,
6 getVideosList,
7 killallServers,
8 rateVideo,
9 runServer,
10 ServerInfo,
11 setAccessTokensToServers,
12 uploadVideo
13} from '../utils'
14import * as Bluebird from 'bluebird'
15
16start()
17 .catch(err => console.error(err))
18
19// ----------------------------------------------------------------------------
20
21async 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
51function getRandomInt (min, max) {
52 return Math.floor(Math.random() * (max - min)) + min
53}
54
55function 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
64function 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
71function 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
88function likeCustom (server: ServerInfo) {
89 return rateCustom(server, 'like')
90}
91
92function dislikeCustom (server: ServerInfo) {
93 return rateCustom(server, 'dislike')
94}
95
96async 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
108function goodbye () {
109 return process.exit(-1)
110}