Skip to content

Commit

Permalink
fix broken tests and slightly increase test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: John Seekins <[email protected]>
  • Loading branch information
johnseekins-pathccm committed Oct 13, 2023
1 parent 6faf656 commit 27cdedf
Showing 1 changed file with 43 additions and 55 deletions.
98 changes: 43 additions & 55 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,24 +224,16 @@ describe('Render task definition', () => {
expect(core.setFailed).toBeCalledWith('Task definition file does not exist: does-not-exist-task-definition.json');
});

test('renders a task definition at an absolute path, and with initial docker labels empty', async () => {
test('renders a task definition with docker labels', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('/hello/task-definition.json') // task-definition
.mockReturnValueOnce('web') // container-name
.mockReturnValueOnce('nginx:latest') // image
.mockReturnValueOnce('EXAMPLE=here') // environment-variables
.mockReturnValueOnce('key1=value1\nkey2=value2'); // docker-labels

jest.mock('/hello/task-definition.json', () => ({
family: 'task-def-family',
containerDefinitions: [
{
name: "web",
image: "some-other-image"
}
]
}), { virtual: true });
.mockReturnValueOnce('task-definition.json')
.mockReturnValueOnce('web')
.mockReturnValueOnce('nginx:latest')
.mockReturnValueOnce('EXAMPLE=here')
.mockReturnValueOnce('awslogs')
.mockReturnValueOnce('awslogs-create-group=true\nawslogs-group=/ecs/web\nawslogs-region=us-east-1\nawslogs-stream-prefix=ecs')
.mockReturnValueOnce('key1=value1\nkey2=value2');

await run();

Expand All @@ -251,7 +243,7 @@ describe('Render task definition', () => {
postfix: '.json',
keep: true,
discardDescriptor: true
});
});

expect(fs.writeFileSync).toHaveBeenNthCalledWith(1, 'new-task-def-file-name',
JSON.stringify({
Expand All @@ -261,30 +253,56 @@ describe('Render task definition', () => {
name: "web",
image: "nginx:latest",
environment: [
{
name: "FOO",
value: "bar"
},
{
name: "DONT-TOUCH",
value: "me"
},
{
name: "HELLO",
value: "world"
},
{
name: "EXAMPLE",
value: "here"
}
],
logConfiguration: {
logDriver: "awslogs",
options: {
"awslogs-create-group": "true",
"awslogs-group": "/ecs/web",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs"
}
},
dockerLabels : {
"key1":"value1",
"key2":"value2"
}
},
{
name: "sidecar",
image: "hello"
}
]
}, null, 2)
);
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'task-definition', 'new-task-def-file-name');
});

test('renders a task definition at an absolute path, and change existed docker labels empty', async () => {
test('renders a task definition at an absolute path with bad docker labels', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('/hello/task-definition.json') // task-definition
.mockReturnValueOnce('web') // container-name
.mockReturnValueOnce('nginx:latest') // image
.mockReturnValueOnce('EXAMPLE=here') // environment-variables
.mockReturnValueOnce('key1=update_value1\nkey2=update_value2\nkey3=value3'); // docker-labels
.mockReturnValueOnce('/hello/task-definition.json')
.mockReturnValueOnce('web')
.mockReturnValueOnce('nginx:latest')
.mockReturnValueOnce('EXAMPLE=here')
.mockReturnValueOnce('awslogs')
.mockReturnValueOnce('awslogs-create-group=true\nawslogs-group=/ecs/web\nawslogs-region=us-east-1\nawslogs-stream-prefix=ecs')
.mockReturnValueOnce('key1=update_value1\nkey2\nkey3=value3');

jest.mock('/hello/task-definition.json', () => ({
family: 'task-def-family',
Expand All @@ -302,37 +320,7 @@ describe('Render task definition', () => {

await run();

expect(tmp.fileSync).toHaveBeenNthCalledWith(1, {
tmpdir: '/home/runner/work/_temp',
prefix: 'task-definition-',
postfix: '.json',
keep: true,
discardDescriptor: true
});

expect(fs.writeFileSync).toHaveBeenNthCalledWith(1, 'new-task-def-file-name',
JSON.stringify({
family: 'task-def-family',
containerDefinitions: [
{
name: "web",
image: "nginx:latest",
environment: [
{
name: "EXAMPLE",
value: "here"
}
],
dockerLabels : {
"key1":"update_value1",
"key2":"update_value2",
"key3":"value3"
}
}
]
}, null, 2)
);
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'task-definition', 'new-task-def-file-name');
expect(core.setFailed).toBeCalledWith('Can\'t parse logConfiguration option key2. Must be in key=value format, one per line');
});

test('error returned for non-JSON task definition contents', async () => {
Expand Down

0 comments on commit 27cdedf

Please sign in to comment.