-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathgetDeploymentImageRef.test.ts
More file actions
304 lines (272 loc) · 9.66 KB
/
getDeploymentImageRef.test.ts
File metadata and controls
304 lines (272 loc) · 9.66 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import { describe, expect, it } from "vitest";
import {
createEcrClient,
getDeploymentImageRef,
getEcrAuthToken,
parseEcrRegistryDomain,
parseRegistryTags,
} from "../app/v3/getDeploymentImageRef.server";
import { DeleteRepositoryCommand } from "@aws-sdk/client-ecr";
describe("getDeploymentImageRef", () => {
const testHost =
process.env.DEPLOY_REGISTRY_HOST || "123456789012.dkr.ecr.us-east-1.amazonaws.com";
const testNamespace = process.env.DEPLOY_REGISTRY_NAMESPACE || "test-namespace";
const testProjectRef = "proj_test_" + Math.random().toString(36).substring(7);
const testProjectRef2 = testProjectRef + "_2";
const registryTags = process.env.DEPLOY_REGISTRY_ECR_TAGS || "test=test,test2=test2";
const roleArn = process.env.DEPLOY_REGISTRY_ECR_ASSUME_ROLE_ARN;
const externalId = process.env.DEPLOY_REGISTRY_ECR_ASSUME_ROLE_EXTERNAL_ID;
const assumeRole = {
roleArn,
externalId,
};
// Clean up test repository after tests
afterAll(async () => {
if (process.env.KEEP_TEST_REPO === "1" || process.env.RUN_ECR_TESTS !== "1") {
return;
}
try {
const { region, accountId } = parseEcrRegistryDomain(testHost);
const ecr = await createEcrClient({ region, assumeRole });
await Promise.all([
ecr.send(
new DeleteRepositoryCommand({
repositoryName: `${testNamespace}/${testProjectRef}`,
registryId: accountId,
force: true,
})
),
ecr.send(
new DeleteRepositoryCommand({
repositoryName: `${testNamespace}/${testProjectRef2}`,
registryId: accountId,
force: true,
})
),
]);
} catch (error) {
console.warn("Failed to delete test repository:", error);
}
});
it("should return the correct image ref for non-ECR registry", async () => {
const imageRef = await getDeploymentImageRef({
registry: {
host: "registry.example.com",
namespace: testNamespace,
username: "test-user",
password: "test-pass",
ecrTags: registryTags,
ecrAssumeRoleArn: roleArn,
ecrAssumeRoleExternalId: externalId,
},
projectRef: testProjectRef,
nextVersion: "20250630.1",
environmentType: "DEVELOPMENT",
deploymentShortCode: "test1234",
});
// Check the image ref structure and that it contains expected parts
expect(imageRef.imageRef).toBe(
`registry.example.com/${testNamespace}/${testProjectRef}:20250630.1.development.test1234`
);
expect(imageRef.isEcr).toBe(false);
});
it.skipIf(process.env.RUN_ECR_TESTS !== "1")(
"should create ECR repository and return correct image ref",
async () => {
const imageRef1 = await getDeploymentImageRef({
registry: {
host: testHost,
namespace: testNamespace,
username: "test-user",
password: "test-pass",
ecrTags: registryTags,
ecrAssumeRoleArn: roleArn,
ecrAssumeRoleExternalId: externalId,
},
projectRef: testProjectRef2,
nextVersion: "20250630.1",
environmentType: "DEVELOPMENT",
deploymentShortCode: "test1234",
});
expect(imageRef1.imageRef).toBe(
`${testHost}/${testNamespace}/${testProjectRef2}:20250630.1.development.test1234`
);
expect(imageRef1.isEcr).toBe(true);
expect(imageRef1.repoCreated).toBe(true);
const imageRef2 = await getDeploymentImageRef({
registry: {
host: testHost,
namespace: testNamespace,
username: "test-user",
password: "test-pass",
ecrTags: registryTags,
ecrAssumeRoleArn: roleArn,
ecrAssumeRoleExternalId: externalId,
},
projectRef: testProjectRef2,
nextVersion: "20250630.2",
environmentType: "DEVELOPMENT",
deploymentShortCode: "test1234",
});
expect(imageRef2.imageRef).toBe(
`${testHost}/${testNamespace}/${testProjectRef2}:20250630.2.development.test1234`
);
expect(imageRef2.isEcr).toBe(true);
expect(imageRef2.repoCreated).toBe(false);
}
);
it.skipIf(process.env.RUN_ECR_TESTS !== "1")("should reuse existing ECR repository", async () => {
// This should use the repository created in the previous test
const imageRef = await getDeploymentImageRef({
registry: {
host: testHost,
namespace: testNamespace,
username: "test-user",
password: "test-pass",
ecrTags: registryTags,
ecrAssumeRoleArn: roleArn,
ecrAssumeRoleExternalId: externalId,
},
projectRef: testProjectRef,
nextVersion: "20250630.2",
environmentType: "PRODUCTION",
deploymentShortCode: "test1234",
});
expect(imageRef.imageRef).toBe(
`${testHost}/${testNamespace}/${testProjectRef}:20250630.2.production.test1234`
);
expect(imageRef.isEcr).toBe(true);
});
it("should generate unique image tags for different deployments with same environment type", async () => {
// Simulates the scenario where multiple deployments happen to the same environment type
const sameEnvironmentType = "PREVIEW";
const sameVersion = "20250630.1";
const firstImageRef = await getDeploymentImageRef({
registry: {
host: "registry.example.com",
namespace: testNamespace,
username: "test-user",
password: "test-pass",
ecrTags: registryTags,
ecrAssumeRoleArn: roleArn,
ecrAssumeRoleExternalId: externalId,
},
projectRef: testProjectRef,
nextVersion: sameVersion,
environmentType: sameEnvironmentType,
deploymentShortCode: "test1234",
});
const secondImageRef = await getDeploymentImageRef({
registry: {
host: "registry.example.com",
namespace: testNamespace,
username: "test-user",
password: "test-pass",
ecrTags: registryTags,
ecrAssumeRoleArn: roleArn,
ecrAssumeRoleExternalId: externalId,
},
projectRef: testProjectRef,
nextVersion: sameVersion,
environmentType: sameEnvironmentType,
deploymentShortCode: "test4321",
});
// Even with the same environment type and version, the image refs should be different due to deployment short codes
expect(firstImageRef.imageRef).toBe(
`registry.example.com/${testNamespace}/${testProjectRef}:${sameVersion}.preview.test1234`
);
expect(secondImageRef.imageRef).toBe(
`registry.example.com/${testNamespace}/${testProjectRef}:${sameVersion}.preview.test4321`
);
expect(firstImageRef.imageRef).not.toBe(secondImageRef.imageRef);
});
});
describe.skipIf(process.env.RUN_ECR_TESTS !== "1")("getEcrAuthToken", () => {
const testHost =
process.env.DEPLOY_REGISTRY_HOST || "123456789012.dkr.ecr.us-east-1.amazonaws.com";
const roleArn = process.env.DEPLOY_REGISTRY_ECR_ASSUME_ROLE_ARN;
const externalId = process.env.DEPLOY_REGISTRY_ECR_ASSUME_ROLE_EXTERNAL_ID;
const assumeRole = {
roleArn,
externalId,
};
it("should return valid ECR credentials", async () => {
const auth = await getEcrAuthToken({
registryHost: testHost,
assumeRole,
});
// Check the structure and basic validation of the returned credentials
expect(auth).toHaveProperty("username");
expect(auth).toHaveProperty("password");
expect(auth.username).toBe("AWS");
expect(typeof auth.password).toBe("string");
expect(auth.password.length).toBeGreaterThan(0);
// Verify the token format (should be a base64-encoded string)
expect(auth.password).toMatch(/^[A-Za-z0-9+/=]+$/);
});
it("should throw error for invalid region", async () => {
await expect(
getEcrAuthToken({
registryHost: "invalid.ecr.amazonaws.com",
assumeRole,
})
).rejects.toThrow();
});
});
describe("parseEcrRegistry", () => {
it("should correctly parse a valid ECR registry host", () => {
const result = parseEcrRegistryDomain("123456789012.dkr.ecr.us-east-1.amazonaws.com");
expect(result).toEqual({
accountId: "123456789012",
region: "us-east-1",
});
});
it("should handle invalid ECR registry hosts", () => {
const invalidHosts = [
"invalid.ecr.amazonaws.com",
"registry.hub.docker.com",
"123456789012.dkr.ecr.us-east-1.not-amazon.com",
"123456789012.wrong.ecr.us-east-1.amazonaws.com",
];
for (const host of invalidHosts) {
expect(() => parseEcrRegistryDomain(host)).toThrow("Invalid ECR registry host");
}
});
});
describe("parseRegistryTags", () => {
it("should handle empty or null input", () => {
expect(parseRegistryTags("")).toEqual([]);
expect(parseRegistryTags(",,,")).toEqual([]);
});
it("should parse key-only tags", () => {
expect(parseRegistryTags("key1,key2")).toEqual([
{ Key: "key1", Value: "" },
{ Key: "key2", Value: "" },
]);
});
it("should parse key-value tags", () => {
expect(parseRegistryTags("key1=value1,key2=value2")).toEqual([
{ Key: "key1", Value: "value1" },
{ Key: "key2", Value: "value2" },
]);
});
it("should handle mixed key-only and key-value tags", () => {
expect(parseRegistryTags("key1,key2=value2,key3")).toEqual([
{ Key: "key1", Value: "" },
{ Key: "key2", Value: "value2" },
{ Key: "key3", Value: "" },
]);
});
it("should handle whitespace", () => {
expect(parseRegistryTags(" key1 , key2 = value2 ")).toEqual([
{ Key: "key1", Value: "" },
{ Key: "key2", Value: "value2" },
]);
});
it("should skip invalid tags", () => {
expect(parseRegistryTags("=value,key1,=,key2=value2")).toEqual([
{ Key: "key1", Value: "" },
{ Key: "key2", Value: "value2" },
]);
});
});