Skip to content

Commit 43e2d90

Browse files
Ace Nassrifhinkel
authored andcommitted
Add region tags + human review (#1499)
1 parent 8fe451e commit 43e2d90

File tree

31 files changed

+2071
-1868
lines changed

31 files changed

+2071
-1868
lines changed

functions/background/test/index.test.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,25 @@ it('should make a promise request', async () => {
6767
assert.ok(response.body.includes(`Example Domain`));
6868
});
6969

70-
it('should return synchronously', () => {
71-
assert.strictEqual(
72-
program.helloSynchronous({
73-
something: true,
74-
}),
75-
'Something is true!'
76-
);
77-
});
78-
79-
it('should throw an error', () => {
80-
assert.throws(
81-
() => {
70+
describe('functions_background_synchronous', () => {
71+
it('should return synchronously', () => {
72+
assert.strictEqual(
8273
program.helloSynchronous({
83-
something: false,
84-
});
85-
},
86-
Error,
87-
'Something was not true!'
88-
);
74+
something: true,
75+
}),
76+
'Something is true!'
77+
);
78+
});
79+
80+
it('should throw an error', () => {
81+
assert.throws(
82+
() => {
83+
program.helloSynchronous({
84+
something: false,
85+
});
86+
},
87+
Error,
88+
'Something was not true!'
89+
);
90+
});
8991
});

functions/billing/test/index.test.js

Lines changed: 72 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,28 @@ describe('functions/billing tests', () => {
6969
await handleLinuxFailures(ffProc);
7070
});
7171

72-
it('should notify Slack when budget is exceeded', async () => {
73-
const jsonData = {costAmount: 500, budgetAmount: 400};
74-
const encodedData = Buffer.from(JSON.stringify(jsonData)).toString(
75-
'base64'
76-
);
77-
const pubsubMessage = {data: encodedData, attributes: {}};
78-
79-
const response = await requestRetry({
80-
url: `${BASE_URL}/notifySlack`,
81-
method: 'POST',
82-
body: {data: pubsubMessage},
83-
retryDelay: 200,
84-
json: true,
72+
describe('functions_billing_slack', () => {
73+
it('should notify Slack when budget is exceeded', async () => {
74+
const jsonData = {costAmount: 500, budgetAmount: 400};
75+
const encodedData = Buffer.from(JSON.stringify(jsonData)).toString(
76+
'base64'
77+
);
78+
const pubsubMessage = {data: encodedData, attributes: {}};
79+
80+
const response = await requestRetry({
81+
url: `${BASE_URL}/notifySlack`,
82+
method: 'POST',
83+
body: {data: pubsubMessage},
84+
retryDelay: 200,
85+
json: true,
86+
});
87+
88+
assert.strictEqual(response.statusCode, 200);
89+
assert.strictEqual(
90+
response.body,
91+
'Slack notification sent successfully'
92+
);
8593
});
86-
87-
assert.strictEqual(response.statusCode, 200);
88-
assert.strictEqual(response.body, 'Slack notification sent successfully');
8994
});
9095
});
9196

@@ -105,60 +110,64 @@ describe('functions/billing tests', () => {
105110
await handleLinuxFailures(ffProc);
106111
});
107112

108-
it('should disable billing when budget is exceeded', async () => {
109-
// Use functions framework to ensure sample follows GCF specification
110-
// (Invoking it directly works too, but DOES NOT ensure GCF compatibility)
113+
describe('functions_billing_stop', () => {
114+
it('should disable billing when budget is exceeded', async () => {
115+
// Use functions framework to ensure sample follows GCF specification
116+
// (Invoking it directly works too, but DOES NOT ensure GCF compatibility)
117+
const jsonData = {costAmount: 500, budgetAmount: 400};
118+
const encodedData = Buffer.from(JSON.stringify(jsonData)).toString(
119+
'base64'
120+
);
121+
const pubsubMessage = {data: encodedData, attributes: {}};
122+
123+
const response = await requestRetry({
124+
url: `${BASE_URL}/stopBilling`,
125+
method: 'POST',
126+
body: {data: pubsubMessage},
127+
retryDelay: 200,
128+
json: true,
129+
});
130+
131+
assert.strictEqual(response.statusCode, 200);
132+
assert.ok(response.body.includes('Billing disabled'));
133+
});
134+
});
135+
});
136+
});
137+
138+
describe('shuts down GCE instances', () => {
139+
describe('functions_billing_limit', () => {
140+
it('should attempt to shut down GCE instances when budget is exceeded', async () => {
141+
// Mock GCE (because real GCE instances take too long to start/stop)
142+
const listInstancesResponseMock = {
143+
data: {
144+
items: [{name: 'test-instance-1', status: 'RUNNING'}],
145+
},
146+
};
147+
148+
const computeMock = {
149+
instances: {
150+
list: sinon.stub().returns(listInstancesResponseMock),
151+
stop: sinon.stub().resolves({data: {}}),
152+
},
153+
};
154+
155+
const googleapisMock = Object.assign({}, googleapis);
156+
googleapisMock.google.compute = sinon.stub().returns(computeMock);
157+
158+
// Run test
111159
const jsonData = {costAmount: 500, budgetAmount: 400};
112160
const encodedData = Buffer.from(JSON.stringify(jsonData)).toString(
113161
'base64'
114162
);
115163
const pubsubMessage = {data: encodedData, attributes: {}};
116164

117-
const response = await requestRetry({
118-
url: `${BASE_URL}/stopBilling`,
119-
method: 'POST',
120-
body: {data: pubsubMessage},
121-
retryDelay: 200,
122-
json: true,
123-
});
165+
const sample = proxyquire('../', {googleapis: googleapisMock}); // kokoro-allow-mock
124166

125-
assert.strictEqual(response.statusCode, 200);
126-
assert.ok(response.body.includes('Billing disabled'));
127-
});
128-
});
129-
});
167+
await sample.limitUse(pubsubMessage);
130168

131-
describe('shuts down GCE instances', () => {
132-
it('should attempt to shut down GCE instances when budget is exceeded', async () => {
133-
// Mock GCE (because real GCE instances take too long to start/stop)
134-
const listInstancesResponseMock = {
135-
data: {
136-
items: [{name: 'test-instance-1', status: 'RUNNING'}],
137-
},
138-
};
139-
140-
const computeMock = {
141-
instances: {
142-
list: sinon.stub().returns(listInstancesResponseMock),
143-
stop: sinon.stub().resolves({data: {}}),
144-
},
145-
};
146-
147-
const googleapisMock = Object.assign({}, googleapis);
148-
googleapisMock.google.compute = sinon.stub().returns(computeMock);
149-
150-
// Run test
151-
const jsonData = {costAmount: 500, budgetAmount: 400};
152-
const encodedData = Buffer.from(JSON.stringify(jsonData)).toString(
153-
'base64'
154-
);
155-
const pubsubMessage = {data: encodedData, attributes: {}};
156-
157-
const sample = proxyquire('../', {googleapis: googleapisMock}); // kokoro-allow-mock
158-
159-
await sample.limitUse(pubsubMessage);
160-
161-
assert.strictEqual(computeMock.instances.list.calledOnce, true);
162-
assert.ok(computeMock.instances.stop.calledOnce);
169+
assert.strictEqual(computeMock.instances.list.calledOnce, true);
170+
assert.ok(computeMock.instances.stop.calledOnce);
171+
});
163172
});
164173
});

functions/billing/test/periodic.test.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,34 @@ before(async () => {
5454
}
5555
});
5656

57-
it('should shut down GCE instances when budget is exceeded', async () => {
58-
const ffProc = execPromise(
59-
`functions-framework --target=limitUse --signature-type=event`,
60-
{timeout: 1000, shell: true, cwd}
61-
);
57+
describe('functions_billing_limit', () => {
58+
it('should shut down GCE instances when budget is exceeded', async () => {
59+
const ffProc = execPromise(
60+
`functions-framework --target=limitUse --signature-type=event`,
61+
{timeout: 1000, shell: true, cwd}
62+
);
6263

63-
const jsonData = {costAmount: 500, budgetAmount: 400};
64-
const encodedData = Buffer.from(JSON.stringify(jsonData)).toString('base64');
65-
const pubsubMessage = {data: encodedData, attributes: {}};
64+
const jsonData = {costAmount: 500, budgetAmount: 400};
65+
const encodedData = Buffer.from(JSON.stringify(jsonData)).toString(
66+
'base64'
67+
);
68+
const pubsubMessage = {data: encodedData, attributes: {}};
6669

67-
const response = await requestRetry({
68-
url: `${BASE_URL}/`,
69-
method: 'POST',
70-
body: {data: pubsubMessage},
71-
retryDelay: 200,
72-
json: true,
73-
});
70+
const response = await requestRetry({
71+
url: `${BASE_URL}/`,
72+
method: 'POST',
73+
body: {data: pubsubMessage},
74+
retryDelay: 200,
75+
json: true,
76+
});
7477

75-
// Wait for the functions framework to stop
76-
// Must be BEFORE assertions, in case they fail
77-
await ffProc;
78+
// Wait for the functions framework to stop
79+
// Must be BEFORE assertions, in case they fail
80+
await ffProc;
7881

79-
console.log(response.body);
82+
console.log(response.body);
8083

81-
assert.strictEqual(response.statusCode, 200);
82-
assert.ok(response.body.includes('instance(s) stopped successfully'));
84+
assert.strictEqual(response.statusCode, 200);
85+
assert.ok(response.body.includes('instance(s) stopped successfully'));
86+
});
8387
});

functions/composer-storage-trigger/test/index.test.js

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -30,62 +30,64 @@ const getSample = FetchStub => {
3030
};
3131
};
3232

33-
it('Handles error in JSON body', async () => {
34-
const event = {
35-
data: {
36-
file: 'some-file',
37-
},
38-
};
39-
const expectedMsg = 'Something bad happened.';
40-
const bodyJson = {error: expectedMsg};
41-
const body = {
42-
json: sinon.stub().returns(bodyJson),
43-
};
44-
const sample = getSample(sinon.stub().resolves(body));
33+
describe('composer_trigger', () => {
34+
it('Handles error in JSON body', async () => {
35+
const event = {
36+
data: {
37+
file: 'some-file',
38+
},
39+
};
40+
const expectedMsg = 'Something bad happened.';
41+
const bodyJson = {error: expectedMsg};
42+
const body = {
43+
json: sinon.stub().returns(bodyJson),
44+
};
45+
const sample = getSample(sinon.stub().resolves(body));
4546

46-
try {
47-
await sample.program.triggerDag(event);
48-
assert.fail('No error thrown');
49-
} catch (err) {
50-
assert.deepStrictEqual(err, new Error('Something bad happened.'));
51-
}
52-
});
47+
try {
48+
await sample.program.triggerDag(event);
49+
assert.fail('No error thrown');
50+
} catch (err) {
51+
assert.deepStrictEqual(err, new Error('Something bad happened.'));
52+
}
53+
});
5354

54-
it('Handles error in IAP response.', async () => {
55-
const event = {
56-
data: {
57-
file: 'some-file',
58-
},
59-
};
60-
const expectedMsg = 'Default IAP Error Message.';
55+
it('Handles error in IAP response.', async () => {
56+
const event = {
57+
data: {
58+
file: 'some-file',
59+
},
60+
};
61+
const expectedMsg = 'Default IAP Error Message.';
6162

62-
const serviceAccountAccessTokenRes = {
63-
json: sinon.stub().resolves({access_token: 'default-access-token'}),
64-
};
65-
const signJsonClaimRes = {
66-
json: sinon.stub().resolves({signature: 'default-jwt-signature'}),
67-
};
68-
const getTokenRes = {
69-
json: sinon.stub().resolves({id_token: 'default-id-token'}),
70-
};
71-
const makeIapPostRequestRes = {
72-
ok: false,
73-
text: sinon.stub().resolves(expectedMsg),
74-
};
75-
const FetchStub = sinon
76-
.stub()
77-
.onCall(0)
78-
.resolves(serviceAccountAccessTokenRes)
79-
.onCall(1)
80-
.resolves(signJsonClaimRes)
81-
.onCall(2)
82-
.resolves(getTokenRes)
83-
.onCall(3)
84-
.resolves(makeIapPostRequestRes);
85-
const sample = getSample(FetchStub);
86-
try {
87-
await sample.program.triggerDag(event);
88-
} catch (err) {
89-
assert.deepStrictEqual(err, new Error(expectedMsg));
90-
}
63+
const serviceAccountAccessTokenRes = {
64+
json: sinon.stub().resolves({access_token: 'default-access-token'}),
65+
};
66+
const signJsonClaimRes = {
67+
json: sinon.stub().resolves({signature: 'default-jwt-signature'}),
68+
};
69+
const getTokenRes = {
70+
json: sinon.stub().resolves({id_token: 'default-id-token'}),
71+
};
72+
const makeIapPostRequestRes = {
73+
ok: false,
74+
text: sinon.stub().resolves(expectedMsg),
75+
};
76+
const FetchStub = sinon
77+
.stub()
78+
.onCall(0)
79+
.resolves(serviceAccountAccessTokenRes)
80+
.onCall(1)
81+
.resolves(signJsonClaimRes)
82+
.onCall(2)
83+
.resolves(getTokenRes)
84+
.onCall(3)
85+
.resolves(makeIapPostRequestRes);
86+
const sample = getSample(FetchStub);
87+
try {
88+
await sample.program.triggerDag(event);
89+
} catch (err) {
90+
assert.deepStrictEqual(err, new Error(expectedMsg));
91+
}
92+
});
9193
});

functions/concepts/test/index.test.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ const sample = require('../');
2424
beforeEach(tools.stubConsole);
2525
afterEach(tools.restoreConsole);
2626

27-
it('should demonstrate error type behavior', () => {
28-
const objError = new Error('Error object!');
27+
describe('functions_concepts_error_object', () => {
28+
it('should demonstrate error type behavior', () => {
29+
const objError = new Error('Error object!');
2930

30-
const req = {body: {throwAsString: true}};
31-
const res = {end: sinon.stub()};
31+
const req = {body: {throwAsString: true}};
32+
const res = {end: sinon.stub()};
3233

33-
sample.errorTypes(req, res);
34-
assert.deepStrictEqual(console.error.getCall(0).args, [objError]);
34+
sample.errorTypes(req, res);
35+
assert.deepStrictEqual(console.error.getCall(0).args, [objError]);
36+
});
3537
});

0 commit comments

Comments
 (0)