| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064 |
- import { vi, expect, describe, beforeEach, afterEach, it } from 'vitest'
- import sinon from 'sinon'
- import Path from 'node:path'
- const modulePath = Path.join(
- import.meta.dirname,
- '../../../app/js/DockerRunner'
- )
- describe('DockerRunner', () => {
- beforeEach(async ctx => {
- let container
- ctx.container = container = {}
- ctx.Settings = {
- clsi: { docker: {} },
- path: {},
- }
- const Docker = (function () {
- const Docker = class Docker {
- static initClass() {
- this.prototype.getContainer = sinon.stub().returns(container)
- this.prototype.createContainer = sinon.stub().yields(null, container)
- this.prototype.listContainers = sinon.stub()
- }
- }
- Docker.initClass()
- return Docker
- })()
- vi.doMock('@overleaf/settings', () => ({
- default: ctx.Settings,
- }))
- vi.doMock('dockerode', () => ({
- default: Docker,
- }))
- vi.doMock('fs', () => ({
- default: (ctx.fs = {
- stat: sinon.stub().yields(null, {
- isDirectory() {
- return true
- },
- }),
- }),
- }))
- const Timer = class Timer {
- done() {}
- }
- vi.doMock('@overleaf/metrics', () => ({
- default: {
- Timer,
- },
- }))
- ctx.LastProjectAccess = {
- getLastProjectAccessTime: sinon
- .stub()
- .returns(Date.now() - 24 * 60 * 60 * 1000),
- }
- vi.doMock('../../../app/js/LastProjectAccess', () => ctx.LastProjectAccess)
- vi.doMock('../../../app/js/LockManager', () => ({
- default: {
- runWithLock(key, runner, callback) {
- return runner(callback)
- },
- },
- }))
- ctx.DockerRunner = (await import(modulePath)).default
- ctx.Docker = Docker
- ctx.getContainer = Docker.prototype.getContainer
- ctx.createContainer = Docker.prototype.createContainer
- ctx.listContainers = Docker.prototype.listContainers
- ctx.directory = '/local/compile/directory'
- ctx.mainFile = 'main-file.tex'
- ctx.compiler = 'pdflatex'
- ctx.image = 'example.com/overleaf/image:2016.2'
- ctx.env = {}
- ctx.callback = sinon.stub()
- ctx.project_id = 'project-id-123'
- ctx.volumes = { '/some/host/dir/compiles/directory': '/compile' }
- ctx.Settings.clsi.docker.image = ctx.defaultImage = 'default-image'
- ctx.Settings.path.sandboxedCompilesHostDirCompiles =
- '/some/host/dir/compiles'
- ctx.Settings.path.sandboxedCompilesHostDirOutput = '/some/host/dir/output'
- ctx.compileGroup = 'compile-group'
- return (ctx.Settings.clsi.docker.env = { PATH: 'mock-path' })
- })
- afterEach(ctx => {
- ctx.DockerRunner.stopContainerMonitor()
- })
- describe('run', () => {
- beforeEach(async ctx => {
- await new Promise((resolve, reject) => {
- ctx.DockerRunner._getContainerOptions = sinon
- .stub()
- .returns((ctx.options = { mockoptions: 'foo' }))
- ctx.DockerRunner._fingerprintContainer = sinon
- .stub()
- .returns((ctx.fingerprint = 'fingerprint'))
- ctx.containerName = `project-${ctx.project_id}-${ctx.fingerprint}`
- ctx.command = ['mock', 'command', '--outdir=$COMPILE_DIR']
- ctx.command_with_dir = ['mock', 'command', '--outdir=/compile']
- ctx.timeout = 42000
- return resolve()
- })
- })
- describe('successfully', () => {
- beforeEach(async ctx => {
- await new Promise((resolve, reject) => {
- ctx.DockerRunner._runAndWaitForContainer = sinon
- .stub()
- .callsArgWith(3, null, (ctx.output = { stdout: 'mock-output' }))
- return ctx.DockerRunner.run(
- ctx.project_id,
- ctx.command,
- ctx.directory,
- ctx.image,
- ctx.timeout,
- ctx.env,
- ctx.compileGroup,
- (err, output) => {
- ctx.callback(err, output)
- return resolve()
- }
- )
- })
- })
- it('should generate the options for the container', ctx => {
- return ctx.DockerRunner._getContainerOptions
- .calledWith(ctx.command_with_dir, ctx.image, ctx.volumes, ctx.timeout)
- .should.equal(true)
- })
- it('should generate the fingerprint from the returned options', ctx => {
- return ctx.DockerRunner._fingerprintContainer
- .calledWith(ctx.options)
- .should.equal(true)
- })
- it('should do the run', ctx => {
- return ctx.DockerRunner._runAndWaitForContainer
- .calledWith(ctx.options, ctx.volumes, ctx.timeout)
- .should.equal(true)
- })
- return it('should call the callback', ctx => {
- return ctx.callback.calledWith(null, ctx.output).should.equal(true)
- })
- })
- describe('standard compile', () => {
- beforeEach(ctx => {
- ctx.directory = '/var/lib/overleaf/data/compiles/xyz'
- ctx.DockerRunner._runAndWaitForContainer = sinon
- .stub()
- .callsArgWith(3, null, (ctx.output = { stdout: 'mock-output' }))
- return ctx.DockerRunner.run(
- ctx.project_id,
- ctx.command,
- ctx.directory,
- ctx.image,
- ctx.timeout,
- ctx.env,
- ctx.compileGroup,
- ctx.callback
- )
- })
- it('should re-write the bind directory', ctx => {
- const volumes =
- ctx.DockerRunner._runAndWaitForContainer.lastCall.args[1]
- return expect(volumes).to.deep.equal({
- '/some/host/dir/compiles/xyz': '/compile',
- })
- })
- return it('should call the callback', ctx => {
- return ctx.callback.calledWith(null, ctx.output).should.equal(true)
- })
- })
- describe('synctex-output', () => {
- beforeEach(ctx => {
- ctx.directory = '/var/lib/overleaf/data/output/xyz/generated-files/id'
- ctx.DockerRunner._runAndWaitForContainer = sinon
- .stub()
- .callsArgWith(3, null, (ctx.output = { stdout: 'mock-output' }))
- ctx.DockerRunner.run(
- ctx.project_id,
- ctx.command,
- ctx.directory,
- ctx.image,
- ctx.timeout,
- ctx.env,
- 'synctex-output',
- ctx.callback
- )
- })
- it('should re-write the bind directory and set ro flag', ctx => {
- const volumes =
- ctx.DockerRunner._runAndWaitForContainer.lastCall.args[1]
- expect(volumes).to.deep.equal({
- '/some/host/dir/output/xyz/generated-files/id': '/compile:ro',
- })
- })
- it('should call the callback', ctx => {
- ctx.callback.calledWith(null, ctx.output).should.equal(true)
- })
- })
- describe('synctex', () => {
- beforeEach(ctx => {
- ctx.directory = '/var/lib/overleaf/data/compile/xyz'
- ctx.DockerRunner._runAndWaitForContainer = sinon
- .stub()
- .callsArgWith(3, null, (ctx.output = { stdout: 'mock-output' }))
- ctx.DockerRunner.run(
- ctx.project_id,
- ctx.command,
- ctx.directory,
- ctx.image,
- ctx.timeout,
- ctx.env,
- 'synctex',
- ctx.callback
- )
- })
- it('should re-write the bind directory', ctx => {
- const volumes =
- ctx.DockerRunner._runAndWaitForContainer.lastCall.args[1]
- expect(volumes).to.deep.equal({
- '/some/host/dir/compiles/xyz': '/compile:ro',
- })
- })
- it('should call the callback', ctx => {
- ctx.callback.calledWith(null, ctx.output).should.equal(true)
- })
- })
- describe('wordcount', () => {
- beforeEach(ctx => {
- ctx.directory = '/var/lib/overleaf/data/compile/xyz'
- ctx.DockerRunner._runAndWaitForContainer = sinon
- .stub()
- .callsArgWith(3, null, (ctx.output = { stdout: 'mock-output' }))
- ctx.DockerRunner.run(
- ctx.project_id,
- ctx.command,
- ctx.directory,
- ctx.image,
- ctx.timeout,
- ctx.env,
- 'wordcount',
- ctx.callback
- )
- })
- it('should re-write the bind directory', ctx => {
- const volumes =
- ctx.DockerRunner._runAndWaitForContainer.lastCall.args[1]
- expect(volumes).to.deep.equal({
- '/some/host/dir/compiles/xyz': '/compile:ro',
- })
- })
- it('should call the callback', ctx => {
- ctx.callback.calledWith(null, ctx.output).should.equal(true)
- })
- })
- describe('when the run throws an error', () => {
- beforeEach(ctx => {
- let firstTime = true
- ctx.output = { stdout: 'mock-output' }
- ctx.DockerRunner._runAndWaitForContainer = (
- options,
- volumes,
- timeout,
- callback
- ) => {
- if (callback == null) {
- callback = function () {}
- }
- if (firstTime) {
- firstTime = false
- const error = new Error('(HTTP code 500) server error - ...')
- error.statusCode = 500
- callback(error)
- } else {
- callback(null, ctx.output)
- }
- }
- sinon.spy(ctx.DockerRunner, '_runAndWaitForContainer')
- ctx.DockerRunner.destroyContainer = sinon.stub().callsArg(3)
- ctx.DockerRunner.run(
- ctx.project_id,
- ctx.command,
- ctx.directory,
- ctx.image,
- ctx.timeout,
- ctx.env,
- ctx.compileGroup,
- ctx.callback
- )
- })
- it('should do the run twice', ctx => {
- ctx.DockerRunner._runAndWaitForContainer.calledTwice.should.equal(true)
- })
- it('should destroy the container in between', ctx => {
- ctx.DockerRunner.destroyContainer
- .calledWith(ctx.containerName, null)
- .should.equal(true)
- })
- it('should call the callback', ctx => {
- ctx.callback.calledWith(null, ctx.output).should.equal(true)
- })
- })
- describe('with no image', () => {
- beforeEach(ctx => {
- ctx.DockerRunner._runAndWaitForContainer = sinon
- .stub()
- .callsArgWith(3, null, (ctx.output = { stdout: 'mock-output' }))
- ctx.DockerRunner.run(
- ctx.project_id,
- ctx.command,
- ctx.directory,
- null,
- ctx.timeout,
- ctx.env,
- ctx.compileGroup,
- ctx.callback
- )
- })
- it('should use the default image', ctx => {
- ctx.DockerRunner._getContainerOptions
- .calledWith(
- ctx.command_with_dir,
- ctx.defaultImage,
- ctx.volumes,
- ctx.timeout
- )
- .should.equal(true)
- })
- })
- describe('with image override', () => {
- beforeEach(ctx => {
- ctx.Settings.texliveImageNameOveride = 'overrideimage.com/something'
- ctx.DockerRunner._runAndWaitForContainer = sinon
- .stub()
- .callsArgWith(3, null, (ctx.output = { stdout: 'mock-output' }))
- ctx.DockerRunner.run(
- ctx.project_id,
- ctx.command,
- ctx.directory,
- ctx.image,
- ctx.timeout,
- ctx.env,
- ctx.compileGroup,
- ctx.callback
- )
- })
- it('should use the override and keep the tag', ctx => {
- const image = ctx.DockerRunner._getContainerOptions.args[0][1]
- image.should.equal('overrideimage.com/something/image:2016.2')
- })
- })
- describe('with image restriction', () => {
- beforeEach(ctx => {
- ctx.Settings.clsi.docker.allowedImages = [
- 'repo/image:tag1',
- 'repo/image:tag2',
- ]
- ctx.DockerRunner._runAndWaitForContainer = sinon
- .stub()
- .callsArgWith(3, null, (ctx.output = { stdout: 'mock-output' }))
- })
- describe('with a valid image', () => {
- beforeEach(ctx => {
- ctx.DockerRunner.run(
- ctx.project_id,
- ctx.command,
- ctx.directory,
- 'repo/image:tag1',
- ctx.timeout,
- ctx.env,
- ctx.compileGroup,
- ctx.callback
- )
- })
- it('should setup the container', ctx => {
- ctx.DockerRunner._getContainerOptions.called.should.equal(true)
- })
- })
- describe('with a invalid image', () => {
- beforeEach(ctx => {
- ctx.DockerRunner.run(
- ctx.project_id,
- ctx.command,
- ctx.directory,
- 'something/different:evil',
- ctx.timeout,
- ctx.env,
- ctx.compileGroup,
- ctx.callback
- )
- })
- it('should call the callback with an error', ctx => {
- const err = new Error('image not allowed')
- ctx.callback.called.should.equal(true)
- ctx.callback.args[0][0].message.should.equal(err.message)
- })
- it('should not setup the container', ctx => {
- ctx.DockerRunner._getContainerOptions.called.should.equal(false)
- })
- })
- })
- })
- describe('run with _getOptions', () => {
- beforeEach(async ctx => {
- await new Promise((resolve, reject) => {
- // this.DockerRunner._getContainerOptions = sinon
- // .stub()
- // .returns((this.options = { mockoptions: 'foo' }))
- ctx.DockerRunner._fingerprintContainer = sinon
- .stub()
- .returns((ctx.fingerprint = 'fingerprint'))
- ctx.containerName = `project-${ctx.project_id}-${ctx.fingerprint}`
- ctx.command = ['mock', 'command', '--outdir=$COMPILE_DIR']
- ctx.command_with_dir = ['mock', 'command', '--outdir=/compile']
- ctx.timeout = 42000
- resolve()
- })
- })
- describe('when a compile group config is set', () => {
- beforeEach(ctx => {
- ctx.Settings.clsi.docker.compileGroupConfig = {
- 'compile-group': {
- 'HostConfig.newProperty': 'new-property',
- },
- 'other-group': { otherProperty: 'other-property' },
- }
- ctx.DockerRunner._runAndWaitForContainer = sinon
- .stub()
- .callsArgWith(3, null, (ctx.output = { stdout: 'mock-output' }))
- ctx.DockerRunner.run(
- ctx.project_id,
- ctx.command,
- ctx.directory,
- ctx.image,
- ctx.timeout,
- ctx.env,
- ctx.compileGroup,
- ctx.callback
- )
- })
- it('should set the docker options for the compile group', ctx => {
- const options =
- ctx.DockerRunner._runAndWaitForContainer.lastCall.args[0]
- expect(options.HostConfig).to.deep.include({
- Binds: ['/some/host/dir/compiles/directory:/compile:rw'],
- LogConfig: { Type: 'none', Config: {} },
- CapDrop: ['ALL'],
- SecurityOpt: ['no-new-privileges'],
- newProperty: 'new-property',
- })
- })
- it('should call the callback', ctx => {
- ctx.callback.calledWith(null, ctx.output).should.equal(true)
- })
- })
- })
- describe('_runAndWaitForContainer', () => {
- beforeEach(ctx => {
- ctx.options = {
- mockoptions: 'foo',
- name: (ctx.containerName = 'mock-name'),
- }
- ctx.DockerRunner.startContainer = (
- options,
- volumes,
- attachStreamHandler,
- callback
- ) => {
- attachStreamHandler(null, (ctx.output = { stdout: 'mock-output' }))
- callback(null, (ctx.containerId = 'container-id'))
- }
- sinon.spy(ctx.DockerRunner, 'startContainer')
- ctx.DockerRunner.waitForContainer = sinon
- .stub()
- .callsArgWith(3, null, (ctx.exitCode = 42))
- ctx.DockerRunner._runAndWaitForContainer(
- ctx.options,
- ctx.volumes,
- ctx.timeout,
- ctx.callback
- )
- })
- it('should create/start the container', ctx => {
- ctx.DockerRunner.startContainer
- .calledWith(ctx.options, ctx.volumes)
- .should.equal(true)
- })
- it('should wait for the container to finish', ctx => {
- ctx.DockerRunner.waitForContainer
- .calledWith(ctx.containerName, ctx.timeout)
- .should.equal(true)
- })
- it('should call the callback with the output', ctx => {
- ctx.callback.calledWith(null, ctx.output).should.equal(true)
- })
- })
- describe('startContainer', () => {
- beforeEach(ctx => {
- ctx.attachStreamHandler = sinon.stub()
- ctx.attachStreamHandler.cock = true
- ctx.options = { mockoptions: 'foo', name: 'mock-name' }
- ctx.container.inspect = sinon.stub().callsArgWith(0)
- ctx.DockerRunner.attachToContainer = (
- containerId,
- attachStreamHandler,
- cb
- ) => {
- attachStreamHandler()
- cb()
- }
- sinon.spy(ctx.DockerRunner, 'attachToContainer')
- })
- describe('when the container exists', () => {
- beforeEach(ctx => {
- ctx.container.inspect = sinon.stub().callsArgWith(0)
- ctx.container.start = sinon.stub().yields()
- ctx.DockerRunner.startContainer(
- ctx.options,
- ctx.volumes,
- () => {},
- ctx.callback
- )
- })
- it('should start the container with the given name', ctx => {
- ctx.getContainer.calledWith(ctx.options.name).should.equal(true)
- ctx.container.start.called.should.equal(true)
- })
- it('should not try to create the container', ctx => {
- ctx.createContainer.called.should.equal(false)
- })
- it('should attach to the container', ctx => {
- ctx.DockerRunner.attachToContainer.called.should.equal(true)
- })
- it('should call the callback', ctx => {
- ctx.callback.called.should.equal(true)
- })
- it('should attach before the container starts', ctx => {
- sinon.assert.callOrder(
- ctx.DockerRunner.attachToContainer,
- ctx.container.start
- )
- })
- })
- describe('when the container does not exist', () => {
- beforeEach(ctx => {
- ctx.container.start = sinon.stub().yields()
- ctx.container.inspect = sinon
- .stub()
- .callsArgWith(0, { statusCode: 404 })
- ctx.DockerRunner.startContainer(
- ctx.options,
- ctx.volumes,
- ctx.attachStreamHandler,
- ctx.callback
- )
- })
- it('should create the container', ctx => {
- ctx.createContainer.calledWith(ctx.options).should.equal(true)
- })
- it('should call the callback and stream handler', ctx => {
- ctx.attachStreamHandler.called.should.equal(true)
- ctx.callback.called.should.equal(true)
- })
- it('should attach to the container', ctx => {
- ctx.DockerRunner.attachToContainer.called.should.equal(true)
- })
- it('should attach before the container starts', ctx => {
- sinon.assert.callOrder(
- ctx.DockerRunner.attachToContainer,
- ctx.container.start
- )
- })
- })
- describe('when the container is already running', () => {
- beforeEach(ctx => {
- const error = new Error(
- `HTTP code is 304 which indicates error: server error - start: Cannot start container ${ctx.containerName}: The container MOCKID is already running.`
- )
- error.statusCode = 304
- ctx.container.start = sinon.stub().yields(error)
- ctx.container.inspect = sinon.stub().callsArgWith(0)
- ctx.DockerRunner.startContainer(
- ctx.options,
- ctx.volumes,
- ctx.attachStreamHandler,
- ctx.callback
- )
- })
- it('should not try to create the container', ctx => {
- ctx.createContainer.called.should.equal(false)
- })
- it('should call the callback and stream handler without an error', ctx => {
- ctx.attachStreamHandler.called.should.equal(true)
- ctx.callback.called.should.equal(true)
- })
- })
- describe.todo(
- 'when the container tries to be created, but already has been (race condition)',
- () => {}
- )
- })
- describe('waitForContainer', () => {
- beforeEach(ctx => {
- ctx.containerId = 'container-id'
- ctx.timeout = 5000
- ctx.container.wait = sinon
- .stub()
- .yields(null, { StatusCode: (ctx.statusCode = 42) })
- ctx.container.kill = sinon.stub().yields()
- })
- describe('when the container returns in time', () => {
- beforeEach(ctx => {
- ctx.DockerRunner.waitForContainer(
- ctx.containerId,
- ctx.timeout,
- {},
- ctx.callback
- )
- })
- it('should wait for the container', ctx => {
- ctx.getContainer.calledWith(ctx.containerId).should.equal(true)
- ctx.container.wait.called.should.equal(true)
- })
- it('should call the callback with the exit', ctx => {
- ctx.callback.calledWith(null, ctx.statusCode).should.equal(true)
- })
- })
- describe('when the container is removed before waiting', () => {
- const err = new Error('not found')
- err.statusCode = 404
- beforeEach(ctx => {
- ctx.container.wait = sinon.stub().yields(err)
- })
- describe('AutoRemove not set', () => {
- beforeEach(ctx => {
- ctx.DockerRunner.waitForContainer(
- ctx.containerId,
- ctx.timeout,
- { HostConfig: {} },
- ctx.callback
- )
- })
- it('should wait for the container', ctx => {
- ctx.getContainer.calledWith(ctx.containerId).should.equal(true)
- ctx.container.wait.called.should.equal(true)
- })
- it('should call the callback with the error', ctx => {
- ctx.callback.calledWith(err).should.equal(true)
- })
- })
- describe('AutoRemove=true', () => {
- beforeEach(ctx => {
- ctx.DockerRunner.waitForContainer(
- ctx.containerId,
- ctx.timeout,
- { HostConfig: { AutoRemove: true } },
- ctx.callback
- )
- })
- it('should wait for the container', ctx => {
- ctx.getContainer.calledWith(ctx.containerId).should.equal(true)
- ctx.container.wait.called.should.equal(true)
- })
- it('should call the callback with exit code 0', ctx => {
- ctx.callback.calledWith(null, 0).should.equal(true)
- })
- })
- })
- describe('when the container does not return before the timeout', () => {
- beforeEach(async ctx => {
- await new Promise((resolve, reject) => {
- ctx.container.wait = function (callback) {
- if (callback == null) {
- callback = function () {}
- }
- setTimeout(() => callback(null, { StatusCode: 42 }), 100)
- }
- ctx.timeout = 5
- ctx.DockerRunner.waitForContainer(
- ctx.containerId,
- ctx.timeout,
- {},
- (...args) => {
- ctx.callback(...Array.from(args || []))
- resolve()
- }
- )
- })
- })
- it('should call kill on the container', ctx => {
- ctx.getContainer.calledWith(ctx.containerId).should.equal(true)
- ctx.container.kill.called.should.equal(true)
- })
- it('should call the callback with an error', ctx => {
- ctx.callback.calledWith(sinon.match(Error)).should.equal(true)
- const errorObj = ctx.callback.args[0][0]
- expect(errorObj.message).to.include('container timed out')
- expect(errorObj.timedout).equal(true)
- })
- })
- })
- describe('destroyOldContainers', () => {
- beforeEach(async ctx => {
- await new Promise((resolve, reject) => {
- const oneHourInSeconds = 60 * 60
- const oneHourInMilliseconds = oneHourInSeconds * 1000
- const nowInSeconds = Date.now() / 1000
- ctx.recentlyAccessProjectId = '68494e7daa5c3680ee7182b1'
- ctx.LastProjectAccess.getLastProjectAccessTime
- .withArgs(ctx.recentlyAccessProjectId)
- .returns(Date.now() - 1)
- ctx.containers = [
- {
- Name: '/project-69b72c66b1a8c2f5846b24a8-container-name',
- Id: 'old-container-id',
- Created: nowInSeconds - oneHourInSeconds - 100,
- },
- {
- Name: '/project-69b72c66b1a8c2f5846b24a9-container-name',
- Id: 'new-container-id',
- Created: nowInSeconds - oneHourInSeconds + 100,
- },
- {
- Name: `/project-${ctx.recentlyAccessProjectId}-container-name`,
- Id: 'recent-access-container-id',
- Created: nowInSeconds - 2 * oneHourInSeconds,
- },
- {
- Name: '/totally-not-a-project-container',
- Id: 'some-random-id',
- Created: nowInSeconds - 2 * oneHourInSeconds,
- },
- ]
- ctx.DockerRunner.MAX_CONTAINER_AGE = oneHourInMilliseconds
- ctx.listContainers.callsArgWith(1, null, ctx.containers)
- ctx.DockerRunner.destroyContainer = sinon.stub().callsArg(3)
- ctx.DockerRunner.destroyOldContainers(error => {
- ctx.callback(error)
- resolve()
- })
- })
- })
- it('should list all containers', ctx => {
- ctx.listContainers.calledWith({ all: true }).should.equal(true)
- })
- it('should destroy old containers', ctx => {
- ctx.DockerRunner.destroyContainer.callCount.should.equal(1)
- ctx.DockerRunner.destroyContainer
- .calledWith(
- 'project-69b72c66b1a8c2f5846b24a8-container-name',
- 'old-container-id'
- )
- .should.equal(true)
- })
- it('should not destroy new containers', ctx => {
- ctx.DockerRunner.destroyContainer
- .calledWith(
- 'project-69b72c66b1a8c2f5846b24a9-container-name',
- 'new-container-id'
- )
- .should.equal(false)
- })
- it('should not destroy old containers that were recently used', ctx => {
- ctx.DockerRunner.destroyContainer
- .calledWith(
- 'project-68494e7daa5c3680ee7182b1-container-name',
- 'recent-access-container-id'
- )
- .should.equal(false)
- })
- it('should not destroy non-project containers', ctx => {
- ctx.DockerRunner.destroyContainer
- .calledWith('totally-not-a-project-container', 'some-random-id')
- .should.equal(false)
- })
- it('should callback the callback', ctx => {
- ctx.callback.called.should.equal(true)
- })
- })
- describe('_destroyContainer', () => {
- beforeEach(ctx => {
- ctx.containerId = 'some_id'
- ctx.fakeContainer = { remove: sinon.stub().callsArgWith(1, null) }
- ctx.Docker.prototype.getContainer = sinon
- .stub()
- .returns(ctx.fakeContainer)
- })
- it('should get the container', async ctx => {
- await new Promise((resolve, reject) => {
- ctx.DockerRunner._destroyContainer(ctx.containerId, false, err => {
- if (err) return reject(err)
- ctx.Docker.prototype.getContainer.callCount.should.equal(1)
- ctx.Docker.prototype.getContainer
- .calledWith(ctx.containerId)
- .should.equal(true)
- resolve()
- })
- })
- })
- it('should try to force-destroy the container when shouldForce=true', async ctx => {
- await new Promise((resolve, reject) => {
- ctx.DockerRunner._destroyContainer(ctx.containerId, true, err => {
- if (err) return reject(err)
- ctx.fakeContainer.remove.callCount.should.equal(1)
- ctx.fakeContainer.remove
- .calledWith({ force: true, v: true })
- .should.equal(true)
- resolve()
- })
- })
- })
- it('should not try to force-destroy the container when shouldForce=false', async ctx => {
- await new Promise((resolve, reject) => {
- ctx.DockerRunner._destroyContainer(ctx.containerId, false, err => {
- if (err) return reject(err)
- ctx.fakeContainer.remove.callCount.should.equal(1)
- ctx.fakeContainer.remove
- .calledWith({ force: false, v: true })
- .should.equal(true)
- resolve()
- })
- })
- })
- it('should not produce an error', async ctx => {
- await new Promise((resolve, reject) => {
- ctx.DockerRunner._destroyContainer(ctx.containerId, false, err => {
- expect(err).to.equal(null)
- resolve()
- })
- })
- })
- describe('when the container is already gone', () => {
- beforeEach(ctx => {
- ctx.fakeError = new Error('woops')
- ctx.fakeError.statusCode = 404
- ctx.fakeContainer = {
- remove: sinon.stub().callsArgWith(1, ctx.fakeError),
- }
- ctx.Docker.prototype.getContainer = sinon
- .stub()
- .returns(ctx.fakeContainer)
- })
- it('should not produce an error', async ctx => {
- await new Promise((resolve, reject) => {
- ctx.DockerRunner._destroyContainer(ctx.containerId, false, err => {
- expect(err).to.equal(null)
- resolve()
- })
- })
- })
- })
- describe('when container.destroy produces an error', () => {
- beforeEach(ctx => {
- ctx.fakeError = new Error('woops')
- ctx.fakeError.statusCode = 500
- ctx.fakeContainer = {
- remove: sinon.stub().callsArgWith(1, ctx.fakeError),
- }
- ctx.Docker.prototype.getContainer = sinon
- .stub()
- .returns(ctx.fakeContainer)
- })
- it('should produce an error', async ctx => {
- await new Promise((resolve, reject) => {
- ctx.DockerRunner._destroyContainer(ctx.containerId, false, err => {
- expect(err).to.not.equal(null)
- expect(err).to.equal(ctx.fakeError)
- resolve()
- })
- })
- })
- })
- })
- describe('kill', () => {
- beforeEach(ctx => {
- ctx.containerId = 'some_id'
- ctx.fakeContainer = { kill: sinon.stub().callsArgWith(0, null) }
- ctx.Docker.prototype.getContainer = sinon
- .stub()
- .returns(ctx.fakeContainer)
- })
- it('should get the container', async ctx => {
- await new Promise((resolve, reject) => {
- ctx.DockerRunner.kill(ctx.containerId, err => {
- if (err) return reject(err)
- ctx.Docker.prototype.getContainer.callCount.should.equal(1)
- ctx.Docker.prototype.getContainer
- .calledWith(ctx.containerId)
- .should.equal(true)
- resolve()
- })
- })
- })
- it('should try to force-destroy the container', async ctx => {
- await new Promise((resolve, reject) => {
- ctx.DockerRunner.kill(ctx.containerId, err => {
- if (err) return reject(err)
- ctx.fakeContainer.kill.callCount.should.equal(1)
- resolve()
- })
- })
- })
- it('should not produce an error', async ctx => {
- await new Promise((resolve, reject) => {
- ctx.DockerRunner.kill(ctx.containerId, err => {
- expect(err).to.equal(undefined)
- resolve()
- })
- })
- })
- describe('when the container is not actually running', () => {
- beforeEach(ctx => {
- ctx.fakeError = new Error('woops')
- ctx.fakeError.statusCode = 500
- ctx.fakeError.message =
- 'Cannot kill container <whatever> is not running'
- ctx.fakeContainer = {
- kill: sinon.stub().callsArgWith(0, ctx.fakeError),
- }
- ctx.Docker.prototype.getContainer = sinon
- .stub()
- .returns(ctx.fakeContainer)
- })
- it('should not produce an error', async ctx => {
- await new Promise((resolve, reject) => {
- ctx.DockerRunner.kill(ctx.containerId, err => {
- expect(err).to.equal(undefined)
- resolve()
- })
- })
- })
- })
- describe('when container.kill produces a legitimate error', () => {
- beforeEach(ctx => {
- ctx.fakeError = new Error('woops')
- ctx.fakeError.statusCode = 500
- ctx.fakeError.message = 'Totally legitimate reason to throw an error'
- ctx.fakeContainer = {
- kill: sinon.stub().callsArgWith(0, ctx.fakeError),
- }
- ctx.Docker.prototype.getContainer = sinon
- .stub()
- .returns(ctx.fakeContainer)
- })
- it('should produce an error', async ctx => {
- await new Promise((resolve, reject) => {
- ctx.DockerRunner.kill(ctx.containerId, err => {
- expect(err).to.not.equal(undefined)
- expect(err).to.equal(ctx.fakeError)
- resolve()
- })
- })
- })
- })
- })
- })
|