create-import-video-file-job.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import { expect } from 'chai'
  3. import { areMockObjectStorageTestsDisabled } from '@shared/core-utils'
  4. import { HttpStatusCode, VideoDetails, VideoFile, VideoInclude } from '@shared/models'
  5. import {
  6. cleanupTests,
  7. createMultipleServers,
  8. doubleFollow,
  9. makeRawRequest,
  10. ObjectStorageCommand,
  11. PeerTubeServer,
  12. setAccessTokensToServers,
  13. waitJobs
  14. } from '@shared/server-commands'
  15. import { expectStartWith } from '../shared'
  16. function assertVideoProperties (video: VideoFile, resolution: number, extname: string, size?: number) {
  17. expect(video).to.have.nested.property('resolution.id', resolution)
  18. expect(video).to.have.property('torrentUrl').that.includes(`-${resolution}.torrent`)
  19. expect(video).to.have.property('fileUrl').that.includes(`.${extname}`)
  20. expect(video).to.have.property('magnetUri').that.includes(`.${extname}`)
  21. expect(video).to.have.property('size').that.is.above(0)
  22. if (size) expect(video.size).to.equal(size)
  23. }
  24. async function checkFiles (video: VideoDetails, objectStorage: boolean) {
  25. for (const file of video.files) {
  26. if (objectStorage) expectStartWith(file.fileUrl, ObjectStorageCommand.getMockWebTorrentBaseUrl())
  27. await makeRawRequest({ url: file.fileUrl, expectedStatus: HttpStatusCode.OK_200 })
  28. }
  29. }
  30. function runTests (objectStorage: boolean) {
  31. let video1ShortId: string
  32. let video2UUID: string
  33. let servers: PeerTubeServer[] = []
  34. before(async function () {
  35. this.timeout(90000)
  36. const config = objectStorage
  37. ? ObjectStorageCommand.getDefaultMockConfig()
  38. : {}
  39. // Run server 2 to have transcoding enabled
  40. servers = await createMultipleServers(2, config)
  41. await setAccessTokensToServers(servers)
  42. await doubleFollow(servers[0], servers[1])
  43. if (objectStorage) await ObjectStorageCommand.prepareDefaultMockBuckets()
  44. // Upload two videos for our needs
  45. {
  46. const { shortUUID } = await servers[0].videos.upload({ attributes: { name: 'video1' } })
  47. video1ShortId = shortUUID
  48. }
  49. {
  50. const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video2' } })
  51. video2UUID = uuid
  52. }
  53. await waitJobs(servers)
  54. for (const server of servers) {
  55. await server.config.enableTranscoding()
  56. }
  57. })
  58. it('Should run a import job on video 1 with a lower resolution', async function () {
  59. const command = `npm run create-import-video-file-job -- -v ${video1ShortId} -i server/tests/fixtures/video_short-480.webm`
  60. await servers[0].cli.execWithEnv(command)
  61. await waitJobs(servers)
  62. for (const server of servers) {
  63. const { data: videos } = await server.videos.list()
  64. expect(videos).to.have.lengthOf(2)
  65. const video = videos.find(({ shortUUID }) => shortUUID === video1ShortId)
  66. const videoDetails = await server.videos.get({ id: video.shortUUID })
  67. expect(videoDetails.files).to.have.lengthOf(2)
  68. const [ originalVideo, transcodedVideo ] = videoDetails.files
  69. assertVideoProperties(originalVideo, 720, 'webm', 218910)
  70. assertVideoProperties(transcodedVideo, 480, 'webm', 69217)
  71. await checkFiles(videoDetails, objectStorage)
  72. }
  73. })
  74. it('Should run a import job on video 2 with the same resolution and a different extension', async function () {
  75. const command = `npm run create-import-video-file-job -- -v ${video2UUID} -i server/tests/fixtures/video_short.ogv`
  76. await servers[1].cli.execWithEnv(command)
  77. await waitJobs(servers)
  78. for (const server of servers) {
  79. const { data: videos } = await server.videos.listWithToken({ include: VideoInclude.NOT_PUBLISHED_STATE })
  80. expect(videos).to.have.lengthOf(2)
  81. const video = videos.find(({ uuid }) => uuid === video2UUID)
  82. const videoDetails = await server.videos.get({ id: video.uuid })
  83. expect(videoDetails.files).to.have.lengthOf(4)
  84. const [ originalVideo, transcodedVideo420, transcodedVideo320, transcodedVideo240 ] = videoDetails.files
  85. assertVideoProperties(originalVideo, 720, 'ogv', 140849)
  86. assertVideoProperties(transcodedVideo420, 480, 'mp4')
  87. assertVideoProperties(transcodedVideo320, 360, 'mp4')
  88. assertVideoProperties(transcodedVideo240, 240, 'mp4')
  89. await checkFiles(videoDetails, objectStorage)
  90. }
  91. })
  92. it('Should run a import job on video 2 with the same resolution and the same extension', async function () {
  93. const command = `npm run create-import-video-file-job -- -v ${video1ShortId} -i server/tests/fixtures/video_short2.webm`
  94. await servers[0].cli.execWithEnv(command)
  95. await waitJobs(servers)
  96. for (const server of servers) {
  97. const { data: videos } = await server.videos.listWithToken({ include: VideoInclude.NOT_PUBLISHED_STATE })
  98. expect(videos).to.have.lengthOf(2)
  99. const video = videos.find(({ shortUUID }) => shortUUID === video1ShortId)
  100. const videoDetails = await server.videos.get({ id: video.uuid })
  101. expect(videoDetails.files).to.have.lengthOf(2)
  102. const [ video720, video480 ] = videoDetails.files
  103. assertVideoProperties(video720, 720, 'webm', 942961)
  104. assertVideoProperties(video480, 480, 'webm', 69217)
  105. await checkFiles(videoDetails, objectStorage)
  106. }
  107. })
  108. it('Should not have run transcoding after an import job', async function () {
  109. const { data } = await servers[0].jobs.list({ jobType: 'video-transcoding' })
  110. expect(data).to.have.lengthOf(0)
  111. })
  112. after(async function () {
  113. await cleanupTests(servers)
  114. })
  115. }
  116. describe('Test create import video jobs', function () {
  117. describe('On filesystem', function () {
  118. runTests(false)
  119. })
  120. describe('On object storage', function () {
  121. if (areMockObjectStorageTestsDisabled()) return
  122. runTests(true)
  123. })
  124. })