2020-02-19 06:15:25 -05:00
|
|
|
/* eslint-disable
|
|
|
|
handle-callback-err,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 06:15:08 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS206: Consider reworking classes to avoid initClass
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-19 06:15:37 -05:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const { expect } = require('chai')
|
|
|
|
const modulePath = require('path').join(
|
|
|
|
__dirname,
|
2020-02-19 06:38:54 -05:00
|
|
|
'../../../app/js/DockerRunner'
|
2020-02-19 06:15:37 -05:00
|
|
|
)
|
|
|
|
const Path = require('path')
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('DockerRunner', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
let container, Docker, Timer
|
|
|
|
this.container = container = {}
|
|
|
|
this.DockerRunner = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2021-07-12 12:47:21 -04:00
|
|
|
'@overleaf/settings': (this.Settings = {
|
2020-02-19 06:15:37 -05:00
|
|
|
clsi: { docker: {} },
|
2021-07-13 07:04:48 -04:00
|
|
|
path: {},
|
2020-02-19 06:15:37 -05:00
|
|
|
}),
|
2020-08-10 12:01:11 -04:00
|
|
|
dockerode: (Docker = (function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
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
|
|
|
|
})()),
|
|
|
|
fs: (this.fs = {
|
|
|
|
stat: sinon.stub().yields(null, {
|
|
|
|
isDirectory() {
|
|
|
|
return true
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
|
|
|
}),
|
2020-02-19 06:15:37 -05:00
|
|
|
}),
|
|
|
|
'./Metrics': {
|
|
|
|
Timer: (Timer = class Timer {
|
|
|
|
done() {}
|
2021-07-13 07:04:48 -04:00
|
|
|
}),
|
2020-02-19 06:15:37 -05:00
|
|
|
},
|
|
|
|
'./LockManager': {
|
|
|
|
runWithLock(key, runner, callback) {
|
|
|
|
return runner(callback)
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
|
|
|
},
|
2020-06-15 04:52:21 -04:00
|
|
|
},
|
2021-07-13 07:04:48 -04:00
|
|
|
globals: { Math }, // used by lodash
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
this.Docker = Docker
|
|
|
|
this.getContainer = Docker.prototype.getContainer
|
|
|
|
this.createContainer = Docker.prototype.createContainer
|
|
|
|
this.listContainers = Docker.prototype.listContainers
|
|
|
|
|
|
|
|
this.directory = '/local/compile/directory'
|
|
|
|
this.mainFile = 'main-file.tex'
|
|
|
|
this.compiler = 'pdflatex'
|
|
|
|
this.image = 'example.com/sharelatex/image:2016.2'
|
|
|
|
this.env = {}
|
|
|
|
this.callback = sinon.stub()
|
|
|
|
this.project_id = 'project-id-123'
|
|
|
|
this.volumes = { '/local/compile/directory': '/compile' }
|
|
|
|
this.Settings.clsi.docker.image = this.defaultImage = 'default-image'
|
2020-06-11 11:01:44 -04:00
|
|
|
this.compileGroup = 'compile-group'
|
2020-02-19 06:15:37 -05:00
|
|
|
return (this.Settings.clsi.docker.env = { PATH: 'mock-path' })
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
afterEach(function () {
|
2020-02-20 11:24:28 -05:00
|
|
|
this.DockerRunner.stopContainerMonitor()
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('run', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.DockerRunner._getContainerOptions = sinon
|
|
|
|
.stub()
|
|
|
|
.returns((this.options = { mockoptions: 'foo' }))
|
|
|
|
this.DockerRunner._fingerprintContainer = sinon
|
|
|
|
.stub()
|
|
|
|
.returns((this.fingerprint = 'fingerprint'))
|
|
|
|
|
|
|
|
this.name = `project-${this.project_id}-${this.fingerprint}`
|
|
|
|
|
|
|
|
this.command = ['mock', 'command', '--outdir=$COMPILE_DIR']
|
|
|
|
this.command_with_dir = ['mock', 'command', '--outdir=/compile']
|
|
|
|
this.timeout = 42000
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.DockerRunner._runAndWaitForContainer = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, (this.output = 'mock-output'))
|
|
|
|
return this.DockerRunner.run(
|
|
|
|
this.project_id,
|
|
|
|
this.command,
|
|
|
|
this.directory,
|
|
|
|
this.image,
|
|
|
|
this.timeout,
|
|
|
|
this.env,
|
2020-06-11 11:01:44 -04:00
|
|
|
this.compileGroup,
|
2020-02-19 06:15:37 -05:00
|
|
|
(err, output) => {
|
|
|
|
this.callback(err, output)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should generate the options for the container', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner._getContainerOptions
|
|
|
|
.calledWith(
|
|
|
|
this.command_with_dir,
|
|
|
|
this.image,
|
|
|
|
this.volumes,
|
|
|
|
this.timeout
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should generate the fingerprint from the returned options', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner._fingerprintContainer
|
|
|
|
.calledWith(this.options)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should do the run', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner._runAndWaitForContainer
|
|
|
|
.calledWith(this.options, this.volumes, this.timeout)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWith(null, this.output).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when path.sandboxedCompilesHostDir is set', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.Settings.path.sandboxedCompilesHostDir = '/some/host/dir/compiles'
|
|
|
|
this.directory = '/var/lib/sharelatex/data/compiles/xyz'
|
|
|
|
this.DockerRunner._runAndWaitForContainer = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, (this.output = 'mock-output'))
|
|
|
|
return this.DockerRunner.run(
|
|
|
|
this.project_id,
|
|
|
|
this.command,
|
|
|
|
this.directory,
|
|
|
|
this.image,
|
|
|
|
this.timeout,
|
|
|
|
this.env,
|
2020-06-11 11:01:44 -04:00
|
|
|
this.compileGroup,
|
2020-02-19 06:15:37 -05:00
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should re-write the bind directory', function () {
|
2021-07-13 07:04:48 -04:00
|
|
|
const volumes =
|
|
|
|
this.DockerRunner._runAndWaitForContainer.lastCall.args[1]
|
2020-02-19 06:15:37 -05:00
|
|
|
return expect(volumes).to.deep.equal({
|
2021-07-13 07:04:48 -04:00
|
|
|
'/some/host/dir/compiles/xyz': '/compile',
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWith(null, this.output).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when the run throws an error', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
let firstTime = true
|
|
|
|
this.output = 'mock-output'
|
|
|
|
this.DockerRunner._runAndWaitForContainer = (
|
|
|
|
options,
|
|
|
|
volumes,
|
|
|
|
timeout,
|
|
|
|
callback
|
|
|
|
) => {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, output) {}
|
2020-02-19 06:15:37 -05:00
|
|
|
}
|
|
|
|
if (firstTime) {
|
|
|
|
firstTime = false
|
2020-03-27 05:39:45 -04:00
|
|
|
const error = new Error('(HTTP code 500) server error - ...')
|
|
|
|
error.statusCode = 500
|
|
|
|
return callback(error)
|
2020-02-19 06:15:37 -05:00
|
|
|
} else {
|
|
|
|
return callback(null, this.output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sinon.spy(this.DockerRunner, '_runAndWaitForContainer')
|
|
|
|
this.DockerRunner.destroyContainer = sinon.stub().callsArg(3)
|
|
|
|
return this.DockerRunner.run(
|
|
|
|
this.project_id,
|
|
|
|
this.command,
|
|
|
|
this.directory,
|
|
|
|
this.image,
|
|
|
|
this.timeout,
|
|
|
|
this.env,
|
2020-06-11 11:01:44 -04:00
|
|
|
this.compileGroup,
|
2020-02-19 06:15:37 -05:00
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should do the run twice', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner._runAndWaitForContainer.calledTwice.should.equal(
|
|
|
|
true
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should destroy the container in between', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner.destroyContainer
|
|
|
|
.calledWith(this.name, null)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWith(null, this.output).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with no image', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.DockerRunner._runAndWaitForContainer = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, (this.output = 'mock-output'))
|
|
|
|
return this.DockerRunner.run(
|
|
|
|
this.project_id,
|
|
|
|
this.command,
|
|
|
|
this.directory,
|
|
|
|
null,
|
|
|
|
this.timeout,
|
|
|
|
this.env,
|
2020-06-11 11:01:44 -04:00
|
|
|
this.compileGroup,
|
2020-02-19 06:15:37 -05:00
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should use the default image', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner._getContainerOptions
|
|
|
|
.calledWith(
|
|
|
|
this.command_with_dir,
|
|
|
|
this.defaultImage,
|
|
|
|
this.volumes,
|
|
|
|
this.timeout
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with image override', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.Settings.texliveImageNameOveride = 'overrideimage.com/something'
|
|
|
|
this.DockerRunner._runAndWaitForContainer = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, (this.output = 'mock-output'))
|
|
|
|
return this.DockerRunner.run(
|
|
|
|
this.project_id,
|
|
|
|
this.command,
|
|
|
|
this.directory,
|
|
|
|
this.image,
|
|
|
|
this.timeout,
|
|
|
|
this.env,
|
2020-06-11 11:01:44 -04:00
|
|
|
this.compileGroup,
|
2020-02-19 06:15:37 -05:00
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should use the override and keep the tag', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
const image = this.DockerRunner._getContainerOptions.args[0][1]
|
|
|
|
return image.should.equal('overrideimage.com/something/image:2016.2')
|
|
|
|
})
|
|
|
|
})
|
2020-06-30 07:00:18 -04:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with image restriction', function () {
|
|
|
|
beforeEach(function () {
|
2020-06-30 07:00:18 -04:00
|
|
|
this.Settings.clsi.docker.allowedImages = [
|
|
|
|
'repo/image:tag1',
|
2021-07-13 07:04:48 -04:00
|
|
|
'repo/image:tag2',
|
2020-06-30 07:00:18 -04:00
|
|
|
]
|
|
|
|
this.DockerRunner._runAndWaitForContainer = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, (this.output = 'mock-output'))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with a valid image', function () {
|
|
|
|
beforeEach(function () {
|
2020-06-30 07:00:18 -04:00
|
|
|
this.DockerRunner.run(
|
|
|
|
this.project_id,
|
|
|
|
this.command,
|
|
|
|
this.directory,
|
|
|
|
'repo/image:tag1',
|
|
|
|
this.timeout,
|
|
|
|
this.env,
|
|
|
|
this.compileGroup,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should setup the container', function () {
|
2020-06-30 07:00:18 -04:00
|
|
|
this.DockerRunner._getContainerOptions.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with a invalid image', function () {
|
|
|
|
beforeEach(function () {
|
2020-06-30 07:00:18 -04:00
|
|
|
this.DockerRunner.run(
|
|
|
|
this.project_id,
|
|
|
|
this.command,
|
|
|
|
this.directory,
|
|
|
|
'something/different:evil',
|
|
|
|
this.timeout,
|
|
|
|
this.env,
|
|
|
|
this.compileGroup,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the callback with an error', function () {
|
2020-06-30 07:00:18 -04:00
|
|
|
const err = new Error('image not allowed')
|
|
|
|
this.callback.called.should.equal(true)
|
|
|
|
this.callback.args[0][0].message.should.equal(err.message)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not setup the container', function () {
|
2020-06-30 07:00:18 -04:00
|
|
|
this.DockerRunner._getContainerOptions.called.should.equal(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('run with _getOptions', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-06-11 11:01:44 -04:00
|
|
|
// this.DockerRunner._getContainerOptions = sinon
|
|
|
|
// .stub()
|
|
|
|
// .returns((this.options = { mockoptions: 'foo' }))
|
|
|
|
this.DockerRunner._fingerprintContainer = sinon
|
|
|
|
.stub()
|
|
|
|
.returns((this.fingerprint = 'fingerprint'))
|
|
|
|
|
|
|
|
this.name = `project-${this.project_id}-${this.fingerprint}`
|
|
|
|
|
|
|
|
this.command = ['mock', 'command', '--outdir=$COMPILE_DIR']
|
|
|
|
this.command_with_dir = ['mock', 'command', '--outdir=/compile']
|
|
|
|
this.timeout = 42000
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when a compile group config is set', function () {
|
|
|
|
beforeEach(function () {
|
2020-06-11 11:01:44 -04:00
|
|
|
this.Settings.clsi.docker.compileGroupConfig = {
|
|
|
|
'compile-group': {
|
2021-07-13 07:04:48 -04:00
|
|
|
'HostConfig.newProperty': 'new-property',
|
2020-06-11 11:01:44 -04:00
|
|
|
},
|
2021-07-13 07:04:48 -04:00
|
|
|
'other-group': { otherProperty: 'other-property' },
|
2020-06-11 11:01:44 -04:00
|
|
|
}
|
|
|
|
this.DockerRunner._runAndWaitForContainer = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, (this.output = 'mock-output'))
|
|
|
|
return this.DockerRunner.run(
|
|
|
|
this.project_id,
|
|
|
|
this.command,
|
|
|
|
this.directory,
|
|
|
|
this.image,
|
|
|
|
this.timeout,
|
|
|
|
this.env,
|
|
|
|
this.compileGroup,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should set the docker options for the compile group', function () {
|
2021-07-13 07:04:48 -04:00
|
|
|
const options =
|
|
|
|
this.DockerRunner._runAndWaitForContainer.lastCall.args[0]
|
2020-06-11 11:01:44 -04:00
|
|
|
return expect(options.HostConfig).to.deep.include({
|
|
|
|
Binds: ['/local/compile/directory:/compile:rw'],
|
|
|
|
LogConfig: { Type: 'none', Config: {} },
|
|
|
|
CapDrop: 'ALL',
|
|
|
|
SecurityOpt: ['no-new-privileges'],
|
2021-07-13 07:04:48 -04:00
|
|
|
newProperty: 'new-property',
|
2020-06-11 11:01:44 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-06-11 11:01:44 -04:00
|
|
|
return this.callback.calledWith(null, this.output).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('_runAndWaitForContainer', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.options = { mockoptions: 'foo', name: (this.name = 'mock-name') }
|
|
|
|
this.DockerRunner.startContainer = (
|
|
|
|
options,
|
|
|
|
volumes,
|
|
|
|
attachStreamHandler,
|
|
|
|
callback
|
|
|
|
) => {
|
|
|
|
attachStreamHandler(null, (this.output = 'mock-output'))
|
|
|
|
return callback(null, (this.containerId = 'container-id'))
|
|
|
|
}
|
|
|
|
sinon.spy(this.DockerRunner, 'startContainer')
|
|
|
|
this.DockerRunner.waitForContainer = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, (this.exitCode = 42))
|
|
|
|
return this.DockerRunner._runAndWaitForContainer(
|
|
|
|
this.options,
|
|
|
|
this.volumes,
|
|
|
|
this.timeout,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should create/start the container', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner.startContainer
|
|
|
|
.calledWith(this.options, this.volumes)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should wait for the container to finish', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner.waitForContainer
|
|
|
|
.calledWith(this.name, this.timeout)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback with the output', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWith(null, this.output).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('startContainer', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.attachStreamHandler = sinon.stub()
|
|
|
|
this.attachStreamHandler.cock = true
|
|
|
|
this.options = { mockoptions: 'foo', name: 'mock-name' }
|
|
|
|
this.container.inspect = sinon.stub().callsArgWith(0)
|
|
|
|
this.DockerRunner.attachToContainer = (
|
|
|
|
containerId,
|
|
|
|
attachStreamHandler,
|
|
|
|
cb
|
|
|
|
) => {
|
|
|
|
attachStreamHandler()
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
return sinon.spy(this.DockerRunner, 'attachToContainer')
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when the container exists', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.container.inspect = sinon.stub().callsArgWith(0)
|
|
|
|
this.container.start = sinon.stub().yields()
|
|
|
|
|
|
|
|
return this.DockerRunner.startContainer(
|
|
|
|
this.options,
|
|
|
|
this.volumes,
|
2020-04-10 06:25:55 -04:00
|
|
|
() => {},
|
|
|
|
this.callback
|
2020-02-19 06:15:37 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should start the container with the given name', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.getContainer.calledWith(this.options.name).should.equal(true)
|
|
|
|
return this.container.start.called.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not try to create the container', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.createContainer.called.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should attach to the container', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner.attachToContainer.called.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should attach before the container starts', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return sinon.assert.callOrder(
|
|
|
|
this.DockerRunner.attachToContainer,
|
|
|
|
this.container.start
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when the container does not exist', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
const exists = false
|
|
|
|
this.container.start = sinon.stub().yields()
|
|
|
|
this.container.inspect = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(0, { statusCode: 404 })
|
|
|
|
return this.DockerRunner.startContainer(
|
|
|
|
this.options,
|
|
|
|
this.volumes,
|
|
|
|
this.attachStreamHandler,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should create the container', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.createContainer.calledWith(this.options).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the callback and stream handler', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.attachStreamHandler.called.should.equal(true)
|
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should attach to the container', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner.attachToContainer.called.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should attach before the container starts', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return sinon.assert.callOrder(
|
|
|
|
this.DockerRunner.attachToContainer,
|
|
|
|
this.container.start
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when the container is already running', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
const error = new Error(
|
|
|
|
`HTTP code is 304 which indicates error: server error - start: Cannot start container ${this.name}: The container MOCKID is already running.`
|
|
|
|
)
|
|
|
|
error.statusCode = 304
|
|
|
|
this.container.start = sinon.stub().yields(error)
|
|
|
|
this.container.inspect = sinon.stub().callsArgWith(0)
|
|
|
|
return this.DockerRunner.startContainer(
|
|
|
|
this.options,
|
|
|
|
this.volumes,
|
|
|
|
this.attachStreamHandler,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not try to create the container', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.createContainer.called.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback and stream handler without an error', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.attachStreamHandler.called.should.equal(true)
|
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when a volume does not exist', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.fs.stat = sinon.stub().yields(new Error('no such path'))
|
|
|
|
return this.DockerRunner.startContainer(
|
|
|
|
this.options,
|
|
|
|
this.volumes,
|
|
|
|
this.attachStreamHandler,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not try to create the container', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.createContainer.called.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the callback with an error', function () {
|
2020-03-12 05:35:11 -04:00
|
|
|
this.callback.calledWith(sinon.match(Error)).should.equal(true)
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when a volume exists but is not a directory', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.fs.stat = sinon.stub().yields(null, {
|
|
|
|
isDirectory() {
|
|
|
|
return false
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
return this.DockerRunner.startContainer(
|
|
|
|
this.options,
|
|
|
|
this.volumes,
|
|
|
|
this.attachStreamHandler,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not try to create the container', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.createContainer.called.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the callback with an error', function () {
|
2020-03-12 05:35:11 -04:00
|
|
|
this.callback.calledWith(sinon.match(Error)).should.equal(true)
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when a volume does not exist, but sibling-containers are used', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.fs.stat = sinon.stub().yields(new Error('no such path'))
|
|
|
|
this.Settings.path.sandboxedCompilesHostDir = '/some/path'
|
|
|
|
this.container.start = sinon.stub().yields()
|
|
|
|
return this.DockerRunner.startContainer(
|
|
|
|
this.options,
|
|
|
|
this.volumes,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
afterEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return delete this.Settings.path.sandboxedCompilesHostDir
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should start the container with the given name', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.getContainer.calledWith(this.options.name).should.equal(true)
|
|
|
|
return this.container.start.called.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not try to create the container', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.createContainer.called.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.callback.called.should.equal(true)
|
|
|
|
return this.callback.calledWith(new Error()).should.equal(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('when the container tries to be created, but already has been (race condition)', function () {})
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('waitForContainer', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.containerId = 'container-id'
|
|
|
|
this.timeout = 5000
|
|
|
|
this.container.wait = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, { StatusCode: (this.statusCode = 42) })
|
|
|
|
return (this.container.kill = sinon.stub().yields())
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when the container returns in time', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner.waitForContainer(
|
|
|
|
this.containerId,
|
|
|
|
this.timeout,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should wait for the container', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.getContainer.calledWith(this.containerId).should.equal(true)
|
|
|
|
return this.container.wait.called.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback with the exit', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(null, this.statusCode)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('when the container does not return before the timeout', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.container.wait = function (callback) {
|
2020-02-19 06:15:37 -05:00
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, exitCode) {}
|
2020-02-19 06:15:37 -05:00
|
|
|
}
|
|
|
|
return setTimeout(() => callback(null, { StatusCode: 42 }), 100)
|
|
|
|
}
|
|
|
|
this.timeout = 5
|
|
|
|
return this.DockerRunner.waitForContainer(
|
|
|
|
this.containerId,
|
|
|
|
this.timeout,
|
|
|
|
(...args) => {
|
|
|
|
this.callback(...Array.from(args || []))
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call kill on the container', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.getContainer.calledWith(this.containerId).should.equal(true)
|
|
|
|
return this.container.kill.called.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the callback with an error', function () {
|
2020-03-12 05:35:11 -04:00
|
|
|
this.callback.calledWith(sinon.match(Error)).should.equal(true)
|
|
|
|
|
|
|
|
const errorObj = this.callback.args[0][0]
|
|
|
|
expect(errorObj.message).to.include('container timed out')
|
|
|
|
expect(errorObj.timedout).equal(true)
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('destroyOldContainers', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
const oneHourInSeconds = 60 * 60
|
|
|
|
const oneHourInMilliseconds = oneHourInSeconds * 1000
|
|
|
|
const nowInSeconds = Date.now() / 1000
|
|
|
|
this.containers = [
|
|
|
|
{
|
|
|
|
Name: '/project-old-container-name',
|
|
|
|
Id: 'old-container-id',
|
2021-07-13 07:04:48 -04:00
|
|
|
Created: nowInSeconds - oneHourInSeconds - 100,
|
2020-02-19 06:15:37 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: '/project-new-container-name',
|
|
|
|
Id: 'new-container-id',
|
2021-07-13 07:04:48 -04:00
|
|
|
Created: nowInSeconds - oneHourInSeconds + 100,
|
2020-02-19 06:15:37 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: '/totally-not-a-project-container',
|
|
|
|
Id: 'some-random-id',
|
2021-07-13 07:04:48 -04:00
|
|
|
Created: nowInSeconds - 2 * oneHourInSeconds,
|
|
|
|
},
|
2020-02-19 06:15:37 -05:00
|
|
|
]
|
|
|
|
this.DockerRunner.MAX_CONTAINER_AGE = oneHourInMilliseconds
|
|
|
|
this.listContainers.callsArgWith(1, null, this.containers)
|
|
|
|
this.DockerRunner.destroyContainer = sinon.stub().callsArg(3)
|
2021-07-13 07:04:48 -04:00
|
|
|
return this.DockerRunner.destroyOldContainers(error => {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.callback(error)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should list all containers', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.listContainers.calledWith({ all: true }).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should destroy old containers', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.DockerRunner.destroyContainer.callCount.should.equal(1)
|
|
|
|
return this.DockerRunner.destroyContainer
|
2019-11-27 15:40:48 -05:00
|
|
|
.calledWith('project-old-container-name', 'old-container-id')
|
2020-02-19 06:15:37 -05:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not destroy new containers', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner.destroyContainer
|
2019-11-27 15:40:48 -05:00
|
|
|
.calledWith('project-new-container-name', 'new-container-id')
|
2020-02-19 06:15:37 -05:00
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not destroy non-project containers', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner.destroyContainer
|
2019-11-27 15:40:48 -05:00
|
|
|
.calledWith('totally-not-a-project-container', 'some-random-id')
|
2020-02-19 06:15:37 -05:00
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should callback the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('_destroyContainer', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.containerId = 'some_id'
|
|
|
|
this.fakeContainer = { remove: sinon.stub().callsArgWith(1, null) }
|
|
|
|
return (this.Docker.prototype.getContainer = sinon
|
|
|
|
.stub()
|
|
|
|
.returns(this.fakeContainer))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should get the container', function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner._destroyContainer(
|
|
|
|
this.containerId,
|
|
|
|
false,
|
2021-07-13 07:04:48 -04:00
|
|
|
err => {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.Docker.prototype.getContainer.callCount.should.equal(1)
|
|
|
|
this.Docker.prototype.getContainer
|
|
|
|
.calledWith(this.containerId)
|
|
|
|
.should.equal(true)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should try to force-destroy the container when shouldForce=true', function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner._destroyContainer(
|
|
|
|
this.containerId,
|
|
|
|
true,
|
2021-07-13 07:04:48 -04:00
|
|
|
err => {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.fakeContainer.remove.callCount.should.equal(1)
|
|
|
|
this.fakeContainer.remove
|
2020-09-04 11:34:08 -04:00
|
|
|
.calledWith({ force: true, v: true })
|
2020-02-19 06:15:37 -05:00
|
|
|
.should.equal(true)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not try to force-destroy the container when shouldForce=false', function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner._destroyContainer(
|
|
|
|
this.containerId,
|
|
|
|
false,
|
2021-07-13 07:04:48 -04:00
|
|
|
err => {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.fakeContainer.remove.callCount.should.equal(1)
|
|
|
|
this.fakeContainer.remove
|
2020-09-04 11:34:08 -04:00
|
|
|
.calledWith({ force: false, v: true })
|
2020-02-19 06:15:37 -05:00
|
|
|
.should.equal(true)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not produce an error', function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner._destroyContainer(
|
|
|
|
this.containerId,
|
|
|
|
false,
|
2021-07-13 07:04:48 -04:00
|
|
|
err => {
|
2020-02-19 06:15:37 -05:00
|
|
|
expect(err).to.equal(null)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when the container is already gone', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.fakeError = new Error('woops')
|
|
|
|
this.fakeError.statusCode = 404
|
|
|
|
this.fakeContainer = {
|
2021-07-13 07:04:48 -04:00
|
|
|
remove: sinon.stub().callsArgWith(1, this.fakeError),
|
2020-02-19 06:15:37 -05:00
|
|
|
}
|
|
|
|
return (this.Docker.prototype.getContainer = sinon
|
|
|
|
.stub()
|
|
|
|
.returns(this.fakeContainer))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should not produce an error', function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner._destroyContainer(
|
|
|
|
this.containerId,
|
|
|
|
false,
|
2021-07-13 07:04:48 -04:00
|
|
|
err => {
|
2020-02-19 06:15:37 -05:00
|
|
|
expect(err).to.equal(null)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('when container.destroy produces an error', function (done) {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.fakeError = new Error('woops')
|
|
|
|
this.fakeError.statusCode = 500
|
|
|
|
this.fakeContainer = {
|
2021-07-13 07:04:48 -04:00
|
|
|
remove: sinon.stub().callsArgWith(1, this.fakeError),
|
2020-02-19 06:15:37 -05:00
|
|
|
}
|
|
|
|
return (this.Docker.prototype.getContainer = sinon
|
|
|
|
.stub()
|
|
|
|
.returns(this.fakeContainer))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should produce an error', function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.DockerRunner._destroyContainer(
|
|
|
|
this.containerId,
|
|
|
|
false,
|
2021-07-13 07:04:48 -04:00
|
|
|
err => {
|
2020-02-19 06:15:37 -05:00
|
|
|
expect(err).to.not.equal(null)
|
|
|
|
expect(err).to.equal(this.fakeError)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('kill', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.containerId = 'some_id'
|
|
|
|
this.fakeContainer = { kill: sinon.stub().callsArgWith(0, null) }
|
|
|
|
return (this.Docker.prototype.getContainer = sinon
|
|
|
|
.stub()
|
|
|
|
.returns(this.fakeContainer))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should get the container', function (done) {
|
2021-07-13 07:04:48 -04:00
|
|
|
return this.DockerRunner.kill(this.containerId, err => {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.Docker.prototype.getContainer.callCount.should.equal(1)
|
|
|
|
this.Docker.prototype.getContainer
|
|
|
|
.calledWith(this.containerId)
|
|
|
|
.should.equal(true)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should try to force-destroy the container', function (done) {
|
2021-07-13 07:04:48 -04:00
|
|
|
return this.DockerRunner.kill(this.containerId, err => {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.fakeContainer.kill.callCount.should.equal(1)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not produce an error', function (done) {
|
2021-07-13 07:04:48 -04:00
|
|
|
return this.DockerRunner.kill(this.containerId, err => {
|
2020-02-19 06:15:37 -05:00
|
|
|
expect(err).to.equal(undefined)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when the container is not actually running', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.fakeError = new Error('woops')
|
|
|
|
this.fakeError.statusCode = 500
|
|
|
|
this.fakeError.message =
|
|
|
|
'Cannot kill container <whatever> is not running'
|
|
|
|
this.fakeContainer = {
|
2021-07-13 07:04:48 -04:00
|
|
|
kill: sinon.stub().callsArgWith(0, this.fakeError),
|
2020-02-19 06:15:37 -05:00
|
|
|
}
|
|
|
|
return (this.Docker.prototype.getContainer = sinon
|
|
|
|
.stub()
|
|
|
|
.returns(this.fakeContainer))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should not produce an error', function (done) {
|
2021-07-13 07:04:48 -04:00
|
|
|
return this.DockerRunner.kill(this.containerId, err => {
|
2020-02-19 06:15:37 -05:00
|
|
|
expect(err).to.equal(undefined)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('when container.kill produces a legitimate error', function (done) {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.fakeError = new Error('woops')
|
|
|
|
this.fakeError.statusCode = 500
|
|
|
|
this.fakeError.message = 'Totally legitimate reason to throw an error'
|
|
|
|
this.fakeContainer = {
|
2021-07-13 07:04:48 -04:00
|
|
|
kill: sinon.stub().callsArgWith(0, this.fakeError),
|
2020-02-19 06:15:37 -05:00
|
|
|
}
|
|
|
|
return (this.Docker.prototype.getContainer = sinon
|
|
|
|
.stub()
|
|
|
|
.returns(this.fakeContainer))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should produce an error', function (done) {
|
2021-07-13 07:04:48 -04:00
|
|
|
return this.DockerRunner.kill(this.containerId, err => {
|
2020-02-19 06:15:37 -05:00
|
|
|
expect(err).to.not.equal(undefined)
|
|
|
|
expect(err).to.equal(this.fakeError)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|