DockerRunnerTests.js 30 KB

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