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

Make Stackdriver trigger sample Node-version-agnostic #769

Merged
merged 3 commits into from
Oct 16, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions functions/log/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,13 @@ function getMetrics (callback) {
}

// [START functions_log_stackdriver]
exports.processLogEntry = (data, callback) => {
const dataBuffer = Buffer.from(data.data.data, 'base64');
exports.processLogEntry = (data) => {
const dataBuffer = Buffer.from(data.data.data || data.data, 'base64');
const logEntry = JSON.parse(dataBuffer.toString('ascii')).protoPayload;

console.log(`Method: ${logEntry.methodName}`);
console.log(`Resource: ${logEntry.resourceName}`);
console.log(`Initiator: ${logEntry.authenticationInfo.principalEmail}`);
callback();
};
// [END functions_log_stackdriver]

Expand Down
28 changes: 24 additions & 4 deletions functions/log/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ test.serial(`getMetrics: should retrieve metrics`, (t) => {

test(`processLogEntry: should process log entry`, (t) => {
const sample = getSample();
const callback = sinon.stub();

const json = JSON.stringify({
protoPayload: {
methodName: 'method',
Expand All @@ -106,10 +104,32 @@ test(`processLogEntry: should process log entry`, (t) => {
}
};

sample.program.processLogEntry(data, callback);
sample.program.processLogEntry(data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am concerned that this line will pass and t.true will fail as the console log hasn't happened yet. This may need to be

sample.program.processLogEntry(data).then(() => {
t.true(...

Copy link
Contributor Author

@ace-n ace-n Oct 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

processLogEntry is a synchronous operation, so that shouldn't be a concern.

(If it's modified to be async in the future, then the test will fail. I would be concerned if it silently passed instead.)


t.true(console.log.calledWith(`Method: method`));
t.true(console.log.calledWith(`Resource: resource`));
t.true(console.log.calledWith(`Initiator: me@example.com`));
});

test(`processLogEntry: should work in Node 8`, (t) => {
const sample = getSample();
const json = JSON.stringify({
protoPayload: {
methodName: 'method',
resourceName: 'resource',
authenticationInfo: {
principalEmail: 'me@example.com'
}
}
});

const data = {
data: Buffer.from(json, 'ascii')
};

sample.program.processLogEntry(data);

t.true(console.log.calledWith(`Method: method`));
t.true(console.log.calledWith(`Resource: resource`));
t.true(console.log.calledWith(`Initiator: me@example.com`));
t.is(callback.callCount, 1);
});