DockerRunnerTests.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /* eslint-disable
  2. handle-callback-err,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS206: Consider reworking classes to avoid initClass
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. const SandboxedModule = require('sandboxed-module')
  17. const sinon = require('sinon')
  18. require('chai').should()
  19. const { expect } = require('chai')
  20. const modulePath = require('path').join(
  21. __dirname,
  22. '../../../app/js/DockerRunner'
  23. )
  24. const Path = require('path')
  25. describe('DockerRunner', function() {
  26. beforeEach(function() {
  27. let container, Docker, Timer
  28. this.container = container = {}
  29. this.DockerRunner = SandboxedModule.require(modulePath, {
  30. requires: {
  31. 'settings-sharelatex': (this.Settings = {
  32. clsi: { docker: {} },
  33. path: {}
  34. }),
  35. 'logger-sharelatex': (this.logger = {
  36. log: sinon.stub(),
  37. error: sinon.stub(),
  38. info: sinon.stub(),
  39. warn: sinon.stub()
  40. }),
  41. dockerode: (Docker = (function() {
  42. Docker = class Docker {
  43. static initClass() {
  44. this.prototype.getContainer = sinon.stub().returns(container)
  45. this.prototype.createContainer = sinon
  46. .stub()
  47. .yields(null, container)
  48. this.prototype.listContainers = sinon.stub()
  49. }
  50. }
  51. Docker.initClass()
  52. return Docker
  53. })()),
  54. fs: (this.fs = {
  55. stat: sinon.stub().yields(null, {
  56. isDirectory() {
  57. return true
  58. }
  59. })
  60. }),
  61. './Metrics': {
  62. Timer: (Timer = class Timer {
  63. done() {}
  64. })
  65. },
  66. './LockManager': {
  67. runWithLock(key, runner, callback) {
  68. return runner(callback)
  69. }
  70. }
  71. }
  72. })
  73. this.Docker = Docker
  74. this.getContainer = Docker.prototype.getContainer
  75. this.createContainer = Docker.prototype.createContainer
  76. this.listContainers = Docker.prototype.listContainers
  77. this.directory = '/local/compile/directory'
  78. this.mainFile = 'main-file.tex'
  79. this.compiler = 'pdflatex'
  80. this.image = 'example.com/sharelatex/image:2016.2'
  81. this.env = {}
  82. this.callback = sinon.stub()
  83. this.project_id = 'project-id-123'
  84. this.volumes = { '/local/compile/directory': '/compile' }
  85. this.Settings.clsi.docker.image = this.defaultImage = 'default-image'
  86. return (this.Settings.clsi.docker.env = { PATH: 'mock-path' })
  87. })
  88. afterEach(function() {
  89. this.DockerRunner.stopContainerMonitor()
  90. })
  91. describe('run', function() {
  92. beforeEach(function(done) {
  93. this.DockerRunner._getContainerOptions = sinon
  94. .stub()
  95. .returns((this.options = { mockoptions: 'foo' }))
  96. this.DockerRunner._fingerprintContainer = sinon
  97. .stub()
  98. .returns((this.fingerprint = 'fingerprint'))
  99. this.name = `project-${this.project_id}-${this.fingerprint}`
  100. this.command = ['mock', 'command', '--outdir=$COMPILE_DIR']
  101. this.command_with_dir = ['mock', 'command', '--outdir=/compile']
  102. this.timeout = 42000
  103. return done()
  104. })
  105. describe('successfully', function() {
  106. beforeEach(function(done) {
  107. this.DockerRunner._runAndWaitForContainer = sinon
  108. .stub()
  109. .callsArgWith(3, null, (this.output = 'mock-output'))
  110. return this.DockerRunner.run(
  111. this.project_id,
  112. this.command,
  113. this.directory,
  114. this.image,
  115. this.timeout,
  116. this.env,
  117. (err, output) => {
  118. this.callback(err, output)
  119. return done()
  120. }
  121. )
  122. })
  123. it('should generate the options for the container', function() {
  124. return this.DockerRunner._getContainerOptions
  125. .calledWith(
  126. this.command_with_dir,
  127. this.image,
  128. this.volumes,
  129. this.timeout
  130. )
  131. .should.equal(true)
  132. })
  133. it('should generate the fingerprint from the returned options', function() {
  134. return this.DockerRunner._fingerprintContainer
  135. .calledWith(this.options)
  136. .should.equal(true)
  137. })
  138. it('should do the run', function() {
  139. return this.DockerRunner._runAndWaitForContainer
  140. .calledWith(this.options, this.volumes, this.timeout)
  141. .should.equal(true)
  142. })
  143. return it('should call the callback', function() {
  144. return this.callback.calledWith(null, this.output).should.equal(true)
  145. })
  146. })
  147. describe('when path.sandboxedCompilesHostDir is set', function() {
  148. beforeEach(function() {
  149. this.Settings.path.sandboxedCompilesHostDir = '/some/host/dir/compiles'
  150. this.directory = '/var/lib/sharelatex/data/compiles/xyz'
  151. this.DockerRunner._runAndWaitForContainer = sinon
  152. .stub()
  153. .callsArgWith(3, null, (this.output = 'mock-output'))
  154. return this.DockerRunner.run(
  155. this.project_id,
  156. this.command,
  157. this.directory,
  158. this.image,
  159. this.timeout,
  160. this.env,
  161. this.callback
  162. )
  163. })
  164. it('should re-write the bind directory', function() {
  165. const volumes = this.DockerRunner._runAndWaitForContainer.lastCall
  166. .args[1]
  167. return expect(volumes).to.deep.equal({
  168. '/some/host/dir/compiles/xyz': '/compile'
  169. })
  170. })
  171. return it('should call the callback', function() {
  172. return this.callback.calledWith(null, this.output).should.equal(true)
  173. })
  174. })
  175. describe('when the run throws an error', function() {
  176. beforeEach(function() {
  177. let firstTime = true
  178. this.output = 'mock-output'
  179. this.DockerRunner._runAndWaitForContainer = (
  180. options,
  181. volumes,
  182. timeout,
  183. callback
  184. ) => {
  185. if (callback == null) {
  186. callback = function(error, output) {}
  187. }
  188. if (firstTime) {
  189. firstTime = false
  190. return callback(
  191. new Error('HTTP code is 500 which indicates error: server error')
  192. )
  193. } else {
  194. return callback(null, this.output)
  195. }
  196. }
  197. sinon.spy(this.DockerRunner, '_runAndWaitForContainer')
  198. this.DockerRunner.destroyContainer = sinon.stub().callsArg(3)
  199. return this.DockerRunner.run(
  200. this.project_id,
  201. this.command,
  202. this.directory,
  203. this.image,
  204. this.timeout,
  205. this.env,
  206. this.callback
  207. )
  208. })
  209. it('should do the run twice', function() {
  210. return this.DockerRunner._runAndWaitForContainer.calledTwice.should.equal(
  211. true
  212. )
  213. })
  214. it('should destroy the container in between', function() {
  215. return this.DockerRunner.destroyContainer
  216. .calledWith(this.name, null)
  217. .should.equal(true)
  218. })
  219. return it('should call the callback', function() {
  220. return this.callback.calledWith(null, this.output).should.equal(true)
  221. })
  222. })
  223. describe('with no image', function() {
  224. beforeEach(function() {
  225. this.DockerRunner._runAndWaitForContainer = sinon
  226. .stub()
  227. .callsArgWith(3, null, (this.output = 'mock-output'))
  228. return this.DockerRunner.run(
  229. this.project_id,
  230. this.command,
  231. this.directory,
  232. null,
  233. this.timeout,
  234. this.env,
  235. this.callback
  236. )
  237. })
  238. return it('should use the default image', function() {
  239. return this.DockerRunner._getContainerOptions
  240. .calledWith(
  241. this.command_with_dir,
  242. this.defaultImage,
  243. this.volumes,
  244. this.timeout
  245. )
  246. .should.equal(true)
  247. })
  248. })
  249. return describe('with image override', function() {
  250. beforeEach(function() {
  251. this.Settings.texliveImageNameOveride = 'overrideimage.com/something'
  252. this.DockerRunner._runAndWaitForContainer = sinon
  253. .stub()
  254. .callsArgWith(3, null, (this.output = 'mock-output'))
  255. return this.DockerRunner.run(
  256. this.project_id,
  257. this.command,
  258. this.directory,
  259. this.image,
  260. this.timeout,
  261. this.env,
  262. this.callback
  263. )
  264. })
  265. return it('should use the override and keep the tag', function() {
  266. const image = this.DockerRunner._getContainerOptions.args[0][1]
  267. return image.should.equal('overrideimage.com/something/image:2016.2')
  268. })
  269. })
  270. })
  271. describe('_runAndWaitForContainer', function() {
  272. beforeEach(function() {
  273. this.options = { mockoptions: 'foo', name: (this.name = 'mock-name') }
  274. this.DockerRunner.startContainer = (
  275. options,
  276. volumes,
  277. attachStreamHandler,
  278. callback
  279. ) => {
  280. attachStreamHandler(null, (this.output = 'mock-output'))
  281. return callback(null, (this.containerId = 'container-id'))
  282. }
  283. sinon.spy(this.DockerRunner, 'startContainer')
  284. this.DockerRunner.waitForContainer = sinon
  285. .stub()
  286. .callsArgWith(2, null, (this.exitCode = 42))
  287. return this.DockerRunner._runAndWaitForContainer(
  288. this.options,
  289. this.volumes,
  290. this.timeout,
  291. this.callback
  292. )
  293. })
  294. it('should create/start the container', function() {
  295. return this.DockerRunner.startContainer
  296. .calledWith(this.options, this.volumes)
  297. .should.equal(true)
  298. })
  299. it('should wait for the container to finish', function() {
  300. return this.DockerRunner.waitForContainer
  301. .calledWith(this.name, this.timeout)
  302. .should.equal(true)
  303. })
  304. return it('should call the callback with the output', function() {
  305. return this.callback.calledWith(null, this.output).should.equal(true)
  306. })
  307. })
  308. describe('startContainer', function() {
  309. beforeEach(function() {
  310. this.attachStreamHandler = sinon.stub()
  311. this.attachStreamHandler.cock = true
  312. this.options = { mockoptions: 'foo', name: 'mock-name' }
  313. this.container.inspect = sinon.stub().callsArgWith(0)
  314. this.DockerRunner.attachToContainer = (
  315. containerId,
  316. attachStreamHandler,
  317. cb
  318. ) => {
  319. attachStreamHandler()
  320. return cb()
  321. }
  322. return sinon.spy(this.DockerRunner, 'attachToContainer')
  323. })
  324. describe('when the container exists', function() {
  325. beforeEach(function() {
  326. this.container.inspect = sinon.stub().callsArgWith(0)
  327. this.container.start = sinon.stub().yields()
  328. return this.DockerRunner.startContainer(
  329. this.options,
  330. this.volumes,
  331. this.callback,
  332. () => {}
  333. )
  334. })
  335. it('should start the container with the given name', function() {
  336. this.getContainer.calledWith(this.options.name).should.equal(true)
  337. return this.container.start.called.should.equal(true)
  338. })
  339. it('should not try to create the container', function() {
  340. return this.createContainer.called.should.equal(false)
  341. })
  342. it('should attach to the container', function() {
  343. return this.DockerRunner.attachToContainer.called.should.equal(true)
  344. })
  345. it('should call the callback', function() {
  346. return this.callback.called.should.equal(true)
  347. })
  348. return it('should attach before the container starts', function() {
  349. return sinon.assert.callOrder(
  350. this.DockerRunner.attachToContainer,
  351. this.container.start
  352. )
  353. })
  354. })
  355. describe('when the container does not exist', function() {
  356. beforeEach(function() {
  357. const exists = false
  358. this.container.start = sinon.stub().yields()
  359. this.container.inspect = sinon
  360. .stub()
  361. .callsArgWith(0, { statusCode: 404 })
  362. return this.DockerRunner.startContainer(
  363. this.options,
  364. this.volumes,
  365. this.attachStreamHandler,
  366. this.callback
  367. )
  368. })
  369. it('should create the container', function() {
  370. return this.createContainer.calledWith(this.options).should.equal(true)
  371. })
  372. it('should call the callback and stream handler', function() {
  373. this.attachStreamHandler.called.should.equal(true)
  374. return this.callback.called.should.equal(true)
  375. })
  376. it('should attach to the container', function() {
  377. return this.DockerRunner.attachToContainer.called.should.equal(true)
  378. })
  379. return it('should attach before the container starts', function() {
  380. return sinon.assert.callOrder(
  381. this.DockerRunner.attachToContainer,
  382. this.container.start
  383. )
  384. })
  385. })
  386. describe('when the container is already running', function() {
  387. beforeEach(function() {
  388. const error = new Error(
  389. `HTTP code is 304 which indicates error: server error - start: Cannot start container ${this.name}: The container MOCKID is already running.`
  390. )
  391. error.statusCode = 304
  392. this.container.start = sinon.stub().yields(error)
  393. this.container.inspect = sinon.stub().callsArgWith(0)
  394. return this.DockerRunner.startContainer(
  395. this.options,
  396. this.volumes,
  397. this.attachStreamHandler,
  398. this.callback
  399. )
  400. })
  401. it('should not try to create the container', function() {
  402. return this.createContainer.called.should.equal(false)
  403. })
  404. return it('should call the callback and stream handler without an error', function() {
  405. this.attachStreamHandler.called.should.equal(true)
  406. return this.callback.called.should.equal(true)
  407. })
  408. })
  409. describe('when a volume does not exist', function() {
  410. beforeEach(function() {
  411. this.fs.stat = sinon.stub().yields(new Error('no such path'))
  412. return this.DockerRunner.startContainer(
  413. this.options,
  414. this.volumes,
  415. this.attachStreamHandler,
  416. this.callback
  417. )
  418. })
  419. it('should not try to create the container', function() {
  420. return this.createContainer.called.should.equal(false)
  421. })
  422. it('should call the callback with an error', function() {
  423. this.callback.calledWith(sinon.match(Error)).should.equal(true)
  424. })
  425. })
  426. describe('when a volume exists but is not a directory', function() {
  427. beforeEach(function() {
  428. this.fs.stat = sinon.stub().yields(null, {
  429. isDirectory() {
  430. return false
  431. }
  432. })
  433. return this.DockerRunner.startContainer(
  434. this.options,
  435. this.volumes,
  436. this.attachStreamHandler,
  437. this.callback
  438. )
  439. })
  440. it('should not try to create the container', function() {
  441. return this.createContainer.called.should.equal(false)
  442. })
  443. it('should call the callback with an error', function() {
  444. this.callback.calledWith(sinon.match(Error)).should.equal(true)
  445. })
  446. })
  447. describe('when a volume does not exist, but sibling-containers are used', function() {
  448. beforeEach(function() {
  449. this.fs.stat = sinon.stub().yields(new Error('no such path'))
  450. this.Settings.path.sandboxedCompilesHostDir = '/some/path'
  451. this.container.start = sinon.stub().yields()
  452. return this.DockerRunner.startContainer(
  453. this.options,
  454. this.volumes,
  455. this.callback
  456. )
  457. })
  458. afterEach(function() {
  459. return delete this.Settings.path.sandboxedCompilesHostDir
  460. })
  461. it('should start the container with the given name', function() {
  462. this.getContainer.calledWith(this.options.name).should.equal(true)
  463. return this.container.start.called.should.equal(true)
  464. })
  465. it('should not try to create the container', function() {
  466. return this.createContainer.called.should.equal(false)
  467. })
  468. return it('should call the callback', function() {
  469. this.callback.called.should.equal(true)
  470. return this.callback.calledWith(new Error()).should.equal(false)
  471. })
  472. })
  473. return describe('when the container tries to be created, but already has been (race condition)', function() {})
  474. })
  475. describe('waitForContainer', function() {
  476. beforeEach(function() {
  477. this.containerId = 'container-id'
  478. this.timeout = 5000
  479. this.container.wait = sinon
  480. .stub()
  481. .yields(null, { StatusCode: (this.statusCode = 42) })
  482. return (this.container.kill = sinon.stub().yields())
  483. })
  484. describe('when the container returns in time', function() {
  485. beforeEach(function() {
  486. return this.DockerRunner.waitForContainer(
  487. this.containerId,
  488. this.timeout,
  489. this.callback
  490. )
  491. })
  492. it('should wait for the container', function() {
  493. this.getContainer.calledWith(this.containerId).should.equal(true)
  494. return this.container.wait.called.should.equal(true)
  495. })
  496. return it('should call the callback with the exit', function() {
  497. return this.callback
  498. .calledWith(null, this.statusCode)
  499. .should.equal(true)
  500. })
  501. })
  502. return describe('when the container does not return before the timeout', function() {
  503. beforeEach(function(done) {
  504. this.container.wait = function(callback) {
  505. if (callback == null) {
  506. callback = function(error, exitCode) {}
  507. }
  508. return setTimeout(() => callback(null, { StatusCode: 42 }), 100)
  509. }
  510. this.timeout = 5
  511. return this.DockerRunner.waitForContainer(
  512. this.containerId,
  513. this.timeout,
  514. (...args) => {
  515. this.callback(...Array.from(args || []))
  516. return done()
  517. }
  518. )
  519. })
  520. it('should call kill on the container', function() {
  521. this.getContainer.calledWith(this.containerId).should.equal(true)
  522. return this.container.kill.called.should.equal(true)
  523. })
  524. it('should call the callback with an error', function() {
  525. this.callback.calledWith(sinon.match(Error)).should.equal(true)
  526. const errorObj = this.callback.args[0][0]
  527. expect(errorObj.message).to.include('container timed out')
  528. expect(errorObj.timedout).equal(true)
  529. })
  530. })
  531. })
  532. describe('destroyOldContainers', function() {
  533. beforeEach(function(done) {
  534. const oneHourInSeconds = 60 * 60
  535. const oneHourInMilliseconds = oneHourInSeconds * 1000
  536. const nowInSeconds = Date.now() / 1000
  537. this.containers = [
  538. {
  539. Name: '/project-old-container-name',
  540. Id: 'old-container-id',
  541. Created: nowInSeconds - oneHourInSeconds - 100
  542. },
  543. {
  544. Name: '/project-new-container-name',
  545. Id: 'new-container-id',
  546. Created: nowInSeconds - oneHourInSeconds + 100
  547. },
  548. {
  549. Name: '/totally-not-a-project-container',
  550. Id: 'some-random-id',
  551. Created: nowInSeconds - 2 * oneHourInSeconds
  552. }
  553. ]
  554. this.DockerRunner.MAX_CONTAINER_AGE = oneHourInMilliseconds
  555. this.listContainers.callsArgWith(1, null, this.containers)
  556. this.DockerRunner.destroyContainer = sinon.stub().callsArg(3)
  557. return this.DockerRunner.destroyOldContainers(error => {
  558. this.callback(error)
  559. return done()
  560. })
  561. })
  562. it('should list all containers', function() {
  563. return this.listContainers.calledWith({ all: true }).should.equal(true)
  564. })
  565. it('should destroy old containers', function() {
  566. this.DockerRunner.destroyContainer.callCount.should.equal(1)
  567. return this.DockerRunner.destroyContainer
  568. .calledWith('/project-old-container-name', 'old-container-id')
  569. .should.equal(true)
  570. })
  571. it('should not destroy new containers', function() {
  572. return this.DockerRunner.destroyContainer
  573. .calledWith('/project-new-container-name', 'new-container-id')
  574. .should.equal(false)
  575. })
  576. it('should not destroy non-project containers', function() {
  577. return this.DockerRunner.destroyContainer
  578. .calledWith('/totally-not-a-project-container', 'some-random-id')
  579. .should.equal(false)
  580. })
  581. return it('should callback the callback', function() {
  582. return this.callback.called.should.equal(true)
  583. })
  584. })
  585. describe('_destroyContainer', function() {
  586. beforeEach(function() {
  587. this.containerId = 'some_id'
  588. this.fakeContainer = { remove: sinon.stub().callsArgWith(1, null) }
  589. return (this.Docker.prototype.getContainer = sinon
  590. .stub()
  591. .returns(this.fakeContainer))
  592. })
  593. it('should get the container', function(done) {
  594. return this.DockerRunner._destroyContainer(
  595. this.containerId,
  596. false,
  597. err => {
  598. this.Docker.prototype.getContainer.callCount.should.equal(1)
  599. this.Docker.prototype.getContainer
  600. .calledWith(this.containerId)
  601. .should.equal(true)
  602. return done()
  603. }
  604. )
  605. })
  606. it('should try to force-destroy the container when shouldForce=true', function(done) {
  607. return this.DockerRunner._destroyContainer(
  608. this.containerId,
  609. true,
  610. err => {
  611. this.fakeContainer.remove.callCount.should.equal(1)
  612. this.fakeContainer.remove
  613. .calledWith({ force: true })
  614. .should.equal(true)
  615. return done()
  616. }
  617. )
  618. })
  619. it('should not try to force-destroy the container when shouldForce=false', function(done) {
  620. return this.DockerRunner._destroyContainer(
  621. this.containerId,
  622. false,
  623. err => {
  624. this.fakeContainer.remove.callCount.should.equal(1)
  625. this.fakeContainer.remove
  626. .calledWith({ force: false })
  627. .should.equal(true)
  628. return done()
  629. }
  630. )
  631. })
  632. it('should not produce an error', function(done) {
  633. return this.DockerRunner._destroyContainer(
  634. this.containerId,
  635. false,
  636. err => {
  637. expect(err).to.equal(null)
  638. return done()
  639. }
  640. )
  641. })
  642. describe('when the container is already gone', function() {
  643. beforeEach(function() {
  644. this.fakeError = new Error('woops')
  645. this.fakeError.statusCode = 404
  646. this.fakeContainer = {
  647. remove: sinon.stub().callsArgWith(1, this.fakeError)
  648. }
  649. return (this.Docker.prototype.getContainer = sinon
  650. .stub()
  651. .returns(this.fakeContainer))
  652. })
  653. return it('should not produce an error', function(done) {
  654. return this.DockerRunner._destroyContainer(
  655. this.containerId,
  656. false,
  657. err => {
  658. expect(err).to.equal(null)
  659. return done()
  660. }
  661. )
  662. })
  663. })
  664. return describe('when container.destroy produces an error', function(done) {
  665. beforeEach(function() {
  666. this.fakeError = new Error('woops')
  667. this.fakeError.statusCode = 500
  668. this.fakeContainer = {
  669. remove: sinon.stub().callsArgWith(1, this.fakeError)
  670. }
  671. return (this.Docker.prototype.getContainer = sinon
  672. .stub()
  673. .returns(this.fakeContainer))
  674. })
  675. return it('should produce an error', function(done) {
  676. return this.DockerRunner._destroyContainer(
  677. this.containerId,
  678. false,
  679. err => {
  680. expect(err).to.not.equal(null)
  681. expect(err).to.equal(this.fakeError)
  682. return done()
  683. }
  684. )
  685. })
  686. })
  687. })
  688. return describe('kill', function() {
  689. beforeEach(function() {
  690. this.containerId = 'some_id'
  691. this.fakeContainer = { kill: sinon.stub().callsArgWith(0, null) }
  692. return (this.Docker.prototype.getContainer = sinon
  693. .stub()
  694. .returns(this.fakeContainer))
  695. })
  696. it('should get the container', function(done) {
  697. return this.DockerRunner.kill(this.containerId, err => {
  698. this.Docker.prototype.getContainer.callCount.should.equal(1)
  699. this.Docker.prototype.getContainer
  700. .calledWith(this.containerId)
  701. .should.equal(true)
  702. return done()
  703. })
  704. })
  705. it('should try to force-destroy the container', function(done) {
  706. return this.DockerRunner.kill(this.containerId, err => {
  707. this.fakeContainer.kill.callCount.should.equal(1)
  708. return done()
  709. })
  710. })
  711. it('should not produce an error', function(done) {
  712. return this.DockerRunner.kill(this.containerId, err => {
  713. expect(err).to.equal(undefined)
  714. return done()
  715. })
  716. })
  717. describe('when the container is not actually running', function() {
  718. beforeEach(function() {
  719. this.fakeError = new Error('woops')
  720. this.fakeError.statusCode = 500
  721. this.fakeError.message =
  722. 'Cannot kill container <whatever> is not running'
  723. this.fakeContainer = {
  724. kill: sinon.stub().callsArgWith(0, this.fakeError)
  725. }
  726. return (this.Docker.prototype.getContainer = sinon
  727. .stub()
  728. .returns(this.fakeContainer))
  729. })
  730. return it('should not produce an error', function(done) {
  731. return this.DockerRunner.kill(this.containerId, err => {
  732. expect(err).to.equal(undefined)
  733. return done()
  734. })
  735. })
  736. })
  737. return describe('when container.kill produces a legitimate error', function(done) {
  738. beforeEach(function() {
  739. this.fakeError = new Error('woops')
  740. this.fakeError.statusCode = 500
  741. this.fakeError.message = 'Totally legitimate reason to throw an error'
  742. this.fakeContainer = {
  743. kill: sinon.stub().callsArgWith(0, this.fakeError)
  744. }
  745. return (this.Docker.prototype.getContainer = sinon
  746. .stub()
  747. .returns(this.fakeContainer))
  748. })
  749. return it('should produce an error', function(done) {
  750. return this.DockerRunner.kill(this.containerId, err => {
  751. expect(err).to.not.equal(undefined)
  752. expect(err).to.equal(this.fakeError)
  753. return done()
  754. })
  755. })
  756. })
  757. })
  758. })