|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const {assert} = require('chai') |
| 4 | +const path = require('path') |
| 5 | +const aws = require('aws-sdk-mock') |
| 6 | +aws.setSDK(path.resolve('node_modules/aws-sdk')) |
| 7 | +const S3Deploy = require('../lib/s3_deploy') |
| 8 | + |
| 9 | +const mockResponse = { |
| 10 | + createBucket: {'Location': 'createBucket'}, |
| 11 | + putObject: {'ETag': 'putObject'} |
| 12 | +} |
| 13 | + |
| 14 | +var s3Deploy = null |
| 15 | + |
| 16 | +/* global describe, it, before, after */ |
| 17 | +describe('lib/s3_deploy', () => { |
| 18 | + before(() => { |
| 19 | + aws.mock('S3', 'putObject', (params, callback) => { |
| 20 | + callback(null, mockResponse.putObject) |
| 21 | + }) |
| 22 | + aws.mock('S3', 'createBucket', (params, callback) => { |
| 23 | + callback(null, mockResponse.createBucket) |
| 24 | + }) |
| 25 | + |
| 26 | + s3Deploy = new S3Deploy(require('aws-sdk')) |
| 27 | + }) |
| 28 | + |
| 29 | + after(() => { |
| 30 | + aws.restore('S3') |
| 31 | + }) |
| 32 | + |
| 33 | + describe('_md5', () => { |
| 34 | + it('md5("hoge") === "ea703e7aa1efda0064eaa507d9e8ab7e"', () => { |
| 35 | + assert.equal(s3Deploy._md5('hoge'), 'ea703e7aa1efda0064eaa507d9e8ab7e') |
| 36 | + }) |
| 37 | + }) |
| 38 | + |
| 39 | + describe('_bucketName', () => { |
| 40 | + it('FunctionName + region + md5()', () => { |
| 41 | + const params = { |
| 42 | + FunctionName: 'node-lambda-name', |
| 43 | + region: 'test_region' |
| 44 | + } |
| 45 | + assert.equal( |
| 46 | + s3Deploy._bucketName(params), |
| 47 | + 'node-lambda-name-test_region-aac849d59d2be828b793609e03d8241d' |
| 48 | + ) |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + describe('_s3Key', () => { |
| 53 | + it('"deploy-package" + FunctionName + ".zip"', () => { |
| 54 | + const params = {FunctionName: 'node-lambda-name'} |
| 55 | + assert.equal( |
| 56 | + s3Deploy._s3Key(params), |
| 57 | + 'deploy-package-node-lambda-name.zip' |
| 58 | + ) |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + describe('_getS3Location', () => { |
| 63 | + it('is null', () => { |
| 64 | + assert.isNull(s3Deploy._getS3Location('hoge')) |
| 65 | + }) |
| 66 | + |
| 67 | + it('=== "ap-southeast-1"', () => { |
| 68 | + assert.equal(s3Deploy._getS3Location('ap-southeast-1'), 'ap-southeast-1') |
| 69 | + }) |
| 70 | + }) |
| 71 | + |
| 72 | + describe('_createBucket', () => { |
| 73 | + it('using mock', () => { |
| 74 | + const params = { |
| 75 | + bucketName: 'node-lambda-test-bucket', |
| 76 | + region: 'ap-southeast-1' |
| 77 | + } |
| 78 | + return s3Deploy._createBucket(params).then((result) => { |
| 79 | + assert.deepEqual(result, mockResponse.createBucket) |
| 80 | + }) |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + describe('_putObject', () => { |
| 85 | + it('using mock', () => { |
| 86 | + const params = { |
| 87 | + bucketName: 'node-lambda-test-bucket', |
| 88 | + s3Key: 'testKey' |
| 89 | + } |
| 90 | + return s3Deploy._putObject(params, 'buffer').then((result) => { |
| 91 | + assert.deepEqual(result, mockResponse.putObject) |
| 92 | + }) |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + describe('putPackage', () => { |
| 97 | + it('using mock', () => { |
| 98 | + const params = {FunctionName: 'node-lambda-test-bucket-20180801'} |
| 99 | + return s3Deploy.putPackage(params, 'ap-southeast-1', 'buffer').then((result) => { |
| 100 | + assert.deepEqual(result, { |
| 101 | + S3Bucket: 'node-lambda-test-bucket-20180801-ap-southeast-1-6c696118a497125', |
| 102 | + S3Key: 'deploy-package-node-lambda-test-bucket-20180801.zip' |
| 103 | + }) |
| 104 | + }) |
| 105 | + }) |
| 106 | + }) |
| 107 | +}) |
0 commit comments