Skip to content

Commit

Permalink
Merge a82e7bf into 84a2421
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah committed Aug 16, 2019
2 parents 84a2421 + a82e7bf commit c84e567
Show file tree
Hide file tree
Showing 14 changed files with 399 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
service: multiple-kinesis-proxy

provider:
name: aws
runtime: nodejs10.x

plugins:
localPath: './../../../../../../'
modules:
- serverless-apigateway-service-proxy

custom:
apiGatewayServiceProxies:
- kinesis:
path: /kinesis1
method: post
streamName: { Ref: 'YourStream1' }
cors: true

- kinesis:
path: /kinesis2
method: post
streamName: { Ref: 'YourStream2' }
cors: true

- kinesis:
path: /kinesis3
method: post
streamName: { Ref: 'YourStream3' }
cors: true

resources:
Resources:
YourStream1:
Type: AWS::Kinesis::Stream
Properties:
ShardCount: 1
YourStream2:
Type: AWS::Kinesis::Stream
Properties:
ShardCount: 1
YourStream3:
Type: AWS::Kinesis::Stream
Properties:
ShardCount: 1
40 changes: 40 additions & 0 deletions __tests__/integration/kinesis/multiple-integrations/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

const expect = require('chai').expect
const fetch = require('node-fetch')
const { deployWithRandomStage, removeService } = require('../../../utils')

describe('Multiple Kinesis Proxy Integrations Test', () => {
let endpoint
let stage
const config = '__tests__/integration/kinesis/multiple-integrations/service/serverless.yml'

beforeAll(async () => {
const result = await deployWithRandomStage(config)
stage = result.stage
endpoint = result.endpoint
})

afterAll(() => {
removeService(stage, config)
})

it('should get correct response from multiple kinesis proxy endpoints', async () => {
const streams = ['kinesis1', 'kinesis2', 'kinesis3']

for (const stream of streams) {
const testEndpoint = `${endpoint}/${stream}`

const response = await fetch(testEndpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ Data: `data for stream ${stream}`, PartitionKey: 'some key' })
})
expect(response.headers.get('access-control-allow-origin')).to.deep.equal('*')
expect(response.status).to.be.equal(200)
const body = await response.json()
expect(body).to.have.own.property('ShardId')
expect(body).to.have.own.property('SequenceNumber')
}
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ provider:
runtime: nodejs10.x

plugins:
localPath: './../'
localPath: './../../../../../../'
modules:
- serverless-apigateway-service-proxy

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@

const expect = require('chai').expect
const fetch = require('node-fetch')
const { deployService, removeService, getApiGatewayEndpoint } = require('./../../utils')
const { deployWithRandomStage, removeService } = require('../../../utils')

describe('Kinesis Proxy Integration Test', () => {
describe('Single Kinesis Proxy Integration Test', () => {
let endpoint
let stackName
let stage
const config = '__tests__/integration/kinesis/service/serverless.yml'
const config = '__tests__/integration/kinesis/single-integration/service/serverless.yml'

beforeAll(async () => {
stage = Math.random()
.toString(32)
.substring(2)
stackName = 'kinesis-proxy-' + stage
deployService(stage, config)
endpoint = await getApiGatewayEndpoint(stackName)
const result = await deployWithRandomStage(config)
stage = result.stage
endpoint = result.endpoint
})

afterAll(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
service: multiple-sqs-proxy

provider:
name: aws
runtime: nodejs10.x

plugins:
localPath: './../../../../../../'
modules:
- serverless-apigateway-service-proxy

custom:
apiGatewayServiceProxies:
- sqs:
path: /sqs1
method: post
queueName: { 'Fn::GetAtt': ['SQSQueue1', 'QueueName'] }
cors: true

- sqs:
path: /sqs2
method: post
queueName: { 'Fn::GetAtt': ['SQSQueue2', 'QueueName'] }
cors: true

- sqs:
path: /sqs3
method: post
queueName: { 'Fn::GetAtt': ['SQSQueue3', 'QueueName'] }
cors: true

resources:
Resources:
SQSQueue1:
Type: 'AWS::SQS::Queue'
SQSQueue2:
Type: 'AWS::SQS::Queue'
SQSQueue3:
Type: 'AWS::SQS::Queue'
44 changes: 44 additions & 0 deletions __tests__/integration/sqs/multiple-integrations/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'

const expect = require('chai').expect
const fetch = require('node-fetch')
const { deployWithRandomStage, removeService } = require('../../../utils')

describe('Multiple SQS Proxy Integrations Test', () => {
let endpoint
let stage
const config = '__tests__/integration/sqs/multiple-integrations/service/serverless.yml'

beforeAll(async () => {
const result = await deployWithRandomStage(config)
stage = result.stage
endpoint = result.endpoint
})

afterAll(() => {
removeService(stage, config)
})

it('should get correct response from multiple sqs proxy endpoints', async () => {
const queues = ['sqs1', 'sqs2', 'sqs3']

for (const queue of queues) {
const testEndpoint = `${endpoint}/${queue}`
const response = await fetch(testEndpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: `message for ${queue}` })
})
expect(response.headers.get('access-control-allow-origin')).to.deep.equal('*')
expect(response.status).to.be.equal(200)
const body = await response.json()
expect(body.SendMessageResponse.SendMessageResult).to.have.own.property(
'MD5OfMessageAttributes'
)
expect(body.SendMessageResponse.SendMessageResult).to.have.own.property('MD5OfMessageBody')
expect(body.SendMessageResponse.SendMessageResult).to.have.own.property('MessageId')
expect(body.SendMessageResponse.SendMessageResult).to.have.own.property('SequenceNumber')
expect(body.SendMessageResponse.ResponseMetadata).to.have.own.property('RequestId')
}
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ provider:
runtime: nodejs10.x

plugins:
localPath: './../'
localPath: './../../../../../../'
modules:
- serverless-apigateway-service-proxy

Expand All @@ -14,10 +14,10 @@ custom:
- sqs:
path: /sqs
method: post
queueName: {"Fn::GetAtt":[ "SQSQueue", "QueueName" ]}
queueName: { 'Fn::GetAtt': ['SQSQueue', 'QueueName'] }
cors: true

resources:
Resources:
SQSQueue:
Type: "AWS::SQS::Queue"
Type: 'AWS::SQS::Queue'
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

const expect = require('chai').expect
const fetch = require('node-fetch')
const { deployService, removeService, getApiGatewayEndpoint } = require('./../../utils')
const { deployWithRandomStage, removeService } = require('../../../utils')

describe('SQS Proxy Integration Test', () => {
describe('Single SQS Proxy Integration Test', () => {
let endpoint
let stackName
let stage
const config = '__tests__/integration/sqs/service/serverless.yml'
const config = '__tests__/integration/sqs/single-integration/service/serverless.yml'

beforeAll(async () => {
stage = Math.random()
.toString(32)
.substring(2)
stackName = 'sqs-proxy-' + stage
deployService(stage, config)
endpoint = await getApiGatewayEndpoint(stackName)
const result = await deployWithRandomStage(config)

stage = result.stage
endpoint = result.endpoint
})

afterAll(() => {
Expand Down
58 changes: 41 additions & 17 deletions __tests__/utils.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
'use strict'

const _ = require('lodash')
const yaml = require('js-yaml')
const fs = require('fs')
const path = require('path')
const execSync = require('child_process').execSync
const aws = require('aws-sdk')
const cloudformation = new aws.CloudFormation({ region: 'us-east-1' })

async function getApiGatewayEndpoint(stackName) {
const result = await cloudformation.describeStacks({ StackName: stackName }).promise()

const endpointOutput = _.find(result.Stacks[0].Outputs, { OutputKey: 'ServiceEndpoint' })
.OutputValue
return endpointOutput.match(/https:\/\/.+\.execute-api\..+\.amazonaws\.com.+/)[0]
}

function deployService(stage, config) {
execSync(`npx serverless deploy --stage ${stage} --config ${path.basename(config)}`, {
stdio: 'inherit',
cwd: path.dirname(config)
})
}

function removeService(stage, config) {
execSync(`npx serverless remove --stage ${stage} --config ${path.basename(config)}`, {
stdio: 'inherit',
cwd: path.dirname(config)
})
}

async function deployWithRandomStage(config) {
const serviceName = yaml.safeLoad(fs.readFileSync(config)).service
const stage = Math.random()
.toString(32)
.substring(2)
const stackName = `${serviceName}-${stage}`
deployService(stage, config)
const endpoint = await getApiGatewayEndpoint(stackName)

return { stage, endpoint }
}

module.exports = {
async getApiGatewayEndpoint(stackName) {
const result = await cloudformation.describeStacks({ StackName: stackName }).promise()

const endpointOutput = _.find(result.Stacks[0].Outputs, { OutputKey: 'ServiceEndpoint' })
.OutputValue
return endpointOutput.match(/https:\/\/.+\.execute-api\..+\.amazonaws\.com.+/)[0]
},

deployService(stage, config) {
execSync(`npx serverless deploy --stage ${stage} --config ${config}`, {
stdio: 'inherit'
})
},

removeService(stage, config) {
execSync(`npx serverless remove --stage ${stage} --config ${config}`, { stdio: 'inherit' })
}
getApiGatewayEndpoint,
deployService,
removeService,
deployWithRandomStage
}

0 comments on commit c84e567

Please sign in to comment.