From ee66c5930e6c3694434a259dfb4b7f3e9f39cf7b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 24 Jan 2016 16:08:09 +0100 Subject: [PATCH] Tests refractoring --- test/api/checkParams.js | 36 +++-- test/api/friendsAdvanced.js | 267 ++++++++++++++++++++++-------------- test/api/friendsBasic.js | 161 ++++++++++++---------- test/api/multiplePods.js | 208 ++++++++++++++-------------- test/api/singlePod.js | 28 ++-- test/api/utils.js | 64 ++++----- 6 files changed, 419 insertions(+), 345 deletions(-) diff --git a/test/api/checkParams.js b/test/api/checkParams.js index 0fa83655f..11fc68ff9 100644 --- a/test/api/checkParams.js +++ b/test/api/checkParams.js @@ -1,6 +1,7 @@ ;(function () { 'use strict' + var async = require('async') var chai = require('chai') var expect = chai.expect var request = require('supertest') @@ -11,18 +12,6 @@ var app = null 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) { var status_code = 400 if (fail !== undefined && fail === false) status_code = 200 @@ -50,6 +39,25 @@ .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 () { var path = '/api/v1/pods/' @@ -284,9 +292,7 @@ // Keep the logs if the test failed if (this.ok) { - utils.flushTests(function () { - done() - }) + utils.flushTests(done) } else { done() } diff --git a/test/api/friendsAdvanced.js b/test/api/friendsAdvanced.js index b24cd39c3..61483bee6 100644 --- a/test/api/friendsAdvanced.js +++ b/test/api/friendsAdvanced.js @@ -35,145 +35,190 @@ return utils.getVideosList(urls[pod_number - 1], callback) } + // --------------------------------------------------------------- + before(function (done) { this.timeout(30000) - utils.runMultipleServers(6, function (apps_run, urls_run) { + utils.flushAndRunMultipleServers(6, function (apps_run, urls_run) { apps = apps_run urls = urls_run 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) { this.timeout(20000) - // Pod 3 makes friend with the first one - makeFriends(3, function () { + async.series([ + // Pod 3 makes friend with the first one + function (next) { + makeFriends(3, next) + }, // Pod 4 makes friend with the second one - makeFriends(4, function () { - // Now if the fifth wants to make friends with the third et the first - makeFriends(5, function () { - setTimeout(function () { - // It should have 0 friends - getFriendsList(5, function (err, res) { - if (err) throw err - - expect(res.body.length).to.equal(0) - - done() - }) - }, 11000) + function (next) { + makeFriends(4, next) + }, + // Now if the fifth wants to make friends with the third et the first + function (next) { + makeFriends(5, next) + }, + function (next) { + setTimeout(next, 11000) + }], + function (err) { + if (err) throw err + + // It should have 0 friends + getFriendsList(5, function (err, res) { + if (err) throw err + + expect(res.body.length).to.equal(0) + + done() }) - }) - }) + } + ) }) it('Should quit all friends', function (done) { 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) { getFriendsList(i, function (err, res) { if (err) throw err + expect(res.body.length).to.equal(0) + callback() }) - }, function () { - done() - }) - }) - }) + }, done) + } + ) }) it('Should make friends with the pods 1, 2, 3', function (done) { this.timeout(150000) - // Pods 1, 2, 3 and 4 become friends (yes this is beautiful) - makeFriends(2, function () { - makeFriends(1, function () { - makeFriends(4, function () { - // Kill the server 4 - apps[3].kill() - - // Expulse pod 4 from pod 1 and 2 - uploadVideo(1, function () { - uploadVideo(2, function () { - setTimeout(function () { - uploadVideo(1, function () { - uploadVideo(2, function () { - setTimeout(function () { - // Rerun server 4 - utils.runServer(4, function (app, url) { - apps[3] = app - getFriendsList(4, function (err, res) { - if (err) throw err - // Pod 4 didn't know pod 1 and 2 removed it - expect(res.body.length).to.equal(3) - - // Pod 6 ask pod 1, 2 and 3 - makeFriends(6, function () { - getFriendsList(6, function (err, res) { - if (err) throw err - - // Pod 4 should not be our friend - var result = res.body - expect(result.length).to.equal(3) - for (var pod of result) { - expect(pod.url).not.equal(urls[3]) - } - - done() - }) - }) - }) - }) - }, 15000) - }) - }) - }, 11000) - }) - }) + async.series([ + // Pods 1, 2, 3 and 4 become friends + function (next) { + makeFriends(2, next) + }, + function (next) { + makeFriends(1, next) + }, + function (next) { + makeFriends(4, next) + }, + // Kill pod 4 + function (next) { + apps[3].kill() + next() + }, + // Expulse pod 4 from pod 1 and 2 + function (next) { + uploadVideo(1, next) + }, + function (next) { + uploadVideo(2, next) + }, + function (next) { + setTimeout(next, 11000) + }, + function (next) { + uploadVideo(1, next) + }, + function (next) { + uploadVideo(2, next) + }, + function (next) { + setTimeout(next, 20000) + }, + // Rerun server 4 + function (next) { + utils.runServer(4, function (app, url) { + apps[3] = app + next() }) - }) - }) + }, + function (next) { + getFriendsList(4, function (err, res) { + if (err) throw err + + // Pod 4 didn't know pod 1 and 2 removed it + expect(res.body.length).to.equal(3) + + next() + }) + }, + // Pod 6 ask pod 1, 2 and 3 + function (next) { + makeFriends(6, next) + }], + function (err) { + if (err) throw err + + getFriendsList(6, function (err, res) { + if (err) throw err + + // Pod 4 should not be our friend + var result = res.body + expect(result.length).to.equal(3) + for (var pod of result) { + expect(pod.url).not.equal(urls[3]) + } + + done() + }) + } + ) }) it('Should pod 1 quit friends', function (done) { this.timeout(25000) - // Upload a video on server 3 for aditionnal tests - uploadVideo(3, function () { - setTimeout(function () { - quitFriends(1, function () { - // Remove pod 1 from pod 2 - getVideos(1, function (err, res) { - if (err) throw err - expect(res.body).to.be.an('array') - expect(res.body.length).to.equal(2) - - getVideos(2, function (err, res) { - if (err) throw err - expect(res.body).to.be.an('array') - expect(res.body.length).to.equal(3) - done() - }) - }) + + async.series([ + // Upload a video on server 3 for aditionnal tests + function (next) { + uploadVideo(3, next) + }, + function (next) { + setTimeout(next, 15000) + }, + function (next) { + quitFriends(1, next) + }, + // Remove pod 1 from pod 2 + function (next) { + getVideos(1, function (err, res) { + if (err) throw err + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(2) + + next() }) - }, 15000) - }) + }], + function (err) { + if (err) throw err + + getVideos(2, function (err, res) { + if (err) throw err + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(3) + done() + }) + } + ) }) it('Should make friends between pod 1 and 2 and exchange their videos', function (done) { @@ -191,5 +236,17 @@ }, 5000) }) }) + + after(function (done) { + apps.forEach(function (app) { + process.kill(-app.pid) + }) + + if (this.ok) { + utils.flushTests(done) + } else { + done() + } + }) }) })() diff --git a/test/api/friendsBasic.js b/test/api/friendsBasic.js index 15b83d421..dbc918383 100644 --- a/test/api/friendsBasic.js +++ b/test/api/friendsBasic.js @@ -9,6 +9,9 @@ var utils = require('./utils') describe('Test basic friends', function () { + var apps = [] + var urls = [] + function testMadeFriends (urls, url_to_test, callback) { var friends = [] for (var i = 0; i < urls.length; i++) { @@ -32,12 +35,11 @@ }) } - var apps = [] - var urls = [] + // --------------------------------------------------------------- before(function (done) { this.timeout(20000) - utils.runMultipleServers(3, function (apps_run, urls_run) { + utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) { apps = apps_run urls = urls_run done() @@ -54,11 +56,7 @@ expect(result.length).to.equal(0) callback() }) - }, function (err) { - if (err) throw err - - done() - }) + }, done) }) it('Should make friends', function (done) { @@ -66,56 +64,65 @@ var path = '/api/v1/pods/makefriends' - // The second pod make friend with the third - request(urls[1]) - .get(path) - .set('Accept', 'application/json') - .expect(204) - .end(function (err, res) { - if (err) throw err + async.series([ + // The second pod make friend with the third + function (next) { + request(urls[1]) + .get(path) + .set('Accept', 'application/json') + .expect(204) + .end(next) + }, + // Wait for the request between pods + function (next) { + setTimeout(next, 1000) + }, + // The second pod should have the third as a friend + function (next) { + utils.getFriendsList(urls[1], function (err, res) { + if (err) throw err - // Wait for the request between pods - setTimeout(function () { - // The second pod should have the third as a friend - utils.getFriendsList(urls[1], function (err, res) { - if (err) throw err + var result = res.body + expect(result).to.be.an('array') + expect(result.length).to.equal(1) + expect(result[0].url).to.be.equal(urls[2]) - var result = res.body - expect(result).to.be.an('array') - expect(result.length).to.equal(1) - expect(result[0].url).to.be.equal(urls[2]) - - // Same here, the third pod should have the second pod as a friend - utils.getFriendsList(urls[2], function (err, res) { - if (err) throw err - - var result = res.body - expect(result).to.be.an('array') - expect(result.length).to.equal(1) - expect(result[0].url).to.be.equal(urls[1]) - - // Finally the first pod make friend with the second pod - request(urls[0]) - .get(path) - .set('Accept', 'application/json') - .expect(204) - .end(function (err, res) { - if (err) throw err - - setTimeout(function () { - // Now each pod should be friend with the other ones - async.each(urls, function (url, callback) { - testMadeFriends(urls, url, callback) - }, function (err) { - if (err) throw err - done() - }) - }, 1000) - }) - }) - }) - }, 1000) - }) + next() + }) + }, + // Same here, the third pod should have the second pod as a friend + function (next) { + utils.getFriendsList(urls[2], function (err, res) { + if (err) throw err + + var result = res.body + expect(result).to.be.an('array') + expect(result.length).to.equal(1) + expect(result[0].url).to.be.equal(urls[1]) + + next() + }) + }, + // Finally the first pod make friend with the second pod + function (next) { + request(urls[0]) + .get(path) + .set('Accept', 'application/json') + .expect(204) + .end(next) + }, + // Wait for the request between pods + function (next) { + setTimeout(next, 1000) + } + ], + // Now each pod should be friend with the other ones + function (err) { + if (err) throw err + async.each(urls, function (url, callback) { + testMadeFriends(urls, url, callback) + }, done) + }) }) it('Should not be allowed to make friend again', function (done) { @@ -123,15 +130,25 @@ }) it('Should quit friends of pod 2', function (done) { - utils.quitFriends(urls[1], function () { - utils.getFriendsList(urls[1], function (err, res) { - if (err) throw err + 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) { + if (err) throw err - var result = res.body - expect(result).to.be.an('array') - expect(result.length).to.equal(0) + var result = res.body + expect(result).to.be.an('array') + 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) { utils.getFriendsList(url, function (err, res) { if (err) throw err @@ -142,22 +159,16 @@ expect(result[0].url).not.to.be.equal(urls[1]) callback() }) - }, function (err) { - if (err) throw err - done() - }) - }) - }) + }, next) + } + ], done) }) it('Should allow pod 2 to make friend again', function (done) { utils.makeFriends(urls[1], function () { async.each(urls, function (url, callback) { testMadeFriends(urls, url, callback) - }, function (err) { - if (err) throw err - done() - }) + }, done) }) }) @@ -167,9 +178,7 @@ }) if (this.ok) { - utils.flushTests(function () { - done() - }) + utils.flushTests(done) } else { done() } diff --git a/test/api/multiplePods.js b/test/api/multiplePods.js index 531e1ef33..b579e5e32 100644 --- a/test/api/multiplePods.js +++ b/test/api/multiplePods.js @@ -17,26 +17,31 @@ before(function (done) { this.timeout(30000) - utils.runMultipleServers(3, function (apps_run, urls_run) { - apps = apps_run - urls = urls_run - + async.series([ + // Run servers + function (next) { + utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) { + apps = apps_run + urls = urls_run + next() + }) + }, // The second pod make friend with the third - utils.makeFriends(urls[1], function (err, res) { - if (err) throw err - - // Wait for the request between pods - setTimeout(function () { - utils.makeFriends(urls[0], function (err, res) { - if (err) throw err - - webtorrent.create({ host: 'client', port: '1' }, function () { - done() - }) - }) - }, 10000) - }) - }) + function (next) { + utils.makeFriends(urls[1], next) + }, + // Wait for the request between pods + function (next) { + setTimeout(next, 10000) + }, + // Pod 1 make friends too + function (next) { + utils.makeFriends(urls[0], next) + }, + function (next) { + webtorrent.create({ host: 'client', port: '1' }, next) + } + ], done) }) it('Should not have videos for all pods', function (done) { @@ -49,22 +54,24 @@ callback() }) - }, function (err) { - if (err) throw err - - done() - }) + }, done) }) 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) { this.timeout(15000) - utils.uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', function (err) { - if (err) throw 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 - setTimeout(function () { - // All pods should have this video async.each(urls, function (url, callback) { var base_magnet = null @@ -89,23 +96,25 @@ callback() }) - }, function (err) { - if (err) throw err - - done() - }) - }, 11000) - }) + }, done) + } + ) }) it('Should upload the video on pod 2 and propagate on each pod', function (done) { this.timeout(15000) - utils.uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', function (err) { - if (err) throw 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 - setTimeout(function () { - // All pods should have this video async.each(urls, function (url, callback) { var base_magnet = null @@ -130,62 +139,60 @@ callback() }) - }, function (err) { - if (err) throw err - - done() - }) - }, 11000) - }) + }, done) + } + ) }) it('Should upload two videos on pod 3 and propagate on each pod', function (done) { this.timeout(30000) - utils.uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', function (err) { - if (err) throw err - utils.uploadVideo(urls[2], 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', function (err) { + async.series([ + function (next) { + 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 - setTimeout(function () { - var base_magnet = null - // All pods should have this video - async.each(urls, function (url, callback) { - utils.getVideosList(url, function (err, res) { - if (err) throw err - - var videos = res.body - expect(videos).to.be.an('array') - expect(videos.length).to.equal(4) - var video = videos[2] - expect(video.name).to.equal('my super name for pod 3') - expect(video.description).to.equal('my super description for pod 3') - expect(video.podUrl).to.equal('http://localhost:9003') - expect(video.magnetUri).to.exist - - video = videos[3] - expect(video.name).to.equal('my super name for pod 3-2') - expect(video.description).to.equal('my super description for pod 3-2') - expect(video.podUrl).to.equal('http://localhost:9003') - expect(video.magnetUri).to.exist - - // All pods should have the same magnet Uri - if (base_magnet === null) { - base_magnet = video.magnetUri - } else { - expect(video.magnetUri).to.equal.magnetUri - } - - callback() - }) - }, function (err) { + var base_magnet = null + // All pods should have this video + async.each(urls, function (url, callback) { + utils.getVideosList(url, function (err, res) { if (err) throw err - done() + var videos = res.body + expect(videos).to.be.an('array') + expect(videos.length).to.equal(4) + var video = videos[2] + expect(video.name).to.equal('my super name for pod 3') + expect(video.description).to.equal('my super description for pod 3') + expect(video.podUrl).to.equal('http://localhost:9003') + expect(video.magnetUri).to.exist + + video = videos[3] + expect(video.name).to.equal('my super name for pod 3-2') + expect(video.description).to.equal('my super description for pod 3-2') + expect(video.podUrl).to.equal('http://localhost:9003') + expect(video.magnetUri).to.exist + + // All pods should have the same magnet Uri + if (base_magnet === null) { + base_magnet = video.magnetUri + } else { + expect(video.magnetUri).to.equal.magnetUri + } + + callback() }) - }, 22000) - }) - }) + }, done) + } + ) }) }) @@ -271,17 +278,18 @@ it('Should remove the file 3 and 3-2 by asking pod 3', function (done) { this.timeout(15000) - utils.removeVideo(urls[2], to_remove[0], function (err) { - if (err) throw err - utils.removeVideo(urls[2], to_remove[1], 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 - - // Wait the propagation to the other pods - setTimeout(function () { - done() - }, 11000) - }) - }) + setTimeout(done, 11000) + } + ) }) it('Should have videos 1 and 3 on each pod', function (done) { @@ -300,11 +308,7 @@ callback() }) - }, function (err) { - if (err) throw err - - done() - }) + }, done) }) }) @@ -316,9 +320,7 @@ // Keep the logs if the test failed if (this.ok) { - utils.flushTests(function () { - done() - }) + utils.flushTests(done) } else { done() } diff --git a/test/api/singlePod.js b/test/api/singlePod.js index be944d8da..a8ae43aee 100644 --- a/test/api/singlePod.js +++ b/test/api/singlePod.js @@ -1,6 +1,7 @@ ;(function () { 'use strict' + var async = require('async') var chai = require('chai') var fs = require('fs') var expect = chai.expect @@ -18,16 +19,21 @@ before(function (done) { this.timeout(20000) - utils.flushTests(function () { - utils.runServer(1, function (app1, url1) { - app = app1 - url = url1 - - webtorrent.create({ host: 'client', port: '1' }, function () { - done() + async.series([ + function (next) { + utils.flushTests(next) + }, + function (next) { + utils.runServer(1, function (app1, url1) { + app = app1 + url = url1 + next() }) - }) - }) + }, + function (next) { + webtorrent.create({ host: 'client', port: '1' }, next) + } + ], done) }) it('Should not have videos', function (done) { @@ -132,9 +138,7 @@ // Keep the logs if the test failed if (this.ok) { - utils.flushTests(function () { - done() - }) + utils.flushTests(done) } else { done() } diff --git a/test/api/utils.js b/test/api/utils.js index b00890539..afb0abb33 100644 --- a/test/api/utils.js +++ b/test/api/utils.js @@ -6,10 +6,23 @@ var fork = child_process.fork 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) { - exec(__dirname + '/../../scripts/clean_test.sh', function () { - callback() - }) + exec(__dirname + '/../../scripts/clean_test.sh', callback) } function getFriendsList (url, end) { @@ -51,9 +64,7 @@ if (err) throw err // Wait for the request between pods - setTimeout(function () { - callback() - }, 1000) + setTimeout(callback, 1000) }) } @@ -69,25 +80,10 @@ if (err) throw err // Wait for the request between pods - setTimeout(function () { - callback() - }, 1000) + setTimeout(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) { var path = '/api/v1/videos' @@ -98,7 +94,7 @@ .end(end) } - function runMultipleServers (total_servers, serversRun) { + function flushAndRunMultipleServers (total_servers, serversRun) { var apps = [] var urls = [] var i = 0 @@ -171,16 +167,16 @@ .end(end) } - module.exports = { - flushTests: flushTests, - getFriendsList: getFriendsList, - getVideosList: getVideosList, - makeFriends: makeFriends, - quitFriends: quitFriends, - removeVideo: removeVideo, - runMultipleServers: runMultipleServers, - runServer: runServer, - searchVideo: searchVideo, - uploadVideo: uploadVideo + 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) } })() -- 2.41.0