Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert Remove __trigger #1274

Merged
merged 8 commits into from Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions spec/v1/cloud-functions.spec.ts
Expand Up @@ -41,7 +41,7 @@ describe("makeCloudFunction", () => {
legacyEventType: "providers/provider/eventTypes/event",
};

it("should put a __endpoint on the returned CloudFunction", () => {
it("should put a __trigger/__endpoint on the returned CloudFunction", () => {
const cf = makeCloudFunction({
provider: "mock.provider",
eventType: "mock.event",
Expand All @@ -50,6 +50,14 @@ describe("makeCloudFunction", () => {
handler: () => null,
});

expect(cf.__trigger).to.deep.equal({
eventTrigger: {
eventType: "mock.provider.mock.event",
resource: "resource",
service: "service",
},
});

expect(cf.__endpoint).to.deep.equal({
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
Expand All @@ -64,9 +72,17 @@ describe("makeCloudFunction", () => {
});
});

it("should have legacy event type in __endpoint if provided", () => {
it("should have legacy event type in __trigger/__endpoint if provided", () => {
const cf = makeCloudFunction(cloudFunctionArgs);

expect(cf.__trigger).to.deep.equal({
eventTrigger: {
eventType: "providers/provider/eventTypes/event",
resource: "resource",
service: "service",
},
});

expect(cf.__endpoint).to.deep.equal({
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
Expand Down
69 changes: 50 additions & 19 deletions spec/v1/function-builder.spec.ts
Expand Up @@ -24,7 +24,6 @@ import { expect } from "chai";
import { clearParams, defineSecret } from "../../src/params";

import * as functions from "../../src/v1";
import { ResetValue } from "../../src/common/options";

describe("FunctionBuilder", () => {
before(() => {
Expand All @@ -41,6 +40,7 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__trigger.regions).to.deep.equal(["us-east1"]);
TheIronDev marked this conversation as resolved.
Show resolved Hide resolved
expect(fn.__endpoint.region).to.deep.equal(["us-east1"]);
});

Expand All @@ -50,6 +50,7 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__trigger.regions).to.deep.equal(["us-east1", "us-central1"]);
expect(fn.__endpoint.region).to.deep.equal(["us-east1", "us-central1"]);
});

Expand All @@ -68,6 +69,17 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__trigger.regions).to.deep.equal([
"us-central1",
"us-east1",
"us-east4",
"europe-west1",
"europe-west2",
"europe-west3",
"asia-east2",
"asia-northeast1",
]);

expect(fn.__endpoint.region).to.deep.equal([
"us-central1",
"us-east1",
Expand All @@ -93,6 +105,8 @@ describe("FunctionBuilder", () => {
expect(fn.__endpoint.availableMemoryMb).to.deep.equal(256);
expect(fn.__endpoint.timeoutSeconds).to.deep.equal(90);
expect(fn.__endpoint.eventTrigger.retry).to.deep.equal(true);
expect(fn.__trigger.availableMemoryMb).to.deep.equal(256);
expect(fn.__trigger.timeout).to.deep.equal("90s");
});

it("should allow SecretParams in the secrets array and convert them", () => {
Expand All @@ -104,6 +118,11 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__trigger.secrets).to.deep.equal([
{
name: "API_KEY",
},
]);
expect(fn.__endpoint.secretEnvironmentVariables).to.deep.equal([
{
key: "API_KEY",
Expand Down Expand Up @@ -136,6 +155,9 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__trigger.regions).to.deep.equal(["europe-west2"]);
expect(fn.__trigger.availableMemoryMb).to.deep.equal(256);
expect(fn.__trigger.timeout).to.deep.equal("90s");
expect(fn.__endpoint.region).to.deep.equal(["europe-west2"]);
expect(fn.__endpoint.availableMemoryMb).to.deep.equal(256);
expect(fn.__endpoint.timeoutSeconds).to.deep.equal(90);
Expand All @@ -151,6 +173,9 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__trigger.regions).to.deep.equal(["europe-west1"]);
expect(fn.__trigger.availableMemoryMb).to.deep.equal(256);
expect(fn.__trigger.timeout).to.deep.equal("90s");
expect(fn.__endpoint.region).to.deep.equal(["europe-west1"]);
expect(fn.__endpoint.availableMemoryMb).to.deep.equal(256);
expect(fn.__endpoint.timeoutSeconds).to.deep.equal(90);
Expand Down Expand Up @@ -223,6 +248,7 @@ describe("FunctionBuilder", () => {
.runWith({ ingressSettings: "ALLOW_INTERNAL_ONLY" })
.https.onRequest(() => undefined);

expect(fn.__trigger.ingressSettings).to.equal("ALLOW_INTERNAL_ONLY");
expect(fn.__endpoint.ingressSettings).to.equal("ALLOW_INTERNAL_ONLY");
});

Expand All @@ -245,11 +271,8 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

if (!(fn.__endpoint.vpc instanceof ResetValue)) {
expect(fn.__endpoint.vpc.connector).to.equal("test-connector");
} else {
expect.fail("__endpoint.vpc unexpectedly set to RESET_VALUE");
}
expect(fn.__trigger.vpcConnector).to.equal("test-connector");
expect(fn.__endpoint.vpc).to.deep.equal({ connector: "test-connector" });
});

it("should allow a vpcConnectorEgressSettings to be set", () => {
Expand All @@ -261,11 +284,11 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

if (!(fn.__endpoint.vpc instanceof ResetValue)) {
expect(fn.__endpoint.vpc.egressSettings).to.equal("PRIVATE_RANGES_ONLY");
} else {
expect.fail("__endpoint.vpc unexpectedly set to RESET_VALUE");
}
expect(fn.__trigger.vpcConnectorEgressSettings).to.equal("PRIVATE_RANGES_ONLY");
expect(fn.__endpoint.vpc).to.deep.equal({
connector: "test-connector",
egressSettings: "PRIVATE_RANGES_ONLY",
});
});

it("should throw an error if user chooses an invalid vpcConnectorEgressSettings", () => {
Expand All @@ -292,17 +315,22 @@ describe("FunctionBuilder", () => {
.onCreate((user) => user);

expect(fn.__endpoint.serviceAccountEmail).to.equal(serviceAccount);
expect(fn.__trigger.serviceAccountEmail).to.equal(serviceAccount);
});

it("should allow a serviceAccount to be set with generated service account email", () => {
const serviceAccount = "test-service-account@";
const projectId = process.env.GCLOUD_PROJECT;
const fn = functions
.runWith({
serviceAccount,
})
.auth.user()
.onCreate((user) => user);

expect(fn.__trigger.serviceAccountEmail).to.equal(
`test-service-account@${projectId}.iam.gserviceaccount.com`
);
expect(fn.__endpoint.serviceAccountEmail).to.equal(`test-service-account@`);
});

Expand All @@ -315,7 +343,8 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__endpoint.serviceAccountEmail).to.equal("default");
expect(fn.__trigger.serviceAccountEmail).to.be.null;
expect(fn.__endpoint.serviceAccountEmail).to.equal(serviceAccount);
});

it("should throw an error if serviceAccount is set to an invalid value", () => {
Expand All @@ -337,6 +366,7 @@ describe("FunctionBuilder", () => {
.onCreate((user) => user);

expect(fn.__endpoint.availableMemoryMb).to.deep.equal(4096);
expect(fn.__trigger.availableMemoryMb).to.deep.equal(4096);
});

it("should allow labels to be set", () => {
Expand All @@ -349,6 +379,9 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__trigger.labels).to.deep.equal({
"valid-key": "valid-value",
});
expect(fn.__endpoint.labels).to.deep.equal({
"valid-key": "valid-value",
});
Expand Down Expand Up @@ -504,22 +537,20 @@ describe("FunctionBuilder", () => {
.auth.user()
.onCreate((user) => user);

expect(fn.__endpoint.secretEnvironmentVariables).to.deep.equal([
{
key: "API_KEY",
},
]);
expect(fn.__trigger.secrets).to.deep.equal(secrets);
expect(fn.__endpoint.secretEnvironmentVariables).to.deep.equal([{ key: secrets[0] }]);
});

it("should throw error given secrets expressed with full resource name", () => {
const sp = defineSecret("projects/my-project/secrets/API_KEY");

expect(() =>
functions.runWith({
secrets: ["projects/my-project/secrets/API_KEY"],
})
).to.throw();
});

it("should throw error given invalid secret config", () => {
const sp = defineSecret("projects/my-project/secrets/API_KEY");
expect(() =>
functions.runWith({
secrets: [sp],
Expand Down
18 changes: 17 additions & 1 deletion spec/v1/providers/analytics.spec.ts
Expand Up @@ -48,6 +48,10 @@ describe("Analytics Functions", () => {
.analytics.event("event")
.onLog((event) => event);

expect(fn.__trigger.regions).to.deep.equal(["us-east1"]);
expect(fn.__trigger.availableMemoryMb).to.deep.equal(256);
expect(fn.__trigger.timeout).to.deep.equal("90s");

expect(fn.__endpoint.region).to.deep.equal(["us-east1"]);
expect(fn.__endpoint.availableMemoryMb).to.deep.equal(256);
expect(fn.__endpoint.timeoutSeconds).to.deep.equal(90);
Expand All @@ -57,6 +61,14 @@ describe("Analytics Functions", () => {
it("should return a trigger/endpoint with appropriate values", () => {
const cloudFunction = analytics.event("first_open").onLog(() => null);

expect(cloudFunction.__trigger).to.deep.equal({
eventTrigger: {
eventType: "providers/google.firebase.analytics/eventTypes/event.log",
resource: "projects/project1/events/first_open",
service: "app-measurement.com",
},
});

expect(cloudFunction.__endpoint).to.deep.equal({
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
Expand Down Expand Up @@ -293,14 +305,18 @@ describe("Analytics Functions", () => {
});

describe("process.env.GCLOUD_PROJECT not set", () => {
it("should not throw if __endpoint is not accessed", () => {
it("should not throw if __trigger is not accessed", () => {
expect(() => analytics.event("event").onLog(() => null)).to.not.throw(Error);
});

it("should throw when __endpoint is accessed", () => {
expect(() => analytics.event("event").onLog(() => null).__endpoint).to.throw(Error);
});

it("should throw when trigger is accessed", () => {
TheIronDev marked this conversation as resolved.
Show resolved Hide resolved
expect(() => analytics.event("event").onLog(() => null).__trigger).to.throw(Error);
});

it("should not throw when #run is called", () => {
const cf = analytics.event("event").onLog(() => null);

Expand Down