/
index.spec.ts
316 lines (268 loc) · 10.3 KB
/
index.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
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,
} from "../../apphosting/index";
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`,
remoteUri: "remoteUri",
createTime: "0",
updateTime: "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",
stages: [
{
progression: "IMMEDIATE",
targetPercent: 100,
},
],
},
});
});
});
describe("ensureAppHostingComputeServiceAccount", () => {
const serviceAccount = "hello@example.com";
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" }];
beforeEach(() => {
listLocationsStub.returns(supportedLocations);
promptOnceStub.returns(supportedLocations[0].locationId);
});
it("returns a location selection", async () => {
const location = await promptLocation(projectId);
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"],
});
});
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"],
});
});
});
describe.only("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")).to.be.rejectedWith(
/No backend named "baz" found./,
);
});
it("returns unambiguous backend", async () => {
listBackendsStub.resolves({ backends: [backendFoo, backendBar] });
await expect(getBackendForAmbiguousLocation(projectId, "foo")).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")).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"],
});
});
});
});