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')
9 process
.env
.NODE_ENV
= 'test'
10 const constants
= require('../../initializers/constants')
12 const loginUtils
= require('../utils/login')
13 const podsUtils
= require('../utils/pods')
14 const serversUtils
= require('../utils/servers')
15 const videosUtils
= require('../utils/videos')
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')
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
34 const numberOfPods
= 6
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
42 console
.log('Create weight: %d, remove weight: %d.', createWeight
, removeWeight
)
44 console
.log('Program will flush data on exit.')
46 console
.log('Program will not flush data on exit.')
48 if (displayDiffOnFail
) {
49 console
.log('Program will display diff on failure.')
51 console
.log('Program will not display diff on failure')
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
)
57 console
.log('Run servers...')
58 runServers(numberOfPods
, function (err
, servers
) {
61 process
.on('exit', function () {
62 exitServers(servers
, flushAtExit
)
64 process
.on('SIGINT', goodbye
)
65 process
.on('SIGTERM', goodbye
)
67 console
.log('Servers runned')
71 setInterval(function () {
72 if (checking
=== true) return
74 const rand
= getRandomInt(0, createWeight
+ removeWeight
)
76 if (rand
< createWeight
) {
77 upload(servers
, getRandomNumServer(servers
))
79 remove(servers
, getRandomNumServer(servers
))
83 setInterval(function () {
84 if (checking
=== true) return
86 console
.log('Checking integrity...')
89 setTimeout(function () {
90 checkIntegrity(servers
, function () {
93 }, waitForBeforeIntegrityCheck
)
97 // ----------------------------------------------------------------------------
99 function getRandomInt (min
, max
) {
100 return Math
.floor(Math
.random() * (max
- min
)) + min
103 function getRandomNumServer (servers
) {
104 return getRandomInt(0, servers
.length
)
107 function runServers (numberOfPods
, callback
) {
113 serversUtils
.flushAndRunMultipleServers(numberOfPods
, function (serversRun
) {
118 // Get the access tokens
120 each(servers
, function (server
, callbackEach
) {
121 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
122 if (err
) return callbackEach(err
)
124 server
.accessToken
= accessToken
130 const server
= servers
[1]
131 podsUtils
.makeFriends(server
.url
, server
.accessToken
, next
)
134 const server
= servers
[0]
135 podsUtils
.makeFriends(server
.url
, server
.accessToken
, next
)
138 setTimeout(next
, 1000)
141 const server
= servers
[3]
142 podsUtils
.makeFriends(server
.url
, server
.accessToken
, next
)
145 const server
= servers
[5]
146 podsUtils
.makeFriends(server
.url
, server
.accessToken
, next
)
149 const server
= servers
[4]
150 podsUtils
.makeFriends(server
.url
, server
.accessToken
, next
)
153 setTimeout(next
, 1000)
156 return callback(err
, servers
)
160 function exitServers (servers
, callback
) {
161 if (!callback
) callback = function () {}
163 servers
.forEach(function (server
) {
164 if (server
.app
) process
.kill(-server
.app
.pid
)
167 if (flushAtExit
) serversUtils
.flushTests(callback
)
170 function upload (servers
, numServer
, callback
) {
171 if (!callback
) callback = function () {}
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'
178 console
.log('Upload video to server ' + numServer
)
180 videosUtils
.uploadVideo(servers
[numServer
].url
, servers
[numServer
].accessToken
, name
, description
, tags
, file
, callback
)
183 function remove (servers
, numServer
, callback
) {
184 if (!callback
) callback = function () {}
186 videosUtils
.getVideosList(servers
[numServer
].url
, function (err
, res
) {
189 const videos
= res
.body
.data
190 if (videos
.length
=== 0) return callback()
192 const toRemove
= videos
[getRandomInt(0, videos
.length
)].id
194 console
.log('Removing video from server ' + numServer
)
195 videosUtils
.removeVideo(servers
[numServer
].url
, servers
[numServer
].accessToken
, toRemove
, callback
)
199 function checkIntegrity (servers
, callback
) {
201 each(servers
, function (server
, callback
) {
202 videosUtils
.getAllVideosListBy(server
.url
, function (err
, res
) {
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
212 videos
.push(serverVideos
)
216 for (const video
of videos
) {
217 if (!isEqual(video
, videos
[0])) {
218 console
.error('Integrity not ok!')
220 if (displayDiffOnFail
) {
221 console
.log(differenceWith(videos
[0], video
, isEqual
))
228 console
.log('Integrity ok.')
233 function goodbye () {
234 return process
.exit(-1)