-
Notifications
You must be signed in to change notification settings - Fork 982
/
Copy pathbackend.spec.ts
383 lines (322 loc) · 12.4 KB
/
backend.spec.ts
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
import * as sinon from "sinon";
import { expect } from "chai";
import * as prompt from "../prompt";
import * as apphosting from "../gcp/apphosting";
import * as iam from "../gcp/iam";
import * as resourceManager from "../gcp/resourceManager";
import * as poller from "../operation-poller";
import {
createBackend,
deleteBackendAndPoll,
promptLocation,
setDefaultTrafficPolicy,
ensureAppHostingComputeServiceAccount,
getBackendForAmbiguousLocation,
getBackend,
} from "./backend";
import * as deploymentTool from "../deploymentTool";
import { FirebaseError } from "../error";
describe("apphosting setup functions", () => {
const projectId = "projectId";
const location = "us-central1";
const backendId = "backendId";
let promptOnceStub: sinon.SinonStub;
let pollOperationStub: sinon.SinonStub;
let createBackendStub: sinon.SinonStub;
let listBackendsStub: sinon.SinonStub;
let deleteBackendStub: sinon.SinonStub;
let updateTrafficStub: sinon.SinonStub;
let listLocationsStub: sinon.SinonStub;
let createServiceAccountStub: sinon.SinonStub;
let addServiceAccountToRolesStub: sinon.SinonStub;
let testResourceIamPermissionsStub: sinon.SinonStub;
beforeEach(() => {
promptOnceStub = sinon.stub(prompt, "promptOnce").throws("Unexpected promptOnce call");
pollOperationStub = sinon.stub(poller, "pollOperation").throws("Unexpected pollOperation call");
createBackendStub = sinon
.stub(apphosting, "createBackend")
.throws("Unexpected createBackend call");
listBackendsStub = sinon
.stub(apphosting, "listBackends")
.throws("Unexpected listBackends call");
deleteBackendStub = sinon
.stub(apphosting, "deleteBackend")
.throws("Unexpected deleteBackend call");
updateTrafficStub = sinon
.stub(apphosting, "updateTraffic")
.throws("Unexpected updateTraffic call");
listLocationsStub = sinon
.stub(apphosting, "listLocations")
.throws("Unexpected listLocations call");
createServiceAccountStub = sinon
.stub(iam, "createServiceAccount")
.throws("Unexpected createServiceAccount call");
addServiceAccountToRolesStub = sinon
.stub(resourceManager, "addServiceAccountToRoles")
.throws("Unexpected addServiceAccountToRoles call");
testResourceIamPermissionsStub = sinon
.stub(iam, "testResourceIamPermissions")
.throws("Unexpected testResourceIamPermissions call");
});
afterEach(() => {
sinon.verifyAndRestore();
});
describe("createBackend", () => {
const webAppId = "webAppId";
const op = {
name: `projects/${projectId}/locations/${location}/backends/${backendId}`,
done: true,
};
const completeBackend = {
name: `projects/${projectId}/locations/${location}/backends/${backendId}`,
labels: {},
createTime: "0",
updateTime: "1",
uri: "https://placeholder.com",
};
const cloudBuildConnRepo = {
name: `projects/${projectId}/locations/${location}/connections/framework-${location}/repositories/repoId`,
cloneUri: "cloneUri",
createTime: "0",
updateTime: "1",
deleteTime: "2",
reconciling: true,
uid: "1",
};
it("should create a new backend", async () => {
createBackendStub.resolves(op);
pollOperationStub.resolves(completeBackend);
await createBackend(
projectId,
location,
backendId,
cloudBuildConnRepo,
"custom-service-account",
webAppId,
);
const backendInput: Omit<apphosting.Backend, apphosting.BackendOutputOnlyFields> = {
servingLocality: "GLOBAL_ACCESS",
codebase: {
repository: cloudBuildConnRepo.name,
rootDirectory: "/",
},
labels: deploymentTool.labels(),
serviceAccount: "custom-service-account",
appId: webAppId,
};
expect(createBackendStub).to.be.calledWith(projectId, location, backendInput);
});
it("should set default rollout policy to 100% all at once", async () => {
const completeTraffic: apphosting.Traffic = {
name: `projects/${projectId}/locations/${location}/backends/${backendId}/traffic`,
current: { splits: [] },
reconciling: false,
createTime: "0",
updateTime: "1",
etag: "",
uid: "",
};
updateTrafficStub.resolves(op);
pollOperationStub.resolves(completeTraffic);
await setDefaultTrafficPolicy(projectId, location, backendId, "main");
expect(updateTrafficStub).to.be.calledWith(projectId, location, backendId, {
rolloutPolicy: {
codebaseBranch: "main",
},
});
});
});
describe("ensureAppHostingComputeServiceAccount", () => {
const serviceAccount = "[email protected]";
it("should succeed if the user has permissions for the service account", async () => {
testResourceIamPermissionsStub.resolves();
await expect(ensureAppHostingComputeServiceAccount(projectId, serviceAccount)).to.be
.fulfilled;
expect(testResourceIamPermissionsStub).to.be.calledOnce;
expect(createServiceAccountStub).to.not.be.called;
expect(addServiceAccountToRolesStub).to.not.be.called;
});
it("should succeed if the user can create the service account when it does not exist", async () => {
testResourceIamPermissionsStub.rejects(
new FirebaseError("Permission denied", { status: 404 }),
);
createServiceAccountStub.resolves();
addServiceAccountToRolesStub.resolves();
await expect(ensureAppHostingComputeServiceAccount(projectId, serviceAccount)).to.be
.fulfilled;
expect(testResourceIamPermissionsStub).to.be.calledOnce;
expect(createServiceAccountStub).to.be.calledOnce;
expect(addServiceAccountToRolesStub).to.be.calledOnce;
});
it("should throw an error if the user does not have permissions", async () => {
testResourceIamPermissionsStub.rejects(
new FirebaseError("Permission denied", { status: 403 }),
);
await expect(
ensureAppHostingComputeServiceAccount(projectId, serviceAccount),
).to.be.rejectedWith(/Failed to create backend due to missing delegation permissions/);
expect(testResourceIamPermissionsStub).to.be.calledOnce;
expect(createServiceAccountStub).to.not.be.called;
expect(addServiceAccountToRolesStub).to.not.be.called;
});
it("should throw the error if the user cannot create the service account", async () => {
testResourceIamPermissionsStub.rejects(
new FirebaseError("Permission denied", { status: 404 }),
);
createServiceAccountStub.rejects(new FirebaseError("failed to create SA"));
await expect(
ensureAppHostingComputeServiceAccount(projectId, serviceAccount),
).to.be.rejectedWith("failed to create SA");
expect(testResourceIamPermissionsStub).to.be.calledOnce;
expect(createServiceAccountStub).to.be.calledOnce;
expect(addServiceAccountToRolesStub).to.not.be.called;
});
});
describe("deleteBackendAndPoll", () => {
it("should delete a backend", async () => {
const op = {
name: `projects/${projectId}/locations/${location}/backends/${backendId}`,
done: true,
};
deleteBackendStub.resolves(op);
pollOperationStub.resolves();
await deleteBackendAndPoll(projectId, location, backendId);
expect(deleteBackendStub).to.be.calledWith(projectId, location, backendId);
});
});
describe("promptLocation", () => {
const supportedLocations = [
{ name: "us-central1", locationId: "us-central1" },
{ name: "us-west1", locationId: "us-west1" },
];
beforeEach(() => {
listLocationsStub.returns(supportedLocations);
promptOnceStub.returns(supportedLocations[0].locationId);
});
it("returns a location selection", async () => {
const location = await promptLocation(projectId, /* prompt= */ "");
expect(location).to.be.eq("us-central1");
});
it("uses a default location prompt if none is provided", async () => {
await promptLocation(projectId);
expect(promptOnceStub).to.be.calledWith({
name: "location",
type: "list",
default: "us-central1",
message: "Please select a location:",
choices: ["us-central1", "us-west1"],
});
});
it("uses a custom location prompt if provided", async () => {
await promptLocation(projectId, "Custom location prompt:");
expect(promptOnceStub).to.be.calledWith({
name: "location",
type: "list",
default: "us-central1",
message: "Custom location prompt:",
choices: ["us-central1", "us-west1"],
});
});
it("skips the prompt if there's only 1 valid location choice", async () => {
listLocationsStub.returns(supportedLocations.slice(0, 1));
await expect(promptLocation(projectId, "Custom location prompt:")).to.eventually.equal(
supportedLocations[0].locationId,
);
expect(promptOnceStub).to.not.be.called;
});
});
describe("getBackendForAmbiguousLocation", () => {
const backendFoo = {
name: `projects/${projectId}/locations/${location}/backends/foo`,
labels: {},
createTime: "0",
updateTime: "1",
uri: "https://placeholder.com",
};
const backendFooOtherRegion = {
name: `projects/${projectId}/locations/otherRegion/backends/foo`,
labels: {},
createTime: "0",
updateTime: "1",
uri: "https://placeholder.com",
};
const backendBar = {
name: `projects/${projectId}/locations/${location}/backends/bar`,
labels: {},
createTime: "0",
updateTime: "1",
uri: "https://placeholder.com",
};
it("throws if there are no matching backends", async () => {
listBackendsStub.resolves({ backends: [] });
await expect(
getBackendForAmbiguousLocation(projectId, "baz", /* prompt= */ ""),
).to.be.rejectedWith(/No backend named "baz" found./);
});
it("returns unambiguous backend", async () => {
listBackendsStub.resolves({ backends: [backendFoo, backendBar] });
await expect(
getBackendForAmbiguousLocation(projectId, "foo", /* prompt= */ ""),
).to.eventually.equal(backendFoo);
});
it("prompts for location if backend is ambiguous", async () => {
listBackendsStub.resolves({ backends: [backendFoo, backendFooOtherRegion, backendBar] });
promptOnceStub.resolves(location);
await expect(
getBackendForAmbiguousLocation(
projectId,
"foo",
"Please select the location of the backend you'd like to delete:",
),
).to.eventually.equal(backendFoo);
expect(promptOnceStub).to.be.calledWith({
name: "location",
type: "list",
message: "Please select the location of the backend you'd like to delete:",
choices: [location, "otherRegion"],
});
});
});
describe("getBackend", () => {
const backendChickenAsia = {
name: `projects/${projectId}/locations/asia-east1/backends/chicken`,
labels: {},
createTime: "0",
updateTime: "1",
uri: "https://placeholder.com",
};
const backendChickenEurope = {
name: `projects/${projectId}/locations/europe-west4/backends/chicken`,
labels: {},
createTime: "0",
updateTime: "1",
uri: "https://placeholder.com",
};
const backendCow = {
name: `projects/${projectId}/locations/us-central1/backends/cow`,
labels: {},
createTime: "0",
updateTime: "1",
uri: "https://placeholder.com",
};
const allBackends = [backendChickenAsia, backendChickenEurope, backendCow];
it("throws if more than one backend is found", async () => {
listBackendsStub.resolves({ backends: allBackends });
await expect(getBackend(projectId, "chicken")).to.be.rejectedWith(
"You have multiple backends with the same chicken ID. " +
"This is not allowed until we can support more locations. " +
"Please delete and recreate any backends that share an ID with another backend.",
);
});
it("throws if no backend is found", async () => {
listBackendsStub.resolves({ backends: allBackends });
await expect(getBackend(projectId, "farmer")).to.be.rejectedWith(
"No backend named farmer found.",
);
});
it("returns backend", async () => {
listBackendsStub.resolves({ backends: allBackends });
await expect(getBackend(projectId, "cow")).to.eventually.equal(backendCow);
});
});
});