UNPKG

@apigeeks/fbl-k8s-plugin

Version:

fbl wrapper plugin for helm and kubectl cli utilities

189 lines 9.06 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 mocha_typescript_1 = require("mocha-typescript"); const chai = require("chai"); const chaiAsPromised = require("chai-as-promised"); const assert = require("assert"); const typedi_1 = require("typedi"); 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 K8sGetObjectActionHandlerTestSuite = class K8sGetObjectActionHandlerTestSuite extends K8sBaseHandlerTestSuite_1.K8sBaseHandlerTestSuite { failValidation() { return __awaiter(this, void 0, void 0, function* () { const actionHandler = new kubectl_1.K8sGetObjectActionHandler(); 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({ kind: 'CustomObject', }, context, snapshot, {})).to.be.rejected; yield chai.expect(actionHandler.validate({ kind: 'CustomObject', name: '', }, context, snapshot, {})).to.be.rejected; }); } passValidation() { return __awaiter(this, void 0, void 0, function* () { const actionHandler = new kubectl_1.K8sGetObjectActionHandler(); const context = fbl_1.ContextUtil.generateEmptyContext(); const snapshot = new fbl_1.ActionSnapshot('.', {}, '', 0, {}); yield actionHandler.validate({ kind: 'CustomObject', name: 'test', assignTo: { ctx: '$.test', }, }, context, snapshot, {}); }); } getObject() { return __awaiter(this, void 0, void 0, function* () { const applyActionHandler = new kubectl_1.K8sApplyObjectActionHandler(); const getActionHandler = new kubectl_1.K8sGetObjectActionHandler(); const context = fbl_1.ContextUtil.generateEmptyContext(); const snapshot = new fbl_1.ActionSnapshot('.', {}, '', 0, {}); const obj = { kind: 'ConfigMap', apiVersion: 'v1', data: { test: 'true', }, metadata: { name: 'delete-obj-test', }, }; yield applyActionHandler.validate(obj, context, snapshot, {}); yield applyActionHandler.execute(obj, context, snapshot, {}); const result = yield typedi_1.Container.get(services_1.K8sKubectlService).execKubectlCommand([ 'get', obj.kind, obj.metadata.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, obj.data); // get object const options = { kind: obj.kind, name: obj.metadata.name, namespace: 'default', assignTo: { ctx: '$.test', secrets: '$.test', }, pushTo: { ctx: '$.testPush', }, }; yield getActionHandler.validate(options, context, snapshot, {}); yield getActionHandler.execute(options, context, snapshot, {}); const optionsWithoutNamespace = { kind: obj.kind, name: obj.metadata.name, assignTo: { ctx: '$.test', secrets: '$.test', }, pushTo: { ctx: '$.testPush', }, }; yield getActionHandler.validate(optionsWithoutNamespace, context, snapshot, {}); yield getActionHandler.execute(optionsWithoutNamespace, context, snapshot, {}); assert.strictEqual(context.ctx.test.kind, obj.kind); assert.strictEqual(context.ctx.test.kind, obj.kind); assert.deepStrictEqual(context.ctx.test.data, obj.data); assert.deepStrictEqual(context.ctx.testPush[0].kind, obj.kind); assert.strictEqual(context.secrets.test.kind, obj.kind); assert.strictEqual(context.secrets.test.kind, obj.kind); assert.deepStrictEqual(context.secrets.test.data, obj.data); }); } getObjectFail() { return __awaiter(this, void 0, void 0, function* () { const getActionHandler = new kubectl_1.K8sGetObjectActionHandler(); const context = fbl_1.ContextUtil.generateEmptyContext(); const snapshot = new fbl_1.ActionSnapshot('.', {}, '', 0, {}); // get object const optionsNotExist = { kind: 'ConfigMap', name: 'configmap-fail', namespace: 'default', assignTo: { ctx: '$.test', secrets: '$.test', }, pushTo: { ctx: '$.testPush', }, }; yield getActionHandler.validate(optionsNotExist, context, snapshot, {}); yield chai .expect(getActionHandler.execute(optionsNotExist, context, snapshot, {})) .to.be.rejectedWith(`Object ${optionsNotExist.kind} with name ${optionsNotExist.name} not found`); const optionsUnexpected = { kind: 'custom-unexpected', name: 'configmap-unexpected', }; yield chai .expect(getActionHandler.execute(optionsUnexpected, context, snapshot, {})) .to.be.rejectedWith(`Unexpected error occurred upon getting object ${optionsUnexpected.kind} with name ${optionsUnexpected.name}. error: the server doesn't have a resource type "${optionsUnexpected.kind}"`); }); } }; __decorate([ mocha_typescript_1.test(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], K8sGetObjectActionHandlerTestSuite.prototype, "failValidation", null); __decorate([ mocha_typescript_1.test(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], K8sGetObjectActionHandlerTestSuite.prototype, "passValidation", null); __decorate([ mocha_typescript_1.test(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], K8sGetObjectActionHandlerTestSuite.prototype, "getObject", null); __decorate([ mocha_typescript_1.test(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], K8sGetObjectActionHandlerTestSuite.prototype, "getObjectFail", null); K8sGetObjectActionHandlerTestSuite = __decorate([ mocha_typescript_1.suite() ], K8sGetObjectActionHandlerTestSuite); //# sourceMappingURL=K8sGetObjectActionHandlerTestSuite.js.map