MigrationPersistorTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 send a stream to the primary', function () {
  177. expect(primaryPersistor.sendStream).to.have.been.calledWithExactly(
  178. bucket,
  179. key,
  180. sinon.match.instanceOf(Stream.PassThrough)
  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. await 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. await 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 send the file to the primary', function () {
  416. expect(primaryPersistor.sendStream).to.have.been.calledWithExactly(
  417. bucket,
  418. destKey,
  419. sinon.match.instanceOf(Stream.PassThrough)
  420. )
  421. })
  422. })
  423. describe('when the file does not exist on the fallback', function () {
  424. let primaryPersistor, fallbackPersistor, migrationPersistor, error
  425. beforeEach(async function () {
  426. primaryPersistor = newPersistor(false)
  427. fallbackPersistor = newPersistor(false)
  428. migrationPersistor = new MigrationPersistor(
  429. primaryPersistor,
  430. fallbackPersistor,
  431. Settings
  432. )
  433. try {
  434. await migrationPersistor.copyObject(bucket, key, destKey)
  435. } catch (err) {
  436. error = err
  437. }
  438. })
  439. it('should call copyObject to copy the file', function () {
  440. expect(primaryPersistor.copyObject).to.have.been.calledWithExactly(
  441. bucket,
  442. key,
  443. destKey
  444. )
  445. })
  446. it('should fetch the file from the fallback', function () {
  447. expect(
  448. fallbackPersistor.getObjectStream
  449. ).not.to.have.been.calledWithExactly(fallbackBucket, key)
  450. })
  451. it('should return a not-found error', function () {
  452. expect(error).to.be.an.instanceOf(Errors.NotFoundError)
  453. })
  454. })
  455. })
  456. })