UNPKG

@apigeeks/fbl-k8s-plugin

Version:

fbl wrapper plugin for helm and kubectl cli utilities

275 lines 12.8 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const assert = require("assert"); const chai = require("chai"); const chaiAsPromised = require("chai-as-promised"); const typedi_1 = require("typedi"); const util_1 = require("util"); const fs_1 = require("fs"); const path_1 = require("path"); const mocha_typescript_1 = require("mocha-typescript"); const fbl_1 = require("fbl"); const kubectl_1 = require("../../../src/handlers/kubectl"); const services_1 = require("../../../src/services"); const K8sBaseHandlerTestSuite_1 = require("../K8sBaseHandlerTestSuite"); chai.use(chaiAsPromised); let K8sApplyConfigMapActionHandlerTestSuite = class K8sApplyConfigMapActionHandlerTestSuite extends K8sBaseHandlerTestSuite_1.K8sBaseHandlerTestSuite { failValidation() { return __awaiter(this, void 0, void 0, function* () { const actionHandler = new kubectl_1.K8sApplyConfigMapActionHandler(); const context = fbl_1.ContextUtil.generateEmptyContext(); const snapshot = new fbl_1.ActionSnapshot('.', {}, '', 0, {}); yield chai.expect(actionHandler.validate([], context, snapshot, {})).to.be.rejected; yield chai.expect(actionHandler.validate({ name: 'test', }, context, snapshot, {})).to.be.rejected; yield chai.expect(actionHandler.validate({ name: 'test', files: [], }, context, snapshot, {})).to.be.rejected; yield chai.expect(actionHandler.validate({ name: 'test', inline: [], }, context, snapshot, {})).to.be.rejected; yield chai.expect(actionHandler.validate({ name: 'test', inline: {}, }, context, snapshot, {})).to.be.rejected; }); } passValidation() { return __awaiter(this, void 0, void 0, function* () { const actionHandler = new kubectl_1.K8sApplyConfigMapActionHandler(); const context = fbl_1.ContextUtil.generateEmptyContext(); const snapshot = new fbl_1.ActionSnapshot('.', {}, '', 0, {}); yield actionHandler.validate({ name: 'test', files: ['test.yml'], }, context, snapshot, {}); yield actionHandler.validate({ name: 'test', inline: { test: 1, }, }, context, snapshot, {}); yield actionHandler.validate({ name: 'test', files: ['test.yml'], inline: { test: 1, }, }, context, snapshot, {}); }); } createNewConfigMapInline() { return __awaiter(this, void 0, void 0, function* () { const actionHandler = new kubectl_1.K8sApplyConfigMapActionHandler(); const context = fbl_1.ContextUtil.generateEmptyContext(); const snapshot = new fbl_1.ActionSnapshot('.', {}, '', 0, {}); const options = { name: 'config-map-test-inline', namespace: 'default', inline: { host: 'foo.bar', port: 8000, }, }; yield actionHandler.validate(options, context, snapshot, {}); yield actionHandler.execute(options, context, snapshot, {}); const result = yield typedi_1.Container.get(services_1.K8sKubectlService).execKubectlCommand([ 'get', 'configmap', options.name, '-o', 'json', ]); if (result.code !== 0) { throw new Error(`code: ${result.code};\nstdout: ${result.stdout};\nstderr: ${result.stderr}`); } const configMap = JSON.parse(result.stdout); assert.deepStrictEqual(configMap.data, { host: 'foo.bar', port: '8000', }); assert.strictEqual(context.entities.registered.length, 1); assert.strictEqual(context.entities.created.length, 1); }); } createNewConfigMapFiles() { return __awaiter(this, void 0, void 0, function* () { const actionHandler = new kubectl_1.K8sApplyConfigMapActionHandler(); const context = fbl_1.ContextUtil.generateEmptyContext(); const snapshot = new fbl_1.ActionSnapshot('.', {}, '', 0, {}); const tempFile = yield typedi_1.Container.get(fbl_1.TempPathsRegistry).createTempFile(false, '.txt'); yield util_1.promisify(fs_1.writeFile)(tempFile, 'test=true', 'utf8'); const options = { name: 'config-map-test-files', files: [tempFile], }; yield actionHandler.validate(options, context, snapshot, {}); yield actionHandler.execute(options, context, snapshot, {}); const result = yield typedi_1.Container.get(services_1.K8sKubectlService).execKubectlCommand([ 'get', 'configmap', options.name, '-o', 'json', ]); if (result.code !== 0) { throw new Error(`code: ${result.code};\nstdout: ${result.stdout};\nstderr: ${result.stderr}`); } const configMap = JSON.parse(result.stdout); assert.deepStrictEqual(configMap.data, { [path_1.basename(tempFile)]: 'test=true', }); assert.strictEqual(context.entities.registered.length, 1); assert.strictEqual(context.entities.created.length, 1); }); } createCombinedConfigMap() { return __awaiter(this, void 0, void 0, function* () { const actionHandler = new kubectl_1.K8sApplyConfigMapActionHandler(); const context = fbl_1.ContextUtil.generateEmptyContext(); const snapshot = new fbl_1.ActionSnapshot('.', {}, '', 0, {}); const tempFile = yield typedi_1.Container.get(fbl_1.TempPathsRegistry).createTempFile(false, '.txt'); yield util_1.promisify(fs_1.writeFile)(tempFile, 'test=true', 'utf8'); const options = { name: 'config-map-test-combined', files: [tempFile], inline: { host: 'foo.bar', port: 8000, }, }; yield actionHandler.validate(options, context, snapshot, {}); yield actionHandler.execute(options, context, snapshot, {}); const result = yield typedi_1.Container.get(services_1.K8sKubectlService).execKubectlCommand([ 'get', 'configmap', options.name, '-o', 'json', ]); if (result.code !== 0) { throw new Error(`code: ${result.code};\nstdout: ${result.stdout};\nstderr: ${result.stderr}`); } const configMap = JSON.parse(result.stdout); assert.deepStrictEqual(configMap.data, { [path_1.basename(tempFile)]: 'test=true', host: 'foo.bar', port: '8000', }); assert.strictEqual(context.entities.registered.length, 1); assert.strictEqual(context.entities.created.length, 1); }); } updateConfigMapByName() { return __awaiter(this, void 0, void 0, function* () { const actionHandler = new kubectl_1.K8sApplyConfigMapActionHandler(); const context = fbl_1.ContextUtil.generateEmptyContext(); const snapshot = new fbl_1.ActionSnapshot('.', {}, '', 0, {}); const options = { name: 'config-map-test-override', inline: { host: 'foo.bar', port: 8000, }, }; yield actionHandler.validate(options, context, snapshot, {}); yield actionHandler.execute(options, context, snapshot, {}); let result = yield typedi_1.Container.get(services_1.K8sKubectlService).execKubectlCommand([ 'get', 'configmap', options.name, '-o', 'json', ]); if (result.code !== 0) { throw new Error(`code: ${result.code};\nstdout: ${result.stdout};\nstderr: ${result.stderr}`); } let configMap = JSON.parse(result.stdout); assert.deepStrictEqual(configMap.data, { host: 'foo.bar', port: '8000', }); // update config map options.inline.port = 9999; yield actionHandler.execute(options, context, snapshot, {}); result = yield typedi_1.Container.get(services_1.K8sKubectlService).execKubectlCommand([ 'get', 'configmap', options.name, '-o', 'json', ]); if (result.code !== 0) { throw new Error(`code: ${result.code};\nstdout: ${result.stdout};\nstderr: ${result.stderr}`); } configMap = JSON.parse(result.stdout); assert.deepStrictEqual(configMap.data, { host: 'foo.bar', port: '9999', }); assert.strictEqual(context.entities.registered.length, 2); assert.strictEqual(context.entities.created.length, 1); assert.strictEqual(context.entities.updated.length, 1); }); } }; __decorate([ mocha_typescript_1.test(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], K8sApplyConfigMapActionHandlerTestSuite.prototype, "failValidation", null); __decorate([ mocha_typescript_1.test(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], K8sApplyConfigMapActionHandlerTestSuite.prototype, "passValidation", null); __decorate([ mocha_typescript_1.test(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], K8sApplyConfigMapActionHandlerTestSuite.prototype, "createNewConfigMapInline", null); __decorate([ mocha_typescript_1.test(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], K8sApplyConfigMapActionHandlerTestSuite.prototype, "createNewConfigMapFiles", null); __decorate([ mocha_typescript_1.test(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], K8sApplyConfigMapActionHandlerTestSuite.prototype, "createCombinedConfigMap", null); __decorate([ mocha_typescript_1.test(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], K8sApplyConfigMapActionHandlerTestSuite.prototype, "updateConfigMapByName", null); K8sApplyConfigMapActionHandlerTestSuite = __decorate([ mocha_typescript_1.suite() ], K8sApplyConfigMapActionHandlerTestSuite); //# sourceMappingURL=K8sApplyConfigMapActionHandlerTestSuite.js.map