Skip to content

Commit

Permalink
fix(options): Adding validation for plugin options
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-cottone committed Aug 3, 2018
1 parent fbc16f9 commit c45aad0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 9 deletions.
26 changes: 18 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class ServerlessEsLogsPlugin {
|| this.serverless.service.provider.region
|| (this.serverless.service.defaults && this.serverless.service.defaults.region)
|| 'us-east-1';
this.validatePluginOptions();

// Add log processing lambda
// TODO: Find the right lifecycle method for this
this.addLogProcesser();
Expand All @@ -53,14 +55,6 @@ class ServerlessEsLogsPlugin {
this.serverless.cli.log('ServerlessEsLogsPlugin.mergeCustomProviderResources()');
const { stage, region } = this.options;
const template = this.serverless.service.provider.compiledCloudFormationTemplate;
const formatOpts = {
options: {
...this.custom.esLogs,
},
region,
service: this.serverless.service.service,
stage,
};

// Add cloudwatch subscriptions to firehose for functions' log groups
this.addCloudwatchSubscriptions();
Expand All @@ -72,6 +66,22 @@ class ServerlessEsLogsPlugin {
this.patchLogProcesserRole();
}

private validatePluginOptions(): void {
const { esLogs } = this.custom;
if (!esLogs) {
throw new this.serverless.classes.Error(`ERROR: No configuration provided for serverless-es-logs!`);
}

const { endpoint, index } = esLogs;
if (!endpoint) {
throw new this.serverless.classes.Error(`ERROR: Must define an endpoint for serverless-es-logs!`);
}

if (!index) {
throw new this.serverless.classes.Error(`ERROR: Must define an index for serverless-es-logs!`);
}
}

private addCloudwatchSubscriptions(): void {
this.addLambdaLogSubscriptions();
}
Expand Down
3 changes: 3 additions & 0 deletions test/support/ServerlessBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const normalizeName = (name: string) => name && `${_.startCase(name).split(' ').

export class ServerlessBuilder {
private readonly defaults = {
classes: {
Error,
},
cli: {
log: sinon.stub(),
},
Expand Down
62 changes: 61 additions & 1 deletion test/unit/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@ import { ServerlessBuilder } from '../support';

// tslint:disable
describe('serverless-es-logs :: Plugin tests', () => {
const defaultOptions = {
service: {
custom: {
esLogs: {
endpoint: 'some_endpoint',
index: 'some_index',
},
},
},
};
let serverless: any;
let options: { [name: string]: any };
let plugin: ServerlessEsLogsPlugin;

beforeEach(() => {
serverless = new ServerlessBuilder().build();
serverless = new ServerlessBuilder(defaultOptions).build();
options = {};
plugin = new ServerlessEsLogsPlugin(serverless, options);
});

Expand All @@ -19,6 +30,55 @@ describe('serverless-es-logs :: Plugin tests', () => {
it('should exist', () => {
expect(plugin.hooks['after:package:initialize']).to.exist;
});

it('should validate plugin options successfully', () => {
expect(plugin.hooks['after:package:initialize']).to.not.throw;
});

it('should throw an error if missing plugin options', () => {
serverless = new ServerlessBuilder().build();
plugin = new ServerlessEsLogsPlugin(serverless, options);
expect(plugin.hooks['after:package:initialize']).to.throw(
Error,
'ERROR: No configuration provided for serverless-es-logs!',
);
});

it('should throw an error if missing option \'endpoint\'', () => {
const opts = {
service: {
custom: {
esLogs: {
index: 'some_index',
},
},
},
};
serverless = new ServerlessBuilder(opts).build();
plugin = new ServerlessEsLogsPlugin(serverless, options);
expect(plugin.hooks['after:package:initialize']).to.throw(
Error,
'ERROR: Must define an endpoint for serverless-es-logs!',
);
});

it('should throw an error if missing option \'index\'', () => {
const opts = {
service: {
custom: {
esLogs: {
endpoint: 'some_endpoint',
},
},
},
};
serverless = new ServerlessBuilder(opts).build();
plugin = new ServerlessEsLogsPlugin(serverless, options);
expect(plugin.hooks['after:package:initialize']).to.throw(
Error,
'ERROR: Must define an index for serverless-es-logs!',
);
});
});

describe('after:package:createDeploymentArtifacts', () => {
Expand Down

0 comments on commit c45aad0

Please sign in to comment.