]>
Commit | Line | Data |
---|---|---|
8f68c31a C |
1 | 'use strict' |
2 | ||
46132744 | 3 | const each = require('async/each') |
8f68c31a | 4 | const isEqual = require('lodash/isEqual') |
bb0b243c | 5 | const differenceWith = require('lodash/differenceWith') |
1a42c9e2 C |
6 | const program = require('commander') |
7 | const series = require('async/series') | |
8f68c31a C |
8 | |
9 | process.env.NODE_ENV = 'test' | |
10 | const constants = require('../../initializers/constants') | |
11 | ||
46132744 C |
12 | const loginUtils = require('../utils/login') |
13 | const podsUtils = require('../utils/pods') | |
14 | const serversUtils = require('../utils/servers') | |
15 | const videosUtils = require('../utils/videos') | |
8f68c31a C |
16 | |
17 | program | |
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') | |
bb0b243c | 24 | .option('-d, --difference', 'Display difference if integrity is not okay') |
8f68c31a C |
25 | .parse(process.argv) |
26 | ||
bb0b243c C |
27 | const createWeight = program.create !== undefined ? parseInt(program.create) : 5 |
28 | const removeWeight = program.remove !== undefined ? parseInt(program.remove) : 4 | |
8f68c31a | 29 | const flushAtExit = program.flush || false |
bb0b243c C |
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 | |
8f68c31a C |
33 | |
34 | const numberOfPods = 6 | |
bb0b243c | 35 | |
8f68c31a | 36 | // Wait requests between pods |
bb0b243c C |
37 | const baseRequestInterval = integrityInterval < constants.REQUESTS_INTERVAL ? integrityInterval : constants.REQUESTS_INTERVAL |
38 | const requestsMaxPerInterval = baseRequestInterval / actionInterval | |
8f68c31a | 39 | const intervalsToMakeAllRequests = Math.ceil(requestsMaxPerInterval / constants.REQUESTS_LIMIT) |
56ac84d0 | 40 | const waitForBeforeIntegrityCheck = (intervalsToMakeAllRequests * constants.REQUESTS_INTERVAL) + 1000 |
8f68c31a | 41 | |
8f68c31a C |
42 | console.log('Create weight: %d, remove weight: %d.', createWeight, removeWeight) |
43 | if (flushAtExit) { | |
44 | console.log('Program will flush data on exit.') | |
45 | } else { | |
46 | console.log('Program will not flush data on exit.') | |
47 | } | |
bb0b243c C |
48 | if (displayDiffOnFail) { |
49 | console.log('Program will display diff on failure.') | |
50 | } else { | |
51 | console.log('Program will not display diff on failure') | |
52 | } | |
8f68c31a C |
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) | |
56 | ||
57 | console.log('Run servers...') | |
58 | runServers(numberOfPods, function (err, servers) { | |
59 | if (err) throw err | |
60 | ||
61 | process.on('exit', function () { | |
62 | exitServers(servers, flushAtExit) | |
63 | }) | |
64 | process.on('SIGINT', goodbye) | |
65 | process.on('SIGTERM', goodbye) | |
66 | ||
67 | console.log('Servers runned') | |
68 | ||
69 | let checking = false | |
70 | ||
71 | setInterval(function () { | |
72 | if (checking === true) return | |
73 | ||
74 | const rand = getRandomInt(0, createWeight + removeWeight) | |
75 | ||
76 | if (rand < createWeight) { | |
77 | upload(servers, getRandomNumServer(servers)) | |
78 | } else { | |
79 | remove(servers, getRandomNumServer(servers)) | |
80 | } | |
81 | }, actionInterval) | |
82 | ||
83 | setInterval(function () { | |
bb0b243c C |
84 | if (checking === true) return |
85 | ||
8f68c31a C |
86 | console.log('Checking integrity...') |
87 | checking = true | |
88 | ||
89 | setTimeout(function () { | |
90 | checkIntegrity(servers, function () { | |
91 | checking = false | |
92 | }) | |
93 | }, waitForBeforeIntegrityCheck) | |
94 | }, integrityInterval) | |
95 | }) | |
96 | ||
97 | // ---------------------------------------------------------------------------- | |
98 | ||
99 | function getRandomInt (min, max) { | |
100 | return Math.floor(Math.random() * (max - min)) + min | |
101 | } | |
102 | ||
103 | function getRandomNumServer (servers) { | |
104 | return getRandomInt(0, servers.length) | |
105 | } | |
106 | ||
107 | function runServers (numberOfPods, callback) { | |
108 | let servers = null | |
109 | ||
1a42c9e2 | 110 | series([ |
8f68c31a C |
111 | // Run servers |
112 | function (next) { | |
46132744 | 113 | serversUtils.flushAndRunMultipleServers(numberOfPods, function (serversRun) { |
8f68c31a C |
114 | servers = serversRun |
115 | next() | |
116 | }) | |
117 | }, | |
118 | // Get the access tokens | |
119 | function (next) { | |
1a42c9e2 | 120 | each(servers, function (server, callbackEach) { |
46132744 | 121 | loginUtils.loginAndGetAccessToken(server, function (err, accessToken) { |
8f68c31a C |
122 | if (err) return callbackEach(err) |
123 | ||
124 | server.accessToken = accessToken | |
125 | callbackEach() | |
126 | }) | |
127 | }, next) | |
128 | }, | |
129 | function (next) { | |
130 | const server = servers[1] | |
46132744 | 131 | podsUtils.makeFriends(server.url, server.accessToken, next) |
8f68c31a C |
132 | }, |
133 | function (next) { | |
134 | const server = servers[0] | |
46132744 | 135 | podsUtils.makeFriends(server.url, server.accessToken, next) |
8f68c31a C |
136 | }, |
137 | function (next) { | |
138 | setTimeout(next, 1000) | |
139 | }, | |
140 | function (next) { | |
141 | const server = servers[3] | |
46132744 | 142 | podsUtils.makeFriends(server.url, server.accessToken, next) |
8f68c31a C |
143 | }, |
144 | function (next) { | |
145 | const server = servers[5] | |
46132744 | 146 | podsUtils.makeFriends(server.url, server.accessToken, next) |
8f68c31a C |
147 | }, |
148 | function (next) { | |
149 | const server = servers[4] | |
46132744 | 150 | podsUtils.makeFriends(server.url, server.accessToken, next) |
8f68c31a C |
151 | }, |
152 | function (next) { | |
153 | setTimeout(next, 1000) | |
154 | } | |
155 | ], function (err) { | |
156 | return callback(err, servers) | |
157 | }) | |
158 | } | |
159 | ||
160 | function exitServers (servers, callback) { | |
161 | if (!callback) callback = function () {} | |
162 | ||
163 | servers.forEach(function (server) { | |
164 | if (server.app) process.kill(-server.app.pid) | |
165 | }) | |
166 | ||
46132744 | 167 | if (flushAtExit) serversUtils.flushTests(callback) |
8f68c31a C |
168 | } |
169 | ||
170 | function upload (servers, numServer, callback) { | |
171 | if (!callback) callback = function () {} | |
172 | ||
56ac84d0 C |
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' ] | |
8f68c31a C |
176 | const file = 'video_short1.webm' |
177 | ||
178 | console.log('Upload video to server ' + numServer) | |
179 | ||
46132744 | 180 | videosUtils.uploadVideo(servers[numServer].url, servers[numServer].accessToken, name, description, tags, file, callback) |
8f68c31a C |
181 | } |
182 | ||
183 | function remove (servers, numServer, callback) { | |
184 | if (!callback) callback = function () {} | |
185 | ||
46132744 | 186 | videosUtils.getVideosList(servers[numServer].url, function (err, res) { |
8f68c31a C |
187 | if (err) throw err |
188 | ||
189 | const videos = res.body.data | |
190 | if (videos.length === 0) return callback() | |
191 | ||
192 | const toRemove = videos[getRandomInt(0, videos.length)].id | |
193 | ||
194 | console.log('Removing video from server ' + numServer) | |
46132744 | 195 | videosUtils.removeVideo(servers[numServer].url, servers[numServer].accessToken, toRemove, callback) |
8f68c31a C |
196 | }) |
197 | } | |
198 | ||
199 | function checkIntegrity (servers, callback) { | |
200 | const videos = [] | |
1a42c9e2 | 201 | each(servers, function (server, callback) { |
46132744 | 202 | videosUtils.getAllVideosListBy(server.url, function (err, res) { |
8f68c31a C |
203 | if (err) throw err |
204 | const serverVideos = res.body.data | |
205 | for (const serverVideo of serverVideos) { | |
206 | delete serverVideo.id | |
207 | delete serverVideo.isLocal | |
208 | delete serverVideo.thumbnailPath | |
bb0b243c | 209 | delete serverVideo.updatedAt |
8f68c31a C |
210 | } |
211 | ||
212 | videos.push(serverVideos) | |
213 | callback() | |
214 | }) | |
215 | }, function () { | |
216 | for (const video of videos) { | |
217 | if (!isEqual(video, videos[0])) { | |
218 | console.error('Integrity not ok!') | |
56ac84d0 | 219 | |
bb0b243c C |
220 | if (displayDiffOnFail) { |
221 | console.log(differenceWith(videos[0], video, isEqual)) | |
222 | } | |
223 | ||
8f68c31a C |
224 | process.exit(-1) |
225 | } | |
226 | } | |
227 | ||
228 | console.log('Integrity ok.') | |
229 | return callback() | |
230 | }) | |
231 | } | |
232 | ||
233 | function goodbye () { | |
234 | return process.exit(-1) | |
235 | } |