MigrationPersistorTests.js 16 KB

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