From 8c308c2bf7f658945d80be9d5880361238635f5b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 9 Jun 2015 17:41:40 +0200 Subject: Spawn --- test/api/friends.js | 149 ++++++++++++++++++ test/api/multiplePods.js | 326 ++++++++++++++++++++++++++++++++++++++++ test/api/singlePod.js | 135 +++++++++++++++++ test/fixtures/video_short.webm | Bin 0 -> 218910 bytes test/fixtures/video_short1.webm | Bin 0 -> 572456 bytes test/fixtures/video_short2.webm | Bin 0 -> 942961 bytes test/fixtures/video_short3.webm | Bin 0 -> 292677 bytes test/utils.js | 81 ++++++++++ 8 files changed, 691 insertions(+) create mode 100644 test/api/friends.js create mode 100644 test/api/multiplePods.js create mode 100644 test/api/singlePod.js create mode 100644 test/fixtures/video_short.webm create mode 100644 test/fixtures/video_short1.webm create mode 100644 test/fixtures/video_short2.webm create mode 100644 test/fixtures/video_short3.webm create mode 100644 test/utils.js (limited to 'test') diff --git a/test/api/friends.js b/test/api/friends.js new file mode 100644 index 000000000..033d3799a --- /dev/null +++ b/test/api/friends.js @@ -0,0 +1,149 @@ +;(function () { + 'use strict' + + var request = require('supertest') + var chai = require('chai') + var expect = chai.expect + var async = require('async') + + var utils = require('../utils') + + function getFriendsList (url, end) { + var path = '/api/pods/' + + request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(end) + } + + describe('Test friends', function () { + var apps = [] + var urls = [] + + before(function (done) { + this.timeout(20000) + utils.runMultipleServers(3, function (apps_run, urls_run) { + apps = apps_run + urls = urls_run + done() + }) + }) + + it('Should not have friends', function (done) { + async.each(urls, function (url, callback) { + getFriendsList(url, function (err, res) { + if (err) throw err + + var result = res.body + expect(result).to.be.an('array') + expect(result.length).to.equal(0) + callback() + }) + }, function (err) { + if (err) throw err + + done() + }) + }) + + it('Should make friends', function (done) { + this.timeout(10000) + + function testMadeFriends (urls, url_to_test, callback) { + var friends = [] + for (var i = 0; i < urls.length; i++) { + if (urls[i] === url_to_test) continue + friends.push(urls[i]) + } + + getFriendsList(url_to_test, function (err, res) { + if (err) throw err + + var result = res.body + var result_urls = [ result[0].url, result[1].url ] + expect(result).to.be.an('array') + expect(result.length).to.equal(2) + expect(result_urls[0]).to.not.equal(result_urls[1]) + + var error_string = 'Friends url do not correspond for ' + url_to_test + expect(friends).to.contain(result_urls[0], error_string) + expect(friends).to.contain(result_urls[1], error_string) + callback() + }) + } + + var path = '/api/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 + + // Wait for the request between pods + setTimeout(function () { + // The second pod should have the third as a friend + 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]) + + // Same here, the third pod should have the second pod as a friend + 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) + }) + }) + + // TODO + it('Should not be able to make friends again') + + after(function (done) { + apps.forEach(function (app) { + process.kill(-app.pid) + }) + + if (this.ok) { + utils.flushTests(function () { + done() + }) + } else { + done() + } + }) + }) +})() diff --git a/test/api/multiplePods.js b/test/api/multiplePods.js new file mode 100644 index 000000000..fff179006 --- /dev/null +++ b/test/api/multiplePods.js @@ -0,0 +1,326 @@ +;(function () { + 'use strict' + + var request = require('supertest') + var chai = require('chai') + var expect = chai.expect + var async = require('async') + + var utils = require('../utils') + var webtorrent = require(__dirname + '/../../src/webTorrentNode') + webtorrent.silent = true + + describe('Test multiple pods', function () { + var path = '/api/videos' + var apps = [] + var urls = [] + var video_id = -1 + + function getVideosList (url, end) { + request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(end) + } + + function uploadVideo (url, name, description, fixture, end) { + request(url) + .post(path) + .set('Accept', 'application/json') + .field('name', name) + .field('description', description) + .attach('input_video', __dirname + '/../fixtures/' + fixture) + .expect(201) + .end(end) + } + + before(function (done) { + this.timeout(20000) + var path_friends = '/api/pods/makefriends' + + utils.runMultipleServers(3, function (apps_run, urls_run) { + apps = apps_run + urls = urls_run + + // The second pod make friend with the third + request(urls[1]) + .get(path_friends) + .set('Accept', 'application/json') + .expect(204) + .end(function (err, res) { + if (err) throw err + + // Wait for the request between pods + setTimeout(function () { + request(urls[0]) + .get(path_friends) + .set('Accept', 'application/json') + .expect(204) + .end(function (err, res) { + if (err) throw err + + webtorrent.create(function () { + done() + }) + }) + }, 1000) + }) + }) + }) + + it('Should not have videos for all pods', function (done) { + async.each(urls, function (url, callback) { + getVideosList(url, function (err, res) { + if (err) throw err + + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(0) + + callback() + }) + }, function (err) { + if (err) throw err + + 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(5000) + + uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', function (err) { + if (err) throw err + + setTimeout(function () { + // All pods should have this video + async.each(urls, function (url, callback) { + var base_magnet = null + + getVideosList(url, function (err, res) { + if (err) throw err + + var videos = res.body + expect(videos).to.be.an('array') + expect(videos.length).to.equal(1) + var video = videos[0] + expect(video.name).to.equal('my super name for pod 1') + expect(video.description).to.equal('my super description for pod 1') + expect(video.podUrl).to.equal('http://localhost:9001') + 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) { + if (err) throw err + + done() + }) + }, 1000) + }) + }) + + it('Should upload the video on pod 2 and propagate on each pod', function (done) { + this.timeout(5000) + + uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', function (err) { + if (err) throw err + + setTimeout(function () { + // All pods should have this video + async.each(urls, function (url, callback) { + var base_magnet = null + + getVideosList(url, function (err, res) { + if (err) throw err + + var videos = res.body + expect(videos).to.be.an('array') + expect(videos.length).to.equal(2) + var video = videos[1] + expect(video.name).to.equal('my super name for pod 2') + expect(video.description).to.equal('my super description for pod 2') + expect(video.podUrl).to.equal('http://localhost:9002') + 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) { + if (err) throw err + + done() + }) + }, 1000) + }) + }) + + it('Should upload the video on pod 3 and propagate on each pod', function (done) { + this.timeout(5000) + + uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', function (err) { + if (err) throw err + + setTimeout(function () { + var base_magnet = null + // All pods should have this video + async.each(urls, function (url, callback) { + getVideosList(url, function (err, res) { + if (err) throw err + + var videos = res.body + expect(videos).to.be.an('array') + expect(videos.length).to.equal(3) + 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 + + // 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) { + if (err) throw err + + done() + }) + }, 1000) + }) + }) + }) + + describe('Should seed the uploaded video', function () { + it('Should add the file 1 by asking pod 3', function (done) { + // Yes, this could be long + this.timeout(60000) + + getVideosList(urls[2], function (err, res) { + if (err) throw err + + var video = res.body[0] + webtorrent.add(video.magnetUri, function (torrent) { + expect(torrent.files).to.exist + expect(torrent.files.length).to.equal(1) + expect(torrent.files[0].path).to.exist.and.to.not.equal('') + + done() + }) + }) + }) + + it('Should add the file 2 by asking pod 1', function (done) { + // Yes, this could be long + this.timeout(60000) + + getVideosList(urls[0], function (err, res) { + if (err) throw err + + var video = res.body[1] + + video_id = video._id + + webtorrent.add(video.magnetUri, function (torrent) { + expect(torrent.files).to.exist + expect(torrent.files.length).to.equal(1) + expect(torrent.files[0].path).to.exist.and.to.not.equal('') + + done() + }) + }) + }) + + it('Should add the file 3 by asking pod 2', function (done) { + // Yes, this could be long + this.timeout(60000) + + getVideosList(urls[1], function (err, res) { + if (err) throw err + + var video = res.body[2] + + webtorrent.add(video.magnetUri, function (torrent) { + expect(torrent.files).to.exist + expect(torrent.files.length).to.equal(1) + expect(torrent.files[0].path).to.exist.and.to.not.equal('') + + done() + }) + }) + }) + + it('Should remove the file 2 by asking pod 2', function (done) { + request(urls[1]) + .delete(path + '/' + video_id) + .set('Accept', 'application/json') + .expect(204) + .end(function (err, res) { + if (err) throw err + + // Wait the propagation to the other pods + setTimeout(function () { + done() + }, 1000) + }) + }) + + it('Should have videos 1 and 2 on each pod', function (done) { + async.each(urls, function (url, callback) { + getVideosList(url, function (err, res) { + if (err) throw err + + var videos = res.body + expect(videos).to.be.an('array') + expect(videos.length).to.equal(2) + expect(videos[0]._id).not.to.equal(videos[1]._id) + expect(videos[0]._id).not.to.equal(video_id) + expect(videos[1]._id).not.to.equal(video_id) + + callback() + }) + }, function (err) { + if (err) throw err + + done() + }) + }) + }) + + after(function (done) { + apps.forEach(function (app) { + process.kill(-app.pid) + }) + process.kill(-webtorrent.app.pid) + + // Keep the logs if the test failed + if (this.ok) { + utils.flushTests(function () { + done() + }) + } else { + done() + } + }) + }) +})() diff --git a/test/api/singlePod.js b/test/api/singlePod.js new file mode 100644 index 000000000..59a617970 --- /dev/null +++ b/test/api/singlePod.js @@ -0,0 +1,135 @@ +;(function () { + 'use strict' + + var request = require('supertest') + var chai = require('chai') + var expect = chai.expect + var webtorrent = require(__dirname + '/../../src/webTorrentNode') + webtorrent.silent = true + + var utils = require('../utils') + + describe('Test a single pod', function () { + var path = '/api/videos' + var app = null + var url = '' + var video_id = -1 + + before(function (done) { + this.timeout(10000) + + utils.flushTests(function () { + utils.runServer(1, function (app1, url1) { + app = app1 + url = url1 + + webtorrent.create(function () { + done() + }) + }) + }) + }) + + it('Should not have videos', function (done) { + request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(function (err, res) { + if (err) throw err + + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(0) + + done() + }) + }) + + it('Should upload the video', function (done) { + this.timeout(5000) + + request(url) + .post(path) + .set('Accept', 'application/json') + .field('name', 'my super name') + .field('description', 'my super description') + .attach('input_video', __dirname + '/../fixtures/video_short.webm') + .expect(201, done) + }) + + it('Should seed the uploaded video', function (done) { + // Yes, this could be long + this.timeout(60000) + + request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(function (err, res) { + if (err) throw err + + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(1) + + var video = res.body[0] + expect(video.name).to.equal('my super name') + expect(video.description).to.equal('my super description') + expect(video.podUrl).to.equal('http://localhost:9001') + expect(video.magnetUri).to.exist + + video_id = video._id + + webtorrent.add(video.magnetUri, function (torrent) { + expect(torrent.files).to.exist + expect(torrent.files.length).to.equal(1) + expect(torrent.files[0].path).to.exist.and.to.not.equal('') + + done() + }) + }) + }) + + it('Should remove the video', function (done) { + request(url) + .delete(path + '/' + video_id) + .set('Accept', 'application/json') + .expect(204) + .end(function (err, res) { + if (err) throw err + done() + }) + }) + + it('Should not have videos', function (done) { + request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(function (err, res) { + if (err) throw err + + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(0) + + done() + }) + }) + + after(function (done) { + process.kill(-app.pid) + process.kill(-webtorrent.app.pid) + + // Keep the logs if the test failed + if (this.ok) { + utils.flushTests(function () { + done() + }) + } else { + done() + } + }) + }) +})() diff --git a/test/fixtures/video_short.webm b/test/fixtures/video_short.webm new file mode 100644 index 000000000..bf4b0ab6c Binary files /dev/null and b/test/fixtures/video_short.webm differ diff --git a/test/fixtures/video_short1.webm b/test/fixtures/video_short1.webm new file mode 100644 index 000000000..70ac0c644 Binary files /dev/null and b/test/fixtures/video_short1.webm differ diff --git a/test/fixtures/video_short2.webm b/test/fixtures/video_short2.webm new file mode 100644 index 000000000..13d72dff7 Binary files /dev/null and b/test/fixtures/video_short2.webm differ diff --git a/test/fixtures/video_short3.webm b/test/fixtures/video_short3.webm new file mode 100644 index 000000000..cde5dcd58 Binary files /dev/null and b/test/fixtures/video_short3.webm differ diff --git a/test/utils.js b/test/utils.js new file mode 100644 index 000000000..69f43d731 --- /dev/null +++ b/test/utils.js @@ -0,0 +1,81 @@ +;(function () { + 'use strict' + + var child_process = require('child_process') + var fork = child_process.fork + var exec = child_process.exec + + function runMultipleServers (total_servers, serversRun) { + var apps = [] + var urls = [] + var i = 0 + + function anotherServerDone (number, app, url) { + apps[number - 1] = app + urls[number - 1] = url + i++ + if (i === total_servers) { + serversRun(apps, urls) + } + } + + flushTests(function () { + for (var j = 1; j <= total_servers; j++) { + (function (k) { // TODO: ES6 with let + // For the virtual buffer + setTimeout(function () { + runServer(k, function (app, url) { + anotherServerDone(k, app, url) + }) + }, 1000 * k) + })(j) + } + }) + } + + function runServer (number, callback) { + var port = 9000 + number + var server_run_string = { + 'Connected to mongodb': false, + 'Server listening on port': false + } + + // Share the environment + var env = Object.create(process.env) + env.NODE_ENV = 'test' + env.NODE_APP_INSTANCE = number + var options = { + silent: true, + env: env, + detached: true + } + + var app = fork(__dirname + '/../server.js', [], options) + app.stdout.on('data', function onStdout (data) { + var dont_continue = false + // Check if all required sentences are here + for (var key of Object.keys(server_run_string)) { + if (data.toString().indexOf(key) !== -1) server_run_string[key] = true + if (server_run_string[key] === false) dont_continue = true + } + + // If no, there is maybe one thing not already initialized (mongodb...) + if (dont_continue === true) return + + app.stdout.removeListener('data', onStdout) + callback(app, 'http://localhost:' + port) + }) + } + + function flushTests (callback) { + exec(__dirname + '/../scripts/clean_test.sh', function () { + callback() + }) + } + + module.exports = { + runMultipleServers: runMultipleServers, + runServer: runServer, + flushTests: flushTests + } +})() -- cgit v1.2.3