diff --git a/test/createcloudjob.test.ts b/test/createcloudjob.test.ts index 90ac3db..a974e4a 100644 --- a/test/createcloudjob.test.ts +++ b/test/createcloudjob.test.ts @@ -3,12 +3,22 @@ import { Job } from '../src/job'; const proxyquire = require('proxyquire').noCallThru(); import * as fakeRunlog from "./src/fakeRunLog"; import fakeEcs from "./src/fakeEcs" +import fakeEcsNeverStops from "./src/fakeEcsNeverStops" import fakeWatchClient from "./src/fakeWatchClient" import promisify from "../src/promisify" import * as fakePublisher from "./src/fakePublisher" const supportedVersions = require("../src/supportedPhpVersions") const composerVersions = [2] +class fakeCapturingPublisher { + static lastData + constructor (config: {baseUrl}) {} + publish (data: {jobId: number}, callback: Function) { + fakeCapturingPublisher.lastData = data + callback(null, { statusCode: 200 }) + } +} + describe('createCloudJob', () => { beforeEach(done => { @@ -96,4 +106,30 @@ describe('createCloudJob', () => { } } }) + + it('Should report the job as a failure to the backend when waiting for the container to stop times out', async function () { + this.timeout(30000) + fakeCapturingPublisher.lastData = undefined + let { createCloudJob } = proxyquire('../src/createCloudJob', { + 'aws-sdk': { + ECS: fakeEcsNeverStops, + CloudWatchLogs: fakeWatchClient + }, + './publisher': { default: fakeCapturingPublisher }, + './RunLog': fakeRunlog, + 'await-sleep': () => Promise.resolve() + }) + let run = createCloudJob({}, new Job({ + slug: 'violinst/example-slug', + job_id: 42 + }), 'efef') + await promisify(run.bind(null)) + let calls = fakeRunlog.Runlog.getCalls() + should(calls.error.length).equal(1) + should(calls.error[0][0].message).equal('Timed out waiting for the job to stop the container. You can try to requeue the project or try again later') + should(fakeCapturingPublisher.lastData).not.be.undefined() + should(fakeCapturingPublisher.lastData.jobId).equal(42) + should(fakeCapturingPublisher.lastData.set_state).equal('failure') + should(fakeCapturingPublisher.lastData.message.stdout[0]).containEql('Timed out waiting for the job to stop the container') + }) }) diff --git a/test/src/fakeEcsNeverStops.ts b/test/src/fakeEcsNeverStops.ts new file mode 100644 index 0000000..f87de1a --- /dev/null +++ b/test/src/fakeEcsNeverStops.ts @@ -0,0 +1,43 @@ +import fakeAwsBase from "./fakeAwsBase" + +// Like fakeEcs, but describeTasks never reports the container as STOPPED, so +// createCloudJob's "wait for the task to stop" polling loop never breaks out +// on its own and is forced to hit the timeout branch. +export default class fakeEcsNeverStops extends fakeAwsBase { + protected currentTask + + runTask (taskConfig: object) { + this.currentTask = 'runTask' + return this + } + + describeTasks (query: object) { + this.currentTask = 'describeTasks' + return this + } + + getPromiseOutput() { + if (this.currentTask === 'runTask') { + return { + tasks: [ + { + taskArn: 'firstpart/secondpart' + } + ] + } + } + if (this.currentTask === 'describeTasks') { + return { + tasks: [ + { + containers: [ + { + lastStatus: 'RUNNING' + } + ] + } + ] + } + } + } +}