Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
pennyZZZZ committed Aug 17, 2023
1 parent b9b131c commit 9ba240f
Show file tree
Hide file tree
Showing 4 changed files with 2,943 additions and 5,726 deletions.
5 changes: 2 additions & 3 deletions handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ async function getAllImages(ecr, registryId, repoName) {
maxResults: 100,
};

const data = await ecr
.send(new DescribeImagesCommand(params))
return data.imageDetails
const data = await ecr.send(new DescribeImagesCommand(params));
return data.imageDetails;
}

function buildReport(
Expand Down
42 changes: 23 additions & 19 deletions handler.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
const { BatchDeleteImageCommand, DescribeImagesCommand, ECRClient } = require("@aws-sdk/client-ecr");
const {
BatchDeleteImageCommand,
DescribeImagesCommand,
ECRClient,
} = require("@aws-sdk/client-ecr");
const moment = require("moment");
const { mockClient } = require("aws-sdk-client-mock");
const { cleanupImages } = require("./handler");
const { expect } = require("expect");
(globalThis).expect = expect;
require('aws-sdk-client-mock-jest')
globalThis.expect = expect;
require("aws-sdk-client-mock-jest");

const deletePromise = {
imageIds: [
Expand All @@ -14,7 +18,7 @@ const deletePromise = {
},
],
failures: [],
}
};

const desribeImagesPromise = {
nextToken: null,
Expand Down Expand Up @@ -60,46 +64,46 @@ const desribeImagesPromise = {
imagePushedAt: moment().add(-31, "days"),
},
],
}
};

const ecrMock = mockClient(ECRClient);

function callback() {}

describe("cleanupImages", () => {

beforeEach(() => {
ecrMock.reset()
ecrMock.reset();
process.env.DRY_RUN = "true";
process.env.REPO_NAMES = "test-repo";
process.env.AWS_ACCOUNT_ID = "1234567890";
ecrMock
.on(DescribeImagesCommand).resolves(desribeImagesPromise)
.on(BatchDeleteImageCommand).resolves(deletePromise)
.on(DescribeImagesCommand)
.resolves(desribeImagesPromise)
.on(BatchDeleteImageCommand)
.resolves(deletePromise);
});

it("Should not remove master images", async () => {
process.env.DRY_RUN = "false";

await cleanupImages(null, null, callback);

expect(ecrMock).toHaveReceivedCommand(DescribeImagesCommand)
expect(ecrMock).toHaveReceivedCommandWith(BatchDeleteImageCommand,{
expect(ecrMock).toHaveReceivedCommand(DescribeImagesCommand);
expect(ecrMock).toHaveReceivedCommandWith(BatchDeleteImageCommand, {
registryId: "1234567890",
repositoryName: "test-repo",
imageIds: [{ imageDigest: "4" }],
})
});
});

it("Should not call delete if dry run", async () => {
await cleanupImages(null, null, callback);

expect(ecrMock).toHaveReceivedCommandWith(DescribeImagesCommand, {
"maxResults": 100,
"registryId": "1234567890",
"repositoryName": "test-repo"
})
expect(ecrMock).toHaveReceivedCommandTimes(BatchDeleteImageCommand, 0)

maxResults: 100,
registryId: "1234567890",
repositoryName: "test-repo",
});
expect(ecrMock).toHaveReceivedCommandTimes(BatchDeleteImageCommand, 0);
});
});

0 comments on commit 9ba240f

Please sign in to comment.