FSPersistorManagerTests.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. const sinon = require('sinon')
  2. const chai = require('chai')
  3. const { expect } = chai
  4. const SandboxedModule = require('sandboxed-module')
  5. const Errors = require('../../../app/js/Errors')
  6. chai.use(require('sinon-chai'))
  7. chai.use(require('chai-as-promised'))
  8. const modulePath = '../../../app/js/FSPersistorManager.js'
  9. describe('FSPersistorManagerTests', function() {
  10. const stat = { size: 4, isFile: sinon.stub().returns(true) }
  11. const fd = 1234
  12. const readStream = 'readStream'
  13. const writeStream = 'writeStream'
  14. const remoteStream = 'remoteStream'
  15. const tempFile = '/tmp/potato.txt'
  16. const location = '/foo'
  17. const error = new Error('guru meditation error')
  18. const files = ['animals/wombat.tex', 'vegetables/potato.tex']
  19. const globs = [`${location}/${files[0]}`, `${location}/${files[1]}`]
  20. const filteredFilenames = ['animals_wombat.tex', 'vegetables_potato.tex']
  21. let fs, rimraf, stream, LocalFileWriter, FSPersistorManager, glob
  22. beforeEach(function() {
  23. fs = {
  24. createReadStream: sinon.stub().returns(readStream),
  25. createWriteStream: sinon.stub().returns(writeStream),
  26. unlink: sinon.stub().yields(),
  27. open: sinon.stub().yields(null, fd),
  28. stat: sinon.stub().yields(null, stat)
  29. }
  30. glob = sinon.stub().yields(null, globs)
  31. rimraf = sinon.stub().yields()
  32. stream = { pipeline: sinon.stub().yields() }
  33. LocalFileWriter = {
  34. promises: {
  35. writeStream: sinon.stub().resolves(tempFile),
  36. deleteFile: sinon.stub().resolves()
  37. }
  38. }
  39. FSPersistorManager = SandboxedModule.require(modulePath, {
  40. requires: {
  41. './LocalFileWriter': LocalFileWriter,
  42. 'logger-sharelatex': {
  43. log() {},
  44. err() {}
  45. },
  46. './Errors': Errors,
  47. fs,
  48. glob,
  49. rimraf,
  50. stream
  51. },
  52. globals: { console }
  53. })
  54. })
  55. describe('sendFile', function() {
  56. const localFilesystemPath = '/path/to/local/file'
  57. it('should copy the file', async function() {
  58. await FSPersistorManager.promises.sendFile(
  59. location,
  60. files[0],
  61. localFilesystemPath
  62. )
  63. expect(fs.createReadStream).to.have.been.calledWith(localFilesystemPath)
  64. expect(fs.createWriteStream).to.have.been.calledWith(
  65. `${location}/${filteredFilenames[0]}`
  66. )
  67. expect(stream.pipeline).to.have.been.calledWith(readStream, writeStream)
  68. })
  69. it('should return an error if the file cannot be stored', async function() {
  70. stream.pipeline.yields(error)
  71. await expect(
  72. FSPersistorManager.promises.sendFile(
  73. location,
  74. files[0],
  75. localFilesystemPath
  76. )
  77. ).to.eventually.be.rejected.and.have.property('cause', error)
  78. })
  79. })
  80. describe('sendStream', function() {
  81. it('should send the stream to LocalFileWriter', async function() {
  82. await FSPersistorManager.promises.sendStream(
  83. location,
  84. files[0],
  85. remoteStream
  86. )
  87. expect(LocalFileWriter.promises.writeStream).to.have.been.calledWith(
  88. remoteStream
  89. )
  90. })
  91. it('should delete the temporary file', async function() {
  92. await FSPersistorManager.promises.sendStream(
  93. location,
  94. files[0],
  95. remoteStream
  96. )
  97. expect(LocalFileWriter.promises.deleteFile).to.have.been.calledWith(
  98. tempFile
  99. )
  100. })
  101. it('should return the error from LocalFileWriter', async function() {
  102. LocalFileWriter.promises.writeStream.rejects(error)
  103. await expect(
  104. FSPersistorManager.promises.sendStream(location, files[0], remoteStream)
  105. ).to.eventually.be.rejectedWith(error)
  106. })
  107. it('should send the temporary file to the filestore', async function() {
  108. await FSPersistorManager.promises.sendStream(
  109. location,
  110. files[0],
  111. remoteStream
  112. )
  113. expect(fs.createReadStream).to.have.been.calledWith(tempFile)
  114. })
  115. })
  116. describe('getFileStream', function() {
  117. it('should use correct file location', async function() {
  118. await FSPersistorManager.promises.getFileStream(location, files[0], {})
  119. expect(fs.open).to.have.been.calledWith(
  120. `${location}/${filteredFilenames[0]}`
  121. )
  122. })
  123. it('should pass the options to createReadStream', async function() {
  124. await FSPersistorManager.promises.getFileStream(location, files[0], {
  125. start: 0,
  126. end: 8
  127. })
  128. expect(fs.createReadStream).to.have.been.calledWith(null, {
  129. start: 0,
  130. end: 8,
  131. fd
  132. })
  133. })
  134. it('should give a NotFoundError if the file does not exist', async function() {
  135. const err = new Error()
  136. err.code = 'ENOENT'
  137. fs.open.yields(err)
  138. await expect(
  139. FSPersistorManager.promises.getFileStream(location, files[0], {})
  140. )
  141. .to.eventually.be.rejected.and.be.an.instanceOf(Errors.NotFoundError)
  142. .and.have.property('cause', err)
  143. })
  144. it('should wrap any other error', async function() {
  145. fs.open.yields(error)
  146. await expect(
  147. FSPersistorManager.promises.getFileStream(location, files[0], {})
  148. )
  149. .to.eventually.be.rejectedWith('failed to open file for streaming')
  150. .and.be.an.instanceOf(Errors.ReadError)
  151. .and.have.property('cause', error)
  152. })
  153. })
  154. describe('getFileSize', function() {
  155. const badFilename = 'neenaw.tex'
  156. const size = 65536
  157. const noentError = new Error('not found')
  158. noentError.code = 'ENOENT'
  159. beforeEach(function() {
  160. fs.stat
  161. .yields(error)
  162. .withArgs(`${location}/${filteredFilenames[0]}`)
  163. .yields(null, { size })
  164. .withArgs(`${location}/${badFilename}`)
  165. .yields(noentError)
  166. })
  167. it('should return the file size', async function() {
  168. expect(
  169. await FSPersistorManager.promises.getFileSize(location, files[0])
  170. ).to.equal(size)
  171. })
  172. it('should throw a NotFoundError if the file does not exist', async function() {
  173. await expect(
  174. FSPersistorManager.promises.getFileSize(location, badFilename)
  175. ).to.eventually.be.rejected.and.be.an.instanceOf(Errors.NotFoundError)
  176. })
  177. it('should wrap any other error', async function() {
  178. await expect(FSPersistorManager.promises.getFileSize(location, 'raccoon'))
  179. .to.eventually.be.rejected.and.be.an.instanceOf(Errors.ReadError)
  180. .and.have.property('cause', error)
  181. })
  182. })
  183. describe('copyFile', function() {
  184. it('Should open the source for reading', async function() {
  185. await FSPersistorManager.promises.copyFile(location, files[0], files[1])
  186. expect(fs.createReadStream).to.have.been.calledWith(
  187. `${location}/${filteredFilenames[0]}`
  188. )
  189. })
  190. it('Should open the target for writing', async function() {
  191. await FSPersistorManager.promises.copyFile(location, files[0], files[1])
  192. expect(fs.createWriteStream).to.have.been.calledWith(
  193. `${location}/${filteredFilenames[1]}`
  194. )
  195. })
  196. it('Should pipe the source to the target', async function() {
  197. await FSPersistorManager.promises.copyFile(location, files[0], files[1])
  198. expect(stream.pipeline).to.have.been.calledWith(readStream, writeStream)
  199. })
  200. })
  201. describe('deleteFile', function() {
  202. it('Should call unlink with correct options', async function() {
  203. await FSPersistorManager.promises.deleteFile(location, files[0])
  204. expect(fs.unlink).to.have.been.calledWith(
  205. `${location}/${filteredFilenames[0]}`
  206. )
  207. })
  208. it('Should propagate the error', async function() {
  209. fs.unlink.yields(error)
  210. await expect(
  211. FSPersistorManager.promises.deleteFile(location, files[0])
  212. ).to.eventually.be.rejected.and.have.property('cause', error)
  213. })
  214. })
  215. describe('deleteDirectory', function() {
  216. it('Should call rmdir(rimraf) with correct options', async function() {
  217. await FSPersistorManager.promises.deleteDirectory(location, files[0])
  218. expect(rimraf).to.have.been.calledWith(
  219. `${location}/${filteredFilenames[0]}`
  220. )
  221. })
  222. it('Should propagate the error', async function() {
  223. rimraf.yields(error)
  224. await expect(
  225. FSPersistorManager.promises.deleteDirectory(location, files[0])
  226. ).to.eventually.be.rejected.and.have.property('cause', error)
  227. })
  228. })
  229. describe('checkIfFileExists', function() {
  230. const badFilename = 'pototo'
  231. const noentError = new Error('not found')
  232. noentError.code = 'ENOENT'
  233. beforeEach(function() {
  234. fs.stat
  235. .yields(error)
  236. .withArgs(`${location}/${filteredFilenames[0]}`)
  237. .yields(null, {})
  238. .withArgs(`${location}/${badFilename}`)
  239. .yields(noentError)
  240. })
  241. it('Should call stat with correct options', async function() {
  242. await FSPersistorManager.promises.checkIfFileExists(location, files[0])
  243. expect(fs.stat).to.have.been.calledWith(
  244. `${location}/${filteredFilenames[0]}`
  245. )
  246. })
  247. it('Should return true for existing files', async function() {
  248. expect(
  249. await FSPersistorManager.promises.checkIfFileExists(location, files[0])
  250. ).to.equal(true)
  251. })
  252. it('Should return false for non-existing files', async function() {
  253. expect(
  254. await FSPersistorManager.promises.checkIfFileExists(
  255. location,
  256. badFilename
  257. )
  258. ).to.equal(false)
  259. })
  260. it('should wrap the error if there is a problem', async function() {
  261. await expect(
  262. FSPersistorManager.promises.checkIfFileExists(location, 'llama')
  263. )
  264. .to.eventually.be.rejected.and.be.an.instanceOf(Errors.ReadError)
  265. .and.have.property('cause', error)
  266. })
  267. })
  268. describe('directorySize', function() {
  269. it('should wrap the error', async function() {
  270. glob.yields(error)
  271. await expect(
  272. FSPersistorManager.promises.directorySize(location, files[0])
  273. )
  274. .to.eventually.be.rejected.and.be.an.instanceOf(Errors.ReadError)
  275. .and.include({ cause: error })
  276. .and.have.property('info')
  277. .which.includes({ location, name: files[0] })
  278. })
  279. it('should filter the directory name', async function() {
  280. await FSPersistorManager.promises.directorySize(location, files[0])
  281. expect(glob).to.have.been.calledWith(
  282. `${location}/${filteredFilenames[0]}_*`
  283. )
  284. })
  285. it('should sum directory files size', async function() {
  286. expect(
  287. await FSPersistorManager.promises.directorySize(location, files[0])
  288. ).to.equal(stat.size * files.length)
  289. })
  290. })
  291. })