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