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