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