github.com/kyma-project/kyma-environment-broker@v0.0.1/testing/e2e/skr/skr-test/helpers.js (about) 1 const uuid = require('uuid'); 2 const {KEBConfig, KEBClient}= require('../kyma-environment-broker'); 3 const {GardenerClient, GardenerConfig} = require('../gardener'); 4 const {KCPWrapper, KCPConfig} = require('../kcp/client'); 5 const fs = require('fs'); 6 const os = require('os'); 7 const {expect} = require('chai'); 8 9 const keb = new KEBClient(KEBConfig.fromEnv()); 10 const gardener = new GardenerClient(GardenerConfig.fromEnv()); 11 const kcp = new KCPWrapper(KCPConfig.fromEnv()); 12 const testNS = 'skr-test'; 13 const DEBUG = process.env.DEBUG === 'true'; 14 const {initializeK8sClient} = require('../utils/index.js'); 15 16 function withInstanceID(instanceID) { 17 return function(options) { 18 options.instanceID = instanceID; 19 }; 20 } 21 22 function withRuntimeName(runtimeName) { 23 return function(options) { 24 options.runtimeName = runtimeName; 25 }; 26 } 27 28 function withAppName(appName) { 29 return function(options) { 30 options.appName = appName; 31 }; 32 } 33 34 function withScenarioName(scenarioName) { 35 return function(options) { 36 options.scenarioName = scenarioName; 37 }; 38 } 39 40 function withTestNS(testNS) { 41 return function(options) { 42 options.testNS = testNS; 43 }; 44 } 45 46 function withSuffix(suffix) { 47 return function(options) { 48 options.suffix = suffix; 49 }; 50 } 51 52 function withCustomParams(customParams) { 53 return function(options) { 54 options.customParams = customParams; 55 }; 56 } 57 58 function gatherOptions(...opts) { 59 // If no opts provided, the options object will be set to these default values. 60 const options = { 61 instanceID: uuid.v4(), 62 testNS: testNS, 63 // These options are not meant to be rewritten apart from env variable for KEB_USER_ID 64 // If that's needed, add a separate function that overrides this field. 65 oidc0: { 66 clientID: '9bd05ed7-a930-44e6-8c79-e6defeb7dec9', 67 groupsClaim: 'groups', 68 issuerURL: 'https://kymatest.accounts400.ondemand.com', 69 signingAlgs: ['RS256'], 70 usernameClaim: 'sub', 71 usernamePrefix: '-', 72 }, 73 oidc1: { 74 clientID: 'foo-bar', 75 groupsClaim: 'groups1', 76 issuerURL: 'https://new.custom.ias.com', 77 signingAlgs: ['RS256'], 78 usernameClaim: 'email', 79 usernamePrefix: 'acme-', 80 }, 81 kebUserId: getEnvOrThrow('KEB_USER_ID'), 82 administrators1: ['admin1@acme.com', 'admin2@acme.com'], 83 customParams: {}, 84 }; 85 86 opts.forEach((opt) => { 87 opt(options); 88 }); 89 90 if (options.suffix === undefined) { 91 options.suffix = genRandom(4); 92 } 93 94 options.runtimeName = `kyma-${options.suffix}`; 95 options.appName = `app-${options.suffix}`; 96 options.scenarioName = `test-${options.suffix}`; 97 98 return options; 99 } 100 101 async function getSKRRuntimeStatus(instanceID) { 102 const runtimeStatus = await kcp.getRuntimeStatusOperations(instanceID); 103 return JSON.parse(runtimeStatus); 104 } 105 106 // gets the SKR config by its instance id 107 async function getSKRConfig(instanceID) { 108 const runtimeStatus = await kcp.getRuntimeStatusOperations(instanceID); 109 const objRuntimeStatus = JSON.parse(runtimeStatus); 110 expect(objRuntimeStatus).to.have.nested.property('data[0].shootName').not.empty; 111 const shootName = objRuntimeStatus.data[0].shootName; 112 113 console.log(`Fetching SKR info for shoot: ${shootName}`); 114 return await gardener.getShoot(shootName); 115 } 116 117 function getEnvOrThrow(key) { 118 if (!process.env[key]) { 119 throw new Error(`Env ${key} not present`); 120 } 121 122 return process.env[key]; 123 } 124 125 function genRandom(len) { 126 let res = ''; 127 const chrs = 'abcdefghijklmnopqrstuvwxyz0123456789'; 128 for (let i = 0; i < len; i++) { 129 res += chrs.charAt(Math.floor(Math.random() * chrs.length)); 130 } 131 132 return res; 133 } 134 135 function debug(...args) { 136 if (!DEBUG) { 137 return; 138 } 139 log('DEBUG', ...args); 140 } 141 142 function log(prefix, ...args) { 143 if (args.length === 0) { 144 return; 145 } 146 147 args = [...args]; 148 const fmt = `[${prefix}] ` + args[0]; 149 args = args.slice(1); 150 console.log.apply(console, [fmt, ...args]); 151 } 152 153 async function saveKubeconfig(kubeconfig) { 154 if (!fs.existsSync(`${os.homedir()}/.kube`)) { 155 fs.mkdirSync(`${os.homedir()}/.kube`, true); 156 } 157 fs.writeFileSync(`${os.homedir()}/.kube/config`, kubeconfig); 158 } 159 160 async function initK8sConfig(shoot) { 161 console.log('Should save kubeconfig for the SKR to ~/.kube/config'); 162 await saveKubeconfig(shoot.kubeconfig); 163 164 console.log('Should initialize K8s client'); 165 await initializeK8sClient({kubeconfig: shoot.kubeconfig}); 166 } 167 168 module.exports = { 169 keb, 170 kcp, 171 gardener, 172 getSKRConfig, 173 getSKRRuntimeStatus, 174 gatherOptions, 175 withInstanceID, 176 withAppName, 177 withRuntimeName, 178 withScenarioName, 179 withTestNS, 180 withSuffix, 181 withCustomParams, 182 getEnvOrThrow, 183 genRandom, 184 debug, 185 saveKubeconfig, 186 log, 187 initK8sConfig, 188 };