@microtica/mocks
Version:
Mock services for automated tests
97 lines • 2.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
// tslint:disable:no-magic-numbers
// tslint:disable:no-console
const AWS = require("aws-sdk");
const _ = require("lodash");
const runner_1 = require("../runner");
const CONTAINER_NAME = "microtica_aws_localstack";
const IMAGE_NAME = "localstack/localstack:latest";
const LOCALHOST_URL = "http://localhost";
const services = {
apigateway: 4567,
kinesis: 4568,
dynamodb: 4569,
dynamodbstreams: 4570,
elasticsearch: 4571,
s3: 4572,
firehose: 4573,
lambda: 4574,
sns: 4575,
sqs: 4576,
redshift: 4577,
es: 4578,
ses: 4579,
route53: 4580,
cloudformation: 4581,
cloudwatch: 4582,
ssm: 4583,
secretsmanager: 4584
};
var AWSRegion;
(function (AWSRegion) {
AWSRegion["FRANKFURT"] = "eu-central-1";
})(AWSRegion || (AWSRegion = {}));
/**
* Runs localstack in Docker
* https://github.com/localstack/localstack
*
* @export
* @class Localstack
*/
class LocalStackDocker {
/**
* Creates an instance of LocalStackDocker.
* This is kinda magical operation since it overrides the AWS endpoints for S3, DynamoDB and etc. with localhost
*
* @memberof LocalStackDocker
*/
constructor() {
this.docker = new runner_1.DockerRunner(IMAGE_NAME, CONTAINER_NAME);
AWS.config.region = AWSRegion.FRANKFURT;
Object.keys(services).forEach(service => {
AWS.config[service] = {
endpoint: this.getURL(services[service])
};
});
}
/**
* Services which localstack supports
*
* @returns {string[]} list of mocked services
* @memberof LocalStackDocker
*/
getMockedServices() {
return Object.keys(services);
}
/**
* Constructs URL(http://localhost:<port>) with the given port
*
* @private
* @param {number} port the TCP port
* @returns
* @memberof LocalStackDocker
*/
getURL(port) {
return `${LOCALHOST_URL}:${port}`;
}
/**
* Spins a new container if it's not already running
*
* @returns {Promise<void>}
* @memberof LocalStackDocker
*/
async start() {
await this.docker.run({ env: [], portsToOpen: _.values(services) });
}
/**
* Stops the container if it's runningw
*
* @memberof LocalStackDocker
*/
async stop() {
await this.docker.remove();
}
}
exports.LocalStackDocker = LocalStackDocker;
//# sourceMappingURL=index.js.map