-
Notifications
You must be signed in to change notification settings - Fork 961
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add warning when emulating extensions that use unemulated APIs (#4226)
* starting to check APIs * merging * more progress * more progress * Adding check for unemulated APIs * reverting shrinkwrap changes * reverting shrinkwrap changes * clean up unused import * add enable API console link * more pr fixes * adding renamed files * one last round of pr clean up * clean up unused marked
- Loading branch information
Showing
4 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as planner from "../../deploy/extensions/planner"; | ||
import { check } from "../../ensureApiEnabled"; | ||
|
||
const EMULATED_APIS = [ | ||
"storage-component.googleapis.com", | ||
"firestore.googleapis.com", | ||
"pubsub.googleapis.com", | ||
"identitytoolkit.googleapis.com", | ||
// TODO: Is there a RTDB API we need to add here? I couldn't find one. | ||
]; | ||
|
||
type APIInfo = { | ||
apiName: string; | ||
instanceIds: string[]; | ||
enabled: boolean; | ||
}; | ||
/** | ||
* getUnemulatedAPIs checks a list of InstanceSpecs for APIs that are not emulated. | ||
* It returns a map of API name to list of instanceIds that use that API. | ||
*/ | ||
export async function getUnemulatedAPIs( | ||
projectId: string, | ||
instances: planner.InstanceSpec[] | ||
): Promise<APIInfo[]> { | ||
const unemulatedAPIs: Record<string, APIInfo> = {}; | ||
for (const i of instances) { | ||
const extensionVersion = await planner.getExtensionVersion(i); | ||
for (const api of extensionVersion.spec.apis ?? []) { | ||
if (!EMULATED_APIS.includes(api.apiName)) { | ||
if (unemulatedAPIs[api.apiName]) { | ||
unemulatedAPIs[api.apiName].instanceIds.push(i.instanceId); | ||
} else { | ||
const enabled = await check(projectId, api.apiName, "extensions", true); | ||
unemulatedAPIs[api.apiName] = { | ||
apiName: api.apiName, | ||
instanceIds: [i.instanceId], | ||
enabled, | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
return Object.values(unemulatedAPIs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { expect } from "chai"; | ||
import * as sinon from "sinon"; | ||
|
||
import * as utils from "../../../emulator/extensions/validation"; | ||
import * as ensureApiEnabled from "../../../ensureApiEnabled"; | ||
import { InstanceSpec } from "../../../deploy/extensions/planner"; | ||
|
||
function getTestInstanceSpecWithAPI(instanceId: string, apiName: string): InstanceSpec { | ||
return { | ||
instanceId, | ||
params: {}, | ||
extensionVersion: { | ||
name: "publishers/test/extensions/test/versions/0.1.0", | ||
ref: "test/[email protected]", | ||
state: "PUBLISHED", | ||
sourceDownloadUri: "test.com", | ||
hash: "abc123", | ||
spec: { | ||
name: "test", | ||
version: "0.1.0", | ||
sourceUrl: "test.com", | ||
resources: [], | ||
params: [], | ||
apis: [{ apiName, reason: "because" }], | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
describe("ExtensionsEmulator validation utils", () => { | ||
describe(`${utils.getUnemulatedAPIs.name}`, () => { | ||
const testProjectId = "test-project"; | ||
const testAPI = "test.googleapis.com"; | ||
const sandbox = sinon.createSandbox(); | ||
|
||
beforeEach(() => { | ||
const checkStub = sandbox.stub(ensureApiEnabled, "check"); | ||
checkStub.withArgs(testProjectId, testAPI, "extensions", true).resolves(true); | ||
checkStub.throws("Unexpected API checked in test"); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it("should check only unemulated APIs", async () => { | ||
const instanceIdWithUnemulatedAPI = "unemulated"; | ||
const instanceId2WithUnemulatedAPI = "unemulated2"; | ||
const instanceIdWithEmulatedAPI = "emulated"; | ||
|
||
const result = await utils.getUnemulatedAPIs(testProjectId, [ | ||
getTestInstanceSpecWithAPI(instanceIdWithEmulatedAPI, "firestore.googleapis.com"), | ||
getTestInstanceSpecWithAPI(instanceIdWithUnemulatedAPI, testAPI), | ||
getTestInstanceSpecWithAPI(instanceId2WithUnemulatedAPI, testAPI), | ||
]); | ||
|
||
expect(result).to.deep.equal([ | ||
{ | ||
apiName: testAPI, | ||
instanceIds: [instanceIdWithUnemulatedAPI, instanceId2WithUnemulatedAPI], | ||
enabled: true, | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |