Add populate database script

pull/821/head
Chocobozzz 2018-07-17 18:17:50 +02:00
parent 3b492bff49
commit b54431049b
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 111 additions and 1 deletions

View File

@ -3,7 +3,7 @@
set -eu set -eu
for i in $(seq 1 6); do for i in $(seq 1 6); do
dropdb "peertube_test$i" dropdb --if-exists "peertube_test$i"
rm -rf "./test$i" rm -rf "./test$i"
rm -f "./config/local-test.json" rm -f "./config/local-test.json"
rm -f "./config/local-test-$i.json" rm -f "./config/local-test-$i.json"

View File

@ -0,0 +1,110 @@
import { VideoRateType } from '../../../shared'
import {
addVideoChannel,
createUser,
flushTests,
getVideosList,
killallServers,
rateVideo,
runServer,
ServerInfo,
setAccessTokensToServers,
uploadVideo
} from '../utils'
import * as Bluebird from 'bluebird'
start()
.catch(err => console.error(err))
// ----------------------------------------------------------------------------
async function start () {
process.on('exit', async () => {
killallServers([ server ])
return
})
process.on('SIGINT', goodbye)
process.on('SIGTERM', goodbye)
await flushTests()
console.log('Flushed tests.')
const server = await runServer(6)
await setAccessTokensToServers([ server ])
console.log('Servers ran.')
// Forever
const fakeTab = Array.from(Array(1000000).keys())
await Bluebird.map(fakeTab, () => {
return Promise.all([
uploadCustom(server),
likeCustom(server),
dislikeCustom(server),
createUserCustom(server),
createCustomChannel(server)
]).catch(err => console.error(err))
}, { concurrency: 5 })
}
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min)) + min
}
function createCustomChannel (server: ServerInfo) {
const videoChannel = {
displayName: Date.now().toString(),
description: Date.now().toString()
}
return addVideoChannel(server.url, server.accessToken, videoChannel)
}
function createUserCustom (server: ServerInfo) {
const username = Date.now().toString() + getRandomInt(0, 100000)
console.log('Creating user %s.', username)
return createUser(server.url, server.accessToken, username, 'coucou')
}
function uploadCustom (server: ServerInfo) {
console.log('Uploading video.')
const videoAttributes = {
name: Date.now() + ' name',
category: 4,
nsfw: false,
licence: 2,
language: 'en',
description: Date.now() + ' description',
tags: [ Date.now().toString().substring(0, 5) + 't1', Date.now().toString().substring(0, 5) + 't2' ],
fixture: 'video_short.mp4'
}
return uploadVideo(server.url, server.accessToken, videoAttributes)
}
function likeCustom (server: ServerInfo) {
return rateCustom(server, 'like')
}
function dislikeCustom (server: ServerInfo) {
return rateCustom(server, 'dislike')
}
async function rateCustom (server: ServerInfo, rating: VideoRateType) {
const res = await getVideosList(server.url)
const videos = res.body.data
if (videos.length === 0) return undefined
const videoToRate = videos[getRandomInt(0, videos.length)]
console.log('Rating (%s) video.', rating)
return rateVideo(server.url, server.accessToken, videoToRate.id, rating)
}
function goodbye () {
return process.exit(-1)
}