]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/real-world/real-world.js
Server: fix update remote video infohash
[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')
bb0b243c 5const differenceWith = require('lodash/differenceWith')
1a42c9e2
C
6const program = require('commander')
7const series = require('async/series')
8f68c31a
C
8
9process.env.NODE_ENV = 'test'
10const constants = require('../../initializers/constants')
11
46132744
C
12const loginUtils = require('../utils/login')
13const podsUtils = require('../utils/pods')
14const serversUtils = require('../utils/servers')
15const videosUtils = require('../utils/videos')
8f68c31a
C
16
17program
18 .option('-c, --create [weight]', 'Weight for creating videos')
19 .option('-r, --remove [weight]', 'Weight for removing videos')
f2cdb866 20 .option('-u, --update [weight]', 'Weight for updating videos')
8f68c31a
C
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')
bb0b243c 25 .option('-d, --difference', 'Display difference if integrity is not okay')
8f68c31a
C
26 .parse(process.argv)
27
bb0b243c
C
28const createWeight = program.create !== undefined ? parseInt(program.create) : 5
29const removeWeight = program.remove !== undefined ? parseInt(program.remove) : 4
f2cdb866 30const updateWeight = program.update !== undefined ? parseInt(program.update) : 4
8f68c31a 31const flushAtExit = program.flush || false
bb0b243c
C
32const actionInterval = program.action !== undefined ? parseInt(program.action) : 500
33let integrityInterval = program.integrity !== undefined ? parseInt(program.integrity) : 60000
34const displayDiffOnFail = program.integrity || false
8f68c31a
C
35
36const numberOfPods = 6
bb0b243c 37
8f68c31a 38// Wait requests between pods
bb0b243c
C
39const baseRequestInterval = integrityInterval < constants.REQUESTS_INTERVAL ? integrityInterval : constants.REQUESTS_INTERVAL
40const requestsMaxPerInterval = baseRequestInterval / actionInterval
7f4e7c36 41const intervalsToMakeAllRequests = Math.ceil(requestsMaxPerInterval / constants.REQUESTS_LIMIT_PER_POD)
56ac84d0 42const waitForBeforeIntegrityCheck = (intervalsToMakeAllRequests * constants.REQUESTS_INTERVAL) + 1000
8f68c31a 43
f2cdb866 44console.log('Create weight: %d, update weight: %d, remove weight: %d.', createWeight, updateWeight, removeWeight)
8f68c31a
C
45if (flushAtExit) {
46 console.log('Program will flush data on exit.')
47} else {
48 console.log('Program will not flush data on exit.')
49}
bb0b243c
C
50if (displayDiffOnFail) {
51 console.log('Program will display diff on failure.')
52} else {
53 console.log('Program will not display diff on failure')
54}
8f68c31a
C
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
f2cdb866 76 const rand = getRandomInt(0, createWeight + updateWeight + removeWeight)
8f68c31a
C
77
78 if (rand < createWeight) {
79 upload(servers, getRandomNumServer(servers))
f2cdb866
C
80 } else if (rand < createWeight + updateWeight) {
81 update(servers, getRandomNumServer(servers))
8f68c31a
C
82 } else {
83 remove(servers, getRandomNumServer(servers))
84 }
85 }, actionInterval)
86
87 setInterval(function () {
bb0b243c
C
88 if (checking === true) return
89
8f68c31a
C
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
1a42c9e2 114 series([
8f68c31a
C
115 // Run servers
116 function (next) {
46132744 117 serversUtils.flushAndRunMultipleServers(numberOfPods, function (serversRun) {
8f68c31a
C
118 servers = serversRun
119 next()
120 })
121 },
122 // Get the access tokens
123 function (next) {
1a42c9e2 124 each(servers, function (server, callbackEach) {
46132744 125 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
8f68c31a
C
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]
46132744 135 podsUtils.makeFriends(server.url, server.accessToken, next)
8f68c31a
C
136 },
137 function (next) {
138 const server = servers[0]
46132744 139 podsUtils.makeFriends(server.url, server.accessToken, next)
8f68c31a
C
140 },
141 function (next) {
142 setTimeout(next, 1000)
143 },
144 function (next) {
145 const server = servers[3]
46132744 146 podsUtils.makeFriends(server.url, server.accessToken, next)
8f68c31a
C
147 },
148 function (next) {
149 const server = servers[5]
46132744 150 podsUtils.makeFriends(server.url, server.accessToken, next)
8f68c31a
C
151 },
152 function (next) {
153 const server = servers[4]
46132744 154 podsUtils.makeFriends(server.url, server.accessToken, next)
8f68c31a
C
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
46132744 171 if (flushAtExit) serversUtils.flushTests(callback)
8f68c31a
C
172}
173
174function upload (servers, numServer, callback) {
175 if (!callback) callback = function () {}
176
56ac84d0
C
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' ]
8f68c31a
C
180 const file = 'video_short1.webm'
181
182 console.log('Upload video to server ' + numServer)
183
46132744 184 videosUtils.uploadVideo(servers[numServer].url, servers[numServer].accessToken, name, description, tags, file, callback)
8f68c31a
C
185}
186
f2cdb866
C
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
8f68c31a
C
207function remove (servers, numServer, callback) {
208 if (!callback) callback = function () {}
209
46132744 210 videosUtils.getVideosList(servers[numServer].url, function (err, res) {
8f68c31a
C
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)
46132744 219 videosUtils.removeVideo(servers[numServer].url, servers[numServer].accessToken, toRemove, callback)
8f68c31a
C
220 })
221}
222
223function checkIntegrity (servers, callback) {
224 const videos = []
1a42c9e2 225 each(servers, function (server, callback) {
46132744 226 videosUtils.getAllVideosListBy(server.url, function (err, res) {
8f68c31a
C
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
bb0b243c 233 delete serverVideo.updatedAt
8f68c31a
C
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!')
56ac84d0 243
bb0b243c
C
244 if (displayDiffOnFail) {
245 console.log(differenceWith(videos[0], video, isEqual))
246 }
247
8f68c31a
C
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}