]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/real-world/real-world.js
Server: create requests utils module
[github/Chocobozzz/PeerTube.git] / server / tests / real-world / real-world.js
1 'use strict'
2
3 const each = require('each')
4 const isEqual = require('lodash/isEqual')
5 const program = require('commander')
6 const series = require('async/series')
7
8 process.env.NODE_ENV = 'test'
9 const constants = require('../../initializers/constants')
10
11 const utils = require('../api/utils')
12
13 program
14 .option('-c, --create [weight]', 'Weight for creating videos')
15 .option('-r, --remove [weight]', 'Weight for removing videos')
16 .option('-p, --pods [n]', 'Number of pods to run (3 or 6)', /^3|6$/, 3)
17 .option('-a, --action [interval]', 'Interval in ms for an action')
18 .option('-i, --integrity [interval]', 'Interval in ms for an integrity check')
19 .option('-f, --flush', 'Flush datas on exit')
20 .parse(process.argv)
21
22 const createWeight = parseInt(program.create) || 5
23 const removeWeight = parseInt(program.remove) || 4
24 const flushAtExit = program.flush || false
25 const actionInterval = parseInt(program.action) || 500
26 let integrityInterval = parseInt(program.integrity) || 60000
27
28 const numberOfPods = 6
29 // Wait requests between pods
30 const requestsMaxPerInterval = constants.INTERVAL / actionInterval
31 const intervalsToMakeAllRequests = Math.ceil(requestsMaxPerInterval / constants.REQUESTS_LIMIT)
32 const waitForBeforeIntegrityCheck = (intervalsToMakeAllRequests * constants.INTERVAL) + 1000
33
34 integrityInterval += waitForBeforeIntegrityCheck
35
36 console.log('Create weight: %d, remove weight: %d.', createWeight, removeWeight)
37 if (flushAtExit) {
38 console.log('Program will flush data on exit.')
39 } else {
40 console.log('Program will not flush data on exit.')
41 }
42 console.log('Interval in ms for each action: %d.', actionInterval)
43 console.log('Interval in ms for each integrity check: %d.', integrityInterval)
44 console.log('Will wait %d ms before an integrity check.', waitForBeforeIntegrityCheck)
45
46 console.log('Run servers...')
47 runServers(numberOfPods, function (err, servers) {
48 if (err) throw err
49
50 process.on('exit', function () {
51 exitServers(servers, flushAtExit)
52 })
53 process.on('SIGINT', goodbye)
54 process.on('SIGTERM', goodbye)
55
56 console.log('Servers runned')
57
58 let checking = false
59
60 setInterval(function () {
61 if (checking === true) return
62
63 const rand = getRandomInt(0, createWeight + removeWeight)
64
65 if (rand < createWeight) {
66 upload(servers, getRandomNumServer(servers))
67 } else {
68 remove(servers, getRandomNumServer(servers))
69 }
70 }, actionInterval)
71
72 setInterval(function () {
73 console.log('Checking integrity...')
74 checking = true
75
76 setTimeout(function () {
77 checkIntegrity(servers, function () {
78 checking = false
79 })
80 }, waitForBeforeIntegrityCheck)
81 }, integrityInterval)
82 })
83
84 // ----------------------------------------------------------------------------
85
86 function getRandomInt (min, max) {
87 return Math.floor(Math.random() * (max - min)) + min
88 }
89
90 function getRandomNumServer (servers) {
91 return getRandomInt(0, servers.length)
92 }
93
94 function runServers (numberOfPods, callback) {
95 let servers = null
96
97 series([
98 // Run servers
99 function (next) {
100 utils.flushAndRunMultipleServers(numberOfPods, function (serversRun) {
101 servers = serversRun
102 next()
103 })
104 },
105 // Get the access tokens
106 function (next) {
107 each(servers, function (server, callbackEach) {
108 utils.loginAndGetAccessToken(server, function (err, accessToken) {
109 if (err) return callbackEach(err)
110
111 server.accessToken = accessToken
112 callbackEach()
113 })
114 }, next)
115 },
116 function (next) {
117 const server = servers[1]
118 utils.makeFriends(server.url, server.accessToken, next)
119 },
120 function (next) {
121 const server = servers[0]
122 utils.makeFriends(server.url, server.accessToken, next)
123 },
124 function (next) {
125 setTimeout(next, 1000)
126 },
127 function (next) {
128 const server = servers[3]
129 utils.makeFriends(server.url, server.accessToken, next)
130 },
131 function (next) {
132 const server = servers[5]
133 utils.makeFriends(server.url, server.accessToken, next)
134 },
135 function (next) {
136 const server = servers[4]
137 utils.makeFriends(server.url, server.accessToken, next)
138 },
139 function (next) {
140 setTimeout(next, 1000)
141 }
142 ], function (err) {
143 return callback(err, servers)
144 })
145 }
146
147 function exitServers (servers, callback) {
148 if (!callback) callback = function () {}
149
150 servers.forEach(function (server) {
151 if (server.app) process.kill(-server.app.pid)
152 })
153
154 if (flushAtExit) utils.flushTests(callback)
155 }
156
157 function upload (servers, numServer, callback) {
158 if (!callback) callback = function () {}
159
160 const name = 'my super name for pod 1'
161 const description = 'my super description for pod 1'
162 const tags = [ 'tag1p1', 'tag2p1' ]
163 const file = 'video_short1.webm'
164
165 console.log('Upload video to server ' + numServer)
166
167 utils.uploadVideo(servers[numServer].url, servers[numServer].accessToken, name, description, tags, file, callback)
168 }
169
170 function remove (servers, numServer, callback) {
171 if (!callback) callback = function () {}
172
173 utils.getVideosList(servers[numServer].url, function (err, res) {
174 if (err) throw err
175
176 const videos = res.body.data
177 if (videos.length === 0) return callback()
178
179 const toRemove = videos[getRandomInt(0, videos.length)].id
180
181 console.log('Removing video from server ' + numServer)
182 utils.removeVideo(servers[numServer].url, servers[numServer].accessToken, toRemove, callback)
183 })
184 }
185
186 function checkIntegrity (servers, callback) {
187 const videos = []
188 each(servers, function (server, callback) {
189 utils.getAllVideosListBy(server.url, function (err, res) {
190 if (err) throw err
191 const serverVideos = res.body.data
192 for (const serverVideo of serverVideos) {
193 delete serverVideo.id
194 delete serverVideo.isLocal
195 delete serverVideo.thumbnailPath
196 }
197
198 videos.push(serverVideos)
199 callback()
200 })
201 }, function () {
202 for (const video of videos) {
203 if (!isEqual(video, videos[0])) {
204 console.error('Integrity not ok!')
205 process.exit(-1)
206 }
207 }
208
209 console.log('Integrity ok.')
210 return callback()
211 })
212 }
213
214 function goodbye () {
215 return process.exit(-1)
216 }