3 const each
= require('async/each')
4 const isEqual
= require('lodash/isEqual')
5 const program
= require('commander')
6 const series
= require('async/series')
8 process
.env
.NODE_ENV
= 'test'
9 const constants
= require('../../initializers/constants')
11 const loginUtils
= require('../utils/login')
12 const podsUtils
= require('../utils/pods')
13 const serversUtils
= require('../utils/servers')
14 const videosUtils
= require('../utils/videos')
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')
25 const createWeight
= parseInt(program
.create
) || 5
26 const removeWeight
= parseInt(program
.remove
) || 4
27 const flushAtExit
= program
.flush
|| false
28 const actionInterval
= parseInt(program
.action
) || 500
29 let integrityInterval
= parseInt(program
.integrity
) || 60000
31 const numberOfPods
= 6
32 // Wait requests between pods
33 const requestsMaxPerInterval
= constants
.INTERVAL
/ actionInterval
34 const intervalsToMakeAllRequests
= Math
.ceil(requestsMaxPerInterval
/ constants
.REQUESTS_LIMIT
)
35 const waitForBeforeIntegrityCheck
= (intervalsToMakeAllRequests
* constants
.INTERVAL
) + 1000
37 integrityInterval
+= waitForBeforeIntegrityCheck
39 console
.log('Create weight: %d, remove weight: %d.', createWeight
, removeWeight
)
41 console
.log('Program will flush data on exit.')
43 console
.log('Program will not flush data on exit.')
45 console
.log('Interval in ms for each action: %d.', actionInterval
)
46 console
.log('Interval in ms for each integrity check: %d.', integrityInterval
)
47 console
.log('Will wait %d ms before an integrity check.', waitForBeforeIntegrityCheck
)
49 console
.log('Run servers...')
50 runServers(numberOfPods
, function (err
, servers
) {
53 process
.on('exit', function () {
54 exitServers(servers
, flushAtExit
)
56 process
.on('SIGINT', goodbye
)
57 process
.on('SIGTERM', goodbye
)
59 console
.log('Servers runned')
63 setInterval(function () {
64 if (checking
=== true) return
66 const rand
= getRandomInt(0, createWeight
+ removeWeight
)
68 if (rand
< createWeight
) {
69 upload(servers
, getRandomNumServer(servers
))
71 remove(servers
, getRandomNumServer(servers
))
75 setInterval(function () {
76 console
.log('Checking integrity...')
79 setTimeout(function () {
80 checkIntegrity(servers
, function () {
83 }, waitForBeforeIntegrityCheck
)
87 // ----------------------------------------------------------------------------
89 function getRandomInt (min
, max
) {
90 return Math
.floor(Math
.random() * (max
- min
)) + min
93 function getRandomNumServer (servers
) {
94 return getRandomInt(0, servers
.length
)
97 function runServers (numberOfPods
, callback
) {
103 serversUtils
.flushAndRunMultipleServers(numberOfPods
, function (serversRun
) {
108 // Get the access tokens
110 each(servers
, function (server
, callbackEach
) {
111 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
112 if (err
) return callbackEach(err
)
114 server
.accessToken
= accessToken
120 const server
= servers
[1]
121 podsUtils
.makeFriends(server
.url
, server
.accessToken
, next
)
124 const server
= servers
[0]
125 podsUtils
.makeFriends(server
.url
, server
.accessToken
, next
)
128 setTimeout(next
, 1000)
131 const server
= servers
[3]
132 podsUtils
.makeFriends(server
.url
, server
.accessToken
, next
)
135 const server
= servers
[5]
136 podsUtils
.makeFriends(server
.url
, server
.accessToken
, next
)
139 const server
= servers
[4]
140 podsUtils
.makeFriends(server
.url
, server
.accessToken
, next
)
143 setTimeout(next
, 1000)
146 return callback(err
, servers
)
150 function exitServers (servers
, callback
) {
151 if (!callback
) callback = function () {}
153 servers
.forEach(function (server
) {
154 if (server
.app
) process
.kill(-server
.app
.pid
)
157 if (flushAtExit
) serversUtils
.flushTests(callback
)
160 function upload (servers
, numServer
, callback
) {
161 if (!callback
) callback = function () {}
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'
168 console
.log('Upload video to server ' + numServer
)
170 videosUtils
.uploadVideo(servers
[numServer
].url
, servers
[numServer
].accessToken
, name
, description
, tags
, file
, callback
)
173 function remove (servers
, numServer
, callback
) {
174 if (!callback
) callback = function () {}
176 videosUtils
.getVideosList(servers
[numServer
].url
, function (err
, res
) {
179 const videos
= res
.body
.data
180 if (videos
.length
=== 0) return callback()
182 const toRemove
= videos
[getRandomInt(0, videos
.length
)].id
184 console
.log('Removing video from server ' + numServer
)
185 videosUtils
.removeVideo(servers
[numServer
].url
, servers
[numServer
].accessToken
, toRemove
, callback
)
189 function checkIntegrity (servers
, callback
) {
191 each(servers
, function (server
, callback
) {
192 videosUtils
.getAllVideosListBy(server
.url
, function (err
, res
) {
194 const serverVideos
= res
.body
.data
195 for (const serverVideo
of serverVideos
) {
196 delete serverVideo
.id
197 delete serverVideo
.isLocal
198 delete serverVideo
.thumbnailPath
201 videos
.push(serverVideos
)
205 for (const video
of videos
) {
206 if (!isEqual(video
, videos
[0])) {
207 console
.error('Integrity not ok!')
212 console
.log('Integrity ok.')
217 function goodbye () {
218 return process
.exit(-1)