-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutils.js
More file actions
96 lines (84 loc) · 2.46 KB
/
Copy pathutils.js
File metadata and controls
96 lines (84 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
const path = require('path');
const AWS = require('aws-sdk');
const { ServerlessSDK } = require('@serverless/platform-client');
const dotenv = require('dotenv').config({ path: path.resolve(__dirname, '.env') }).parsed || {};
/*
* Pauses execution for an X period of time
*/
const sleep = async (wait) => new Promise((resolve) => setTimeout(() => resolve(), wait));
/*
* Generate random id
*/
const generateId = () => Math.random().toString(36).substring(6);
/*
* Fetches AWS credentials from the current environment
* either from env vars, or .env file in the /test directory
*/
const getCredentials = () => {
const credentials = {
aws: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID || dotenv.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || dotenv.AWS_SECRET_ACCESS_KEY,
},
};
if (!credentials.aws.accessKeyId || !credentials.aws.accessKeyId) {
throw new Error('Unable to run tests. AWS credentials not found in the environment');
}
return credentials;
};
/*
* Initializes and returns an instance of the serverless sdk
* @param ${string} orgName - the serverless org name. Must correspond to the access key in the env
*/
const getServerlessSdk = (orgName) => {
const sdk = new ServerlessSDK({
accessKey: process.env.SERVERLESS_ACCESS_KEY || dotenv.SERVERLESS_ACCESS_KEY,
context: {
orgName,
},
});
return sdk;
};
/*
* Fetches a DynamoDB table from aws for validation
* @param ${object} credentials - the cross provider credentials object
* @param ${string} tableName - the name of the dynamodb table
*/
const getTable = async (credentials, tableName) => {
const config = {
credentials: credentials.aws,
region: 'us-east-1',
};
const dynamodb = new AWS.DynamoDB(config);
return dynamodb
.describeTable({
TableName: tableName,
})
.promise();
};
/*
* Fetches a DynamoDB timeToLive for a table from aws for validation
* @param ${object} credentials - the cross provider credentials object
* @param ${string} tableName - the name of the dynamodb table
*/
const getTableTimeToLive = async (credentials, tableName) => {
const config = {
credentials: credentials.aws,
region: 'us-east-1',
};
const dynamodb = new AWS.DynamoDB(config);
return dynamodb
.describeTimeToLive({
TableName: tableName,
})
.promise();
};
module.exports = {
sleep,
generateId,
getCredentials,
getServerlessSdk,
getTable,
getTableTimeToLive,
};