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 | let s3Deploy = null
|
15 |
|
16 |
|
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('_convertRegionStringToEnvVarName', () => {
|
40 | it('Upper case. Replace "-" with "_".', () => {
|
41 | [{
|
42 | value: 'us-west-1',
|
43 | expected: 'US_WEST_1'
|
44 | }, {
|
45 | value: 'ap-southeast-2',
|
46 | expected: 'AP_SOUTHEAST_2'
|
47 | }].forEach((test) => {
|
48 | assert.equal(
|
49 | s3Deploy._convertRegionStringToEnvVarName(test.value),
|
50 | test.expected,
|
51 | test
|
52 | )
|
53 | })
|
54 | })
|
55 | })
|
56 |
|
57 | describe('_getBucketNameFromEnvVar', () => {
|
58 | after(() => {
|
59 | delete process.env.S3_US_WEST_1_BUCKET
|
60 | })
|
61 |
|
62 | it('is undefined', () => {
|
63 | assert.isUndefined(s3Deploy._getBucketNameFromEnvVar('us-west-1'))
|
64 | })
|
65 |
|
66 | it('Get values from environment variables', () => {
|
67 | process.env.S3_US_WEST_1_BUCKET = 'bucketName'
|
68 | assert.equal(
|
69 | s3Deploy._getBucketNameFromEnvVar('us-west-1'),
|
70 | 'bucketName'
|
71 | )
|
72 | })
|
73 | })
|
74 |
|
75 | describe('_getS3KeyPrefixFromEnvVar', () => {
|
76 | after(() => {
|
77 | delete process.env.S3_US_WEST_1_PREFIX
|
78 | })
|
79 |
|
80 | it('is undefined', () => {
|
81 | assert.isUndefined(s3Deploy._getS3KeyPrefixFromEnvVar('us-west-1'))
|
82 | })
|
83 |
|
84 | it('Get values from environment variables', () => {
|
85 | process.env.S3_US_WEST_1_PREFIX = 's3KeyPrefix'
|
86 | assert.equal(
|
87 | s3Deploy._getS3KeyPrefixFromEnvVar('us-west-1'),
|
88 | 's3KeyPrefix'
|
89 | )
|
90 | })
|
91 | })
|
92 |
|
93 | describe('_bucketName', () => {
|
94 | after(() => {
|
95 | delete process.env.S3_TEST_REGION_BUCKET
|
96 | })
|
97 |
|
98 | it('FunctionName + region + md5()', () => {
|
99 | const params = {
|
100 | FunctionName: 'node-lambda-name',
|
101 | region: 'test_region'
|
102 | }
|
103 | assert.equal(
|
104 | s3Deploy._bucketName(params),
|
105 | 'node-lambda-name-test_region-aac849d59d2be828b793609e03d8241d'
|
106 | )
|
107 | })
|
108 |
|
109 | it('Use environment variables', () => {
|
110 | process.env.S3_TEST_REGION_BUCKET = 's3-test-region-bucket'
|
111 | const params = {
|
112 | FunctionName: 'node-lambda-name',
|
113 | region: 'test_region'
|
114 | }
|
115 | assert.equal(s3Deploy._bucketName(params), 's3-test-region-bucket')
|
116 | })
|
117 | })
|
118 |
|
119 | describe('_s3Key', () => {
|
120 | after(() => {
|
121 | delete process.env.S3_TEST_REGION_PREFIX
|
122 | })
|
123 |
|
124 | it('"deploy-package" + FunctionName + ".zip"', () => {
|
125 | const params = {
|
126 | FunctionName: 'node-lambda-name',
|
127 | region: 'test_region'
|
128 | }
|
129 | assert.equal(
|
130 | s3Deploy._s3Key(params),
|
131 | 'deploy-package-node-lambda-name.zip'
|
132 | )
|
133 | })
|
134 |
|
135 | it('Use environment variables', () => {
|
136 | process.env.S3_TEST_REGION_PREFIX = 's3-test-region-prefix/'
|
137 | const params = {
|
138 | FunctionName: 'node-lambda-name',
|
139 | region: 'test_region'
|
140 | }
|
141 | assert.equal(
|
142 | s3Deploy._s3Key(params),
|
143 | 's3-test-region-prefix/deploy-package-node-lambda-name.zip'
|
144 | )
|
145 | })
|
146 | })
|
147 |
|
148 | describe('_getS3Location', () => {
|
149 | it('is null', () => {
|
150 | assert.isNull(s3Deploy._getS3Location('hoge'))
|
151 | })
|
152 |
|
153 | it('=== "ap-southeast-1"', () => {
|
154 | assert.equal(s3Deploy._getS3Location('ap-southeast-1'), 'ap-southeast-1')
|
155 | })
|
156 | })
|
157 |
|
158 | describe('_createBucket', () => {
|
159 | it('using mock', () => {
|
160 | const params = {
|
161 | bucketName: 'node-lambda-test-bucket',
|
162 | region: 'ap-southeast-1'
|
163 | }
|
164 | return s3Deploy._createBucket(params).then((result) => {
|
165 | assert.deepEqual(result, mockResponse.createBucket)
|
166 | })
|
167 | })
|
168 | })
|
169 |
|
170 | describe('_putObject', () => {
|
171 | it('using mock', () => {
|
172 | const params = {
|
173 | bucketName: 'node-lambda-test-bucket',
|
174 | s3Key: 'testKey'
|
175 | }
|
176 | return s3Deploy._putObject(params, 'buffer').then((result) => {
|
177 | assert.deepEqual(result, mockResponse.putObject)
|
178 | })
|
179 | })
|
180 | })
|
181 |
|
182 | describe('putPackage', () => {
|
183 | it('using mock', () => {
|
184 | const params = { FunctionName: 'node-lambda-test-bucket-20180801' }
|
185 | return s3Deploy.putPackage(params, 'ap-southeast-1', 'buffer').then((result) => {
|
186 | assert.deepEqual(result, {
|
187 | S3Bucket: 'node-lambda-test-bucket-20180801-ap-southeast-1-6c696118a497125',
|
188 | S3Key: 'deploy-package-node-lambda-test-bucket-20180801.zip'
|
189 | })
|
190 | })
|
191 | })
|
192 | })
|
193 | })
|