-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathrunv2.spec.ts
More file actions
577 lines (530 loc) · 18.8 KB
/
runv2.spec.ts
File metadata and controls
577 lines (530 loc) · 18.8 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
import { expect } from "chai";
import * as sinon from "sinon";
import * as runv2 from "./runv2";
import * as backend from "../deploy/functions/backend";
import { latest } from "../deploy/functions/runtimes/supported";
import { CODEBASE_LABEL } from "../functions/constants";
import { Client } from "../apiv2";
import { FirebaseError } from "../error";
describe("runv2", () => {
const PROJECT_ID = "project-id";
const LOCATION = "us-central1";
const SERVICE_ID = "functionid"; // TODO: use other normalization method if/when implemented.
const FUNCTION_ID = "functionId"; // Logical function ID
const IMAGE_URI = "gcr.io/project/image:latest";
const BASE_ENDPOINT_RUN: Omit<backend.Endpoint, "httpsTrigger"> = {
platform: "run",
id: FUNCTION_ID,
project: PROJECT_ID,
region: LOCATION,
entryPoint: FUNCTION_ID,
runtime: latest("nodejs"),
availableMemoryMb: 256,
cpu: 1,
};
const BASE_RUN_SERVICE: Omit<runv2.Service, runv2.ServiceOutputFields> = {
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${SERVICE_ID}`,
labels: {
[runv2.RUNTIME_LABEL]: latest("nodejs"),
[runv2.CLIENT_NAME_LABEL]: "firebase-functions",
},
annotations: {
[runv2.FIREBASE_FUNCTION_METADTA_ANNOTATION]: `{"functionId":"${FUNCTION_ID}"}`,
},
template: {
containers: [
{
name: "worker",
image: IMAGE_URI,
env: [
{
name: runv2.FUNCTION_TARGET_ENV,
value: FUNCTION_ID,
},
{
name: runv2.FUNCTION_SIGNATURE_TYPE_ENV,
value: "http",
},
],
resources: {
limits: {
cpu: "1",
memory: "256Mi",
},
startupCpuBoost: true,
cpuIdle: true,
},
},
],
maxInstanceRequestConcurrency: backend.DEFAULT_CONCURRENCY,
},
client: "cli-firebase",
};
describe("serviceFromEndpoint", () => {
it("should copy a minimal endpoint", () => {
const endpoint: backend.Endpoint = {
...BASE_ENDPOINT_RUN,
httpsTrigger: {},
};
expect(runv2.serviceFromEndpoint(endpoint, IMAGE_URI)).to.deep.equal(BASE_RUN_SERVICE);
});
it("should handle different codebase", () => {
const endpoint: backend.Endpoint = {
...BASE_ENDPOINT_RUN,
codebase: "my-codebase",
httpsTrigger: {},
};
const expectedServiceInput: Omit<runv2.Service, runv2.ServiceOutputFields> = {
...BASE_RUN_SERVICE,
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${FUNCTION_ID.toLowerCase()}`,
labels: {
...BASE_RUN_SERVICE.labels,
[CODEBASE_LABEL]: "my-codebase",
},
};
expect(runv2.serviceFromEndpoint(endpoint, IMAGE_URI)).to.deep.equal(expectedServiceInput);
});
it("should copy environment variables", () => {
const endpoint: backend.Endpoint = {
...BASE_ENDPOINT_RUN,
httpsTrigger: {},
environmentVariables: { FOO: "bar" },
};
const expectedServiceInput = JSON.parse(
JSON.stringify({
...BASE_RUN_SERVICE,
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${FUNCTION_ID.toLowerCase()}`,
}),
);
expectedServiceInput.template.containers[0].env.unshift({ name: "FOO", value: "bar" });
expect(runv2.serviceFromEndpoint(endpoint, IMAGE_URI)).to.deep.equal(expectedServiceInput);
});
it("should copy secret environment variables", () => {
const endpoint: backend.Endpoint = {
...BASE_ENDPOINT_RUN,
httpsTrigger: {},
secretEnvironmentVariables: [
{ key: "MY_SECRET", secret: "secret-name", projectId: PROJECT_ID, version: "1" },
],
};
const expectedServiceInput = JSON.parse(
JSON.stringify({
...BASE_RUN_SERVICE,
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${FUNCTION_ID.toLowerCase()}`,
}),
);
expectedServiceInput.template.containers[0].env.unshift({
name: "MY_SECRET",
valueSource: { secretKeyRef: { secret: "secret-name", version: "1" } },
});
expect(runv2.serviceFromEndpoint(endpoint, IMAGE_URI)).to.deep.equal(expectedServiceInput);
});
it("should set min/max instances annotations", () => {
const endpoint: backend.Endpoint = {
...BASE_ENDPOINT_RUN,
httpsTrigger: {},
minInstances: 1,
maxInstances: 10,
};
const expectedServiceInput = JSON.parse(
JSON.stringify({
...BASE_RUN_SERVICE,
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${FUNCTION_ID.toLowerCase()}`,
}),
);
expectedServiceInput.scaling = {
minInstanceCount: 1,
maxInstanceCount: 10,
};
expect(runv2.serviceFromEndpoint(endpoint, IMAGE_URI)).to.deep.equal(expectedServiceInput);
});
it("should set concurrency", () => {
const endpoint: backend.Endpoint = {
...BASE_ENDPOINT_RUN,
httpsTrigger: {},
concurrency: 50,
};
const expectedServiceInput = JSON.parse(
JSON.stringify({
...BASE_RUN_SERVICE,
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${FUNCTION_ID.toLowerCase()}`,
}),
);
expectedServiceInput.template.maxInstanceRequestConcurrency = 50;
expect(runv2.serviceFromEndpoint(endpoint, IMAGE_URI)).to.deep.equal(expectedServiceInput);
});
it("should set memory and CPU", () => {
const endpoint: backend.Endpoint = {
...BASE_ENDPOINT_RUN,
httpsTrigger: {},
availableMemoryMb: 512,
cpu: 2,
};
const expectedServiceInput = JSON.parse(
JSON.stringify({
...BASE_RUN_SERVICE,
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${FUNCTION_ID.toLowerCase()}`,
}),
);
expectedServiceInput.template.containers[0].resources.limits.memory = "512Mi";
expectedServiceInput.template.containers[0].resources.limits.cpu = "2";
expect(runv2.serviceFromEndpoint(endpoint, IMAGE_URI)).to.deep.equal(expectedServiceInput);
});
it("should remove deployment-tool label", () => {
const endpoint: backend.Endpoint = {
...BASE_ENDPOINT_RUN,
httpsTrigger: {},
labels: { "deployment-tool": "firebase-cli" },
};
const result = runv2.serviceFromEndpoint(endpoint, IMAGE_URI);
expect(result.labels?.["deployment-tool"]).to.be.undefined;
expect(result.labels?.[runv2.CLIENT_NAME_LABEL]).to.equal("firebase-functions");
});
});
describe("endpointFromService", () => {
it("should copy a minimal service", () => {
const service: Omit<runv2.Service, runv2.ServiceOutputFields> = {
...BASE_RUN_SERVICE,
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${SERVICE_ID}`,
annotations: {
...BASE_RUN_SERVICE.annotations,
[runv2.FUNCTION_ID_ANNOTATION]: FUNCTION_ID, // Using FUNCTION_ID_ANNOTATION as primary source for id
[runv2.FUNCTION_TARGET_ANNOTATION]: "customEntryPoint",
[runv2.TRIGGER_TYPE_ANNOTATION]: "HTTP_TRIGGER",
},
template: {
containers: [
{
name: "worker",
image: IMAGE_URI,
resources: {
limits: {
cpu: "1",
memory: "256Mi",
},
cpuIdle: true,
startupCpuBoost: true,
},
},
],
},
};
const expectedEndpoint: backend.Endpoint = {
platform: "run",
id: FUNCTION_ID,
project: PROJECT_ID,
region: LOCATION,
runtime: latest("nodejs"),
entryPoint: "customEntryPoint",
availableMemoryMb: 256,
cpu: 1,
httpsTrigger: {},
labels: {
[runv2.RUNTIME_LABEL]: latest("nodejs"),
[runv2.CLIENT_NAME_LABEL]: "firebase-functions",
},
environmentVariables: {},
secretEnvironmentVariables: [],
ingressSettings: "ALLOW_ALL",
serviceAccount: null,
timeoutSeconds: 60,
};
expect(runv2.endpointFromService(service)).to.deep.equal(expectedEndpoint);
});
it("should detect a service that's GCF managed", () => {
const service: Omit<runv2.Service, runv2.ServiceOutputFields> = {
...BASE_RUN_SERVICE,
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${SERVICE_ID}`,
labels: {
[runv2.RUNTIME_LABEL]: latest("nodejs"),
[runv2.CLIENT_NAME_LABEL]: "cloud-functions", // This indicates it's GCF managed
},
annotations: {
...BASE_RUN_SERVICE.annotations,
[runv2.FUNCTION_ID_ANNOTATION]: FUNCTION_ID, // Using FUNCTION_ID_ANNOTATION as primary source for id
[runv2.FUNCTION_TARGET_ANNOTATION]: "customEntryPoint",
[runv2.TRIGGER_TYPE_ANNOTATION]: "HTTP_TRIGGER",
},
template: {
containers: [
{
name: "worker",
image: IMAGE_URI,
resources: {
limits: {
cpu: "1",
memory: "256Mi",
},
},
},
],
},
};
const expectedEndpoint: backend.Endpoint = {
platform: "gcfv2",
id: FUNCTION_ID,
project: PROJECT_ID,
region: LOCATION,
runtime: latest("nodejs"),
entryPoint: "customEntryPoint",
availableMemoryMb: 256,
cpu: 1,
httpsTrigger: {},
labels: {
[runv2.RUNTIME_LABEL]: latest("nodejs"),
[runv2.CLIENT_NAME_LABEL]: "cloud-functions",
},
environmentVariables: {},
secretEnvironmentVariables: [],
ingressSettings: "ALLOW_ALL",
serviceAccount: null,
timeoutSeconds: 60,
};
expect(runv2.endpointFromService(service)).to.deep.equal(expectedEndpoint);
});
it("should derive id from FIREBASE_FUNCTIONS_METADATA if present", () => {
const service: Omit<runv2.Service, runv2.ServiceOutputFields> = {
...BASE_RUN_SERVICE,
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${SERVICE_ID}`,
labels: {
[runv2.RUNTIME_LABEL]: latest("nodejs"),
},
annotations: {
[runv2.FIREBASE_FUNCTION_METADTA_ANNOTATION]: `{"functionId":"firebaseMetadataId"}`,
[runv2.FUNCTION_TARGET_ANNOTATION]: "targetId",
[runv2.FUNCTION_ID_ANNOTATION]: "annotationId",
},
template: {
containers: [
{
name: "worker",
image: IMAGE_URI,
resources: { limits: { cpu: "1", memory: "256Mi" } },
},
],
},
};
const result = runv2.endpointFromService(service);
expect(result.id).to.equal("firebaseMetadataId");
expect(result.entryPoint).to.equal("targetId");
});
it("should derive id from FUNCTION_TARGET_ANNOTATION if FUNCTION_ID_ANNOTATION is missing", () => {
const service: Omit<runv2.Service, runv2.ServiceOutputFields> = {
...BASE_RUN_SERVICE,
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${SERVICE_ID}`,
labels: {
[runv2.RUNTIME_LABEL]: latest("nodejs"),
},
annotations: {
...BASE_RUN_SERVICE.annotations,
[runv2.FUNCTION_TARGET_ANNOTATION]: FUNCTION_ID, // This will be used for id and entryPoint
},
template: {
containers: [
{
name: "worker",
image: IMAGE_URI,
resources: { limits: { cpu: "1", memory: "256Mi" } },
},
],
},
};
const result = runv2.endpointFromService(service);
expect(result.id).to.equal(FUNCTION_ID);
expect(result.entryPoint).to.equal(FUNCTION_ID);
});
it("should derive id from service name part if FUNCTION_ID_ANNOTATION and FUNCTION_TARGET_ANNOTATION are missing", () => {
const service: Omit<runv2.Service, runv2.ServiceOutputFields> = {
...BASE_RUN_SERVICE,
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${SERVICE_ID}`,
labels: {
[runv2.RUNTIME_LABEL]: latest("nodejs"),
},
annotations: {
// No FUNCTION_ID_ANNOTATION or FUNCTION_TARGET_ANNOTATION
},
template: {
containers: [
{
name: "worker",
image: IMAGE_URI,
resources: { limits: { cpu: "1", memory: "256Mi" } },
},
],
},
};
const result = runv2.endpointFromService(service);
expect(result.id).to.equal(SERVICE_ID);
expect(result.entryPoint).to.equal(SERVICE_ID);
});
it("should copy env vars and secrets", () => {
const service: runv2.Service = JSON.parse(JSON.stringify(BASE_RUN_SERVICE));
service.template.containers![0].env = [
{ name: "FOO", value: "bar" },
{
name: "MY_SECRET",
valueSource: {
secretKeyRef: {
secret: `projects/${PROJECT_ID}/secrets/secret-name`,
version: "1",
},
},
},
];
const result = runv2.endpointFromService(service);
expect(result.environmentVariables).to.deep.equal({ FOO: "bar" });
expect(result.secretEnvironmentVariables).to.deep.equal([
{ key: "MY_SECRET", secret: "secret-name", projectId: PROJECT_ID, version: "1" },
]);
});
it("should copy concurrency, min/max instances", () => {
const service: runv2.Service = JSON.parse(JSON.stringify(BASE_RUN_SERVICE));
service.template.maxInstanceRequestConcurrency = 10;
service.scaling = {
minInstanceCount: 2,
maxInstanceCount: 5,
};
const result = runv2.endpointFromService(service);
expect(result.concurrency).to.equal(10);
expect(result.minInstances).to.equal(2);
expect(result.maxInstances).to.equal(5);
});
it("should handle missing optional fields gracefully", () => {
const service: runv2.Service = {
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${SERVICE_ID}`,
generation: 1,
template: {
containers: [
{
name: "worker",
image: IMAGE_URI,
resources: { limits: { memory: "128Mi", cpu: "0.5" } }, // Minimal resources
},
],
// No containerConcurrency, no serviceAccount
},
// No labels, no annotations
createTime: new Date().toISOString(),
updateTime: new Date().toISOString(),
creator: "test@example.com",
lastModifier: "test@example.com",
etag: "test-etag",
};
const expectedEndpoint: backend.Endpoint = {
platform: "run",
id: SERVICE_ID, // Derived from service name
project: PROJECT_ID,
region: LOCATION,
runtime: latest("nodejs"), // Default runtime
entryPoint: SERVICE_ID, // No FUNCTION_TARGET_ANNOTATION
availableMemoryMb: 128,
cpu: 0.5,
httpsTrigger: {},
labels: {},
environmentVariables: {},
secretEnvironmentVariables: [],
ingressSettings: "ALLOW_ALL",
serviceAccount: null,
timeoutSeconds: 60,
// concurrency, minInstances, maxInstances will be undefined
};
expect(runv2.endpointFromService(service)).to.deep.equal(expectedEndpoint);
});
});
describe("listServices", () => {
let sandbox: sinon.SinonSandbox;
let getStub: sinon.SinonStub;
beforeEach(() => {
sandbox = sinon.createSandbox();
getStub = sandbox.stub(Client.prototype, "get");
});
afterEach(() => {
sandbox.restore();
});
it("should return a list of services", async () => {
const mockServices = [
{
name: "service1",
labels: { "goog-managed-by": "cloud-functions" },
},
{
name: "service2",
labels: { "goog-managed-by": "firebase-functions" },
},
];
getStub.resolves({ status: 200, body: { services: mockServices } });
const services = await runv2.listServices(PROJECT_ID);
expect(services).to.deep.equal(mockServices);
expect(getStub).to.have.been.calledOnceWithExactly(
`/projects/${PROJECT_ID}/locations/-/services`,
{ queryParams: {} },
);
});
it("should handle pagination", async () => {
const mockServices1 = [
{
name: "service1",
labels: { "goog-managed-by": "cloud-functions" },
},
];
const mockServices2 = [
{
name: "service2",
labels: { "goog-managed-by": "firebase-functions" },
},
];
getStub
.onFirstCall()
.resolves({ status: 200, body: { services: mockServices1, nextPageToken: "nextPage" } });
getStub.onSecondCall().resolves({ status: 200, body: { services: mockServices2 } });
const services = await runv2.listServices(PROJECT_ID);
expect(services).to.deep.equal([...mockServices1, ...mockServices2]);
expect(getStub).to.have.been.calledTwice;
expect(getStub.firstCall).to.have.been.calledWithExactly(
`/projects/${PROJECT_ID}/locations/-/services`,
{ queryParams: {} },
);
expect(getStub.secondCall).to.have.been.calledWithExactly(
`/projects/${PROJECT_ID}/locations/-/services`,
{ queryParams: { pageToken: "nextPage" } },
);
});
it("should throw an error if the API call fails", async () => {
getStub.resolves({ status: 500, body: "Internal Server Error" });
try {
await runv2.listServices(PROJECT_ID);
expect.fail("Should have thrown an error");
} catch (err: any) {
expect(err).to.be.instanceOf(FirebaseError);
expect(err.message).to.contain("Failed to list services. HTTP Error: 500");
}
});
it("should filter for gcfv2 and firebase-managed services", async () => {
const mockServices = [
{
name: "service1",
labels: { "goog-managed-by": "cloud-functions" },
},
{
name: "service2",
labels: { "goog-managed-by": "firebase-functions" },
},
{
name: "service3",
labels: { "goog-managed-by": "other" },
},
{
name: "service4",
labels: {},
},
];
getStub.resolves({ status: 200, body: { services: mockServices } });
const services = await runv2.listServices(PROJECT_ID);
expect(services).to.deep.equal([mockServices[0], mockServices[1]]);
expect(getStub).to.have.been.calledOnceWithExactly(
`/projects/${PROJECT_ID}/locations/-/services`,
{ queryParams: {} },
);
});
});
});