MigrationPersistorTests.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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(primaryPersistor.sendStream).to.have.been.calledWithExactly(
  175. bucket,
  176. key,
  177. sinon.match.instanceOf(Stream.PassThrough),
  178. { sourceMd5: md5 }
  179. )
  180. })
  181. it('should send a stream to the client', function () {
  182. expect(returnedStream).to.be.an.instanceOf(Stream.PassThrough)
  183. })
  184. })
  185. describe('when neither persistor has the file', function () {
  186. it('rejects with a NotFoundError', async function () {
  187. const migrationPersistor = new MigrationPersistor(
  188. newPersistor(false),
  189. newPersistor(false),
  190. Settings
  191. )
  192. return expect(
  193. migrationPersistor.getObjectStream(bucket, key)
  194. ).to.eventually.be.rejected.and.be.an.instanceOf(Errors.NotFoundError)
  195. })
  196. })
  197. describe('when the primary persistor throws an unexpected error', function () {
  198. let primaryPersistor, fallbackPersistor, migrationPersistor, error
  199. beforeEach(async function () {
  200. primaryPersistor = newPersistor(false)
  201. fallbackPersistor = newPersistor(true)
  202. primaryPersistor.getObjectStream = sinon.stub().rejects(genericError)
  203. migrationPersistor = new MigrationPersistor(
  204. primaryPersistor,
  205. fallbackPersistor,
  206. Settings
  207. )
  208. try {
  209. await migrationPersistor.getObjectStream(bucket, key, options)
  210. } catch (err) {
  211. error = err
  212. }
  213. })
  214. it('rejects with the error', function () {
  215. expect(error).to.equal(genericError)
  216. })
  217. it('does not call the fallback', function () {
  218. expect(fallbackPersistor.getObjectStream).not.to.have.been.called
  219. })
  220. })
  221. describe('when the fallback persistor throws an unexpected error', function () {
  222. let primaryPersistor, fallbackPersistor, migrationPersistor, error
  223. beforeEach(async function () {
  224. primaryPersistor = newPersistor(false)
  225. fallbackPersistor = newPersistor(false)
  226. fallbackPersistor.getObjectStream = sinon.stub().rejects(genericError)
  227. migrationPersistor = new MigrationPersistor(
  228. primaryPersistor,
  229. fallbackPersistor,
  230. Settings
  231. )
  232. try {
  233. await migrationPersistor.getObjectStream(bucket, key, options)
  234. } catch (err) {
  235. error = err
  236. }
  237. })
  238. it('rejects with the error', function () {
  239. expect(error).to.equal(genericError)
  240. })
  241. it('should have called the fallback', function () {
  242. expect(fallbackPersistor.getObjectStream).to.have.been.calledWith(
  243. fallbackBucket,
  244. key
  245. )
  246. })
  247. })
  248. })
  249. describe('sendStream', function () {
  250. let primaryPersistor, fallbackPersistor, migrationPersistor
  251. beforeEach(function () {
  252. primaryPersistor = newPersistor(false)
  253. fallbackPersistor = newPersistor(false)
  254. migrationPersistor = new MigrationPersistor(
  255. primaryPersistor,
  256. fallbackPersistor,
  257. Settings
  258. )
  259. })
  260. describe('when it works', function () {
  261. beforeEach(async function () {
  262. return migrationPersistor.sendStream(bucket, key, fileStream)
  263. })
  264. it('should send the file to the primary persistor', function () {
  265. expect(primaryPersistor.sendStream).to.have.been.calledWithExactly(
  266. bucket,
  267. key,
  268. fileStream
  269. )
  270. })
  271. it('should not send the file to the fallback persistor', function () {
  272. expect(fallbackPersistor.sendStream).not.to.have.been.called
  273. })
  274. })
  275. describe('when the primary persistor throws an error', function () {
  276. it('returns the error', async function () {
  277. primaryPersistor.sendStream.rejects(notFoundError)
  278. return expect(
  279. migrationPersistor.sendStream(bucket, key, fileStream)
  280. ).to.eventually.be.rejected.and.be.an.instanceOf(Errors.NotFoundError)
  281. })
  282. })
  283. })
  284. describe('deleteObject', function () {
  285. let primaryPersistor, fallbackPersistor, migrationPersistor
  286. beforeEach(function () {
  287. primaryPersistor = newPersistor(false)
  288. fallbackPersistor = newPersistor(false)
  289. migrationPersistor = new MigrationPersistor(
  290. primaryPersistor,
  291. fallbackPersistor,
  292. Settings
  293. )
  294. })
  295. describe('when it works', function () {
  296. beforeEach(async function () {
  297. return migrationPersistor.deleteObject(bucket, key)
  298. })
  299. it('should delete the file from the primary', function () {
  300. expect(primaryPersistor.deleteObject).to.have.been.calledWithExactly(
  301. bucket,
  302. key
  303. )
  304. })
  305. it('should delete the file from the fallback', function () {
  306. expect(fallbackPersistor.deleteObject).to.have.been.calledWithExactly(
  307. fallbackBucket,
  308. key
  309. )
  310. })
  311. })
  312. describe('when the primary persistor throws an error', function () {
  313. let error
  314. beforeEach(async function () {
  315. primaryPersistor.deleteObject.rejects(genericError)
  316. try {
  317. await migrationPersistor.deleteObject(bucket, key)
  318. } catch (err) {
  319. error = err
  320. }
  321. })
  322. it('should return the error', function () {
  323. expect(error).to.equal(genericError)
  324. })
  325. it('should delete the file from the primary', function () {
  326. expect(primaryPersistor.deleteObject).to.have.been.calledWithExactly(
  327. bucket,
  328. key
  329. )
  330. })
  331. it('should delete the file from the fallback', function () {
  332. expect(fallbackPersistor.deleteObject).to.have.been.calledWithExactly(
  333. fallbackBucket,
  334. key
  335. )
  336. })
  337. })
  338. describe('when the fallback persistor throws an error', function () {
  339. let error
  340. beforeEach(async function () {
  341. fallbackPersistor.deleteObject.rejects(genericError)
  342. try {
  343. await migrationPersistor.deleteObject(bucket, key)
  344. } catch (err) {
  345. error = err
  346. }
  347. })
  348. it('should return the error', function () {
  349. expect(error).to.equal(genericError)
  350. })
  351. it('should delete the file from the primary', function () {
  352. expect(primaryPersistor.deleteObject).to.have.been.calledWithExactly(
  353. bucket,
  354. key
  355. )
  356. })
  357. it('should delete the file from the fallback', function () {
  358. expect(fallbackPersistor.deleteObject).to.have.been.calledWithExactly(
  359. fallbackBucket,
  360. key
  361. )
  362. })
  363. })
  364. })
  365. describe('copyObject', function () {
  366. describe('when the file exists on the primary', function () {
  367. let primaryPersistor, fallbackPersistor, migrationPersistor
  368. beforeEach(async function () {
  369. primaryPersistor = newPersistor(true)
  370. fallbackPersistor = newPersistor(false)
  371. migrationPersistor = new MigrationPersistor(
  372. primaryPersistor,
  373. fallbackPersistor,
  374. Settings
  375. )
  376. return migrationPersistor.copyObject(bucket, key, destKey)
  377. })
  378. it('should call copyObject to copy the file', function () {
  379. expect(primaryPersistor.copyObject).to.have.been.calledWithExactly(
  380. bucket,
  381. key,
  382. destKey
  383. )
  384. })
  385. it('should not try to read from the fallback', function () {
  386. expect(fallbackPersistor.getObjectStream).not.to.have.been.called
  387. })
  388. })
  389. describe('when the file does not exist on the primary', function () {
  390. let primaryPersistor, fallbackPersistor, migrationPersistor
  391. beforeEach(async function () {
  392. primaryPersistor = newPersistor(false)
  393. fallbackPersistor = newPersistor(true)
  394. migrationPersistor = new MigrationPersistor(
  395. primaryPersistor,
  396. fallbackPersistor,
  397. Settings
  398. )
  399. return migrationPersistor.copyObject(bucket, key, destKey)
  400. })
  401. it('should call copyObject to copy the file', function () {
  402. expect(primaryPersistor.copyObject).to.have.been.calledWithExactly(
  403. bucket,
  404. key,
  405. destKey
  406. )
  407. })
  408. it('should fetch the file from the fallback', function () {
  409. expect(
  410. fallbackPersistor.getObjectStream
  411. ).not.to.have.been.calledWithExactly(fallbackBucket, key)
  412. })
  413. it('should get the md5 hash from the source', function () {
  414. expect(fallbackPersistor.getObjectMd5Hash).to.have.been.calledWith(
  415. fallbackBucket,
  416. key
  417. )
  418. })
  419. it('should send the file to the primary', function () {
  420. expect(primaryPersistor.sendStream).to.have.been.calledWithExactly(
  421. bucket,
  422. destKey,
  423. sinon.match.instanceOf(Stream.PassThrough),
  424. { sourceMd5: md5 }
  425. )
  426. })
  427. })
  428. describe('when the file does not exist on the fallback', function () {
  429. let primaryPersistor, fallbackPersistor, migrationPersistor, error
  430. beforeEach(async function () {
  431. primaryPersistor = newPersistor(false)
  432. fallbackPersistor = newPersistor(false)
  433. migrationPersistor = new MigrationPersistor(
  434. primaryPersistor,
  435. fallbackPersistor,
  436. Settings
  437. )
  438. try {
  439. await migrationPersistor.copyObject(bucket, key, destKey)
  440. } catch (err) {
  441. error = err
  442. }
  443. })
  444. it('should call copyObject to copy the file', function () {
  445. expect(primaryPersistor.copyObject).to.have.been.calledWithExactly(
  446. bucket,
  447. key,
  448. destKey
  449. )
  450. })
  451. it('should fetch the file from the fallback', function () {
  452. expect(
  453. fallbackPersistor.getObjectStream
  454. ).not.to.have.been.calledWithExactly(fallbackBucket, key)
  455. })
  456. it('should return a not-found error', function () {
  457. expect(error).to.be.an.instanceOf(Errors.NotFoundError)
  458. })
  459. })
  460. })
  461. })