diff options
Diffstat (limited to 'test/api/multiplePods.js')
-rw-r--r-- | test/api/multiplePods.js | 326 |
1 files changed, 326 insertions, 0 deletions
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 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var request = require('supertest') | ||
5 | var chai = require('chai') | ||
6 | var expect = chai.expect | ||
7 | var async = require('async') | ||
8 | |||
9 | var utils = require('../utils') | ||
10 | var webtorrent = require(__dirname + '/../../src/webTorrentNode') | ||
11 | webtorrent.silent = true | ||
12 | |||
13 | describe('Test multiple pods', function () { | ||
14 | var path = '/api/videos' | ||
15 | var apps = [] | ||
16 | var urls = [] | ||
17 | var video_id = -1 | ||
18 | |||
19 | function getVideosList (url, end) { | ||
20 | request(url) | ||
21 | .get(path) | ||
22 | .set('Accept', 'application/json') | ||
23 | .expect(200) | ||
24 | .expect('Content-Type', /json/) | ||
25 | .end(end) | ||
26 | } | ||
27 | |||
28 | function uploadVideo (url, name, description, fixture, end) { | ||
29 | request(url) | ||
30 | .post(path) | ||
31 | .set('Accept', 'application/json') | ||
32 | .field('name', name) | ||
33 | .field('description', description) | ||
34 | .attach('input_video', __dirname + '/../fixtures/' + fixture) | ||
35 | .expect(201) | ||
36 | .end(end) | ||
37 | } | ||
38 | |||
39 | before(function (done) { | ||
40 | this.timeout(20000) | ||
41 | var path_friends = '/api/pods/makefriends' | ||
42 | |||
43 | utils.runMultipleServers(3, function (apps_run, urls_run) { | ||
44 | apps = apps_run | ||
45 | urls = urls_run | ||
46 | |||
47 | // The second pod make friend with the third | ||
48 | request(urls[1]) | ||
49 | .get(path_friends) | ||
50 | .set('Accept', 'application/json') | ||
51 | .expect(204) | ||
52 | .end(function (err, res) { | ||
53 | if (err) throw err | ||
54 | |||
55 | // Wait for the request between pods | ||
56 | setTimeout(function () { | ||
57 | request(urls[0]) | ||
58 | .get(path_friends) | ||
59 | .set('Accept', 'application/json') | ||
60 | .expect(204) | ||
61 | .end(function (err, res) { | ||
62 | if (err) throw err | ||
63 | |||
64 | webtorrent.create(function () { | ||
65 | done() | ||
66 | }) | ||
67 | }) | ||
68 | }, 1000) | ||
69 | }) | ||
70 | }) | ||
71 | }) | ||
72 | |||
73 | it('Should not have videos for all pods', function (done) { | ||
74 | async.each(urls, function (url, callback) { | ||
75 | getVideosList(url, function (err, res) { | ||
76 | if (err) throw err | ||
77 | |||
78 | expect(res.body).to.be.an('array') | ||
79 | expect(res.body.length).to.equal(0) | ||
80 | |||
81 | callback() | ||
82 | }) | ||
83 | }, function (err) { | ||
84 | if (err) throw err | ||
85 | |||
86 | done() | ||
87 | }) | ||
88 | }) | ||
89 | |||
90 | describe('Should upload the video and propagate on each pod', function () { | ||
91 | it('Should upload the video on pod 1 and propagate on each pod', function (done) { | ||
92 | this.timeout(5000) | ||
93 | |||
94 | uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', function (err) { | ||
95 | if (err) throw err | ||
96 | |||
97 | setTimeout(function () { | ||
98 | // All pods should have this video | ||
99 | async.each(urls, function (url, callback) { | ||
100 | var base_magnet = null | ||
101 | |||
102 | getVideosList(url, function (err, res) { | ||
103 | if (err) throw err | ||
104 | |||
105 | var videos = res.body | ||
106 | expect(videos).to.be.an('array') | ||
107 | expect(videos.length).to.equal(1) | ||
108 | var video = videos[0] | ||
109 | expect(video.name).to.equal('my super name for pod 1') | ||
110 | expect(video.description).to.equal('my super description for pod 1') | ||
111 | expect(video.podUrl).to.equal('http://localhost:9001') | ||
112 | expect(video.magnetUri).to.exist | ||
113 | |||
114 | // All pods should have the same magnet Uri | ||
115 | if (base_magnet === null) { | ||
116 | base_magnet = video.magnetUri | ||
117 | } else { | ||
118 | expect(video.magnetUri).to.equal.magnetUri | ||
119 | } | ||
120 | |||
121 | callback() | ||
122 | }) | ||
123 | }, function (err) { | ||
124 | if (err) throw err | ||
125 | |||
126 | done() | ||
127 | }) | ||
128 | }, 1000) | ||
129 | }) | ||
130 | }) | ||
131 | |||
132 | it('Should upload the video on pod 2 and propagate on each pod', function (done) { | ||
133 | this.timeout(5000) | ||
134 | |||
135 | uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', function (err) { | ||
136 | if (err) throw err | ||
137 | |||
138 | setTimeout(function () { | ||
139 | // All pods should have this video | ||
140 | async.each(urls, function (url, callback) { | ||
141 | var base_magnet = null | ||
142 | |||
143 | getVideosList(url, function (err, res) { | ||
144 | if (err) throw err | ||
145 | |||
146 | var videos = res.body | ||
147 | expect(videos).to.be.an('array') | ||
148 | expect(videos.length).to.equal(2) | ||
149 | var video = videos[1] | ||
150 | expect(video.name).to.equal('my super name for pod 2') | ||
151 | expect(video.description).to.equal('my super description for pod 2') | ||
152 | expect(video.podUrl).to.equal('http://localhost:9002') | ||
153 | expect(video.magnetUri).to.exist | ||
154 | |||
155 | // All pods should have the same magnet Uri | ||
156 | if (base_magnet === null) { | ||
157 | base_magnet = video.magnetUri | ||
158 | } else { | ||
159 | expect(video.magnetUri).to.equal.magnetUri | ||
160 | } | ||
161 | |||
162 | callback() | ||
163 | }) | ||
164 | }, function (err) { | ||
165 | if (err) throw err | ||
166 | |||
167 | done() | ||
168 | }) | ||
169 | }, 1000) | ||
170 | }) | ||
171 | }) | ||
172 | |||
173 | it('Should upload the video on pod 3 and propagate on each pod', function (done) { | ||
174 | this.timeout(5000) | ||
175 | |||
176 | uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', function (err) { | ||
177 | if (err) throw err | ||
178 | |||
179 | setTimeout(function () { | ||
180 | var base_magnet = null | ||
181 | // All pods should have this video | ||
182 | async.each(urls, function (url, callback) { | ||
183 | getVideosList(url, function (err, res) { | ||
184 | if (err) throw err | ||
185 | |||
186 | var videos = res.body | ||
187 | expect(videos).to.be.an('array') | ||
188 | expect(videos.length).to.equal(3) | ||
189 | var video = videos[2] | ||
190 | expect(video.name).to.equal('my super name for pod 3') | ||
191 | expect(video.description).to.equal('my super description for pod 3') | ||
192 | expect(video.podUrl).to.equal('http://localhost:9003') | ||
193 | expect(video.magnetUri).to.exist | ||
194 | |||
195 | // All pods should have the same magnet Uri | ||
196 | if (base_magnet === null) { | ||
197 | base_magnet = video.magnetUri | ||
198 | } else { | ||
199 | expect(video.magnetUri).to.equal.magnetUri | ||
200 | } | ||
201 | |||
202 | callback() | ||
203 | }) | ||
204 | }, function (err) { | ||
205 | if (err) throw err | ||
206 | |||
207 | done() | ||
208 | }) | ||
209 | }, 1000) | ||
210 | }) | ||
211 | }) | ||
212 | }) | ||
213 | |||
214 | describe('Should seed the uploaded video', function () { | ||
215 | it('Should add the file 1 by asking pod 3', function (done) { | ||
216 | // Yes, this could be long | ||
217 | this.timeout(60000) | ||
218 | |||
219 | getVideosList(urls[2], function (err, res) { | ||
220 | if (err) throw err | ||
221 | |||
222 | var video = res.body[0] | ||
223 | webtorrent.add(video.magnetUri, function (torrent) { | ||
224 | expect(torrent.files).to.exist | ||
225 | expect(torrent.files.length).to.equal(1) | ||
226 | expect(torrent.files[0].path).to.exist.and.to.not.equal('') | ||
227 | |||
228 | done() | ||
229 | }) | ||
230 | }) | ||
231 | }) | ||
232 | |||
233 | it('Should add the file 2 by asking pod 1', function (done) { | ||
234 | // Yes, this could be long | ||
235 | this.timeout(60000) | ||
236 | |||
237 | getVideosList(urls[0], function (err, res) { | ||
238 | if (err) throw err | ||
239 | |||
240 | var video = res.body[1] | ||
241 | |||
242 | video_id = video._id | ||
243 | |||
244 | webtorrent.add(video.magnetUri, function (torrent) { | ||
245 | expect(torrent.files).to.exist | ||
246 | expect(torrent.files.length).to.equal(1) | ||
247 | expect(torrent.files[0].path).to.exist.and.to.not.equal('') | ||
248 | |||
249 | done() | ||
250 | }) | ||
251 | }) | ||
252 | }) | ||
253 | |||
254 | it('Should add the file 3 by asking pod 2', function (done) { | ||
255 | // Yes, this could be long | ||
256 | this.timeout(60000) | ||
257 | |||
258 | getVideosList(urls[1], function (err, res) { | ||
259 | if (err) throw err | ||
260 | |||
261 | var video = res.body[2] | ||
262 | |||
263 | webtorrent.add(video.magnetUri, function (torrent) { | ||
264 | expect(torrent.files).to.exist | ||
265 | expect(torrent.files.length).to.equal(1) | ||
266 | expect(torrent.files[0].path).to.exist.and.to.not.equal('') | ||
267 | |||
268 | done() | ||
269 | }) | ||
270 | }) | ||
271 | }) | ||
272 | |||
273 | it('Should remove the file 2 by asking pod 2', function (done) { | ||
274 | request(urls[1]) | ||
275 | .delete(path + '/' + video_id) | ||
276 | .set('Accept', 'application/json') | ||
277 | .expect(204) | ||
278 | .end(function (err, res) { | ||
279 | if (err) throw err | ||
280 | |||
281 | // Wait the propagation to the other pods | ||
282 | setTimeout(function () { | ||
283 | done() | ||
284 | }, 1000) | ||
285 | }) | ||
286 | }) | ||
287 | |||
288 | it('Should have videos 1 and 2 on each pod', function (done) { | ||
289 | async.each(urls, function (url, callback) { | ||
290 | getVideosList(url, function (err, res) { | ||
291 | if (err) throw err | ||
292 | |||
293 | var videos = res.body | ||
294 | expect(videos).to.be.an('array') | ||
295 | expect(videos.length).to.equal(2) | ||
296 | expect(videos[0]._id).not.to.equal(videos[1]._id) | ||
297 | expect(videos[0]._id).not.to.equal(video_id) | ||
298 | expect(videos[1]._id).not.to.equal(video_id) | ||
299 | |||
300 | callback() | ||
301 | }) | ||
302 | }, function (err) { | ||
303 | if (err) throw err | ||
304 | |||
305 | done() | ||
306 | }) | ||
307 | }) | ||
308 | }) | ||
309 | |||
310 | after(function (done) { | ||
311 | apps.forEach(function (app) { | ||
312 | process.kill(-app.pid) | ||
313 | }) | ||
314 | process.kill(-webtorrent.app.pid) | ||
315 | |||
316 | // Keep the logs if the test failed | ||
317 | if (this.ok) { | ||
318 | utils.flushTests(function () { | ||
319 | done() | ||
320 | }) | ||
321 | } else { | ||
322 | done() | ||
323 | } | ||
324 | }) | ||
325 | }) | ||
326 | })() | ||