Skip to content

Commit 2414957

Browse files
committed
feat: add aws test suites
1 parent 99c7a1c commit 2414957

File tree

27 files changed

+2701
-285
lines changed

27 files changed

+2701
-285
lines changed

.mdeprc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ module.exports = {
88
post_exec: 'yarn coverage:report'
99
};
1010

11+
if (process.env.PROVIDER === 'aws') {
12+
module.exports.tests = './test/suites/providers/aws/*.js';
13+
}
14+
15+
if (process.env.PROVIDER !== 'aws') {
16+
module.exports.tests = './test/suites/**/!(providers)/*.js';
17+
}
18+
1119
switch (process.env.DB) {
1220
case 'sentinel':
1321
module.exports.services.push('redisSentinel');

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"compile": "rimraf ./lib && babel -d ./lib --copy-files ./src",
88
"pretest": "yarn compile",
99
"test": "rimraf ./coverage && yarn lint && yarn test:e2e",
10-
"test:e2e": "yarn test:e2e:cluster && yarn test:e2e:sentinel",
11-
"test:e2e:cluster": "DB=cluster mdep test run",
12-
"test:e2e:sentinel": "DB=sentinel mdep test run --docker_compose ./test/docker-compose.sentinel.yml",
10+
"test:e2e": "yarn test:e2e:cluster && yarn test:e2e:sentinel && test:e2e:aws-provider",
11+
"test:e2e:cluster": "PROVIDER=gce DB=cluster mdep test run",
12+
"test:e2e:sentinel": "PROVIDER=gce DB=sentinel mdep test run --docker_compose ./test/docker-compose.sentinel.yml",
13+
"test:e2e:aws-provider": "PROVIDER=aws DB=sentinel mdep test run --docker_compose ./test/docker-compose.sentinel.yml",
1314
"start": "mfleet",
1415
"lint": "eslint ./src ./test",
1516
"prepublishOnly": "yarn compile",

src/actions/upload.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const {
3838
* @return {Promise}
3939
*/
4040
async function initFileUpload({ params }) {
41+
console.log('init file upload');
4142
const {
4243
files,
4344
meta,

src/configs/core.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ exports.transport = [{
9393
* @returns {Provider}
9494
*/
9595
exports.selectTransport = function selectTransport() {
96+
// console.log('my provider 1', this.providers[0]);
9697
return this.providers[0];
9798
};
9899

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ class Files extends Microfleet {
164164
await super.connect();
165165
await this.initWebhook();
166166
await Promise.mapSeries(this.providers, (provider) => {
167-
// @todo
168-
if (provider.config.name !== 'gce' || provider.config.name !== 'aws') return null;
167+
if (!['aws', 'gce'].includes(provider.config.name)) return null;
168+
169169
if (!provider.config.bucket.channel.pubsub) return null;
170170
return provider.subscribe(this.handleUploadNotification.bind(this));
171171
});

src/providers/aws.js

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const Promise = require('bluebird');
22
const AbstractFileTransfer = require('ms-files-transport');
33
const { merge } = require('lodash');
44
const S3 = require('aws-sdk/clients/s3');
5+
const SNS = require('aws-sdk/clients/sns');
56

67
const DOWNLOAD_URL_EXPIRES_IN_SEC = 60000;
78

@@ -61,7 +62,25 @@ class AWSTransport extends AbstractFileTransfer {
6162
* @return {Subscription}
6263
*/
6364
async subscribe() {
64-
this.log.warn('the method is not implemented yet');
65+
const { Topics: topics } = await new SNS({ region: this._config.aws.credentials.region }).listTopics({}).promise();
66+
67+
const topicArn = `arn:aws:sns:us-west-2:178085672309:${this._config.aws.credentials.topicName}`;
68+
69+
const topic = topics.find(
70+
(_topic) => _topic.TopicArn === topicArn
71+
);
72+
73+
if (!topic) {
74+
await new SNS({ region: this._config.aws.credentials.region }).createTopic({ Name: this._config.aws.credentials.topicName }).promise();
75+
}
76+
77+
const params = {
78+
Protocol: 'https',
79+
TopicArn: topicArn,
80+
Endpoint: 'localhost:443',
81+
};
82+
83+
await new SNS().subscribe(params).promise();
6584
}
6685

6786
/**
@@ -93,6 +112,7 @@ class AWSTransport extends AbstractFileTransfer {
93112
if (!isBucketExist) {
94113
const bucketParams = {
95114
Bucket: bucketName,
115+
region: this._config.aws.credentials.region,
96116
};
97117

98118
await aws.createBucket(bucketParams, function handleCreateBucket(_err) {
@@ -125,11 +145,9 @@ class AWSTransport extends AbstractFileTransfer {
125145
}
126146

127147
// @todo interface
128-
getDownloadUrlSigned(filename, downloadName) {
129-
this.log.warn(`${downloadName} is not implemented yet`);
130-
148+
getDownloadUrlSigned(filename) {
131149
const params = {
132-
Bucket: this._config.bucketName,
150+
Bucket: this.getBucketName(),
133151
Expires: DOWNLOAD_URL_EXPIRES_IN_SEC,
134152
Key: filename,
135153
};
@@ -174,11 +192,9 @@ class AWSTransport extends AbstractFileTransfer {
174192
return new Promise((resolve, reject) => {
175193
this._aws.getSignedUrl('putObject', params, (err, url) => {
176194
if (err) {
177-
console.log('init resumable upload err', err);
178195
return reject(err);
179196
}
180197

181-
console.log('init resumable upload url', url);
182198
return resolve(url);
183199
});
184200
});
@@ -239,15 +255,11 @@ class AWSTransport extends AbstractFileTransfer {
239255
const params = {
240256
Bucket: this._config.bucket.name,
241257
Expires: DOWNLOAD_URL_EXPIRES_IN_SEC,
242-
Key: opts.filename,
258+
Key: opts.resource,
243259
};
244260

245-
params.ContentType = opts.contentType;
246-
247261
return new Promise((resolve, reject) => {
248-
console.log('signed url params', params);
249262
this._aws.getSignedUrl('putObject', params, (err, url) => {
250-
console.log('signed url err', err);
251263
if (err) {
252264
return reject(err);
253265
}
@@ -305,12 +317,10 @@ class AWSTransport extends AbstractFileTransfer {
305317
console.log(`exists method aws for: ${filename}`);
306318

307319
return new Promise((resolve) => {
308-
this._aws.headObject({ Key: filename, Bucket: this._config.bucket.name }, (err, data) => {
320+
this._aws.headObject({ Key: filename, Bucket: this._config.bucket.name }, (err) => {
309321
if (err) {
310-
console.log('exists err', err);
311322
return resolve(false);
312323
}
313-
console.log('exists data', data);
314324
return resolve(true);
315325
});
316326
});
@@ -322,7 +332,14 @@ class AWSTransport extends AbstractFileTransfer {
322332
* @return {Promise}
323333
*/
324334
remove(filename) {
325-
this.log.warn('the method is not implemented yet', { filename });
335+
return new Promise((resolve) => {
336+
this._aws.deleteObject({ Key: filename, Bucket: this._config.bucket.name }, (err) => {
337+
if (err) {
338+
return resolve(false);
339+
}
340+
return resolve(true);
341+
});
342+
});
326343
}
327344
}
328345

@@ -348,7 +365,7 @@ AWSTransport.defaultOpts = {
348365
token: undefined,
349366
},
350367
},
351-
mсetadata: {},
368+
metadata: {},
352369
},
353370
};
354371

src/utils/fetch-data.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ async function selectMaster(redis) {
102102
* @param {String[]} [fieldsFilter.pick]
103103
*/
104104
module.exports = function fetchData(key, fieldFilter = {}) {
105+
console.log('fetch data for key', key);
105106
const { redis } = this;
106107
const timer = perf(`fetchData:${key}`);
107108

src/utils/process.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = function processFile(key, data) {
3131
return Promise
3232
.using(acquireLock(this, `postprocess:${key}`), (lock) => {
3333
const { uploadId } = data;
34+
console.log('port srocess uploadId', uploadId);
3435
const { redis } = this;
3536

3637
return Promise

test/configs/generic/core.js

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,16 @@ exports.amqp = {
1919
},
2020
};
2121

22-
exports.transport = [
23-
// {
24-
// name: 'gce',
25-
// options: {
26-
// gce: {
27-
// projectId: env.GCLOUD_PROJECT_ID,
28-
// credentials: {
29-
// client_email: env.GCLOUD_PROJECT_EMAIL,
30-
// private_key: env.GCLOUD_PROJECT_PK,
31-
// },
32-
// },
33-
// bucket: {
34-
// name: env.TEST_BUCKET,
35-
// metadata: {
36-
// location: env.GCLOUD_BUCKET_LOCATION || 'EUROPE-WEST1',
37-
// dra: true,
38-
// },
39-
// },
40-
// // test for direct public URLs
41-
// },
42-
// // its not a public name!
43-
// cname: 'gce',
44-
// },
22+
const awsTransport = [
4523
{
4624
name: 'aws',
4725
options: {
4826
aws: {
4927
credentials: {
28+
region: env.AWS_REGION,
5029
accessKeyId: env.AWS_ACCESS_KEY_ID,
5130
secretAccessKey: env.AWS_SECRET_ACCESS_KEY,
31+
topicName: env.TOPIC_NAME,
5232
},
5333
},
5434
bucket: {
@@ -58,7 +38,35 @@ exports.transport = [
5838
},
5939
// its not a public name!
6040
cname: 'aws',
61-
}];
41+
},
42+
];
43+
44+
// const gceTransport = [
45+
// {
46+
// name: 'gce',
47+
// options: {
48+
// gce: {
49+
// projectId: env.GCLOUD_PROJECT_ID,
50+
// credentials: {
51+
// client_email: env.GCLOUD_PROJECT_EMAIL,
52+
// private_key: env.GCLOUD_PROJECT_PK,
53+
// },
54+
// },
55+
// bucket: {
56+
// name: env.TEST_BUCKET,
57+
// metadata: {
58+
// location: env.GCLOUD_BUCKET_LOCATION || 'EUROPE-WEST1',
59+
// dra: true,
60+
// },
61+
// },
62+
// // test for direct public URLs
63+
// },
64+
// // its not a public name!
65+
// cname: 'gce',
66+
// },
67+
// ];
68+
69+
exports.transport = env.PROVIDER === 'aws' ? awsTransport : awsTransport;
6270

6371
exports.hooks = {
6472
// return input, assume there are models
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: '3'
2+
3+
services:
4+
tester:
5+
volumes:
6+
- ${PWD}:/src
7+
- ${PWD}/test/configs/generic:/configs/core:ro
8+
# - ${PWD}/test/configs/generic:/configs/aws:ro
9+
- ${PWD}/test/configs/redis-sentinel:/configs/redis:ro
10+
environment:
11+
NODE_ENV: "test"
12+
DEBUG: "${DEBUG}"
13+
TEST_BUCKET: "makeomatic-test"
14+
DOTENV_FILE_PATH: "/src/test/.env"
15+
NCONF_FILE_PATH: '["/configs/core","/configs/redis"]'
16+

0 commit comments

Comments
 (0)