UNPKG

@piiano/testcontainers-vault

Version:

Add a local Piiano Vault for testing purposes using testcontainers

78 lines 3.33 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Vault = void 0; const testcontainers_1 = require("testcontainers"); const vaultPort = 8123; /** * A local Vault instance. */ class Vault { constructor(options = {}) { this.options = options; const { version = 'latest', env = {}, license = process.env.PVAULT_SERVICE_LICENSE, port, reuse = false, withDB = true, bindMounts = [] } = this.options; const vaultImage = withDB ? 'piiano/pvault-dev' : 'piiano/pvault-server'; if (!license) { throw new Error('Missing Vault license'); } const vars = Object.entries(env).reduce((acc, [key, value]) => { acc[key] = String(value); return acc; }, { PVAULT_SERVICE_LICENSE: license }); this.container = new testcontainers_1.GenericContainer(`${vaultImage}:${version}`) .withExposedPorts(port ? { container: vaultPort, host: port } : vaultPort) .withEnvironment(vars) .withBindMounts(bindMounts) // A wait strategy that waits for Vault to be ready. .withWaitStrategy(testcontainers_1.Wait.forAll(['data', 'ctl'].map(service => testcontainers_1.Wait .forHttp(`/api/pvlt/1.0/${service}/info/health`, vaultPort) .forStatusCode(200) .forResponsePredicate((res) => { const json = JSON.parse(res); return json && typeof json === 'object' && 'status' in json && (json === null || json === void 0 ? void 0 : json.status) === 'pass'; })))); if (reuse) { this.container = this.container.withReuse(); } } /** * Starts the Vault container. */ start() { return __awaiter(this, void 0, void 0, function* () { if (!this.startedContainer) { this.startedContainer = yield this.container.start(); } return this.startedContainer.getMappedPort(vaultPort); }); } /** * Stops the Vault container. */ stop() { var _a; return __awaiter(this, void 0, void 0, function* () { yield ((_a = this.startedContainer) === null || _a === void 0 ? void 0 : _a.stop()); this.startedContainer = undefined; }); } /** * Executes a command in the Vault container. */ exec(command, ...args) { if (!this.startedContainer) { throw new Error('Vault container not started'); } return this.startedContainer.exec([command, ...args]); } } exports.Vault = Vault; //# sourceMappingURL=vault.js.map