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

Annotate function triggers with __endpoint property #999

Merged
merged 27 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 101 additions & 2 deletions spec/v1/cloud-functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,131 @@ describe('makeCloudFunction', () => {
legacyEventType: 'providers/provider/eventTypes/event',
};

it('should put a __trigger on the returned CloudFunction', () => {
it('should put a __trigger/__endpoint on the returned CloudFunction', () => {
const cf = makeCloudFunction({
provider: 'mock.provider',
eventType: 'mock.event',
service: 'service',
triggerResource: () => 'resource',
handler: () => null,
});

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

expect(cf.__endpoint).to.deep.equal({
platform: 'gcfv1',
eventTrigger: {
eventType: 'mock.provider.mock.event',
eventFilters: {
resource: 'resource',
},
retry: false,
},
});
});

it('should have legacy event type in __trigger 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({
platform: 'gcfv1',
eventTrigger: {
eventType: 'providers/provider/eventTypes/event',
eventFilters: {
resource: 'resource',
},
retry: false,
},
});
});

it('should include converted options in __endpoint', () => {
const cf = makeCloudFunction({
provider: 'mock.provider',
eventType: 'mock.event',
service: 'service',
triggerResource: () => 'resource',
handler: () => null,
options: {
timeoutSeconds: 10,
regions: ['us-central1'],
memory: '128MB',
serviceAccount: 'foo@google.com',
},
});

expect(cf.__endpoint).to.deep.equal({
platform: 'gcfv1',
timeout: '10s',
taeold marked this conversation as resolved.
Show resolved Hide resolved
region: ['us-central1'],
availableMemoryMb: 128,
serviceAccountEmail: 'foo@google.com',
eventTrigger: {
eventType: 'mock.provider.mock.event',
eventFilters: {
resource: 'resource',
},
retry: false,
},
});
});

it('should set retry given failure policy in __endpoint', () => {
const cf = makeCloudFunction({
provider: 'mock.provider',
eventType: 'mock.event',
service: 'service',
triggerResource: () => 'resource',
handler: () => null,
options: { failurePolicy: { retry: {} } },
});

expect(cf.__endpoint).to.deep.equal({
platform: 'gcfv1',
eventTrigger: {
eventType: 'mock.provider.mock.event',
eventFilters: {
resource: 'resource',
},
retry: true,
},
});
});

it('should setup a scheduleTrigger in __endpoint given a schedule', () => {
const schedule = {
schedule: 'every 5 minutes',
retryConfig: { retryCount: 3 },
timeZone: 'America/New_York',
};
const cf = makeCloudFunction({
provider: 'mock.provider',
eventType: 'mock.event',
service: 'service',
triggerResource: () => 'resource',
handler: () => null,
options: {
schedule,
},
});
expect(cf.__endpoint).to.deep.equal({
platform: 'gcfv1',
scheduleTrigger: schedule,
});
});

it('should construct the right context for event', () => {
Expand Down
32 changes: 27 additions & 5 deletions spec/v1/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import { expect } from 'chai';
import * as express from 'express';
import * as _ from 'lodash';

import * as functions from '../../../src/index';
import * as https from '../../../src/providers/https';
import {
Expand Down Expand Up @@ -94,11 +94,15 @@ function runHandler(

describe('CloudHttpsBuilder', () => {
describe('#onRequest', () => {
it('should return a Trigger with appropriate values', () => {
it('should return a trigger/endpoint with appropriate values', () => {
const result = https.onRequest((req, resp) => {
resp.send(200);
});
expect(result.__trigger).to.deep.equal({ httpsTrigger: {} });
expect(result.__endpoint).to.deep.equal({
platform: 'gcfv1',
httpsTrigger: {},
});
});

it('should allow both region and runtime options to be set', () => {
Expand All @@ -115,37 +119,51 @@ describe('CloudHttpsBuilder', () => {
expect(fn.__trigger.availableMemoryMb).to.deep.equal(256);
expect(fn.__trigger.timeout).to.deep.equal('90s');
expect(fn.__trigger.httpsTrigger.invoker).to.deep.equal(['private']);

expect(fn.__endpoint.region).to.deep.equal(['us-east1']);
expect(fn.__endpoint.availableMemoryMb).to.deep.equal(256);
expect(fn.__endpoint.timeout).to.deep.equal('90s');
expect(fn.__endpoint.httpsTrigger.invoker).to.deep.equal(['private']);
});
});
});

describe('handler namespace', () => {
describe('#onRequest', () => {
it('should return an empty trigger', () => {
it('should return an empty trigger and endpoint', () => {
const result = functions.handler.https.onRequest((req, res) => {
res.send(200);
});
expect(result.__trigger).to.deep.equal({});
expect(result.__endpoint).to.deep.equal({});
});
});

describe('#onCall', () => {
it('should return an empty trigger', () => {
it('should return an empty trigger and endpoint', () => {
const result = functions.handler.https.onCall(() => null);
expect(result.__trigger).to.deep.equal({});
expect(result.__endpoint).to.deep.equal({});
});
});
});

describe('#onCall', () => {
it('should return a Trigger with appropriate values', () => {
it('should return a trigger/endpoint with appropriate values', () => {
const result = https.onCall((data) => {
return 'response';
});

expect(result.__trigger).to.deep.equal({
httpsTrigger: {},
labels: { 'deployment-callable': 'true' },
});

expect(result.__endpoint).to.deep.equal({
platform: 'gcfv1',
httpsTrigger: {},
labels: { 'deployment-callable': 'true' },
});
});

it('should allow both region and runtime options to be set', () => {
Expand All @@ -160,6 +178,10 @@ describe('#onCall', () => {
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.timeout).to.deep.equal('90s');
});

it('has a .run method', () => {
Expand Down
17 changes: 17 additions & 0 deletions spec/v2/providers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,20 @@ export const FULL_TRIGGER = {
hello: 'world',
},
};

export const FULL_ENDPOINT = {
platform: 'gcfv2',
region: ['us-west1'],
availableMemoryMb: 512,
timeout: '60s',
minInstances: 1,
maxInstances: 3,
concurrency: 20,
vpcConnector: 'aConnector',
vpcConnectorEgressSettings: 'ALL_TRAFFIC',
serviceAccountEmail: 'root@aProject.iam.gserviceaccount.com',
ingressSettings: 'ALLOW_ALL',
labels: {
hello: 'world',
},
};
67 changes: 62 additions & 5 deletions spec/v2/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
expectedResponseHeaders,
MockRequest,
} from '../../fixtures/mockrequest';
import { FULL_OPTIONS, FULL_TRIGGER } from './helpers';
import { FULL_ENDPOINT, FULL_OPTIONS, FULL_TRIGGER } from './helpers';

/**
* RunHandlerResult contains the data from an express.Response.
Expand Down Expand Up @@ -82,10 +82,11 @@ describe('onRequest', () => {
delete process.env.GCLOUD_PROJECT;
});

it('should return a minimal trigger with appropriate values', () => {
it('should return a minimal trigger/endpoint with appropriate values', () => {
const result = https.onRequest((req, res) => {
res.send(200);
});

expect(result.__trigger).to.deep.equal({
apiVersion: 2,
platform: 'gcfv2',
Expand All @@ -94,9 +95,15 @@ describe('onRequest', () => {
},
labels: {},
});

expect(result.__endpoint).to.deep.equal({
platform: 'gcfv2',
httpsTrigger: {},
labels: {},
});
});

it('should create a complex trigger with appropriate values', () => {
it('should create a complex trigger/endpoint with appropriate values', () => {
const result = https.onRequest(
{
...FULL_OPTIONS,
Expand All @@ -107,6 +114,7 @@ describe('onRequest', () => {
res.send(200);
}
);

expect(result.__trigger).to.deep.equal({
...FULL_TRIGGER,
httpsTrigger: {
Expand All @@ -115,6 +123,14 @@ describe('onRequest', () => {
},
regions: ['us-west1', 'us-central1'],
});

expect(result.__endpoint).to.deep.equal({
...FULL_ENDPOINT,
httpsTrigger: {
invoker: ['service-account1@', 'service-account2@'],
},
region: ['us-west1', 'us-central1'],
});
});

it('should merge options and globalOptions', () => {
Expand Down Expand Up @@ -148,6 +164,17 @@ describe('onRequest', () => {
regions: ['us-west1', 'us-central1'],
labels: {},
});

expect(result.__endpoint).to.deep.equal({
platform: 'gcfv2',
httpsTrigger: {
invoker: ['private'],
},
concurrency: 20,
minInstances: 3,
region: ['us-west1', 'us-central1'],
labels: {},
});
});

it('should be an express handler', async () => {
Expand Down Expand Up @@ -209,8 +236,9 @@ describe('onCall', () => {
delete process.env.GCLOUD_PROJECT;
});

it('should return a minimal trigger with appropriate values', () => {
it('should return a minimal trigger/endpoint with appropriate values', () => {
const result = https.onCall((request) => 42);

expect(result.__trigger).to.deep.equal({
apiVersion: 2,
platform: 'gcfv2',
Expand All @@ -221,10 +249,19 @@ describe('onCall', () => {
'deployment-callable': 'true',
},
});

expect(result.__endpoint).to.deep.equal({
platform: 'gcfv2',
httpsTrigger: {},
labels: {
'deployment-callable': 'true',
taeold marked this conversation as resolved.
Show resolved Hide resolved
},
});
});

it('should create a complex trigger with appropriate values', () => {
it('should create a complex trigger/endpoint with appropriate values', () => {
const result = https.onCall(FULL_OPTIONS, (request) => 42);

expect(result.__trigger).to.deep.equal({
...FULL_TRIGGER,
httpsTrigger: {
Expand All @@ -235,6 +272,15 @@ describe('onCall', () => {
'deployment-callable': 'true',
},
});

expect(result.__endpoint).to.deep.equal({
...FULL_ENDPOINT,
httpsTrigger: {},
labels: {
...FULL_ENDPOINT.labels,
'deployment-callable': 'true',
},
});
});

it('should merge options and globalOptions', () => {
Expand Down Expand Up @@ -265,6 +311,17 @@ describe('onCall', () => {
'deployment-callable': 'true',
},
});

expect(result.__endpoint).to.deep.equal({
platform: 'gcfv2',
httpsTrigger: {},
concurrency: 20,
minInstances: 3,
region: ['us-west1', 'us-central1'],
labels: {
'deployment-callable': 'true',
},
});
});

it('has a .run method', () => {
Expand Down
Loading