-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtaskrunner.js
More file actions
111 lines (91 loc) · 3.56 KB
/
Copy pathtaskrunner.js
File metadata and controls
111 lines (91 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'use strict'
const { mockClient } = require('aws-sdk-client-mock');
const { ECS, StopTaskCommand, RunTaskCommand } = require("@aws-sdk/client-ecs");
const expect = require('expect.js');
const taskRunner = require('../lib/taskrunner');
describe('TaskRunner', function () {
it('should construct command', function () {
const options = {
cmd: 'mycommand --arg "Woot"',
endOfStreamIdentifier: "1234"
};
let cmd = taskRunner.makeCmd(options);
expect(cmd).to.eql([
'sh', '-c',
`sh -c "${options.cmd}"; EXITCODE=$?; echo "TASK FINISHED: $(echo -n ${options.endOfStreamIdentifier} | base64), EXITCODE: $EXITCODE"`
]);
})
describe('#run', function () {
let ecsMock;
beforeEach(() => {
ecsMock = mockClient(ECS);
});
it('should make a call to AWS.ECS with correct arguments not including env', function (done) {
const options = {
clusterArn: 'cluster.arn',
taskDefinitionArn: 'task-definition.arn',
containerName: 'container name',
cmd: 'mycommand --arg "Woot"',
endOfStreamIdentifier: '1234'
};
ecsMock.on(RunTaskCommand).callsFake((params) => {
expect(params.cluster).to.equal(options.clusterArn);
expect(params.taskDefinition).to.equal(options.taskDefinitionArn);
const cmdOverride = params.overrides.containerOverrides[0];
expect(cmdOverride.name).to.equal(options.containerName);
expect(cmdOverride.command).to.eql(taskRunner.makeCmd(options));
return Promise.resolve({ taskArn: "Yo" });
});
taskRunner.run(options, function (err, _task) {
expect(err).to.equal(null);
done();
});
});
it('should make a call to AWS.ECS with correct arguments including env', function (done) {
const options = {
clusterArn: 'cluster.arn',
taskDefinitionArn: 'task-definition.arn',
containerName: 'container name',
cmd: 'mycommand --arg "Woot"',
endOfStreamIdentifier: '1234',
startedBy: 'Bugs Bunny',
env: [{ name: 'TERM', value: 'xterm-256color' }, { name: 'OTHER', value: 'things' }]
};
ecsMock.on(RunTaskCommand).callsFake((params) => {
expect(params.cluster).to.equal(options.clusterArn);
expect(params.taskDefinition).to.equal(options.taskDefinitionArn);
expect(params.startedBy).to.equal(options.startedBy);
const cmdOverride = params.overrides.containerOverrides[0];
expect(cmdOverride.name).to.equal(options.containerName);
expect(cmdOverride.command).to.eql(taskRunner.makeCmd(options));
expect(cmdOverride.environment).to.equal(options.env);
return Promise.resolve({ taskArn: "Yo" });
});
taskRunner.run(options, function (err, _task) {
expect(err).to.equal(null);
done();
});
});
});
describe('#stop', function () {
const ecsMock = mockClient(ECS);
beforeEach(() => { ecsMock.reset(); });
it('should make a call to AWS.ECS with correct arguments', function (done) {
const options = {
clusterArn: 'cluster.arn',
taskId: 'some-task-id',
reason: 'user pressed ctrl-c'
};
ecsMock.on(StopTaskCommand).callsFake((params) => {
expect(params.cluster).to.equal(options.clusterArn);
expect(params.task).to.equal(options.taskId);
expect(params.reason).to.equal(options.reason);
return Promise.resolve({ taskArn: "Yo" });
});
taskRunner.stop(options, function (err, _task) {
expect(err).to.equal(null);
done();
});
});
});
});