DockerRunnerTests.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. globals: { Math } // used by lodash
  74. })
  75. this.Docker = Docker
  76. this.getContainer = Docker.prototype.getContainer
  77. this.createContainer = Docker.prototype.createContainer
  78. this.listContainers = Docker.prototype.listContainers
  79. this.directory = '/local/compile/directory'
  80. this.mainFile = 'main-file.tex'
  81. this.compiler = 'pdflatex'
  82. this.image = 'example.com/sharelatex/image:2016.2'
  83. this.env = {}
  84. this.callback = sinon.stub()
  85. this.project_id = 'project-id-123'
  86. this.volumes = { '/local/compile/directory': '/compile' }
  87. this.Settings.clsi.docker.image = this.defaultImage = 'default-image'
  88. this.compileGroup = 'compile-group'
  89. return (this.Settings.clsi.docker.env = { PATH: 'mock-path' })
  90. })
  91. afterEach(function() {
  92. this.DockerRunner.stopContainerMonitor()
  93. })
  94. describe('run', function() {
  95. beforeEach(function(done) {
  96. this.DockerRunner._getContainerOptions = sinon
  97. .stub()
  98. .returns((this.options = { mockoptions: 'foo' }))
  99. this.DockerRunner._fingerprintContainer = sinon
  100. .stub()
  101. .returns((this.fingerprint = 'fingerprint'))
  102. this.name = `project-${this.project_id}-${this.fingerprint}`
  103. this.command = ['mock', 'command', '--outdir=$COMPILE_DIR']
  104. this.command_with_dir = ['mock', 'command', '--outdir=/compile']
  105. this.timeout = 42000
  106. return done()
  107. })
  108. describe('successfully', function() {
  109. beforeEach(function(done) {
  110. this.DockerRunner._runAndWaitForContainer = sinon
  111. .stub()
  112. .callsArgWith(3, null, (this.output = 'mock-output'))
  113. return this.DockerRunner.run(
  114. this.project_id,
  115. this.command,
  116. this.directory,
  117. this.image,
  118. this.timeout,
  119. this.env,
  120. this.compileGroup,
  121. (err, output) => {
  122. this.callback(err, output)
  123. return done()
  124. }
  125. )
  126. })
  127. it('should generate the options for the container', function() {
  128. return this.DockerRunner._getContainerOptions
  129. .calledWith(
  130. this.command_with_dir,
  131. this.image,
  132. this.volumes,
  133. this.timeout
  134. )
  135. .should.equal(true)
  136. })
  137. it('should generate the fingerprint from the returned options', function() {
  138. return this.DockerRunner._fingerprintContainer
  139. .calledWith(this.options)
  140. .should.equal(true)
  141. })
  142. it('should do the run', function() {
  143. return this.DockerRunner._runAndWaitForContainer
  144. .calledWith(this.options, this.volumes, this.timeout)
  145. .should.equal(true)
  146. })
  147. return it('should call the callback', function() {
  148. return this.callback.calledWith(null, this.output).should.equal(true)
  149. })
  150. })
  151. describe('when path.sandboxedCompilesHostDir is set', function() {
  152. beforeEach(function() {
  153. this.Settings.path.sandboxedCompilesHostDir = '/some/host/dir/compiles'
  154. this.directory = '/var/lib/sharelatex/data/compiles/xyz'
  155. this.DockerRunner._runAndWaitForContainer = sinon
  156. .stub()
  157. .callsArgWith(3, null, (this.output = 'mock-output'))
  158. return this.DockerRunner.run(
  159. this.project_id,
  160. this.command,
  161. this.directory,
  162. this.image,
  163. this.timeout,
  164. this.env,
  165. this.compileGroup,
  166. this.callback
  167. )
  168. })
  169. it('should re-write the bind directory', function() {
  170. const volumes = this.DockerRunner._runAndWaitForContainer.lastCall
  171. .args[1]
  172. return expect(volumes).to.deep.equal({
  173. '/some/host/dir/compiles/xyz': '/compile'
  174. })
  175. })
  176. return it('should call the callback', function() {
  177. return this.callback.calledWith(null, this.output).should.equal(true)
  178. })
  179. })
  180. describe('when the run throws an error', function() {
  181. beforeEach(function() {
  182. let firstTime = true
  183. this.output = 'mock-output'
  184. this.DockerRunner._runAndWaitForContainer = (
  185. options,
  186. volumes,
  187. timeout,
  188. callback
  189. ) => {
  190. if (callback == null) {
  191. callback = function(error, output) {}
  192. }
  193. if (firstTime) {
  194. firstTime = false
  195. const error = new Error('(HTTP code 500) server error - ...')
  196. error.statusCode = 500
  197. return callback(error)
  198. } else {
  199. return callback(null, this.output)
  200. }
  201. }
  202. sinon.spy(this.DockerRunner, '_runAndWaitForContainer')
  203. this.DockerRunner.destroyContainer = sinon.stub().callsArg(3)
  204. return this.DockerRunner.run(
  205. this.project_id,
  206. this.command,
  207. this.directory,
  208. this.image,
  209. this.timeout,
  210. this.env,
  211. this.compileGroup,
  212. this.callback
  213. )
  214. })
  215. it('should do the run twice', function() {
  216. return this.DockerRunner._runAndWaitForContainer.calledTwice.should.equal(
  217. true
  218. )
  219. })
  220. it('should destroy the container in between', function() {
  221. return this.DockerRunner.destroyContainer
  222. .calledWith(this.name, null)
  223. .should.equal(true)
  224. })
  225. return it('should call the callback', function() {
  226. return this.callback.calledWith(null, this.output).should.equal(true)
  227. })
  228. })
  229. describe('with no image', function() {
  230. beforeEach(function() {
  231. this.DockerRunner._runAndWaitForContainer = sinon
  232. .stub()
  233. .callsArgWith(3, null, (this.output = 'mock-output'))
  234. return this.DockerRunner.run(
  235. this.project_id,
  236. this.command,
  237. this.directory,
  238. null,
  239. this.timeout,
  240. this.env,
  241. this.compileGroup,
  242. this.callback
  243. )
  244. })
  245. return it('should use the default image', function() {
  246. return this.DockerRunner._getContainerOptions
  247. .calledWith(
  248. this.command_with_dir,
  249. this.defaultImage,
  250. this.volumes,
  251. this.timeout
  252. )
  253. .should.equal(true)
  254. })
  255. })
  256. return describe('with image override', function() {
  257. beforeEach(function() {
  258. this.Settings.texliveImageNameOveride = 'overrideimage.com/something'
  259. this.DockerRunner._runAndWaitForContainer = sinon
  260. .stub()
  261. .callsArgWith(3, null, (this.output = 'mock-output'))
  262. return this.DockerRunner.run(
  263. this.project_id,
  264. this.command,
  265. this.directory,
  266. this.image,
  267. this.timeout,
  268. this.env,
  269. this.compileGroup,
  270. this.callback
  271. )
  272. })
  273. return it('should use the override and keep the tag', function() {
  274. const image = this.DockerRunner._getContainerOptions.args[0][1]
  275. return image.should.equal('overrideimage.com/something/image:2016.2')
  276. })
  277. })
  278. })
  279. describe('run with _getOptions', function() {
  280. beforeEach(function(done) {
  281. // this.DockerRunner._getContainerOptions = sinon
  282. // .stub()
  283. // .returns((this.options = { mockoptions: 'foo' }))
  284. this.DockerRunner._fingerprintContainer = sinon
  285. .stub()
  286. .returns((this.fingerprint = 'fingerprint'))
  287. this.name = `project-${this.project_id}-${this.fingerprint}`
  288. this.command = ['mock', 'command', '--outdir=$COMPILE_DIR']
  289. this.command_with_dir = ['mock', 'command', '--outdir=/compile']
  290. this.timeout = 42000
  291. return done()
  292. })
  293. describe('when a compile group config is set', function() {
  294. beforeEach(function() {
  295. this.Settings.clsi.docker.compileGroupConfig = {
  296. 'compile-group': {
  297. 'HostConfig.newProperty': 'new-property'
  298. },
  299. 'other-group': { otherProperty: 'other-property' }
  300. }
  301. this.DockerRunner._runAndWaitForContainer = sinon
  302. .stub()
  303. .callsArgWith(3, null, (this.output = 'mock-output'))
  304. return this.DockerRunner.run(
  305. this.project_id,
  306. this.command,
  307. this.directory,
  308. this.image,
  309. this.timeout,
  310. this.env,
  311. this.compileGroup,
  312. this.callback
  313. )
  314. })
  315. it('should set the docker options for the compile group', function() {
  316. const options = this.DockerRunner._runAndWaitForContainer.lastCall
  317. .args[0]
  318. return expect(options.HostConfig).to.deep.include({
  319. Binds: ['/local/compile/directory:/compile:rw'],
  320. LogConfig: { Type: 'none', Config: {} },
  321. CapDrop: 'ALL',
  322. SecurityOpt: ['no-new-privileges'],
  323. newProperty: 'new-property'
  324. })
  325. })
  326. return it('should call the callback', function() {
  327. return this.callback.calledWith(null, this.output).should.equal(true)
  328. })
  329. })
  330. })
  331. describe('_runAndWaitForContainer', function() {
  332. beforeEach(function() {
  333. this.options = { mockoptions: 'foo', name: (this.name = 'mock-name') }
  334. this.DockerRunner.startContainer = (
  335. options,
  336. volumes,
  337. attachStreamHandler,
  338. callback
  339. ) => {
  340. attachStreamHandler(null, (this.output = 'mock-output'))
  341. return callback(null, (this.containerId = 'container-id'))
  342. }
  343. sinon.spy(this.DockerRunner, 'startContainer')
  344. this.DockerRunner.waitForContainer = sinon
  345. .stub()
  346. .callsArgWith(2, null, (this.exitCode = 42))
  347. return this.DockerRunner._runAndWaitForContainer(
  348. this.options,
  349. this.volumes,
  350. this.timeout,
  351. this.callback
  352. )
  353. })
  354. it('should create/start the container', function() {
  355. return this.DockerRunner.startContainer
  356. .calledWith(this.options, this.volumes)
  357. .should.equal(true)
  358. })
  359. it('should wait for the container to finish', function() {
  360. return this.DockerRunner.waitForContainer
  361. .calledWith(this.name, this.timeout)
  362. .should.equal(true)
  363. })
  364. return it('should call the callback with the output', function() {
  365. return this.callback.calledWith(null, this.output).should.equal(true)
  366. })
  367. })
  368. describe('startContainer', function() {
  369. beforeEach(function() {
  370. this.attachStreamHandler = sinon.stub()
  371. this.attachStreamHandler.cock = true
  372. this.options = { mockoptions: 'foo', name: 'mock-name' }
  373. this.container.inspect = sinon.stub().callsArgWith(0)
  374. this.DockerRunner.attachToContainer = (
  375. containerId,
  376. attachStreamHandler,
  377. cb
  378. ) => {
  379. attachStreamHandler()
  380. return cb()
  381. }
  382. return sinon.spy(this.DockerRunner, 'attachToContainer')
  383. })
  384. describe('when the container exists', function() {
  385. beforeEach(function() {
  386. this.container.inspect = sinon.stub().callsArgWith(0)
  387. this.container.start = sinon.stub().yields()
  388. return this.DockerRunner.startContainer(
  389. this.options,
  390. this.volumes,
  391. () => {},
  392. this.callback
  393. )
  394. })
  395. it('should start the container with the given name', function() {
  396. this.getContainer.calledWith(this.options.name).should.equal(true)
  397. return this.container.start.called.should.equal(true)
  398. })
  399. it('should not try to create the container', function() {
  400. return this.createContainer.called.should.equal(false)
  401. })
  402. it('should attach to the container', function() {
  403. return this.DockerRunner.attachToContainer.called.should.equal(true)
  404. })
  405. it('should call the callback', function() {
  406. return this.callback.called.should.equal(true)
  407. })
  408. return it('should attach before the container starts', function() {
  409. return sinon.assert.callOrder(
  410. this.DockerRunner.attachToContainer,
  411. this.container.start
  412. )
  413. })
  414. })
  415. describe('when inspect always fails with EPIPE error', function() {
  416. beforeEach(function() {
  417. this.error = new Error('write EPIPE')
  418. this.container.inspect = sinon.stub().yields(this.error)
  419. this.container.start = sinon.stub().yields()
  420. this.DockerRunner.startContainer(
  421. this.options,
  422. this.volumes,
  423. () => {},
  424. this.callback
  425. )
  426. })
  427. it('should retry once', function() {
  428. sinon.assert.callOrder(
  429. this.container.inspect,
  430. this.container.inspect,
  431. this.callback
  432. )
  433. })
  434. it('should call back with error', function() {
  435. sinon.assert.calledWith(this.callback, this.error)
  436. })
  437. })
  438. describe('when inspect fails once with EPIPE error', function() {
  439. beforeEach(function() {
  440. this.container.inspect = sinon.stub()
  441. this.container.inspect.onFirstCall().yields(new Error('write EPIPE'))
  442. this.container.inspect.onSecondCall().yields()
  443. this.container.start = sinon.stub().yields()
  444. this.DockerRunner.startContainer(
  445. this.options,
  446. this.volumes,
  447. () => {},
  448. this.callback
  449. )
  450. })
  451. it('should retry once and start container', function() {
  452. sinon.assert.callOrder(
  453. this.container.inspect,
  454. this.container.inspect,
  455. this.DockerRunner.attachToContainer,
  456. this.container.start,
  457. this.callback
  458. )
  459. })
  460. it('should call back without error', function() {
  461. sinon.assert.calledWith(this.callback, null)
  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 })
  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 })
  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. })