Skip to content
This repository has been archived by the owner on Jun 4, 2019. It is now read-only.

Commit

Permalink
Update plugin.js (#6)
Browse files Browse the repository at this point in the history
* Update plugin.js

* Rewrite validation

* Rename test cases
  • Loading branch information
sbstjn committed Apr 28, 2017
1 parent 7f2868c commit b83db98
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 95 deletions.
37 changes: 18 additions & 19 deletions src/plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict'

const AWS = require('aws-sdk')
const assert = require('assert')
const util = require('util')
const AWS = require('aws-sdk')

class Plugin {
constructor (serverless, options) {
Expand All @@ -15,29 +16,25 @@ class Plugin {
}
}

afterDeploy () {
if (typeof this.options.noDeploy !== 'undefined' && this.options.noDeploy) {
return Promise.resolve()
}

const list = this.configuration()

if (list.length === 0) {
this.serverless.cli.log('Skipping TTL setting(s) for DynamoDB: no configuration found')

return Promise.resolve()
}

this.serverless.cli.log('Enabling TTL setting(s) for DynamoDB')
validate () {
assert(this.options && !this.options.noDeploy, 'noDeploy')
assert(this.configuration().constructor === Array, 'invalid configuration found')
assert(this.configuration().length > 0, 'no configuration found')
}

return Promise.all(
this.configuration().map(
afterDeploy () {
return Promise.resolve().then(
this.validate.bind(this)
).then(
() => this.serverless.cli.log('Enabling TTL setting(s) for DynamoDB')
).then(
() => this.configuration().map(
data => this.check(data.table).then(
enabled => enabled || this.enable(data)
)
)
).catch(
error => this.serverless.cli.log(util.format('Failed to set TTL for DynamoDB: %s', error))
err => this.serverless.cli.log(util.format('Skipping TTL setting(s) for DynamoDB: %s', err.message))
)
}

Expand All @@ -64,8 +61,10 @@ class Plugin {
}

configuration () {
if (this.serverless && this.serverless.service && this.serverless.service.custom && this.serverless.service.custom.dynamodb) {
try {
return this.serverless.service.custom.dynamodb.ttl || []
} catch (e) {
return []
}
}
}
Expand Down
193 changes: 117 additions & 76 deletions test/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,146 @@ describe('Plugin', () => {
AWS.restore('DynamoDB')
})

it('reads configuration', () => {
const config = {
cli: { log: () => {} },
service: {
custom: {
dynamodb: {
ttl: [
{
table: 'my-table-1',
field: 'my-field-1'
},
{
table: 'my-table-2',
field: 'my-field-2'
}
]
describe('Configuration', () => {
it('Reads configuration', () => {
const config = {
cli: { log: () => {} },
service: {
custom: {
dynamodb: {
ttl: [
{ table: 'my-table-1', field: 'my-field-1' },
{ table: 'my-table-2', field: 'my-field-2' }
]
}
}
}
}
}

const test = new Plugin(config)
const test = new Plugin(config)

expect(test.configuration()).toContainEqual({ table: 'my-table-1', field: 'my-field-1' })
expect(test.configuration()).toContainEqual({ table: 'my-table-2', field: 'my-field-2' })
})
expect(test.configuration()).toContainEqual({ table: 'my-table-1', field: 'my-field-1' })
expect(test.configuration()).toContainEqual({ table: 'my-table-2', field: 'my-field-2' })
})

it('Works without configuration', () => {
let dynamoDBdescribeTimeToLiveSpy = jest.fn((_, cb) => cb(null, ''))
let dynamoDBupdateTimeToLiveSpy = jest.fn((_, cb) => cb(null, ''))
it('Skips on noDeploy', () => {
let log = jest.fn()

AWS.mock('DynamoDB', 'describeTimeToLive', dynamoDBdescribeTimeToLiveSpy)
AWS.mock('DynamoDB', 'updateTimeToLive', dynamoDBupdateTimeToLiveSpy)
const config = { cli: { log }, service: { } }
return new Plugin(config, { noDeploy: true }).afterDeploy().then(
() => expect(log).toBeCalledWith('Skipping TTL setting(s) for DynamoDB: noDeploy')
)
})

const config = {
cli: { log: () => {} },
service: {
custom: {
dynamodb: { }
it('Skips when no custom.dynamodb.ttl is found', () => {
let log = jest.fn()
let dynamoDBdescribeTimeToLiveSpy = jest.fn((_, cb) => cb(null, ''))
let dynamoDBupdateTimeToLiveSpy = jest.fn((_, cb) => cb(null, ''))

AWS.mock('DynamoDB', 'describeTimeToLive', dynamoDBdescribeTimeToLiveSpy)
AWS.mock('DynamoDB', 'updateTimeToLive', dynamoDBupdateTimeToLiveSpy)

const config = {
cli: { log },
service: { custom: { dynamodb: { } } }
}

return new Plugin(config, { region: 'eu-west-1' }).afterDeploy().then(
() => {
expect(log).toBeCalledWith('Skipping TTL setting(s) for DynamoDB: no configuration found')
expect(dynamoDBdescribeTimeToLiveSpy).toHaveBeenCalledTimes(0)
expect(dynamoDBupdateTimeToLiveSpy).toHaveBeenCalledTimes(0)
}
)
})

it('Skips when no custom.dynamodb is found', () => {
let log = jest.fn()
let dynamoDBdescribeTimeToLiveSpy = jest.fn((_, cb) => cb(null, ''))
let dynamoDBupdateTimeToLiveSpy = jest.fn((_, cb) => cb(null, ''))

AWS.mock('DynamoDB', 'describeTimeToLive', dynamoDBdescribeTimeToLiveSpy)
AWS.mock('DynamoDB', 'updateTimeToLive', dynamoDBupdateTimeToLiveSpy)

const config = {
cli: { log },
service: { custom: { } }
}
}

return new Plugin(config, { region: 'eu-west-1' }).afterDeploy().then(
() => {
expect(dynamoDBdescribeTimeToLiveSpy).toHaveBeenCalledTimes(0)
expect(dynamoDBupdateTimeToLiveSpy).toHaveBeenCalledTimes(0)
return new Plugin(config, { region: 'eu-west-1' }).afterDeploy().then(
() => {
expect(log).toBeCalledWith('Skipping TTL setting(s) for DynamoDB: no configuration found')
expect(dynamoDBdescribeTimeToLiveSpy).toHaveBeenCalledTimes(0)
expect(dynamoDBupdateTimeToLiveSpy).toHaveBeenCalledTimes(0)
}
)
})

it('Skips when no custom is found', () => {
let log = jest.fn()
let dynamoDBdescribeTimeToLiveSpy = jest.fn((_, cb) => cb(null, ''))
let dynamoDBupdateTimeToLiveSpy = jest.fn((_, cb) => cb(null, ''))

AWS.mock('DynamoDB', 'describeTimeToLive', dynamoDBdescribeTimeToLiveSpy)
AWS.mock('DynamoDB', 'updateTimeToLive', dynamoDBupdateTimeToLiveSpy)

const config = {
cli: { log },
service: { }
}
)

return new Plugin(config, { region: 'eu-west-1' }).afterDeploy().then(
() => {
expect(log).toBeCalledWith('Skipping TTL setting(s) for DynamoDB: no configuration found')
expect(dynamoDBdescribeTimeToLiveSpy).toHaveBeenCalledTimes(0)
expect(dynamoDBupdateTimeToLiveSpy).toHaveBeenCalledTimes(0)
}
)
})

it('Skips when custom.dynamodb.ttl is not an array', () => {
let log = jest.fn()
let dynamoDBdescribeTimeToLiveSpy = jest.fn((_, cb) => cb(null, ''))
let dynamoDBupdateTimeToLiveSpy = jest.fn((_, cb) => cb(null, ''))

AWS.mock('DynamoDB', 'describeTimeToLive', dynamoDBdescribeTimeToLiveSpy)
AWS.mock('DynamoDB', 'updateTimeToLive', dynamoDBupdateTimeToLiveSpy)

const config = {
cli: { log },
service: { custom: { dynamodb: { ttl: { invalid: true } } } }
}

return new Plugin(config, { region: 'eu-west-1' }).afterDeploy().then(
() => {
expect(log).toBeCalledWith('Skipping TTL setting(s) for DynamoDB: invalid configuration found')
expect(dynamoDBdescribeTimeToLiveSpy).toHaveBeenCalledTimes(0)
expect(dynamoDBupdateTimeToLiveSpy).toHaveBeenCalledTimes(0)
}
)
})
})

it('Updates TTL setting if not alreadt set', () => {
let log = jest.fn()
let dynamoDBdescribeTimeToLiveSpy = jest.fn((_, cb) => cb(null, { TimeToLiveDescription: { TimeToLiveStatus: false } }))
let dynamoDBupdateTimeToLiveSpy = jest.fn((_, cb) => cb(null, ''))

AWS.mock('DynamoDB', 'describeTimeToLive', dynamoDBdescribeTimeToLiveSpy)
AWS.mock('DynamoDB', 'updateTimeToLive', dynamoDBupdateTimeToLiveSpy)

const config = {
cli: { log: () => {} },
cli: { log },
service: {
custom: {
dynamodb: {
ttl: [
{
table: 'my-table-1',
field: 'my-field-1'
}
]
}
dynamodb: { ttl: [ { table: 'my-table-1', field: 'my-field-1' } ] }
}
}
}

return new Plugin(config, { region: 'eu-west-1' }).afterDeploy().then(
() => {
expect(log).toBeCalledWith('Enabling TTL setting(s) for DynamoDB')
expect(dynamoDBdescribeTimeToLiveSpy).toHaveBeenCalledTimes(1)
expect(dynamoDBupdateTimeToLiveSpy).toHaveBeenCalledTimes(1)
}
Expand All @@ -102,12 +166,7 @@ describe('Plugin', () => {
service: {
custom: {
dynamodb: {
ttl: [
{
table: 'my-table-1',
field: 'my-field-1'
}
]
ttl: [ { table: 'my-table-1', field: 'my-field-1' } ]
}
}
}
Expand All @@ -134,14 +193,8 @@ describe('Plugin', () => {
custom: {
dynamodb: {
ttl: [
{
table: 'my-table-1',
field: 'my-field-1'
},
{
table: 'my-table-1',
field: 'my-field-1'
}
{ table: 'my-table-1', field: 'my-field-1' },
{ table: 'my-table-1', field: 'my-field-1' }
]
}
}
Expand Down Expand Up @@ -169,14 +222,8 @@ describe('Plugin', () => {
custom: {
dynamodb: {
ttl: [
{
table: 'my-table-1',
field: 'my-field-1'
},
{
table: 'my-table-1',
field: 'my-field-1'
}
{ table: 'my-table-1', field: 'my-field-1' },
{ table: 'my-table-1', field: 'my-field-1' }
]
}
}
Expand Down Expand Up @@ -207,14 +254,8 @@ describe('Plugin', () => {
custom: {
dynamodb: {
ttl: [
{
table: 'my-table-1',
field: 'my-field-1'
},
{
table: 'my-table-1',
field: 'my-field-1'
}
{ table: 'my-table-1', field: 'my-field-1' },
{ table: 'my-table-1', field: 'my-field-1' }
]
}
}
Expand Down

0 comments on commit b83db98

Please sign in to comment.