-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* eslint-disable sonarjs/cognitive-complexity */ | ||
export enum Environment { | ||
dev = 'dev', | ||
ops = 'ops', | ||
staging = 'staging', | ||
live = 'live' | ||
} | ||
|
||
export enum Network { | ||
mainnet = 'mainnet', | ||
preprod = 'preprod', | ||
preview = 'preview', | ||
sanchonet = 'sanchonet' | ||
} | ||
|
||
/** | ||
* Gets the target URL domain under test (dut) based on `TARGET_ENV`, `TARGET_NET` or `DUT` environment variables. | ||
* | ||
Check warning on line 18 in packages/util-dev/src/k6-utils.ts GitHub Actions / build_and_test (ubuntu-20.04)
Check warning on line 18 in packages/util-dev/src/k6-utils.ts GitHub Actions / build_and_test (ubuntu-20.04)
Check warning on line 18 in packages/util-dev/src/k6-utils.ts GitHub Actions / build_and_test (ubuntu-20.04)
Check warning on line 18 in packages/util-dev/src/k6-utils.ts GitHub Actions / build_and_test (ubuntu-20.04)
Check warning on line 18 in packages/util-dev/src/k6-utils.ts GitHub Actions / build_and_test (ubuntu-20.04)
Check warning on line 18 in packages/util-dev/src/k6-utils.ts GitHub Actions / build_and_test (ubuntu-20.04)
|
||
* @param options default `undefined`: use default value for each option.<br /> | ||
Check warning on line 19 in packages/util-dev/src/k6-utils.ts GitHub Actions / build_and_test (ubuntu-20.04)
Check warning on line 19 in packages/util-dev/src/k6-utils.ts GitHub Actions / build_and_test (ubuntu-20.04)
|
||
* - `environments`: the array of allowed environments - default: `['dev', 'ops', 'staging', 'prod']`. | ||
* - `networks`: the array of allowed networks - default: `['mainnet', 'preprod', 'preview', 'sanchonet']`. | ||
* @returns the domain under test (dut) based on the environment variables, (e.g. `live-preprod.lw.iog.io`) | ||
* @throws if none of `TARGET_ENV` and `TARGET_NET`, or `DUT` are configured, or if the options are invalid. | ||
*/ | ||
// eslint-disable-next-line complexity, sonarjs/cognitive-complexity | ||
export const getDut = ( | ||
k6Env: { TARGET_ENV?: string; TARGET_NET?: string; DUT?: string }, | ||
options?: { environments?: Environment[]; networks?: Network[] } | ||
) => { | ||
const allowedEnvironments = new Set<Environment>([ | ||
Environment.dev, | ||
Environment.ops, | ||
Environment.staging, | ||
Environment.live | ||
]); | ||
const allowedNetworks = new Set<Network>([Network.mainnet, Network.preprod, Network.preview, Network.sanchonet]); | ||
const allowedOptions = new Set(['environments', 'networks']); | ||
|
||
const { TARGET_ENV, TARGET_NET, DUT } = k6Env; | ||
|
||
if (!(TARGET_ENV && TARGET_NET) && !DUT) | ||
throw new Error('Please specify both TARGET_ENV and TARGET_NET or DUT (Domain Under Test'); | ||
|
||
let urlEnvironments = [...allowedEnvironments]; | ||
let urlNetworks = [...allowedNetworks]; | ||
|
||
if (options) { | ||
if (typeof options !== 'object') throw new Error(`${typeof options}: not allowed type for options`); | ||
|
||
for (const option of Object.keys(options)) | ||
if (!allowedOptions.has(option)) throw new Error(`${options}: not allowed option`); | ||
|
||
const { environments, networks } = options; | ||
|
||
switch (typeof environments) { | ||
case 'undefined': | ||
break; | ||
|
||
case 'object': | ||
if (!Array.isArray(environments)) throw new Error('options.environments must be an array'); | ||
|
||
for (const environment of environments) | ||
if (!allowedEnvironments.has(environment)) throw new Error(`${environment}: not allowed environment`); | ||
|
||
urlEnvironments = environments; | ||
|
||
break; | ||
|
||
default: | ||
throw new Error(`${typeof environments}: not allowed type for options.environments`); | ||
} | ||
|
||
switch (typeof networks) { | ||
case 'undefined': | ||
break; | ||
|
||
case 'object': | ||
if (!Array.isArray(networks)) throw new Error('options.networks must be an array'); | ||
|
||
for (const network of networks) | ||
if (!allowedNetworks.has(network)) throw new Error(`${network}: not allowed network`); | ||
|
||
urlNetworks = networks; | ||
|
||
break; | ||
|
||
default: | ||
throw new Error(`${typeof networks}: not allowed type for options.networks`); | ||
} | ||
} | ||
|
||
if (TARGET_ENV && !urlEnvironments.includes(TARGET_ENV as Environment)) | ||
throw new Error(`${TARGET_ENV}: not allowed environment`); | ||
if (TARGET_NET && !urlNetworks.includes(TARGET_NET as Network)) throw new Error(`${TARGET_NET}: not allowed network`); | ||
|
||
return DUT || `${TARGET_ENV}-${TARGET_NET}${TARGET_ENV === 'ops' ? '-1' : ''}.lw.iog.io`; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as k6Utils from '../src/k6-utils'; | ||
|
||
describe('K6 utils', () => { | ||
const k6Env = { TARGET_ENV: 'dev', TARGET_NET: 'mainnet' }; | ||
|
||
it('uses custom DUT if configured', () => { | ||
expect(k6Utils.getDut({ DUT: 'localhost:3000' })).toBe('localhost:3000'); | ||
}); | ||
|
||
it('builds the correct domain under test based on TARGET_ENV and TARGET_NET', () => { | ||
expect(k6Utils.getDut(k6Env)).toBe('dev-mainnet.lw.iog.io'); | ||
expect(k6Utils.getDut({ TARGET_ENV: k6Utils.Environment.live, TARGET_NET: k6Utils.Network.preprod })).toBe( | ||
'live-preprod.lw.iog.io' | ||
); | ||
expect(k6Utils.getDut({ TARGET_ENV: k6Utils.Environment.ops, TARGET_NET: k6Utils.Network.preview })).toBe( | ||
'ops-preview-1.lw.iog.io' | ||
); | ||
}); | ||
|
||
describe('Runtime validations', () => { | ||
it('throws an error if none of TARGET_ENV and TARGET_NETWORK, or DUT are configured', () => { | ||
expect(() => k6Utils.getDut({})).toThrow(); | ||
expect(() => k6Utils.getDut({ TARGET_ENV: k6Utils.Environment.dev })).toThrow(); | ||
expect(() => k6Utils.getDut({ TARGET_NET: k6Utils.Network.mainnet })).toThrow(); | ||
}); | ||
|
||
it('checks options type', () => { | ||
expect(() => k6Utils.getDut(k6Env, 'invalid' as any)).toThrow(); | ||
Check warning on line 28 in packages/util-dev/test/k6-utils.test.ts GitHub Actions / build_and_test (ubuntu-20.04)
|
||
expect(() => k6Utils.getDut(k6Env, { unknownProp: 'invalid' } as any)).toThrow(); | ||
Check warning on line 29 in packages/util-dev/test/k6-utils.test.ts GitHub Actions / build_and_test (ubuntu-20.04)
|
||
}); | ||
|
||
it('checks options.environments and options.network to be array', () => { | ||
expect(() => k6Utils.getDut(k6Env, { environments: 'invalid' as any })).toThrow(); | ||
Check warning on line 33 in packages/util-dev/test/k6-utils.test.ts GitHub Actions / build_and_test (ubuntu-20.04)
|
||
expect(() => k6Utils.getDut(k6Env, { networks: 'invalid' as any })).toThrow(); | ||
Check warning on line 34 in packages/util-dev/test/k6-utils.test.ts GitHub Actions / build_and_test (ubuntu-20.04)
|
||
}); | ||
|
||
it('checks options.environments and options.network to be valid', () => { | ||
expect(() => k6Utils.getDut(k6Env, { environments: ['invalid' as k6Utils.Environment] })).toThrow(); | ||
expect(() => k6Utils.getDut(k6Env, { networks: ['invalid' as k6Utils.Network] })).toThrow(); | ||
}); | ||
|
||
it('checks TARGET_ENV and TARGET_NET to be from options allowed values', () => { | ||
expect(() => | ||
k6Utils.getDut( | ||
{ TARGET_ENV: 'dev', TARGET_NET: 'mainnet' }, | ||
{ environments: [k6Utils.Environment.ops], networks: [k6Utils.Network.mainnet] } | ||
) | ||
).toThrow(); | ||
|
||
expect(() => | ||
k6Utils.getDut( | ||
{ TARGET_ENV: 'dev', TARGET_NET: 'mainnet' }, | ||
{ environments: [k6Utils.Environment.dev], networks: [k6Utils.Network.preprod] } | ||
) | ||
).toThrow(); | ||
}); | ||
}); | ||
}); |