github.com/kyma-project/kyma-environment-broker@v0.0.1/testing/e2e/skr/kyma-environment-broker/helpers.js (about) 1 const {wait, debug} = require('../utils'); 2 const {expect} = require('chai'); 3 const fs = require('fs'); 4 const os = require('os'); 5 6 async function provisionSKR( 7 keb, 8 kcp, 9 gardener, 10 instanceID, 11 name, 12 platformCreds, 13 btpOperatorCreds, 14 customParams, 15 timeout, 16 ) { 17 const resp = await keb.provisionSKR( 18 name, 19 instanceID, 20 platformCreds, 21 btpOperatorCreds, 22 customParams, 23 ); 24 expect(resp).to.have.property('operation'); 25 26 const operationID = resp.operation; 27 debug(`Operation ID ${operationID}`); 28 29 await ensureOperationSucceeded(keb, kcp, instanceID, operationID, timeout); 30 31 debug('Fetching runtime operation status...'); 32 const runtimeStatus = await kcp.getRuntimeStatusOperations(instanceID); 33 const objRuntimeStatus = JSON.parse(runtimeStatus); 34 expect(objRuntimeStatus).to.have.nested.property('data[0].shootName').not.empty; 35 debug('Fetching shoot info from gardener...'); 36 const shoot = await gardener.getShoot(objRuntimeStatus.data[0].shootName); 37 debug(`Compass ID ${shoot.compassID}`); 38 39 return { 40 operationID, 41 shoot, 42 }; 43 } 44 45 function ensureValidShootOIDCConfig(shoot, targetOIDCConfig) { 46 expect(shoot).to.have.nested.property('oidcConfig.clientID', targetOIDCConfig.clientID); 47 expect(shoot).to.have.nested.property('oidcConfig.issuerURL', targetOIDCConfig.issuerURL); 48 expect(shoot).to.have.nested.property('oidcConfig.groupsClaim', targetOIDCConfig.groupsClaim); 49 expect(shoot).to.have.nested.property('oidcConfig.usernameClaim', targetOIDCConfig.usernameClaim); 50 expect(shoot).to.have.nested.property( 51 'oidcConfig.usernamePrefix', 52 targetOIDCConfig.usernamePrefix, 53 ); 54 expect(shoot.oidcConfig.signingAlgs).to.eql(targetOIDCConfig.signingAlgs); 55 } 56 57 async function deprovisionSKR(keb, kcp, instanceID, timeout, ensureSuccess=true) { 58 const resp = await keb.deprovisionSKR(instanceID); 59 expect(resp).to.have.property('operation'); 60 61 const operationID = resp.operation; 62 console.log(`Deprovision SKR - operation ID ${operationID}`); 63 64 if (ensureSuccess) { 65 await ensureOperationSucceeded(keb, kcp, instanceID, operationID, timeout); 66 } 67 68 return operationID; 69 } 70 71 async function updateSKR(keb, 72 kcp, 73 gardener, 74 instanceID, 75 shootName, 76 customParams, 77 timeout, 78 btpOperatorCreds = null, 79 isMigration = false) { 80 const resp = await keb.updateSKR(instanceID, customParams, btpOperatorCreds, isMigration); 81 expect(resp).to.have.property('operation'); 82 83 const operationID = resp.operation; 84 debug(`Operation ID ${operationID}`); 85 86 await ensureOperationSucceeded(keb, kcp, instanceID, operationID, timeout); 87 88 const shoot = await gardener.getShoot(shootName); 89 90 return { 91 operationID, 92 shoot, 93 }; 94 } 95 96 async function ensureOperationSucceeded(keb, kcp, instanceID, operationID, timeout) { 97 const res = await wait( 98 () => keb.getOperation(instanceID, operationID), 99 (res) => res && res.state && (res.state === 'succeeded' || res.state === 'failed'), 100 timeout, 101 1000 * 30, // 30 seconds 102 ).catch(async (err) => { 103 const runtimeStatus = await kcp.getRuntimeStatusOperations(instanceID); 104 const events = await kcp.getRuntimeEvents(instanceID); 105 const msg = `${err}\nError thrown by ensureOperationSucceeded: Runtime status: ${runtimeStatus}`; 106 throw new Error(`${msg}\nEvents:\n${events}`); 107 }); 108 109 if (res.state !== 'succeeded') { 110 const runtimeStatus = await kcp.getRuntimeStatusOperations(instanceID); 111 throw new Error(`Error thrown by ensureOperationSucceeded: operation didn't succeed in time: 112 ${JSON.stringify(res, null, `\t`)}\nRuntime status: ${runtimeStatus}`); 113 } 114 115 console.log(`Operation ${operationID} finished with state ${res.state}`); 116 117 return res; 118 } 119 120 async function getShootName(keb, instanceID) { 121 const resp = await keb.getRuntime(instanceID); 122 expect(resp.data).to.be.lengthOf(1); 123 124 return resp.data[0].shootName; 125 } 126 127 async function getCatalog(keb) { 128 return keb.getCatalog(); 129 } 130 131 async function ensureValidOIDCConfigInCustomerFacingKubeconfig(keb, instanceID, oidcConfig) { 132 let kubeconfigContent; 133 try { 134 kubeconfigContent = await keb.downloadKubeconfig(instanceID); 135 } catch (err) { 136 console.log(err); 137 } 138 139 const issuerMatchPattern = '\\b' + oidcConfig.issuerURL + '\\b'; 140 const clientIDMatchPattern = '\\b' + oidcConfig.clientID + '\\b'; 141 expect(kubeconfigContent).to.match(new RegExp(issuerMatchPattern, 'g')); 142 expect(kubeconfigContent).to.match(new RegExp(clientIDMatchPattern, 'g')); 143 } 144 145 async function saveKubeconfig(kubeconfig) { 146 const directory = `${os.homedir()}/.kube`; 147 if (!fs.existsSync(directory)) { 148 fs.mkdirSync(directory, {recursive: true}); 149 } 150 151 fs.writeFileSync(`${directory}/config`, kubeconfig); 152 } 153 154 module.exports = { 155 provisionSKR, 156 deprovisionSKR, 157 saveKubeconfig, 158 updateSKR, 159 ensureOperationSucceeded, 160 getShootName, 161 ensureValidShootOIDCConfig, 162 ensureValidOIDCConfigInCustomerFacingKubeconfig, 163 getCatalog, 164 };