Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn on RunTask failures #93

Merged
merged 3 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 6,
"ecmaVersion": 8,
"sourceType": "script"
},
"rules": {
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ module.exports = function (options, cb) {
return taskRunner.runPromisified(params);
})
.then((taskDefinition) => {
if (taskDefinition.failures) {
throw new Error("ECS RunTask returned failure messages", { cause: taskDefinition.failures });
}

const taskArn = taskDefinition.tasks[0].taskArn;
const taskId = taskArn.substring(taskArn.lastIndexOf('/') + 1);
const formatter = new FormatStream();
Expand Down
2 changes: 1 addition & 1 deletion test/format-transform-stream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const expect = require('expect.js');
const expect = require('expect.js');
const FormatTransformStream = require('../lib/format-transform-stream');

describe('FormatTransformStream', function() {
Expand Down
112 changes: 112 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
'use strict'

const { mockClient } = require('aws-sdk-client-mock');
const { ECS, DescribeTaskDefinitionCommand, RunTaskCommand } = require("@aws-sdk/client-ecs");
const { CloudWatchLogs, GetLogEventsCommand } = require("@aws-sdk/client-cloudwatch-logs");
const expect = require('expect.js');
const { promisify } = require('node:util');
const index = promisify(require('../index'));

describe('index', function () {
let cwlMock;
let ecsMock;
beforeEach(() => {
cwlMock = mockClient(CloudWatchLogs);
ecsMock = mockClient(ECS);
});

it('should do the thing', async function () {
const options = {
taskDefinitionArn: 'task-definition.arn',
containerName: 'meow'
};

ecsMock.on(DescribeTaskDefinitionCommand).callsFake(async params => {
expect(params.taskDefinition).to.eql(options.taskDefinitionArn)

return {
taskDefinition: {
taskDefinitionArn: options.taskDefinitionArn,
containerDefinitions: [{
name: options.containerName,
logConfiguration: {
logDriver: 'awslogs',
options: { 'awslogs-group': '', 'awslogs-stream-prefix': '' },
}
}],
}
}
});

// thread the randon EOF through to kill the stream
let eofSet;
const eof = new Promise(r => {
eofSet = r;
})
ecsMock.on(RunTaskCommand).callsFake(async params => {
expect(params.taskDefinition).to.eql(options.taskDefinitionArn)

// Grab the `xxx` from ..."TASK FINISHED: $(echo -n xxx | base64),"...
eofSet(params.overrides.containerOverrides[0].command[2].split(' ')[9])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really get what this does

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a comment, but yeah this is ugliest bit of this. This is the only place we can grab the random value the log stuff uses to detect the end of the task run.


return {
tasks: [{
taskArn: ''
}]
}
});

cwlMock.on(GetLogEventsCommand).callsFake(async _params => {
return {
events: [{
timestamp: 1477346285562,
message: Buffer.from(await eof).toString('base64'),
}]
};
});

// if this returns without crashing we're gucci
return index(options);
});


it('should warn us when a task fails to launch', async function () {
const options = {
taskDefinitionArn: 'task-definition.arn',
containerName: 'meow'
};

ecsMock.on(DescribeTaskDefinitionCommand).callsFake(async params => {
expect(params.taskDefinition).to.eql(options.taskDefinitionArn)

return {
taskDefinition: {
taskDefinitionArn: options.taskDefinitionArn,
containerDefinitions: [{
name: options.containerName,
logConfiguration: {
logDriver: 'awslogs',
options: { 'awslogs-group': '', 'awslogs-stream-prefix': '' },
}
}],
}
}
});

const reason = 'ECS is haunted'
ecsMock.on(RunTaskCommand).callsFake(async params => {
expect(params.taskDefinition).to.eql(options.taskDefinitionArn)

return {
failures: [{ reason }]
}
});

try {
await index(options);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not something like expect(index(options).to.throw ... ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't work ¯_(ツ)_/¯
expect.js hasn't been updated in a decade, maybe they don't support promises?

Copy link
Contributor Author

@liath liath Jun 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think mocha and expect.js might be kind dead projects. Something that annoyed my the whole time in working on this is that errors were always "test timed out" and apparently that's just mocha being terrible with promises.
mochajs/mocha#2640

expect().fail("App should have thrown an error about ECS returning errors")
} catch (err) {
expect(err.cause[0].reason).to.eql(reason)
}
});
});
6 changes: 4 additions & 2 deletions test/log-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ const LogsStream = require('../lib/log-stream');

describe('LogStream', function () {
this.timeout(5000);
const cwlMock = mockClient(CloudWatchLogs);
afterEach(() => { cwlMock.reset(); });
let cwlMock ;
beforeEach(() => {
cwlMock = mockClient(CloudWatchLogs);
});

const options = {
logGroup: 'yee',
Expand Down
10 changes: 6 additions & 4 deletions test/taskrunner.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { mockClient } = require('aws-sdk-client-mock');
const { ECS, StartTaskCommand, StopTaskCommand, RunTaskCommand } = require("@aws-sdk/client-ecs");
const { ECS, StopTaskCommand, RunTaskCommand } = require("@aws-sdk/client-ecs");
const expect = require('expect.js');
const taskRunner = require('../lib/taskrunner');

Expand All @@ -21,8 +21,10 @@ describe('TaskRunner', function () {
})

describe('#run', function () {
const ecsMock = mockClient(ECS);
afterEach(() => { ecsMock.reset(); });
let ecsMock;
beforeEach(() => {
ecsMock = mockClient(ECS);
});

it('should make a call to AWS.ECS with correct arguments not including env', function (done) {
const options = {
Expand Down Expand Up @@ -83,7 +85,7 @@ describe('TaskRunner', function () {

describe('#stop', function () {
const ecsMock = mockClient(ECS);
afterEach(() => { ecsMock.reset(); });
beforeEach(() => { ecsMock.reset(); });

it('should make a call to AWS.ECS with correct arguments', function (done) {
const options = {
Expand Down
Loading