prune-storage.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* tslint:disable:no-unused-expression */
  2. import 'mocha'
  3. import * as chai from 'chai'
  4. import { waitJobs } from '../../../shared/extra-utils/server/jobs'
  5. import {
  6. buildServerDirectory,
  7. cleanupTests,
  8. createVideoPlaylist,
  9. doubleFollow,
  10. execCLI,
  11. flushAndRunMultipleServers,
  12. getAccount,
  13. getEnvCli, makeGetRequest, makeRawRequest,
  14. ServerInfo,
  15. setAccessTokensToServers, setDefaultVideoChannel,
  16. updateMyAvatar,
  17. uploadVideo,
  18. wait
  19. } from '../../../shared/extra-utils'
  20. import { Account, VideoPlaylistPrivacy } from '../../../shared/models'
  21. import { createFile, readdir } from 'fs-extra'
  22. import * as uuidv4 from 'uuid/v4'
  23. import { join } from 'path'
  24. import * as request from 'supertest'
  25. const expect = chai.expect
  26. async function countFiles (internalServerNumber: number, directory: string) {
  27. const files = await readdir(buildServerDirectory(internalServerNumber, directory))
  28. return files.length
  29. }
  30. async function assertNotExists (internalServerNumber: number, directory: string, substring: string) {
  31. const files = await readdir(buildServerDirectory(internalServerNumber, directory))
  32. for (const f of files) {
  33. expect(f).to.not.contain(substring)
  34. }
  35. }
  36. async function assertCountAreOkay (servers: ServerInfo[]) {
  37. for (const server of servers) {
  38. const videosCount = await countFiles(server.internalServerNumber, 'videos')
  39. expect(videosCount).to.equal(8)
  40. const torrentsCount = await countFiles(server.internalServerNumber, 'torrents')
  41. expect(torrentsCount).to.equal(16)
  42. const previewsCount = await countFiles(server.internalServerNumber, 'previews')
  43. expect(previewsCount).to.equal(2)
  44. const thumbnailsCount = await countFiles(server.internalServerNumber, 'thumbnails')
  45. expect(thumbnailsCount).to.equal(6)
  46. const avatarsCount = await countFiles(server.internalServerNumber, 'avatars')
  47. expect(avatarsCount).to.equal(2)
  48. }
  49. }
  50. describe('Test prune storage scripts', function () {
  51. let servers: ServerInfo[]
  52. const badNames: { [ directory: string ]: string[] } = {}
  53. before(async function () {
  54. this.timeout(120000)
  55. servers = await flushAndRunMultipleServers(2, { transcoding: { enabled: true } })
  56. await setAccessTokensToServers(servers)
  57. await setDefaultVideoChannel(servers)
  58. for (const server of servers) {
  59. await uploadVideo(server.url, server.accessToken, { name: 'video 1' })
  60. await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
  61. await updateMyAvatar({ url: server.url, accessToken: server.accessToken, fixture: 'avatar.png' })
  62. await createVideoPlaylist({
  63. url: server.url,
  64. token: server.accessToken,
  65. playlistAttrs: {
  66. displayName: 'playlist',
  67. privacy: VideoPlaylistPrivacy.PUBLIC,
  68. videoChannelId: server.videoChannel.id,
  69. thumbnailfile: 'thumbnail.jpg'
  70. }
  71. })
  72. }
  73. await doubleFollow(servers[0], servers[1])
  74. // Lazy load the remote avatar
  75. {
  76. const res = await getAccount(servers[ 0 ].url, 'root@localhost:' + servers[ 1 ].port)
  77. const account: Account = res.body
  78. await makeGetRequest({
  79. url: servers[ 0 ].url,
  80. path: account.avatar.path,
  81. statusCodeExpected: 200
  82. })
  83. }
  84. {
  85. const res = await getAccount(servers[ 1 ].url, 'root@localhost:' + servers[ 0 ].port)
  86. const account: Account = res.body
  87. await makeGetRequest({
  88. url: servers[ 1 ].url,
  89. path: account.avatar.path,
  90. statusCodeExpected: 200
  91. })
  92. }
  93. await wait(1000)
  94. await waitJobs(servers)
  95. })
  96. it('Should have the files on the disk', async function () {
  97. await assertCountAreOkay(servers)
  98. })
  99. it('Should create some dirty files', async function () {
  100. for (let i = 0; i < 2; i++) {
  101. {
  102. const base = buildServerDirectory(servers[0].internalServerNumber, 'videos')
  103. const n1 = uuidv4() + '.mp4'
  104. const n2 = uuidv4() + '.webm'
  105. await createFile(join(base, n1))
  106. await createFile(join(base, n2))
  107. badNames['videos'] = [ n1, n2 ]
  108. }
  109. {
  110. const base = buildServerDirectory(servers[0].internalServerNumber, 'torrents')
  111. const n1 = uuidv4() + '-240.torrent'
  112. const n2 = uuidv4() + '-480.torrent'
  113. await createFile(join(base, n1))
  114. await createFile(join(base, n2))
  115. badNames['torrents'] = [ n1, n2 ]
  116. }
  117. {
  118. const base = buildServerDirectory(servers[0].internalServerNumber, 'thumbnails')
  119. const n1 = uuidv4() + '.jpg'
  120. const n2 = uuidv4() + '.jpg'
  121. await createFile(join(base, n1))
  122. await createFile(join(base, n2))
  123. badNames['thumbnails'] = [ n1, n2 ]
  124. }
  125. {
  126. const base = buildServerDirectory(servers[0].internalServerNumber, 'previews')
  127. const n1 = uuidv4() + '.jpg'
  128. const n2 = uuidv4() + '.jpg'
  129. await createFile(join(base, n1))
  130. await createFile(join(base, n2))
  131. badNames['previews'] = [ n1, n2 ]
  132. }
  133. {
  134. const base = buildServerDirectory(servers[0].internalServerNumber, 'avatars')
  135. const n1 = uuidv4() + '.png'
  136. const n2 = uuidv4() + '.jpg'
  137. await createFile(join(base, n1))
  138. await createFile(join(base, n2))
  139. badNames['avatars'] = [ n1, n2 ]
  140. }
  141. }
  142. })
  143. it('Should run prune storage', async function () {
  144. this.timeout(30000)
  145. const env = getEnvCli(servers[0])
  146. await execCLI(`echo y | ${env} npm run prune-storage`)
  147. })
  148. it('Should have removed files', async function () {
  149. await assertCountAreOkay(servers)
  150. for (const directory of Object.keys(badNames)) {
  151. for (const name of badNames[directory]) {
  152. await assertNotExists(servers[0].internalServerNumber, directory, name)
  153. }
  154. }
  155. })
  156. after(async function () {
  157. await cleanupTests(servers)
  158. })
  159. })