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