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('-u, --update [weight]', 'Weight for updating videos')
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')
25 .option('-d, --difference', 'Display difference if integrity is not okay')
28 const createWeight
= program
.create
!== undefined ? parseInt(program
.create
) : 5
29 const removeWeight
= program
.remove
!== undefined ? parseInt(program
.remove
) : 4
30 const updateWeight
= program
.update
!== undefined ? parseInt(program
.update
) : 4
31 const flushAtExit
= program
.flush
|| false
32 const actionInterval
= program
.action
!== undefined ? parseInt(program
.action
) : 500
33 let integrityInterval
= program
.integrity
!== undefined ? parseInt(program
.integrity
) : 60000
34 const displayDiffOnFail
= program
.integrity
|| false
36 const numberOfPods
= 6
38 // Wait requests between pods
39 const baseRequestInterval
= integrityInterval
< constants
.REQUESTS_INTERVAL
? integrityInterval : constants
.REQUESTS_INTERVAL
40 const requestsMaxPerInterval
= baseRequestInterval
/ actionInterval
41 const intervalsToMakeAllRequests
= Math
.ceil(requestsMaxPerInterval
/ constants
.REQUESTS_LIMIT_PER_POD
)
42 const waitForBeforeIntegrityCheck
= (intervalsToMakeAllRequests
* constants
.REQUESTS_INTERVAL
) + 1000
44 console
.log('Create weight: %d, update weight: %d, remove weight: %d.', createWeight
, updateWeight
, removeWeight
)
46 console
.log('Program will flush data on exit.')
48 console
.log('Program will not flush data on exit.')
50 if (displayDiffOnFail
) {
51 console
.log('Program will display diff on failure.')
53 console
.log('Program will not display diff on failure')
55 console
.log('Interval in ms for each action: %d.', actionInterval
)
56 console
.log('Interval in ms for each integrity check: %d.', integrityInterval
)
57 console
.log('Will wait %d ms before an integrity check.', waitForBeforeIntegrityCheck
)
59 console
.log('Run servers...')
60 runServers(numberOfPods
, function (err
, servers
) {
63 process
.on('exit', function () {
64 exitServers(servers
, flushAtExit
)
66 process
.on('SIGINT', goodbye
)
67 process
.on('SIGTERM', goodbye
)
69 console
.log('Servers runned')
73 setInterval(function () {
74 if (checking
=== true) return
76 const rand
= getRandomInt(0, createWeight
+ updateWeight
+ removeWeight
)
78 if (rand
< createWeight
) {
79 upload(servers
, getRandomNumServer(servers
))
80 } else if (rand
< createWeight
+ updateWeight
) {
81 update(servers
, getRandomNumServer(servers
))
83 remove(servers
, getRandomNumServer(servers
))
87 setInterval(function () {
88 if (checking
=== true) return
90 console
.log('Checking integrity...')
93 setTimeout(function () {
94 checkIntegrity(servers
, function () {
97 }, waitForBeforeIntegrityCheck
)
101 // ----------------------------------------------------------------------------
103 function getRandomInt (min
, max
) {
104 return Math
.floor(Math
.random() * (max
- min
)) + min
107 function getRandomNumServer (servers
) {
108 return getRandomInt(0, servers
.length
)
111 function runServers (numberOfPods
, callback
) {
117 serversUtils
.flushAndRunMultipleServers(numberOfPods
, function (serversRun
) {
122 // Get the access tokens
124 each(servers
, function (server
, callbackEach
) {
125 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
126 if (err
) return callbackEach(err
)
128 server
.accessToken
= accessToken
134 const server
= servers
[1]
135 podsUtils
.makeFriends(server
.url
, server
.accessToken
, next
)
138 const server
= servers
[0]
139 podsUtils
.makeFriends(server
.url
, server
.accessToken
, next
)
142 setTimeout(next
, 1000)
145 const server
= servers
[3]
146 podsUtils
.makeFriends(server
.url
, server
.accessToken
, next
)
149 const server
= servers
[5]
150 podsUtils
.makeFriends(server
.url
, server
.accessToken
, next
)
153 const server
= servers
[4]
154 podsUtils
.makeFriends(server
.url
, server
.accessToken
, next
)
157 setTimeout(next
, 1000)
160 return callback(err
, servers
)
164 function exitServers (servers
, callback
) {
165 if (!callback
) callback = function () {}
167 servers
.forEach(function (server
) {
168 if (server
.app
) process
.kill(-server
.app
.pid
)
171 if (flushAtExit
) serversUtils
.flushTests(callback
)
174 function upload (servers
, numServer
, callback
) {
175 if (!callback
) callback = function () {}
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' ]
180 const file
= 'video_short1.webm'
182 console
.log('Upload video to server ' + numServer
)
184 videosUtils
.uploadVideo(servers
[numServer
].url
, servers
[numServer
].accessToken
, name
, description
, tags
, file
, callback
)
187 function update (servers
, numServer
, callback
) {
188 if (!callback
) callback = function () {}
190 videosUtils
.getVideosList(servers
[numServer
].url
, function (err
, res
) {
193 const videos
= res
.body
.data
.filter(function (video
) { return video
.isLocal
})
194 if (videos
.length
=== 0) return callback()
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' ]
201 console
.log('Updating video of server ' + numServer
)
203 videosUtils
.updateVideo(servers
[numServer
].url
, servers
[numServer
].accessToken
, toUpdate
, name
, description
, tags
, callback
)
207 function remove (servers
, numServer
, callback
) {
208 if (!callback
) callback = function () {}
210 videosUtils
.getVideosList(servers
[numServer
].url
, function (err
, res
) {
213 const videos
= res
.body
.data
214 if (videos
.length
=== 0) return callback()
216 const toRemove
= videos
[getRandomInt(0, videos
.length
)].id
218 console
.log('Removing video from server ' + numServer
)
219 videosUtils
.removeVideo(servers
[numServer
].url
, servers
[numServer
].accessToken
, toRemove
, callback
)
223 function checkIntegrity (servers
, callback
) {
225 each(servers
, function (server
, callback
) {
226 videosUtils
.getAllVideosListBy(server
.url
, function (err
, res
) {
228 const serverVideos
= res
.body
.data
229 for (const serverVideo
of serverVideos
) {
230 delete serverVideo
.id
231 delete serverVideo
.isLocal
232 delete serverVideo
.thumbnailPath
233 delete serverVideo
.updatedAt
236 videos
.push(serverVideos
)
240 for (const video
of videos
) {
241 if (!isEqual(video
, videos
[0])) {
242 console
.error('Integrity not ok!')
244 if (displayDiffOnFail
) {
245 console
.log(differenceWith(videos
[0], video
, isEqual
))
252 console
.log('Integrity ok.')
257 function goodbye () {
258 return process
.exit(-1)