-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler.test.js
109 lines (99 loc) · 2.85 KB
/
handler.test.js
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
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");
const deletePromise = {
imageIds: [
{
imageDigest: "3797470f-bb69-11e6-90e4-19b39eb619f7",
imageTag: "some-tag",
},
],
failures: [],
};
const desribeImagesPromise = {
nextToken: null,
imageDetails: [
{
registryId: "384553929753",
repositoryName: "test-repo",
imageDigest: "1",
imageTags: ["1.0.0-master", "ignore-this"],
imageSizeInBytes: "1024",
imagePushedAt: moment(),
},
{
registryId: "384553929753",
repositoryName: "test-repo",
imageDigest: "2",
imageTags: ["1.0.0-master", "ignore-this"],
imageSizeInBytes: "1024",
imagePushedAt: moment().add(-31, "days"),
},
{
registryId: "384553929753",
repositoryName: "test-repo",
imageDigest: "3",
imageTags: ["1.0.0-other", "ignore-this"],
imageSizeInBytes: "1024",
imagePushedAt: moment(),
},
{
registryId: "384553929753",
repositoryName: "test-repo",
imageDigest: "4",
imageTags: ["1.0.0-other", "dont-ignore-this"],
imageSizeInBytes: "1024",
imagePushedAt: moment().add(-31, "days"),
},
{
registryId: "384553929753",
repositoryName: "test-repo",
imageDigest: "5",
imageTags: ["1.0.0-main", "ignore-this"],
imageSizeInBytes: "1024",
imagePushedAt: moment().add(-31, "days"),
},
],
};
const ecrMock = mockClient(ECRClient);
function callback() {}
describe("cleanupImages", () => {
beforeEach(() => {
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);
});
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, {
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);
});
});