MigrationPersistorTests.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. const sinon = require('sinon')
  2. const chai = require('chai')
  3. const { expect } = chai
  4. const modulePath = '../../../app/js/MigrationPersistor.js'
  5. const SandboxedModule = require('sandboxed-module')
  6. const Errors = require('../../../app/js/Errors')
  7. // Not all methods are tested here, but a method with each type of wrapping has
  8. // tests. Specifically, the following wrapping methods are tested here:
  9. // getFileStream: _wrapFallbackMethod
  10. // sendStream: forward-to-primary
  11. // deleteFile: _wrapMethodOnBothPersistors
  12. // copyFile: copyFileWithFallback
  13. describe('MigrationPersistorTests', function() {
  14. const bucket = 'womBucket'
  15. const fallbackBucket = 'bucKangaroo'
  16. const key = 'monKey'
  17. const destKey = 'donKey'
  18. const genericError = new Error('guru meditation error')
  19. const notFoundError = new Errors.NotFoundError('not found')
  20. const size = 33
  21. const md5 = 'ffffffff'
  22. let Metrics,
  23. Settings,
  24. Logger,
  25. Stream,
  26. MigrationPersistor,
  27. fileStream,
  28. newPersistor
  29. beforeEach(function() {
  30. fileStream = {
  31. name: 'fileStream',
  32. on: sinon
  33. .stub()
  34. .withArgs('end')
  35. .yields(),
  36. pipe: sinon.stub()
  37. }
  38. newPersistor = function(hasFile) {
  39. return {
  40. promises: {
  41. sendFile: sinon.stub().resolves(),
  42. sendStream: sinon.stub().resolves(),
  43. getFileStream: hasFile
  44. ? sinon.stub().resolves(fileStream)
  45. : sinon.stub().rejects(notFoundError),
  46. deleteDirectory: sinon.stub().resolves(),
  47. getFileSize: hasFile
  48. ? sinon.stub().resolves(size)
  49. : sinon.stub().rejects(notFoundError),
  50. deleteFile: sinon.stub().resolves(),
  51. copyFile: hasFile
  52. ? sinon.stub().resolves()
  53. : sinon.stub().rejects(notFoundError),
  54. checkIfFileExists: sinon.stub().resolves(hasFile),
  55. directorySize: hasFile
  56. ? sinon.stub().resolves(size)
  57. : sinon.stub().rejects(notFoundError),
  58. getFileMd5Hash: hasFile
  59. ? sinon.stub().resolves(md5)
  60. : sinon.stub().rejects(notFoundError)
  61. }
  62. }
  63. }
  64. Settings = {
  65. filestore: {
  66. fallback: {
  67. buckets: {
  68. [bucket]: fallbackBucket
  69. }
  70. }
  71. }
  72. }
  73. Metrics = {
  74. inc: sinon.stub()
  75. }
  76. Stream = {
  77. pipeline: sinon.stub().yields(),
  78. PassThrough: sinon.stub()
  79. }
  80. Logger = {
  81. warn: sinon.stub()
  82. }
  83. MigrationPersistor = SandboxedModule.require(modulePath, {
  84. requires: {
  85. 'settings-sharelatex': Settings,
  86. stream: Stream,
  87. './Errors': Errors,
  88. 'metrics-sharelatex': Metrics,
  89. 'logger-sharelatex': Logger
  90. },
  91. globals: { console }
  92. })
  93. })
  94. describe('getFileStream', function() {
  95. const options = { wombat: 'potato' }
  96. describe('when the primary persistor has the file', function() {
  97. let primaryPersistor, fallbackPersistor, migrationPersistor, response
  98. beforeEach(async function() {
  99. primaryPersistor = newPersistor(true)
  100. fallbackPersistor = newPersistor(false)
  101. migrationPersistor = MigrationPersistor(
  102. primaryPersistor,
  103. fallbackPersistor
  104. )
  105. response = await migrationPersistor.promises.getFileStream(
  106. bucket,
  107. key,
  108. options
  109. )
  110. })
  111. it('should return the file stream', function() {
  112. expect(response).to.equal(fileStream)
  113. })
  114. it('should fetch the file from the primary persistor, with the correct options', function() {
  115. expect(
  116. primaryPersistor.promises.getFileStream
  117. ).to.have.been.calledWithExactly(bucket, key, options)
  118. })
  119. it('should not query the fallback persistor', function() {
  120. expect(fallbackPersistor.promises.getFileStream).not.to.have.been.called
  121. })
  122. })
  123. describe('when the fallback persistor has the file', function() {
  124. let primaryPersistor, fallbackPersistor, migrationPersistor, response
  125. beforeEach(async function() {
  126. primaryPersistor = newPersistor(false)
  127. fallbackPersistor = newPersistor(true)
  128. migrationPersistor = MigrationPersistor(
  129. primaryPersistor,
  130. fallbackPersistor
  131. )
  132. response = await migrationPersistor.promises.getFileStream(
  133. bucket,
  134. key,
  135. options
  136. )
  137. })
  138. it('should return the file stream', function() {
  139. expect(response).to.be.an.instanceOf(Stream.PassThrough)
  140. })
  141. it('should fetch the file from the primary persistor with the correct options', function() {
  142. expect(
  143. primaryPersistor.promises.getFileStream
  144. ).to.have.been.calledWithExactly(bucket, key, options)
  145. })
  146. it('should fetch the file from the fallback persistor with the fallback bucket with the correct options', function() {
  147. expect(
  148. fallbackPersistor.promises.getFileStream
  149. ).to.have.been.calledWithExactly(fallbackBucket, key, options)
  150. })
  151. it('should create one read stream', function() {
  152. expect(fallbackPersistor.promises.getFileStream).to.have.been.calledOnce
  153. })
  154. it('should not send the file to the primary', function() {
  155. expect(primaryPersistor.promises.sendStream).not.to.have.been.called
  156. })
  157. })
  158. describe('when the file should be copied to the primary', function() {
  159. let primaryPersistor,
  160. fallbackPersistor,
  161. migrationPersistor,
  162. returnedStream
  163. beforeEach(async function() {
  164. primaryPersistor = newPersistor(false)
  165. fallbackPersistor = newPersistor(true)
  166. migrationPersistor = MigrationPersistor(
  167. primaryPersistor,
  168. fallbackPersistor
  169. )
  170. Settings.filestore.fallback.copyOnMiss = true
  171. returnedStream = await migrationPersistor.promises.getFileStream(
  172. bucket,
  173. key,
  174. options
  175. )
  176. })
  177. it('should create one read stream', function() {
  178. expect(fallbackPersistor.promises.getFileStream).to.have.been.calledOnce
  179. })
  180. it('should get the md5 hash from the source', function() {
  181. expect(
  182. fallbackPersistor.promises.getFileMd5Hash
  183. ).to.have.been.calledWith(fallbackBucket, key)
  184. })
  185. it('should send a stream to the primary', function() {
  186. expect(
  187. primaryPersistor.promises.sendStream
  188. ).to.have.been.calledWithExactly(
  189. bucket,
  190. key,
  191. sinon.match.instanceOf(Stream.PassThrough),
  192. md5
  193. )
  194. })
  195. it('should send a stream to the client', function() {
  196. expect(returnedStream).to.be.an.instanceOf(Stream.PassThrough)
  197. })
  198. })
  199. describe('when neither persistor has the file', function() {
  200. it('rejects with a NotFoundError', async function() {
  201. const migrationPersistor = MigrationPersistor(
  202. newPersistor(false),
  203. newPersistor(false)
  204. )
  205. return expect(
  206. migrationPersistor.promises.getFileStream(bucket, key)
  207. ).to.eventually.be.rejected.and.be.an.instanceOf(Errors.NotFoundError)
  208. })
  209. })
  210. describe('when the primary persistor throws an unexpected error', function() {
  211. let primaryPersistor, fallbackPersistor, migrationPersistor, error
  212. beforeEach(async function() {
  213. primaryPersistor = newPersistor(false)
  214. fallbackPersistor = newPersistor(true)
  215. primaryPersistor.promises.getFileStream = sinon
  216. .stub()
  217. .rejects(genericError)
  218. migrationPersistor = MigrationPersistor(
  219. primaryPersistor,
  220. fallbackPersistor
  221. )
  222. try {
  223. await migrationPersistor.promises.getFileStream(bucket, key, options)
  224. } catch (err) {
  225. error = err
  226. }
  227. })
  228. it('rejects with the error', function() {
  229. expect(error).to.equal(genericError)
  230. })
  231. it('does not call the fallback', function() {
  232. expect(fallbackPersistor.promises.getFileStream).not.to.have.been.called
  233. })
  234. })
  235. describe('when the fallback persistor throws an unexpected error', function() {
  236. let primaryPersistor, fallbackPersistor, migrationPersistor, error
  237. beforeEach(async function() {
  238. primaryPersistor = newPersistor(false)
  239. fallbackPersistor = newPersistor(false)
  240. fallbackPersistor.promises.getFileStream = sinon
  241. .stub()
  242. .rejects(genericError)
  243. migrationPersistor = MigrationPersistor(
  244. primaryPersistor,
  245. fallbackPersistor
  246. )
  247. try {
  248. await migrationPersistor.promises.getFileStream(bucket, key, options)
  249. } catch (err) {
  250. error = err
  251. }
  252. })
  253. it('rejects with the error', function() {
  254. expect(error).to.equal(genericError)
  255. })
  256. it('should have called the fallback', function() {
  257. expect(
  258. fallbackPersistor.promises.getFileStream
  259. ).to.have.been.calledWith(fallbackBucket, key)
  260. })
  261. })
  262. })
  263. describe('sendStream', function() {
  264. let primaryPersistor, fallbackPersistor, migrationPersistor
  265. beforeEach(function() {
  266. primaryPersistor = newPersistor(false)
  267. fallbackPersistor = newPersistor(false)
  268. migrationPersistor = MigrationPersistor(
  269. primaryPersistor,
  270. fallbackPersistor
  271. )
  272. })
  273. describe('when it works', function() {
  274. beforeEach(async function() {
  275. return migrationPersistor.promises.sendStream(bucket, key, fileStream)
  276. })
  277. it('should send the file to the primary persistor', function() {
  278. expect(
  279. primaryPersistor.promises.sendStream
  280. ).to.have.been.calledWithExactly(bucket, key, fileStream)
  281. })
  282. it('should not send the file to the fallback persistor', function() {
  283. expect(fallbackPersistor.promises.sendStream).not.to.have.been.called
  284. })
  285. })
  286. describe('when the primary persistor throws an error', function() {
  287. it('returns the error', async function() {
  288. primaryPersistor.promises.sendStream.rejects(notFoundError)
  289. return expect(
  290. migrationPersistor.promises.sendStream(bucket, key, fileStream)
  291. ).to.eventually.be.rejected.and.be.an.instanceOf(Errors.NotFoundError)
  292. })
  293. })
  294. })
  295. describe('deleteFile', function() {
  296. let primaryPersistor, fallbackPersistor, migrationPersistor
  297. beforeEach(function() {
  298. primaryPersistor = newPersistor(false)
  299. fallbackPersistor = newPersistor(false)
  300. migrationPersistor = MigrationPersistor(
  301. primaryPersistor,
  302. fallbackPersistor
  303. )
  304. })
  305. describe('when it works', function() {
  306. beforeEach(async function() {
  307. return migrationPersistor.promises.deleteFile(bucket, key)
  308. })
  309. it('should delete the file from the primary', function() {
  310. expect(
  311. primaryPersistor.promises.deleteFile
  312. ).to.have.been.calledWithExactly(bucket, key)
  313. })
  314. it('should delete the file from the fallback', function() {
  315. expect(
  316. fallbackPersistor.promises.deleteFile
  317. ).to.have.been.calledWithExactly(fallbackBucket, key)
  318. })
  319. })
  320. describe('when the primary persistor throws an error', function() {
  321. let error
  322. beforeEach(async function() {
  323. primaryPersistor.promises.deleteFile.rejects(genericError)
  324. try {
  325. await migrationPersistor.promises.deleteFile(bucket, key)
  326. } catch (err) {
  327. error = err
  328. }
  329. })
  330. it('should return the error', function() {
  331. expect(error).to.equal(genericError)
  332. })
  333. it('should delete the file from the primary', function() {
  334. expect(
  335. primaryPersistor.promises.deleteFile
  336. ).to.have.been.calledWithExactly(bucket, key)
  337. })
  338. it('should delete the file from the fallback', function() {
  339. expect(
  340. fallbackPersistor.promises.deleteFile
  341. ).to.have.been.calledWithExactly(fallbackBucket, key)
  342. })
  343. })
  344. describe('when the fallback persistor throws an error', function() {
  345. let error
  346. beforeEach(async function() {
  347. fallbackPersistor.promises.deleteFile.rejects(genericError)
  348. try {
  349. await migrationPersistor.promises.deleteFile(bucket, key)
  350. } catch (err) {
  351. error = err
  352. }
  353. })
  354. it('should return the error', function() {
  355. expect(error).to.equal(genericError)
  356. })
  357. it('should delete the file from the primary', function() {
  358. expect(
  359. primaryPersistor.promises.deleteFile
  360. ).to.have.been.calledWithExactly(bucket, key)
  361. })
  362. it('should delete the file from the fallback', function() {
  363. expect(
  364. fallbackPersistor.promises.deleteFile
  365. ).to.have.been.calledWithExactly(fallbackBucket, key)
  366. })
  367. })
  368. })
  369. describe('copyFile', function() {
  370. describe('when the file exists on the primary', function() {
  371. let primaryPersistor, fallbackPersistor, migrationPersistor
  372. beforeEach(async function() {
  373. primaryPersistor = newPersistor(true)
  374. fallbackPersistor = newPersistor(false)
  375. migrationPersistor = MigrationPersistor(
  376. primaryPersistor,
  377. fallbackPersistor
  378. )
  379. return migrationPersistor.promises.copyFile(bucket, key, destKey)
  380. })
  381. it('should call copyFile to copy the file', function() {
  382. expect(
  383. primaryPersistor.promises.copyFile
  384. ).to.have.been.calledWithExactly(bucket, key, destKey)
  385. })
  386. it('should not try to read from the fallback', function() {
  387. expect(fallbackPersistor.promises.getFileStream).not.to.have.been.called
  388. })
  389. })
  390. describe('when the file does not exist on the primary', function() {
  391. let primaryPersistor, fallbackPersistor, migrationPersistor
  392. beforeEach(async function() {
  393. primaryPersistor = newPersistor(false)
  394. fallbackPersistor = newPersistor(true)
  395. migrationPersistor = MigrationPersistor(
  396. primaryPersistor,
  397. fallbackPersistor
  398. )
  399. return migrationPersistor.promises.copyFile(bucket, key, destKey)
  400. })
  401. it('should call copyFile to copy the file', function() {
  402. expect(
  403. primaryPersistor.promises.copyFile
  404. ).to.have.been.calledWithExactly(bucket, key, destKey)
  405. })
  406. it('should fetch the file from the fallback', function() {
  407. expect(
  408. fallbackPersistor.promises.getFileStream
  409. ).not.to.have.been.calledWithExactly(fallbackBucket, key)
  410. })
  411. it('should get the md5 hash from the source', function() {
  412. expect(
  413. fallbackPersistor.promises.getFileMd5Hash
  414. ).to.have.been.calledWith(fallbackBucket, key)
  415. })
  416. it('should send the file to the primary', function() {
  417. expect(
  418. primaryPersistor.promises.sendStream
  419. ).to.have.been.calledWithExactly(
  420. bucket,
  421. destKey,
  422. sinon.match.instanceOf(Stream.PassThrough),
  423. md5
  424. )
  425. })
  426. })
  427. describe('when the file does not exist on the fallback', function() {
  428. let primaryPersistor, fallbackPersistor, migrationPersistor, error
  429. beforeEach(async function() {
  430. primaryPersistor = newPersistor(false)
  431. fallbackPersistor = newPersistor(false)
  432. migrationPersistor = MigrationPersistor(
  433. primaryPersistor,
  434. fallbackPersistor
  435. )
  436. try {
  437. await migrationPersistor.promises.copyFile(bucket, key, destKey)
  438. } catch (err) {
  439. error = err
  440. }
  441. })
  442. it('should call copyFile to copy the file', function() {
  443. expect(
  444. primaryPersistor.promises.copyFile
  445. ).to.have.been.calledWithExactly(bucket, key, destKey)
  446. })
  447. it('should fetch the file from the fallback', function() {
  448. expect(
  449. fallbackPersistor.promises.getFileStream
  450. ).not.to.have.been.calledWithExactly(fallbackBucket, key)
  451. })
  452. it('should return a not-found error', function() {
  453. expect(error).to.be.an.instanceOf(Errors.NotFoundError)
  454. })
  455. })
  456. })
  457. })