1
0
Fork 0

Tests refractoring

This commit is contained in:
Chocobozzz 2016-01-24 16:08:09 +01:00
parent 45239549bf
commit ee66c5930e
6 changed files with 414 additions and 340 deletions

View file

@ -1,6 +1,7 @@
;(function () { ;(function () {
'use strict' 'use strict'
var async = require('async')
var chai = require('chai') var chai = require('chai')
var expect = chai.expect var expect = chai.expect
var request = require('supertest') var request = require('supertest')
@ -11,18 +12,6 @@
var app = null var app = null
var url = '' var url = ''
before(function (done) {
this.timeout(20000)
utils.flushTests(function () {
utils.runServer(1, function (app1, url1) {
app = app1
url = url1
done()
})
})
})
function makePostRequest (path, fields, attach, done, fail) { function makePostRequest (path, fields, attach, done, fail) {
var status_code = 400 var status_code = 400
if (fail !== undefined && fail === false) status_code = 200 if (fail !== undefined && fail === false) status_code = 200
@ -50,6 +39,25 @@
.expect(status_code, done) .expect(status_code, done)
} }
// ---------------------------------------------------------------
before(function (done) {
this.timeout(20000)
async.series([
function (next) {
utils.flushTests(next)
},
function (next) {
utils.runServer(1, function (app1, url1) {
app = app1
url = url1
next()
})
}
], done)
})
describe('Of the pods API', function () { describe('Of the pods API', function () {
var path = '/api/v1/pods/' var path = '/api/v1/pods/'
@ -284,9 +292,7 @@
// Keep the logs if the test failed // Keep the logs if the test failed
if (this.ok) { if (this.ok) {
utils.flushTests(function () { utils.flushTests(done)
done()
})
} else { } else {
done() done()
} }

View file

@ -35,39 +35,39 @@
return utils.getVideosList(urls[pod_number - 1], callback) return utils.getVideosList(urls[pod_number - 1], callback)
} }
// ---------------------------------------------------------------
before(function (done) { before(function (done) {
this.timeout(30000) this.timeout(30000)
utils.runMultipleServers(6, function (apps_run, urls_run) { utils.flushAndRunMultipleServers(6, function (apps_run, urls_run) {
apps = apps_run apps = apps_run
urls = urls_run urls = urls_run
done() done()
}) })
}) })
after(function (done) {
apps.forEach(function (app) {
process.kill(-app.pid)
})
if (this.ok) {
utils.flushTests(function () {
done()
})
} else {
done()
}
})
it('Should make friends with two pod each in a different group', function (done) { it('Should make friends with two pod each in a different group', function (done) {
this.timeout(20000) this.timeout(20000)
async.series([
// Pod 3 makes friend with the first one // Pod 3 makes friend with the first one
makeFriends(3, function () { function (next) {
makeFriends(3, next)
},
// Pod 4 makes friend with the second one // Pod 4 makes friend with the second one
makeFriends(4, function () { function (next) {
makeFriends(4, next)
},
// Now if the fifth wants to make friends with the third et the first // Now if the fifth wants to make friends with the third et the first
makeFriends(5, function () { function (next) {
setTimeout(function () { makeFriends(5, next)
},
function (next) {
setTimeout(next, 11000)
}],
function (err) {
if (err) throw err
// It should have 0 friends // It should have 0 friends
getFriendsList(5, function (err, res) { getFriendsList(5, function (err, res) {
if (err) throw err if (err) throw err
@ -76,56 +76,98 @@
done() done()
}) })
}, 11000) }
}) )
})
})
}) })
it('Should quit all friends', function (done) { it('Should quit all friends', function (done) {
this.timeout(10000) this.timeout(10000)
quitFriends(1, function () {
quitFriends(2, function () { async.series([
function (next) {
quitFriends(1, next)
},
function (next) {
quitFriends(2, next)
}],
function (err) {
if (err) throw err
async.each([ 1, 2, 3, 4, 5, 6 ], function (i, callback) { async.each([ 1, 2, 3, 4, 5, 6 ], function (i, callback) {
getFriendsList(i, function (err, res) { getFriendsList(i, function (err, res) {
if (err) throw err if (err) throw err
expect(res.body.length).to.equal(0) expect(res.body.length).to.equal(0)
callback() callback()
}) })
}, function () { }, done)
done() }
}) )
})
})
}) })
it('Should make friends with the pods 1, 2, 3', function (done) { it('Should make friends with the pods 1, 2, 3', function (done) {
this.timeout(150000) this.timeout(150000)
// Pods 1, 2, 3 and 4 become friends (yes this is beautiful) async.series([
makeFriends(2, function () { // Pods 1, 2, 3 and 4 become friends
makeFriends(1, function () { function (next) {
makeFriends(4, function () { makeFriends(2, next)
// Kill the server 4 },
function (next) {
makeFriends(1, next)
},
function (next) {
makeFriends(4, next)
},
// Kill pod 4
function (next) {
apps[3].kill() apps[3].kill()
next()
},
// Expulse pod 4 from pod 1 and 2 // Expulse pod 4 from pod 1 and 2
uploadVideo(1, function () { function (next) {
uploadVideo(2, function () { uploadVideo(1, next)
setTimeout(function () { },
uploadVideo(1, function () { function (next) {
uploadVideo(2, function () { uploadVideo(2, next)
setTimeout(function () { },
function (next) {
setTimeout(next, 11000)
},
function (next) {
uploadVideo(1, next)
},
function (next) {
uploadVideo(2, next)
},
function (next) {
setTimeout(next, 20000)
},
// Rerun server 4 // Rerun server 4
function (next) {
utils.runServer(4, function (app, url) { utils.runServer(4, function (app, url) {
apps[3] = app apps[3] = app
next()
})
},
function (next) {
getFriendsList(4, function (err, res) { getFriendsList(4, function (err, res) {
if (err) throw err if (err) throw err
// Pod 4 didn't know pod 1 and 2 removed it // Pod 4 didn't know pod 1 and 2 removed it
expect(res.body.length).to.equal(3) expect(res.body.length).to.equal(3)
next()
})
},
// Pod 6 ask pod 1, 2 and 3 // Pod 6 ask pod 1, 2 and 3
makeFriends(6, function () { function (next) {
makeFriends(6, next)
}],
function (err) {
if (err) throw err
getFriendsList(6, function (err, res) { getFriendsList(6, function (err, res) {
if (err) throw err if (err) throw err
@ -138,42 +180,45 @@
done() done()
}) })
}) }
}) )
})
}, 15000)
})
})
}, 11000)
})
})
})
})
})
}) })
it('Should pod 1 quit friends', function (done) { it('Should pod 1 quit friends', function (done) {
this.timeout(25000) this.timeout(25000)
async.series([
// Upload a video on server 3 for aditionnal tests // Upload a video on server 3 for aditionnal tests
uploadVideo(3, function () { function (next) {
setTimeout(function () { uploadVideo(3, next)
quitFriends(1, function () { },
function (next) {
setTimeout(next, 15000)
},
function (next) {
quitFriends(1, next)
},
// Remove pod 1 from pod 2 // Remove pod 1 from pod 2
function (next) {
getVideos(1, function (err, res) { getVideos(1, function (err, res) {
if (err) throw err if (err) throw err
expect(res.body).to.be.an('array') expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(2) expect(res.body.length).to.equal(2)
next()
})
}],
function (err) {
if (err) throw err
getVideos(2, function (err, res) { getVideos(2, function (err, res) {
if (err) throw err if (err) throw err
expect(res.body).to.be.an('array') expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(3) expect(res.body.length).to.equal(3)
done() done()
}) })
}) }
}) )
}, 15000)
})
}) })
it('Should make friends between pod 1 and 2 and exchange their videos', function (done) { it('Should make friends between pod 1 and 2 and exchange their videos', function (done) {
@ -191,5 +236,17 @@
}, 5000) }, 5000)
}) })
}) })
after(function (done) {
apps.forEach(function (app) {
process.kill(-app.pid)
})
if (this.ok) {
utils.flushTests(done)
} else {
done()
}
})
}) })
})() })()

View file

@ -9,6 +9,9 @@
var utils = require('./utils') var utils = require('./utils')
describe('Test basic friends', function () { describe('Test basic friends', function () {
var apps = []
var urls = []
function testMadeFriends (urls, url_to_test, callback) { function testMadeFriends (urls, url_to_test, callback) {
var friends = [] var friends = []
for (var i = 0; i < urls.length; i++) { for (var i = 0; i < urls.length; i++) {
@ -32,12 +35,11 @@
}) })
} }
var apps = [] // ---------------------------------------------------------------
var urls = []
before(function (done) { before(function (done) {
this.timeout(20000) this.timeout(20000)
utils.runMultipleServers(3, function (apps_run, urls_run) { utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) {
apps = apps_run apps = apps_run
urls = urls_run urls = urls_run
done() done()
@ -54,11 +56,7 @@
expect(result.length).to.equal(0) expect(result.length).to.equal(0)
callback() callback()
}) })
}, function (err) { }, done)
if (err) throw err
done()
})
}) })
it('Should make friends', function (done) { it('Should make friends', function (done) {
@ -66,17 +64,21 @@
var path = '/api/v1/pods/makefriends' var path = '/api/v1/pods/makefriends'
async.series([
// The second pod make friend with the third // The second pod make friend with the third
function (next) {
request(urls[1]) request(urls[1])
.get(path) .get(path)
.set('Accept', 'application/json') .set('Accept', 'application/json')
.expect(204) .expect(204)
.end(function (err, res) { .end(next)
if (err) throw err },
// Wait for the request between pods // Wait for the request between pods
setTimeout(function () { function (next) {
setTimeout(next, 1000)
},
// The second pod should have the third as a friend // The second pod should have the third as a friend
function (next) {
utils.getFriendsList(urls[1], function (err, res) { utils.getFriendsList(urls[1], function (err, res) {
if (err) throw err if (err) throw err
@ -85,7 +87,11 @@
expect(result.length).to.equal(1) expect(result.length).to.equal(1)
expect(result[0].url).to.be.equal(urls[2]) expect(result[0].url).to.be.equal(urls[2])
next()
})
},
// Same here, the third pod should have the second pod as a friend // Same here, the third pod should have the second pod as a friend
function (next) {
utils.getFriendsList(urls[2], function (err, res) { utils.getFriendsList(urls[2], function (err, res) {
if (err) throw err if (err) throw err
@ -94,27 +100,28 @@
expect(result.length).to.equal(1) expect(result.length).to.equal(1)
expect(result[0].url).to.be.equal(urls[1]) expect(result[0].url).to.be.equal(urls[1])
next()
})
},
// Finally the first pod make friend with the second pod // Finally the first pod make friend with the second pod
function (next) {
request(urls[0]) request(urls[0])
.get(path) .get(path)
.set('Accept', 'application/json') .set('Accept', 'application/json')
.expect(204) .expect(204)
.end(function (err, res) { .end(next)
if (err) throw err },
// Wait for the request between pods
setTimeout(function () { function (next) {
setTimeout(next, 1000)
}
],
// Now each pod should be friend with the other ones // Now each pod should be friend with the other ones
function (err) {
if (err) throw err
async.each(urls, function (url, callback) { async.each(urls, function (url, callback) {
testMadeFriends(urls, url, callback) testMadeFriends(urls, url, callback)
}, function (err) { }, done)
if (err) throw err
done()
})
}, 1000)
})
})
})
}, 1000)
}) })
}) })
@ -123,7 +130,13 @@
}) })
it('Should quit friends of pod 2', function (done) { it('Should quit friends of pod 2', function (done) {
utils.quitFriends(urls[1], function () { async.series([
// Pod 1 quit friends
function (next) {
utils.quitFriends(urls[1], next)
},
// Pod 1 should not have friends anymore
function (next) {
utils.getFriendsList(urls[1], function (err, res) { utils.getFriendsList(urls[1], function (err, res) {
if (err) throw err if (err) throw err
@ -131,7 +144,11 @@
expect(result).to.be.an('array') expect(result).to.be.an('array')
expect(result.length).to.equal(0) expect(result.length).to.equal(0)
// Other pods shouldn't have pod 2 too next()
})
},
// Other pods shouldn't have pod 1 too
function (next) {
async.each([ urls[0], urls[2] ], function (url, callback) { async.each([ urls[0], urls[2] ], function (url, callback) {
utils.getFriendsList(url, function (err, res) { utils.getFriendsList(url, function (err, res) {
if (err) throw err if (err) throw err
@ -142,22 +159,16 @@
expect(result[0].url).not.to.be.equal(urls[1]) expect(result[0].url).not.to.be.equal(urls[1])
callback() callback()
}) })
}, function (err) { }, next)
if (err) throw err }
done() ], done)
})
})
})
}) })
it('Should allow pod 2 to make friend again', function (done) { it('Should allow pod 2 to make friend again', function (done) {
utils.makeFriends(urls[1], function () { utils.makeFriends(urls[1], function () {
async.each(urls, function (url, callback) { async.each(urls, function (url, callback) {
testMadeFriends(urls, url, callback) testMadeFriends(urls, url, callback)
}, function (err) { }, done)
if (err) throw err
done()
})
}) })
}) })
@ -167,9 +178,7 @@
}) })
if (this.ok) { if (this.ok) {
utils.flushTests(function () { utils.flushTests(done)
done()
})
} else { } else {
done() done()
} }

View file

@ -17,26 +17,31 @@
before(function (done) { before(function (done) {
this.timeout(30000) this.timeout(30000)
utils.runMultipleServers(3, function (apps_run, urls_run) { async.series([
// Run servers
function (next) {
utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) {
apps = apps_run apps = apps_run
urls = urls_run urls = urls_run
next()
})
},
// The second pod make friend with the third // The second pod make friend with the third
utils.makeFriends(urls[1], function (err, res) { function (next) {
if (err) throw err utils.makeFriends(urls[1], next)
},
// Wait for the request between pods // Wait for the request between pods
setTimeout(function () { function (next) {
utils.makeFriends(urls[0], function (err, res) { setTimeout(next, 10000)
if (err) throw err },
// Pod 1 make friends too
webtorrent.create({ host: 'client', port: '1' }, function () { function (next) {
done() utils.makeFriends(urls[0], next)
}) },
}) function (next) {
}, 10000) webtorrent.create({ host: 'client', port: '1' }, next)
}) }
}) ], done)
}) })
it('Should not have videos for all pods', function (done) { it('Should not have videos for all pods', function (done) {
@ -49,22 +54,24 @@
callback() callback()
}) })
}, function (err) { }, done)
if (err) throw err
done()
})
}) })
describe('Should upload the video and propagate on each pod', function () { describe('Should upload the video and propagate on each pod', function () {
it('Should upload the video on pod 1 and propagate on each pod', function (done) { it('Should upload the video on pod 1 and propagate on each pod', function (done) {
this.timeout(15000) this.timeout(15000)
utils.uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', function (err) { async.series([
function (next) {
utils.uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', next)
},
function (next) {
setTimeout(next, 11000)
}],
// All pods should have this video
function (err) {
if (err) throw err if (err) throw err
setTimeout(function () {
// All pods should have this video
async.each(urls, function (url, callback) { async.each(urls, function (url, callback) {
var base_magnet = null var base_magnet = null
@ -89,23 +96,25 @@
callback() callback()
}) })
}, function (err) { }, done)
if (err) throw err }
)
done()
})
}, 11000)
})
}) })
it('Should upload the video on pod 2 and propagate on each pod', function (done) { it('Should upload the video on pod 2 and propagate on each pod', function (done) {
this.timeout(15000) this.timeout(15000)
utils.uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', function (err) { async.series([
function (next) {
utils.uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', next)
},
function (next) {
setTimeout(next, 11000)
}],
// All pods should have this video
function (err) {
if (err) throw err if (err) throw err
setTimeout(function () {
// All pods should have this video
async.each(urls, function (url, callback) { async.each(urls, function (url, callback) {
var base_magnet = null var base_magnet = null
@ -130,24 +139,27 @@
callback() callback()
}) })
}, function (err) { }, done)
if (err) throw err }
)
done()
})
}, 11000)
})
}) })
it('Should upload two videos on pod 3 and propagate on each pod', function (done) { it('Should upload two videos on pod 3 and propagate on each pod', function (done) {
this.timeout(30000) this.timeout(30000)
utils.uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', function (err) { async.series([
if (err) throw err function (next) {
utils.uploadVideo(urls[2], 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', function (err) { utils.uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', next)
},
function (next) {
utils.uploadVideo(urls[2], 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', next)
},
function (next) {
setTimeout(next, 22000)
}],
function (err) {
if (err) throw err if (err) throw err
setTimeout(function () {
var base_magnet = null var base_magnet = null
// All pods should have this video // All pods should have this video
async.each(urls, function (url, callback) { async.each(urls, function (url, callback) {
@ -178,14 +190,9 @@
callback() callback()
}) })
}, function (err) { }, done)
if (err) throw err }
)
done()
})
}, 22000)
})
})
}) })
}) })
@ -271,17 +278,18 @@
it('Should remove the file 3 and 3-2 by asking pod 3', function (done) { it('Should remove the file 3 and 3-2 by asking pod 3', function (done) {
this.timeout(15000) this.timeout(15000)
utils.removeVideo(urls[2], to_remove[0], function (err) { async.series([
function (next) {
utils.removeVideo(urls[2], to_remove[0], next)
},
function (next) {
utils.removeVideo(urls[2], to_remove[1], next)
}],
function (err) {
if (err) throw err if (err) throw err
utils.removeVideo(urls[2], to_remove[1], function (err) { setTimeout(done, 11000)
if (err) throw err }
)
// Wait the propagation to the other pods
setTimeout(function () {
done()
}, 11000)
})
})
}) })
it('Should have videos 1 and 3 on each pod', function (done) { it('Should have videos 1 and 3 on each pod', function (done) {
@ -300,11 +308,7 @@
callback() callback()
}) })
}, function (err) { }, done)
if (err) throw err
done()
})
}) })
}) })
@ -316,9 +320,7 @@
// Keep the logs if the test failed // Keep the logs if the test failed
if (this.ok) { if (this.ok) {
utils.flushTests(function () { utils.flushTests(done)
done()
})
} else { } else {
done() done()
} }

View file

@ -1,6 +1,7 @@
;(function () { ;(function () {
'use strict' 'use strict'
var async = require('async')
var chai = require('chai') var chai = require('chai')
var fs = require('fs') var fs = require('fs')
var expect = chai.expect var expect = chai.expect
@ -18,16 +19,21 @@
before(function (done) { before(function (done) {
this.timeout(20000) this.timeout(20000)
utils.flushTests(function () { async.series([
function (next) {
utils.flushTests(next)
},
function (next) {
utils.runServer(1, function (app1, url1) { utils.runServer(1, function (app1, url1) {
app = app1 app = app1
url = url1 url = url1
next()
webtorrent.create({ host: 'client', port: '1' }, function () {
done()
})
})
}) })
},
function (next) {
webtorrent.create({ host: 'client', port: '1' }, next)
}
], done)
}) })
it('Should not have videos', function (done) { it('Should not have videos', function (done) {
@ -132,9 +138,7 @@
// Keep the logs if the test failed // Keep the logs if the test failed
if (this.ok) { if (this.ok) {
utils.flushTests(function () { utils.flushTests(done)
done()
})
} else { } else {
done() done()
} }

View file

@ -6,10 +6,23 @@
var fork = child_process.fork var fork = child_process.fork
var request = require('supertest') var request = require('supertest')
module.exports = {
flushTests: flushTests,
getFriendsList: getFriendsList,
getVideosList: getVideosList,
makeFriends: makeFriends,
quitFriends: quitFriends,
removeVideo: removeVideo,
flushAndRunMultipleServers: flushAndRunMultipleServers,
runServer: runServer,
searchVideo: searchVideo,
uploadVideo: uploadVideo
}
// ---------------------- Export functions --------------------
function flushTests (callback) { function flushTests (callback) {
exec(__dirname + '/../../scripts/clean_test.sh', function () { exec(__dirname + '/../../scripts/clean_test.sh', callback)
callback()
})
} }
function getFriendsList (url, end) { function getFriendsList (url, end) {
@ -51,9 +64,7 @@
if (err) throw err if (err) throw err
// Wait for the request between pods // Wait for the request between pods
setTimeout(function () { setTimeout(callback, 1000)
callback()
}, 1000)
}) })
} }
@ -69,25 +80,10 @@
if (err) throw err if (err) throw err
// Wait for the request between pods // Wait for the request between pods
setTimeout(function () { setTimeout(callback, 1000)
callback()
}, 1000)
}) })
} }
function uploadVideo (url, name, description, fixture, end) {
var path = '/api/v1/videos'
request(url)
.post(path)
.set('Accept', 'application/json')
.field('name', name)
.field('description', description)
.attach('input_video', __dirname + '/fixtures/' + fixture)
.expect(201)
.end(end)
}
function removeVideo (url, id, end) { function removeVideo (url, id, end) {
var path = '/api/v1/videos' var path = '/api/v1/videos'
@ -98,7 +94,7 @@
.end(end) .end(end)
} }
function runMultipleServers (total_servers, serversRun) { function flushAndRunMultipleServers (total_servers, serversRun) {
var apps = [] var apps = []
var urls = [] var urls = []
var i = 0 var i = 0
@ -171,16 +167,16 @@
.end(end) .end(end)
} }
module.exports = { function uploadVideo (url, name, description, fixture, end) {
flushTests: flushTests, var path = '/api/v1/videos'
getFriendsList: getFriendsList,
getVideosList: getVideosList, request(url)
makeFriends: makeFriends, .post(path)
quitFriends: quitFriends, .set('Accept', 'application/json')
removeVideo: removeVideo, .field('name', name)
runMultipleServers: runMultipleServers, .field('description', description)
runServer: runServer, .attach('input_video', __dirname + '/fixtures/' + fixture)
searchVideo: searchVideo, .expect(201)
uploadVideo: uploadVideo .end(end)
} }
})() })()