MigrationPersistorTests.js 16 KB

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