audiobookshelf/test/server/managers/BinaryManager.test.js

382 lines
13 KiB
JavaScript
Raw Normal View History

2023-12-05 21:18:37 +01:00
const chai = require('chai')
const sinon = require('sinon')
const fs = require('../../../server/libs/fsExtra')
const fileUtils = require('../../../server/utils/fileUtils')
2023-12-05 21:18:37 +01:00
const which = require('../../../server/libs/which')
const ffbinaries = require('../../../server/libs/ffbinaries')
2023-12-05 21:18:37 +01:00
const path = require('path')
const BinaryManager = require('../../../server/managers/BinaryManager')
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
const expect = chai.expect
2023-12-05 20:19:17 +01:00
describe('BinaryManager', () => {
2023-12-05 21:18:37 +01:00
let binaryManager
2023-12-05 20:19:17 +01:00
describe('init', () => {
2023-12-05 21:18:37 +01:00
let findStub
let installStub
let removeOldBinariesStub
2023-12-05 21:18:37 +01:00
let errorStub
let exitStub
2023-12-05 20:19:17 +01:00
beforeEach(() => {
2023-12-05 21:18:37 +01:00
binaryManager = new BinaryManager()
findStub = sinon.stub(binaryManager, 'findRequiredBinaries')
installStub = sinon.stub(binaryManager, 'install')
removeOldBinariesStub = sinon.stub(binaryManager, 'removeOldBinaries')
2023-12-05 21:18:37 +01:00
errorStub = sinon.stub(console, 'error')
exitStub = sinon.stub(process, 'exit')
})
2023-12-05 20:19:17 +01:00
afterEach(() => {
2023-12-05 21:18:37 +01:00
findStub.restore()
installStub.restore()
removeOldBinariesStub.restore()
2023-12-05 21:18:37 +01:00
errorStub.restore()
exitStub.restore()
})
2023-12-05 20:19:17 +01:00
it('should not install binaries if they are already found', async () => {
2023-12-05 21:18:37 +01:00
findStub.resolves([])
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
await binaryManager.init()
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
expect(installStub.called).to.be.false
expect(removeOldBinariesStub.called).to.be.false
2023-12-05 21:18:37 +01:00
expect(findStub.calledOnce).to.be.true
expect(errorStub.called).to.be.false
expect(exitStub.called).to.be.false
})
2023-12-05 20:19:17 +01:00
it('should install missing binaries', async () => {
2023-12-05 21:18:37 +01:00
const missingBinaries = ['ffmpeg', 'ffprobe']
const missingBinariesAfterInstall = []
findStub.onFirstCall().resolves(missingBinaries)
findStub.onSecondCall().resolves(missingBinariesAfterInstall)
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
await binaryManager.init()
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
expect(findStub.calledTwice).to.be.true
expect(installStub.calledOnce).to.be.true
expect(removeOldBinariesStub.calledOnce).to.be.true
2023-12-05 21:18:37 +01:00
expect(errorStub.called).to.be.false
expect(exitStub.called).to.be.false
})
2023-12-05 20:19:17 +01:00
it('exit if binaries are not found after installation', async () => {
2023-12-05 21:18:37 +01:00
const missingBinaries = ['ffmpeg', 'ffprobe']
const missingBinariesAfterInstall = ['ffmpeg', 'ffprobe']
findStub.onFirstCall().resolves(missingBinaries)
findStub.onSecondCall().resolves(missingBinariesAfterInstall)
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
await binaryManager.init()
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
expect(findStub.calledTwice).to.be.true
expect(installStub.calledOnce).to.be.true
expect(removeOldBinariesStub.calledOnce).to.be.true
2023-12-05 21:18:37 +01:00
expect(errorStub.calledOnce).to.be.true
expect(exitStub.calledOnce).to.be.true
expect(exitStub.calledWith(1)).to.be.true
})
})
2023-12-05 20:19:17 +01:00
describe('findRequiredBinaries', () => {
2023-12-05 21:18:37 +01:00
let findBinaryStub
2023-12-05 20:19:17 +01:00
beforeEach(() => {
2023-12-05 21:18:37 +01:00
const requiredBinaries = [{ name: 'ffmpeg', envVariable: 'FFMPEG_PATH' }]
binaryManager = new BinaryManager(requiredBinaries)
findBinaryStub = sinon.stub(binaryManager, 'findBinary')
})
2023-12-05 20:19:17 +01:00
afterEach(() => {
2023-12-05 21:18:37 +01:00
findBinaryStub.restore()
})
2023-12-05 20:19:17 +01:00
it('should put found paths in the correct environment variables', async () => {
2023-12-05 21:18:37 +01:00
const pathToFFmpeg = '/path/to/ffmpeg'
const missingBinaries = []
delete process.env.FFMPEG_PATH
findBinaryStub.resolves(pathToFFmpeg)
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
const result = await binaryManager.findRequiredBinaries()
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
expect(result).to.deep.equal(missingBinaries)
expect(findBinaryStub.calledOnce).to.be.true
expect(process.env.FFMPEG_PATH).to.equal(pathToFFmpeg)
})
2023-12-05 20:19:17 +01:00
it('should add missing binaries to result', async () => {
2023-12-05 21:18:37 +01:00
const missingBinaries = ['ffmpeg']
delete process.env.FFMPEG_PATH
findBinaryStub.resolves(null)
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
const result = await binaryManager.findRequiredBinaries()
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
expect(result).to.deep.equal(missingBinaries)
expect(findBinaryStub.calledOnce).to.be.true
expect(process.env.FFMPEG_PATH).to.be.undefined
})
})
2023-12-05 20:19:17 +01:00
describe('install', () => {
let isWritableStub
2023-12-05 21:18:37 +01:00
let downloadBinariesStub
2023-12-05 20:19:17 +01:00
beforeEach(() => {
2023-12-05 21:18:37 +01:00
binaryManager = new BinaryManager()
isWritableStub = sinon.stub(fileUtils, 'isWritable')
downloadBinariesStub = sinon.stub(ffbinaries, 'downloadBinaries')
2023-12-05 20:19:17 +01:00
binaryManager.mainInstallPath = '/path/to/main/install'
binaryManager.altInstallPath = '/path/to/alt/install'
2023-12-05 21:18:37 +01:00
})
2023-12-05 20:19:17 +01:00
afterEach(() => {
isWritableStub.restore()
2023-12-05 21:18:37 +01:00
downloadBinariesStub.restore()
})
2023-12-05 20:19:17 +01:00
it('should not install binaries if no binaries are passed', async () => {
2023-12-05 21:18:37 +01:00
const binaries = []
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
await binaryManager.install(binaries)
2023-12-05 20:19:17 +01:00
expect(isWritableStub.called).to.be.false
2023-12-05 21:18:37 +01:00
expect(downloadBinariesStub.called).to.be.false
})
2023-12-05 20:19:17 +01:00
it('should install binaries in main install path if has access', async () => {
2023-12-05 21:18:37 +01:00
const binaries = ['ffmpeg']
const destination = binaryManager.mainInstallPath
isWritableStub.withArgs(destination).resolves(true)
2023-12-05 21:18:37 +01:00
downloadBinariesStub.resolves()
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
await binaryManager.install(binaries)
2023-12-05 20:19:17 +01:00
expect(isWritableStub.calledOnce).to.be.true
2023-12-05 21:18:37 +01:00
expect(downloadBinariesStub.calledOnce).to.be.true
expect(downloadBinariesStub.calledWith(binaries, sinon.match({ destination: destination }))).to.be.true
})
2023-12-05 20:19:17 +01:00
it('should install binaries in alt install path if has no access to main', async () => {
2023-12-05 21:18:37 +01:00
const binaries = ['ffmpeg']
const mainDestination = binaryManager.mainInstallPath
const destination = binaryManager.altInstallPath
isWritableStub.withArgs(mainDestination).resolves(false)
2023-12-05 21:18:37 +01:00
downloadBinariesStub.resolves()
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
await binaryManager.install(binaries)
2023-12-05 20:19:17 +01:00
expect(isWritableStub.calledOnce).to.be.true
2023-12-05 21:18:37 +01:00
expect(downloadBinariesStub.calledOnce).to.be.true
expect(downloadBinariesStub.calledWith(binaries, sinon.match({ destination: destination }))).to.be.true
})
})
})
2023-12-05 20:19:17 +01:00
describe('findBinary', () => {
2023-12-05 21:18:37 +01:00
let binaryManager
let isBinaryGoodStub
2023-12-05 21:18:37 +01:00
let whichSyncStub
let mainInstallPath
let altInstallPath
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
const name = 'ffmpeg'
const envVariable = 'FFMPEG_PATH'
const defaultPath = '/path/to/ffmpeg'
const executable = name + (process.platform == 'win32' ? '.exe' : '')
const whichPath = '/usr/bin/ffmpeg'
2023-12-05 20:19:17 +01:00
beforeEach(() => {
2023-12-05 21:18:37 +01:00
binaryManager = new BinaryManager()
isBinaryGoodStub = sinon.stub(binaryManager, 'isBinaryGood')
2023-12-05 21:18:37 +01:00
whichSyncStub = sinon.stub(which, 'sync')
2023-12-05 20:19:17 +01:00
binaryManager.mainInstallPath = '/path/to/main/install'
2023-12-05 21:18:37 +01:00
mainInstallPath = path.join(binaryManager.mainInstallPath, executable)
2023-12-05 20:19:17 +01:00
binaryManager.altInstallPath = '/path/to/alt/install'
2023-12-05 21:18:37 +01:00
altInstallPath = path.join(binaryManager.altInstallPath, executable)
})
2023-12-05 20:19:17 +01:00
afterEach(() => {
isBinaryGoodStub.restore()
2023-12-05 21:18:37 +01:00
whichSyncStub.restore()
})
it('should return the defaultPath if it exists and is a good binary', async () => {
2023-12-05 21:18:37 +01:00
process.env[envVariable] = defaultPath
isBinaryGoodStub.withArgs(defaultPath).resolves(true)
2023-12-05 21:18:37 +01:00
const result = await binaryManager.findBinary(name, envVariable)
expect(result).to.equal(defaultPath)
expect(isBinaryGoodStub.calledOnce).to.be.true
expect(isBinaryGoodStub.calledWith(defaultPath)).to.be.true
2023-12-05 21:18:37 +01:00
})
it('should return the whichPath if it exists and is a good binary', async () => {
2023-12-05 21:18:37 +01:00
delete process.env[envVariable]
isBinaryGoodStub.withArgs(undefined).resolves(false)
isBinaryGoodStub.withArgs(whichPath).resolves(true)
2023-12-05 21:18:37 +01:00
whichSyncStub.returns(whichPath)
2023-12-05 21:18:37 +01:00
const result = await binaryManager.findBinary(name, envVariable)
2023-12-05 21:18:37 +01:00
expect(result).to.equal(whichPath)
expect(isBinaryGoodStub.calledTwice).to.be.true
expect(isBinaryGoodStub.calledWith(undefined)).to.be.true
expect(isBinaryGoodStub.calledWith(whichPath)).to.be.true
2023-12-05 21:18:37 +01:00
})
it('should return the mainInstallPath if it exists and is a good binary', async () => {
2023-12-05 21:18:37 +01:00
delete process.env[envVariable]
isBinaryGoodStub.withArgs(undefined).resolves(false)
isBinaryGoodStub.withArgs(null).resolves(false)
isBinaryGoodStub.withArgs(mainInstallPath).resolves(true)
2023-12-05 21:18:37 +01:00
whichSyncStub.returns(null)
2023-12-05 21:18:37 +01:00
const result = await binaryManager.findBinary(name, envVariable)
2023-12-05 21:18:37 +01:00
expect(result).to.equal(mainInstallPath)
expect(isBinaryGoodStub.callCount).to.be.equal(3)
expect(isBinaryGoodStub.calledWith(undefined)).to.be.true
expect(isBinaryGoodStub.calledWith(null)).to.be.true
expect(isBinaryGoodStub.calledWith(mainInstallPath)).to.be.true
2023-12-05 21:18:37 +01:00
})
it('should return the altInstallPath if it exists and is a good binary', async () => {
2023-12-05 21:18:37 +01:00
delete process.env[envVariable]
isBinaryGoodStub.withArgs(undefined).resolves(false)
isBinaryGoodStub.withArgs(null).resolves(false)
isBinaryGoodStub.withArgs(mainInstallPath).resolves(false)
isBinaryGoodStub.withArgs(altInstallPath).resolves(true)
2023-12-05 21:18:37 +01:00
whichSyncStub.returns(null)
2023-12-05 21:18:37 +01:00
const result = await binaryManager.findBinary(name, envVariable)
2023-12-05 21:18:37 +01:00
expect(result).to.equal(altInstallPath)
expect(isBinaryGoodStub.callCount).to.be.equal(4)
expect(isBinaryGoodStub.calledWith(undefined)).to.be.true
expect(isBinaryGoodStub.calledWith(null)).to.be.true
expect(isBinaryGoodStub.calledWith(mainInstallPath)).to.be.true
expect(isBinaryGoodStub.calledWith(altInstallPath)).to.be.true
2023-12-05 21:18:37 +01:00
})
it('should return null if no good binary is found', async () => {
2023-12-05 21:18:37 +01:00
delete process.env[envVariable]
isBinaryGoodStub.withArgs(undefined).resolves(false)
isBinaryGoodStub.withArgs(null).resolves(false)
isBinaryGoodStub.withArgs(mainInstallPath).resolves(false)
isBinaryGoodStub.withArgs(altInstallPath).resolves(false)
2023-12-05 21:18:37 +01:00
whichSyncStub.returns(null)
2023-12-05 21:18:37 +01:00
const result = await binaryManager.findBinary(name, envVariable)
2023-12-05 21:18:37 +01:00
expect(result).to.be.null
expect(isBinaryGoodStub.callCount).to.be.equal(4)
expect(isBinaryGoodStub.calledWith(undefined)).to.be.true
expect(isBinaryGoodStub.calledWith(null)).to.be.true
expect(isBinaryGoodStub.calledWith(mainInstallPath)).to.be.true
expect(isBinaryGoodStub.calledWith(altInstallPath)).to.be.true
})
})
describe('isBinaryGood', () => {
let binaryManager
let fsPathExistsStub
let execStub
let loggerInfoStub
let loggerErrorStub
const binaryPath = '/path/to/binary'
const execCommand = '"' + binaryPath + '"' + ' -version'
const goodVersions = ['5.1', '6']
beforeEach(() => {
binaryManager = new BinaryManager()
fsPathExistsStub = sinon.stub(fs, 'pathExists')
execStub = sinon.stub(binaryManager, 'exec')
})
afterEach(() => {
fsPathExistsStub.restore()
execStub.restore()
})
it('should return false if binaryPath is falsy', async () => {
fsPathExistsStub.resolves(true)
const result = await binaryManager.isBinaryGood(null, goodVersions)
expect(result).to.be.false
expect(fsPathExistsStub.called).to.be.false
expect(execStub.called).to.be.false
})
it('should return false if binaryPath does not exist', async () => {
fsPathExistsStub.resolves(false)
const result = await binaryManager.isBinaryGood(binaryPath, goodVersions)
expect(result).to.be.false
expect(fsPathExistsStub.calledOnce).to.be.true
expect(fsPathExistsStub.calledWith(binaryPath)).to.be.true
expect(execStub.called).to.be.false
})
it('should return false if failed to check version of binary', async () => {
fsPathExistsStub.resolves(true)
execStub.rejects(new Error('Failed to execute command'))
const result = await binaryManager.isBinaryGood(binaryPath, goodVersions)
expect(result).to.be.false
expect(fsPathExistsStub.calledOnce).to.be.true
expect(fsPathExistsStub.calledWith(binaryPath)).to.be.true
expect(execStub.calledOnce).to.be.true
expect(execStub.calledWith(execCommand)).to.be.true
})
it('should return false if version is not found', async () => {
const stdout = 'Some output without version'
fsPathExistsStub.resolves(true)
execStub.resolves({ stdout })
const result = await binaryManager.isBinaryGood(binaryPath, goodVersions)
expect(result).to.be.false
expect(fsPathExistsStub.calledOnce).to.be.true
expect(fsPathExistsStub.calledWith(binaryPath)).to.be.true
expect(execStub.calledOnce).to.be.true
expect(execStub.calledWith(execCommand)).to.be.true
})
it('should return false if version is found but does not match a good version', async () => {
const stdout = 'version 1.2.3'
fsPathExistsStub.resolves(true)
execStub.resolves({ stdout })
const result = await binaryManager.isBinaryGood(binaryPath, goodVersions)
expect(result).to.be.false
expect(fsPathExistsStub.calledOnce).to.be.true
expect(fsPathExistsStub.calledWith(binaryPath)).to.be.true
expect(execStub.calledOnce).to.be.true
expect(execStub.calledWith(execCommand)).to.be.true
})
it('should return true if version is found and matches a good version', async () => {
const stdout = 'version 6.1.2'
fsPathExistsStub.resolves(true)
execStub.resolves({ stdout })
const result = await binaryManager.isBinaryGood(binaryPath, goodVersions)
expect(result).to.be.true
expect(fsPathExistsStub.calledOnce).to.be.true
expect(fsPathExistsStub.calledWith(binaryPath)).to.be.true
expect(execStub.calledOnce).to.be.true
expect(execStub.calledWith(execCommand)).to.be.true
2023-12-05 21:18:37 +01:00
})
})