Skip to content

Commit

Permalink
Fix tests and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
rothso committed Dec 26, 2020
1 parent 59a702d commit fd9794e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 38 deletions.
16 changes: 8 additions & 8 deletions logs/lib/retrieveLogs.js
Expand Up @@ -16,8 +16,7 @@ module.exports = {
const pageSize = this.options.count || 10;

func = getGoogleCloudFunctionName(this.serverless.service.functions, func);

// Actually the function name on GCP is service-stage-handler

return this.provider.request('logging', 'entries', 'list', {
filter: `resource.labels.function_name="${func}" AND NOT textPayload=""`,
orderBy: 'timestamp desc',
Expand All @@ -29,7 +28,6 @@ module.exports = {
printLogs(logs) {
if (!logs.entries || !logs.entries.length) {
logs = {
//eslint-disable-line
entries: [
{
timestamp: new Date().toISOString().slice(0, 10),
Expand All @@ -39,10 +37,13 @@ module.exports = {
};
}

let output = logs.entries.reduce(
(p, c) => (p += `${chalk.grey(`${c.timestamp}:`)} | Execution ID: ${c.labels.execution_id} | ${c.textPayload}\n`),
''
); //eslint-disable-line
let output = logs.entries.reduce((p, c) => {
let message = '';
message += `${chalk.grey(`${c.timestamp}:`)}\t`;
message += c.labels && c.labels.execution_id ? `[${c.labels.execution_id}]\t` : '';
message += `${c.textPayload}\n`;
return p + message;
}, '');

output = `Displaying the ${logs.entries.length} most recent log(s):\n\n${output}`; // prettify output
output = output.slice(0, output.length - 1); // remove "\n---\n\n" for the last log entry
Expand All @@ -53,7 +54,6 @@ module.exports = {
},
};

// retrieve the functions name (Google uses our handler property as the function name)
const getGoogleCloudFunctionName = (serviceFunctions, func) => {
if (!serviceFunctions[func]) {
const errorMessage = [
Expand Down
25 changes: 16 additions & 9 deletions logs/lib/retrieveLogs.test.js
Expand Up @@ -23,6 +23,7 @@ describe('RetrieveLogs', () => {
serverless.service.functions = {
func1: {
handler: 'foo',
name: 'full-function-name',
},
};
serverless.setProvider('google', new GoogleProvider(serverless));
Expand Down Expand Up @@ -67,13 +68,11 @@ describe('RetrieveLogs', () => {

it('should return a default amount of logs for the function if the "count" option is not given', () => {
googleLogs.options.function = 'func1';
let func = googleLogs.options.function;
func = googleLogs.getGoogleCloudFunctionName(serverless.service.functions, func);

return googleLogs.getLogs().then(() => {
expect(
requestStub.calledWithExactly('logging', 'entries', 'list', {
filter: `resource.labels.function_name="${func}" AND NOT textPayload=""`,
filter: 'resource.labels.function_name="full-function-name" AND NOT textPayload=""',
orderBy: 'timestamp desc',
resourceNames: ['projects/my-project'],
pageSize: 10,
Expand All @@ -89,7 +88,7 @@ describe('RetrieveLogs', () => {
return googleLogs.getLogs().then(() => {
expect(
requestStub.calledWithExactly('logging', 'entries', 'list', {
filter: 'Function execution foo us-central1',
filter: 'resource.labels.function_name="full-function-name" AND NOT textPayload=""',
orderBy: 'timestamp desc',
resourceNames: ['projects/my-project'],
pageSize: googleLogs.options.count,
Expand Down Expand Up @@ -119,13 +118,21 @@ describe('RetrieveLogs', () => {
it('should print the received execution result log on the console', () => {
const logs = {
entries: [
{ timestamp: '1970-01-01 00:00', textPayload: 'Entry 1', labels: {execution_id: 'foo'}, },
{ timestamp: '1970-01-01 00:01', textPayload: 'Entry 2', labels: {execution_id: 'bar'}, },
{
timestamp: '1970-01-01 00:00',
textPayload: 'Entry 1',
labels: { execution_id: 'foo' },
},
{
timestamp: '1970-01-01 00:01',
textPayload: 'Entry 2',
labels: { execution_id: 'bar' },
},
],
};

const logEntry1 = `${chalk.grey('1970-01-01 00:00:')} | Execution ID: foo | Entry 1`;
const logEntry2 = `${chalk.grey('1970-01-01 00:01:')} | Execution ID: bar | Entry 2`;
const logEntry1 = `${chalk.grey('1970-01-01 00:00:')}\t[foo]\tEntry 1`;
const logEntry2 = `${chalk.grey('1970-01-01 00:01:')}\t[bar]\tEntry 2`;
const expectedOutput = `Displaying the 2 most recent log(s):\n\n${logEntry1}\n${logEntry2}`;

return googleLogs.printLogs(logs).then(() => {
Expand All @@ -135,7 +142,7 @@ describe('RetrieveLogs', () => {

it('should print a default message to the console when no logs were received', () => {
const date = `${new Date().toISOString().slice(0, 10)}:`;
const logEntry = `${chalk.grey(date)} There is no log data to show...`;
const logEntry = `${chalk.grey(date)}\tThere is no log data to show...`;
const expectedOutput = `Displaying the 1 most recent log(s):\n\n${logEntry}`;

return googleLogs.printLogs({}).then(() => {
Expand Down
16 changes: 6 additions & 10 deletions provider/googleProvider.js
@@ -1,7 +1,6 @@
'use strict';

const path = require('path');
const fs = require('fs');
const os = require('os');

const _ = require('lodash');
Expand Down Expand Up @@ -114,18 +113,15 @@ class GoogleProvider {
credentials = credParts.reduce((memo, part) => path.join(memo, part), '');
}

const auth = new google.auth.GoogleAuth({
return new google.auth.GoogleAuth({
keyFile: credentials.toString(),
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
scopes: 'https://www.googleapis.com/auth/cloud-platform',
});
return auth
}

const auth = new google.auth.GoogleAuth({
scopes: 'https://www.googleapis.com/auth/cloud-platform'
}

return new google.auth.GoogleAuth({
scopes: 'https://www.googleapis.com/auth/cloud-platform',
});
return auth

}

isServiceSupported(service) {
Expand Down
26 changes: 15 additions & 11 deletions provider/googleProvider.test.js
@@ -1,6 +1,5 @@
'use strict';

const fs = require('fs');
const os = require('os');

const sinon = require('sinon');
Expand All @@ -10,7 +9,6 @@ const GoogleProvider = require('./googleProvider');
const Serverless = require('../test/serverless');

describe('GoogleProvider', () => {
let readFileSyncStub;
let googleProvider;
let serverless;
let setProviderStub;
Expand All @@ -22,20 +20,15 @@ describe('GoogleProvider', () => {
serverless.service = {
provider: {
project: 'example-project',
credentials: '/root/.gcloud/project-1234.json',
},
};
setProviderStub = sinon.stub(serverless, 'setProvider').returns();
readFileSyncStub = sinon
.stub(fs, 'readFileSync')
.returns('{"client_email": "foo@bar.de","private_key": "wasdqwerty"}');
homedirStub = sinon.stub(os, 'homedir').returns('/root');
googleProvider = new GoogleProvider(serverless);
});

afterEach(() => {
serverless.setProvider.restore();
fs.readFileSync.restore();
os.homedir.restore();
});

Expand Down Expand Up @@ -94,7 +87,7 @@ describe('GoogleProvider', () => {
},
};
sinon.stub(googleProvider, 'getAuthClient').returns({
authorize: sinon.stub().resolves(),
getClient: sinon.stub().resolves(),
});
sinon.stub(googleProvider, 'isServiceSupported').returns();
});
Expand Down Expand Up @@ -134,10 +127,20 @@ describe('GoogleProvider', () => {
});

describe('#getAuthClient()', () => {
it('should return a new authClient', () => {
it('should return a new authClient when using default credentials', () => {
const authClient = googleProvider.getAuthClient();

expect(readFileSyncStub.calledWithExactly('/root/.gcloud/project-1234.json')).toEqual(true);
expect(authClient.keyFilename).toEqual(undefined);
expect(authClient).toBeInstanceOf(google.auth.GoogleAuth);
});

it('should return a new authClient when using a credentials file', () => {
googleProvider.serverless.service.provider.credentials = '/root/.gcloud/project-1234.json';

const authClient = googleProvider.getAuthClient();

expect(authClient.keyFilename).toEqual('/root/.gcloud/project-1234.json');
expect(authClient).toBeInstanceOf(google.auth.GoogleAuth);
});

it('should expand tilde characters in credentials file paths', () => {
Expand All @@ -146,7 +149,8 @@ describe('GoogleProvider', () => {
const authClient = googleProvider.getAuthClient();

expect(homedirStub.calledOnce).toEqual(true);
expect(readFileSyncStub.calledWithExactly('/root/.gcloud/project-1234.json')).toEqual(true);
expect(authClient.keyFilename).toEqual('/root/.gcloud/project-1234.json');
expect(authClient).toBeInstanceOf(google.auth.GoogleAuth);
});
});

Expand Down

0 comments on commit fd9794e

Please sign in to comment.