| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150 |
- 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,
- null,
- (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,
- null,
- 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',
- null,
- 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',
- null,
- 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',
- null,
- 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('with a cwd', () => {
- 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,
- ctx.image,
- ctx.timeout,
- ctx.env,
- ctx.compileGroup,
- 'subdir',
- ctx.callback
- )
- })
- it('should pass the cwd through to _getContainerOptions', ctx => {
- ctx.DockerRunner._getContainerOptions
- .calledWith(
- ctx.command_with_dir,
- ctx.image,
- ctx.volumes,
- ctx.timeout,
- ctx.env,
- ctx.compileGroup,
- 'subdir'
- )
- .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,
- null,
- 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,
- null,
- 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,
- null,
- 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,
- null,
- 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,
- null,
- 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,
- null,
- 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('WorkingDir with cwd', () => {
- beforeEach(ctx => {
- ctx.DockerRunner._runAndWaitForContainer = sinon
- .stub()
- .callsArgWith(3, null, (ctx.output = { stdout: 'mock-output' }))
- })
- it('should default WorkingDir to /compile when cwd is null', ctx => {
- ctx.DockerRunner.run(
- ctx.project_id,
- ctx.command,
- ctx.directory,
- ctx.image,
- ctx.timeout,
- ctx.env,
- ctx.compileGroup,
- null,
- ctx.callback
- )
- const options =
- ctx.DockerRunner._runAndWaitForContainer.lastCall.args[0]
- expect(options.WorkingDir).to.equal('/compile')
- })
- it('should join cwd onto /compile when provided', ctx => {
- ctx.DockerRunner.run(
- ctx.project_id,
- ctx.command,
- ctx.directory,
- ctx.image,
- ctx.timeout,
- ctx.env,
- ctx.compileGroup,
- 'subdir/nested',
- ctx.callback
- )
- const options =
- ctx.DockerRunner._runAndWaitForContainer.lastCall.args[0]
- expect(options.WorkingDir).to.equal('/compile/subdir/nested')
- })
- })
- })
- 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()
- })
- })
- })
- })
- })
- })
|