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