2016-07-06 20:01:27 +02:00
|
|
|
'use strict'
|
|
|
|
|
2016-08-20 17:19:27 +02:00
|
|
|
const each = require('async/each')
|
2016-07-06 20:01:27 +02:00
|
|
|
const isEqual = require('lodash/isEqual')
|
2017-01-06 23:24:20 +01:00
|
|
|
const differenceWith = require('lodash/differenceWith')
|
2016-07-18 17:17:52 +02:00
|
|
|
const program = require('commander')
|
|
|
|
const series = require('async/series')
|
2016-07-06 20:01:27 +02:00
|
|
|
|
|
|
|
process.env.NODE_ENV = 'test'
|
|
|
|
const constants = require('../../initializers/constants')
|
|
|
|
|
2016-08-20 17:19:27 +02:00
|
|
|
const loginUtils = require('../utils/login')
|
|
|
|
const podsUtils = require('../utils/pods')
|
|
|
|
const serversUtils = require('../utils/servers')
|
|
|
|
const videosUtils = require('../utils/videos')
|
2017-06-25 17:19:46 +02:00
|
|
|
const requestSchedulersUtils = require('../utils/request-schedulers')
|
2016-07-06 20:01:27 +02:00
|
|
|
|
|
|
|
program
|
|
|
|
.option('-c, --create [weight]', 'Weight for creating videos')
|
|
|
|
.option('-r, --remove [weight]', 'Weight for removing videos')
|
2017-01-11 18:41:28 +01:00
|
|
|
.option('-u, --update [weight]', 'Weight for updating videos')
|
2017-03-19 09:16:33 +01:00
|
|
|
.option('-v, --view [weight]', 'Weight for viewing videos')
|
|
|
|
.option('-l, --like [weight]', 'Weight for liking videos')
|
2017-04-07 12:13:37 +02:00
|
|
|
.option('-s, --dislike [weight]', 'Weight for disliking videos')
|
2016-07-06 20:01:27 +02:00
|
|
|
.option('-p, --pods [n]', 'Number of pods to run (3 or 6)', /^3|6$/, 3)
|
|
|
|
.option('-a, --action [interval]', 'Interval in ms for an action')
|
|
|
|
.option('-i, --integrity [interval]', 'Interval in ms for an integrity check')
|
|
|
|
.option('-f, --flush', 'Flush datas on exit')
|
2017-01-06 23:24:20 +01:00
|
|
|
.option('-d, --difference', 'Display difference if integrity is not okay')
|
2016-07-06 20:01:27 +02:00
|
|
|
.parse(process.argv)
|
|
|
|
|
2017-01-06 23:24:20 +01:00
|
|
|
const createWeight = program.create !== undefined ? parseInt(program.create) : 5
|
|
|
|
const removeWeight = program.remove !== undefined ? parseInt(program.remove) : 4
|
2017-01-11 18:41:28 +01:00
|
|
|
const updateWeight = program.update !== undefined ? parseInt(program.update) : 4
|
2017-03-19 09:16:33 +01:00
|
|
|
const viewWeight = program.view !== undefined ? parseInt(program.view) : 4
|
|
|
|
const likeWeight = program.like !== undefined ? parseInt(program.like) : 4
|
|
|
|
const dislikeWeight = program.dislike !== undefined ? parseInt(program.dislike) : 4
|
2016-07-06 20:01:27 +02:00
|
|
|
const flushAtExit = program.flush || false
|
2017-01-06 23:24:20 +01:00
|
|
|
const actionInterval = program.action !== undefined ? parseInt(program.action) : 500
|
2017-01-12 10:06:03 +01:00
|
|
|
const integrityInterval = program.integrity !== undefined ? parseInt(program.integrity) : 60000
|
2017-01-06 23:24:20 +01:00
|
|
|
const displayDiffOnFail = program.integrity || false
|
2016-07-06 20:01:27 +02:00
|
|
|
|
|
|
|
const numberOfPods = 6
|
2017-01-06 23:24:20 +01:00
|
|
|
|
2017-03-19 09:16:33 +01:00
|
|
|
console.log('Create weight: %d, update weight: %d, remove weight: %d, view weight: %d, like weight: %d, dislike weight: %d.', createWeight, updateWeight, removeWeight, viewWeight, likeWeight, dislikeWeight)
|
2016-07-06 20:01:27 +02:00
|
|
|
if (flushAtExit) {
|
|
|
|
console.log('Program will flush data on exit.')
|
|
|
|
} else {
|
|
|
|
console.log('Program will not flush data on exit.')
|
|
|
|
}
|
2017-01-06 23:24:20 +01:00
|
|
|
if (displayDiffOnFail) {
|
|
|
|
console.log('Program will display diff on failure.')
|
|
|
|
} else {
|
|
|
|
console.log('Program will not display diff on failure')
|
|
|
|
}
|
2016-07-06 20:01:27 +02:00
|
|
|
console.log('Interval in ms for each action: %d.', actionInterval)
|
|
|
|
console.log('Interval in ms for each integrity check: %d.', integrityInterval)
|
|
|
|
|
|
|
|
console.log('Run servers...')
|
|
|
|
runServers(numberOfPods, function (err, servers) {
|
|
|
|
if (err) throw err
|
|
|
|
|
|
|
|
process.on('exit', function () {
|
|
|
|
exitServers(servers, flushAtExit)
|
|
|
|
})
|
|
|
|
process.on('SIGINT', goodbye)
|
|
|
|
process.on('SIGTERM', goodbye)
|
|
|
|
|
|
|
|
console.log('Servers runned')
|
2017-03-19 09:16:33 +01:00
|
|
|
initializeRequestsPerServer(servers)
|
2016-07-06 20:01:27 +02:00
|
|
|
|
|
|
|
let checking = false
|
|
|
|
|
|
|
|
setInterval(function () {
|
|
|
|
if (checking === true) return
|
|
|
|
|
2017-03-19 09:16:33 +01:00
|
|
|
const rand = getRandomInt(0, createWeight + updateWeight + removeWeight + viewWeight + likeWeight + dislikeWeight)
|
|
|
|
|
|
|
|
const numServer = getRandomNumServer(servers)
|
|
|
|
servers[numServer].requestsNumber++
|
2016-07-06 20:01:27 +02:00
|
|
|
|
|
|
|
if (rand < createWeight) {
|
2017-03-19 09:16:33 +01:00
|
|
|
upload(servers, numServer)
|
2017-01-11 18:41:28 +01:00
|
|
|
} else if (rand < createWeight + updateWeight) {
|
2017-03-19 09:16:33 +01:00
|
|
|
update(servers, numServer)
|
|
|
|
} else if (rand < createWeight + updateWeight + removeWeight) {
|
|
|
|
remove(servers, numServer)
|
|
|
|
} else if (rand < createWeight + updateWeight + removeWeight + viewWeight) {
|
|
|
|
view(servers, numServer)
|
|
|
|
} else if (rand < createWeight + updateWeight + removeWeight + viewWeight + likeWeight) {
|
|
|
|
like(servers, numServer)
|
2016-07-06 20:01:27 +02:00
|
|
|
} else {
|
2017-03-19 09:16:33 +01:00
|
|
|
dislike(servers, numServer)
|
2016-07-06 20:01:27 +02:00
|
|
|
}
|
|
|
|
}, actionInterval)
|
|
|
|
|
2017-03-19 09:16:33 +01:00
|
|
|
// The function will check the consistency between servers (should have the same videos with same attributes...)
|
2016-07-06 20:01:27 +02:00
|
|
|
setInterval(function () {
|
2017-01-06 23:24:20 +01:00
|
|
|
if (checking === true) return
|
|
|
|
|
2016-07-06 20:01:27 +02:00
|
|
|
console.log('Checking integrity...')
|
|
|
|
checking = true
|
|
|
|
|
2017-03-19 09:16:33 +01:00
|
|
|
const waitingInterval = setInterval(function () {
|
2017-04-16 14:25:37 +02:00
|
|
|
isThereAwaitingRequests(servers, function (err, res) {
|
|
|
|
if (err) throw err
|
|
|
|
|
2017-03-19 09:16:33 +01:00
|
|
|
if (res === true) {
|
|
|
|
console.log('A server has awaiting requests, waiting...')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
checkIntegrity(servers, function () {
|
|
|
|
initializeRequestsPerServer(servers)
|
|
|
|
checking = false
|
|
|
|
clearInterval(waitingInterval)
|
|
|
|
})
|
2016-07-06 20:01:27 +02:00
|
|
|
})
|
2017-03-19 09:16:33 +01:00
|
|
|
}, constants.REQUESTS_INTERVAL)
|
2016-07-06 20:01:27 +02:00
|
|
|
}, integrityInterval)
|
|
|
|
})
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2017-03-19 09:16:33 +01:00
|
|
|
function initializeRequestsPerServer (servers) {
|
|
|
|
servers.forEach(function (server) {
|
|
|
|
server.requestsNumber = 0
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-06 20:01:27 +02:00
|
|
|
function getRandomInt (min, max) {
|
|
|
|
return Math.floor(Math.random() * (max - min)) + min
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRandomNumServer (servers) {
|
|
|
|
return getRandomInt(0, servers.length)
|
|
|
|
}
|
|
|
|
|
|
|
|
function runServers (numberOfPods, callback) {
|
|
|
|
let servers = null
|
|
|
|
|
2016-07-18 17:17:52 +02:00
|
|
|
series([
|
2016-07-06 20:01:27 +02:00
|
|
|
// Run servers
|
|
|
|
function (next) {
|
2016-08-20 17:19:27 +02:00
|
|
|
serversUtils.flushAndRunMultipleServers(numberOfPods, function (serversRun) {
|
2016-07-06 20:01:27 +02:00
|
|
|
servers = serversRun
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
// Get the access tokens
|
|
|
|
function (next) {
|
2016-07-18 17:17:52 +02:00
|
|
|
each(servers, function (server, callbackEach) {
|
2016-08-20 17:19:27 +02:00
|
|
|
loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
|
2016-07-06 20:01:27 +02:00
|
|
|
if (err) return callbackEach(err)
|
|
|
|
|
|
|
|
server.accessToken = accessToken
|
|
|
|
callbackEach()
|
|
|
|
})
|
|
|
|
}, next)
|
|
|
|
},
|
|
|
|
function (next) {
|
|
|
|
const server = servers[1]
|
2016-08-20 17:19:27 +02:00
|
|
|
podsUtils.makeFriends(server.url, server.accessToken, next)
|
2016-07-06 20:01:27 +02:00
|
|
|
},
|
|
|
|
function (next) {
|
|
|
|
const server = servers[0]
|
2016-08-20 17:19:27 +02:00
|
|
|
podsUtils.makeFriends(server.url, server.accessToken, next)
|
2016-07-06 20:01:27 +02:00
|
|
|
},
|
|
|
|
function (next) {
|
|
|
|
setTimeout(next, 1000)
|
|
|
|
},
|
|
|
|
function (next) {
|
|
|
|
const server = servers[3]
|
2016-08-20 17:19:27 +02:00
|
|
|
podsUtils.makeFriends(server.url, server.accessToken, next)
|
2016-07-06 20:01:27 +02:00
|
|
|
},
|
|
|
|
function (next) {
|
|
|
|
const server = servers[5]
|
2016-08-20 17:19:27 +02:00
|
|
|
podsUtils.makeFriends(server.url, server.accessToken, next)
|
2016-07-06 20:01:27 +02:00
|
|
|
},
|
|
|
|
function (next) {
|
|
|
|
const server = servers[4]
|
2016-08-20 17:19:27 +02:00
|
|
|
podsUtils.makeFriends(server.url, server.accessToken, next)
|
2016-07-06 20:01:27 +02:00
|
|
|
},
|
|
|
|
function (next) {
|
|
|
|
setTimeout(next, 1000)
|
|
|
|
}
|
|
|
|
], function (err) {
|
|
|
|
return callback(err, servers)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function exitServers (servers, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
|
|
|
|
|
|
|
servers.forEach(function (server) {
|
|
|
|
if (server.app) process.kill(-server.app.pid)
|
|
|
|
})
|
|
|
|
|
2016-08-20 17:19:27 +02:00
|
|
|
if (flushAtExit) serversUtils.flushTests(callback)
|
2016-07-06 20:01:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function upload (servers, numServer, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
|
|
|
|
2017-03-19 09:16:33 +01:00
|
|
|
console.log('Uploading video to server ' + numServer)
|
2016-07-06 20:01:27 +02:00
|
|
|
|
2017-03-26 18:39:01 +02:00
|
|
|
const videoAttributes = {
|
|
|
|
name: Date.now() + ' name',
|
|
|
|
category: 4,
|
2017-03-28 21:19:46 +02:00
|
|
|
nsfw: false,
|
2017-03-27 20:53:11 +02:00
|
|
|
licence: 2,
|
2017-04-07 12:13:37 +02:00
|
|
|
language: 1,
|
2017-03-26 18:39:01 +02:00
|
|
|
description: Date.now() + ' description',
|
|
|
|
tags: [ Date.now().toString().substring(0, 5) + 't1', Date.now().toString().substring(0, 5) + 't2' ],
|
|
|
|
fixture: 'video_short1.webm'
|
|
|
|
}
|
|
|
|
videosUtils.uploadVideo(servers[numServer].url, servers[numServer].accessToken, videoAttributes, callback)
|
2016-07-06 20:01:27 +02:00
|
|
|
}
|
|
|
|
|
2017-01-11 18:41:28 +01:00
|
|
|
function update (servers, numServer, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
|
|
|
|
|
|
|
videosUtils.getVideosList(servers[numServer].url, function (err, res) {
|
|
|
|
if (err) throw err
|
|
|
|
|
|
|
|
const videos = res.body.data.filter(function (video) { return video.isLocal })
|
|
|
|
if (videos.length === 0) return callback()
|
|
|
|
|
|
|
|
const toUpdate = videos[getRandomInt(0, videos.length)].id
|
2017-03-26 18:39:01 +02:00
|
|
|
const attributes = {
|
|
|
|
name: Date.now() + ' name',
|
|
|
|
description: Date.now() + ' description',
|
|
|
|
tags: [ Date.now().toString().substring(0, 5) + 't1', Date.now().toString().substring(0, 5) + 't2' ]
|
|
|
|
}
|
2017-01-11 18:41:28 +01:00
|
|
|
|
|
|
|
console.log('Updating video of server ' + numServer)
|
|
|
|
|
2017-03-26 18:39:01 +02:00
|
|
|
videosUtils.updateVideo(servers[numServer].url, servers[numServer].accessToken, toUpdate, attributes, callback)
|
2017-01-11 18:41:28 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-06 20:01:27 +02:00
|
|
|
function remove (servers, numServer, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
|
|
|
|
2016-08-20 17:19:27 +02:00
|
|
|
videosUtils.getVideosList(servers[numServer].url, function (err, res) {
|
2016-07-06 20:01:27 +02:00
|
|
|
if (err) throw err
|
|
|
|
|
|
|
|
const videos = res.body.data
|
|
|
|
if (videos.length === 0) return callback()
|
|
|
|
|
|
|
|
const toRemove = videos[getRandomInt(0, videos.length)].id
|
|
|
|
|
|
|
|
console.log('Removing video from server ' + numServer)
|
2016-08-20 17:19:27 +02:00
|
|
|
videosUtils.removeVideo(servers[numServer].url, servers[numServer].accessToken, toRemove, callback)
|
2016-07-06 20:01:27 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-19 09:16:33 +01:00
|
|
|
function view (servers, numServer, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
|
|
|
|
|
|
|
videosUtils.getVideosList(servers[numServer].url, function (err, res) {
|
|
|
|
if (err) throw err
|
|
|
|
|
|
|
|
const videos = res.body.data
|
|
|
|
if (videos.length === 0) return callback()
|
|
|
|
|
|
|
|
const toView = videos[getRandomInt(0, videos.length)].id
|
|
|
|
|
|
|
|
console.log('Viewing video from server ' + numServer)
|
|
|
|
videosUtils.getVideo(servers[numServer].url, toView, callback)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function like (servers, numServer, callback) {
|
|
|
|
rate(servers, numServer, 'like', callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function dislike (servers, numServer, callback) {
|
|
|
|
rate(servers, numServer, 'dislike', callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function rate (servers, numServer, rating, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
|
|
|
|
|
|
|
videosUtils.getVideosList(servers[numServer].url, function (err, res) {
|
|
|
|
if (err) throw err
|
|
|
|
|
|
|
|
const videos = res.body.data
|
|
|
|
if (videos.length === 0) return callback()
|
|
|
|
|
|
|
|
const toRate = videos[getRandomInt(0, videos.length)].id
|
|
|
|
|
|
|
|
console.log('Rating (%s) video from server %d', rating, numServer)
|
|
|
|
videosUtils.getVideo(servers[numServer].url, toRate, callback)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-06 20:01:27 +02:00
|
|
|
function checkIntegrity (servers, callback) {
|
|
|
|
const videos = []
|
2016-07-18 17:17:52 +02:00
|
|
|
each(servers, function (server, callback) {
|
2016-08-20 17:19:27 +02:00
|
|
|
videosUtils.getAllVideosListBy(server.url, function (err, res) {
|
2016-07-06 20:01:27 +02:00
|
|
|
if (err) throw err
|
|
|
|
const serverVideos = res.body.data
|
|
|
|
for (const serverVideo of serverVideos) {
|
|
|
|
delete serverVideo.id
|
|
|
|
delete serverVideo.isLocal
|
|
|
|
delete serverVideo.thumbnailPath
|
2017-01-06 23:24:20 +01:00
|
|
|
delete serverVideo.updatedAt
|
2017-03-19 09:16:33 +01:00
|
|
|
delete serverVideo.views
|
2016-07-06 20:01:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
videos.push(serverVideos)
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
}, function () {
|
2017-03-19 09:16:33 +01:00
|
|
|
let i = 0
|
|
|
|
|
2016-07-06 20:01:27 +02:00
|
|
|
for (const video of videos) {
|
|
|
|
if (!isEqual(video, videos[0])) {
|
2017-03-19 09:16:33 +01:00
|
|
|
console.error('Integrity not ok with server %d!', i + 1)
|
2016-12-25 12:05:47 +01:00
|
|
|
|
2017-01-06 23:24:20 +01:00
|
|
|
if (displayDiffOnFail) {
|
|
|
|
console.log(differenceWith(videos[0], video, isEqual))
|
2017-03-19 09:16:33 +01:00
|
|
|
console.log(differenceWith(video, videos[0], isEqual))
|
2017-01-06 23:24:20 +01:00
|
|
|
}
|
|
|
|
|
2016-07-06 20:01:27 +02:00
|
|
|
process.exit(-1)
|
|
|
|
}
|
2017-03-19 09:16:33 +01:00
|
|
|
|
|
|
|
i++
|
2016-07-06 20:01:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log('Integrity ok.')
|
|
|
|
return callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function goodbye () {
|
|
|
|
return process.exit(-1)
|
|
|
|
}
|
2017-03-19 09:16:33 +01:00
|
|
|
|
|
|
|
function isThereAwaitingRequests (servers, callback) {
|
|
|
|
let noRequests = true
|
|
|
|
|
|
|
|
// Check is each server has awaiting requestq
|
|
|
|
each(servers, function (server, callbackEach) {
|
2017-06-25 17:19:46 +02:00
|
|
|
requestSchedulersUtils.getRequestsStats(server, server.accessToken, function (err, res) {
|
2017-03-19 09:16:33 +01:00
|
|
|
if (err) throw err
|
|
|
|
|
|
|
|
const stats = res.body
|
|
|
|
|
|
|
|
if (
|
|
|
|
stats.requestScheduler.totalRequests !== 0 ||
|
|
|
|
stats.requestVideoEventScheduler.totalRequests !== 0 ||
|
|
|
|
stats.requestVideoQaduScheduler.totalRequests !== 0
|
|
|
|
) {
|
|
|
|
noRequests = false
|
|
|
|
}
|
|
|
|
|
|
|
|
callbackEach()
|
|
|
|
})
|
|
|
|
}, function (err) {
|
|
|
|
if (err) throw err
|
|
|
|
|
2017-04-16 14:25:37 +02:00
|
|
|
return callback(null, noRequests === false)
|
2017-03-19 09:16:33 +01:00
|
|
|
})
|
|
|
|
}
|