DockerRunnerTests.js 28 KB

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