Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add basic browser support #26

Merged
merged 13 commits into from
Nov 16, 2022
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
"engines": {
"node": ">=14.0.0"
},
"browser": {
"os": "./src/shims/os.js",
"./src/env/env": "./src/shims/env/env.js",
"./src/runtime/logger": "./src/shims/runtime/logger.js"
},
"aegir": {
"test": {
"target": [
Expand All @@ -66,6 +71,7 @@
},
"contributors": [
"Henrique Dias <hacdias@gmail.com>",
"Laurent Senta <Laurent.Senta@gmail.com>"
"Laurent Senta <Laurent.Senta@gmail.com>",
"Glen De Cauwsemaecker <glen@littlebearlabs.io>"
]
}
24 changes: 24 additions & 0 deletions src/env/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

/**
* Gets the environment that can be used by the environment
* to create the runtime.
*
* @returns {Record<string, string|undefined>}
*/
function getProcessEnv () {
return process.env
}

/**
* @param {unknown} _result
*/
function registerTestcaseResult (_result) {
// function is used in the browser shim
// to gain the ability to wait until invokeMap is finished
}

module.exports = {
getProcessEnv,
registerTestcaseResult
}
46 changes: 46 additions & 0 deletions src/env/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict'

const runtime = require('./runtime')
const sync = require('./sync')
const { getProcessEnv } = require('./env')

const ENV_TEST_PARAMETERS = [
runtime.ENV_TEST_BRANCH,
runtime.ENV_TEST_CASE,
runtime.ENV_TEST_GROUP_ID,
runtime.ENV_TEST_GROUP_INSTANCE_COUNT,
runtime.ENV_TEST_INSTANCE_COUNT,
runtime.ENV_TEST_INSTANCE_PARAMS,
runtime.ENV_TEST_INSTANCE_ROLE,
runtime.ENV_TEST_OUTPUTS_PATH,
runtime.ENV_TEST_PLAN,
runtime.ENV_TEST_REPO,
runtime.ENV_TEST_RUN,
runtime.ENV_TEST_SIDECAR,
runtime.ENV_TEST_START_TIME,
runtime.ENV_TEST_SUBNET,
runtime.ENV_TEST_TAG,

sync.ENV_SYNC_SERVICE_HOST,
sync.ENV_SYNC_SERVICE_PORT
]

/**
* Gets the parameters from the environment
* that can be used by the environment to create the runtime.
*
* @returns {Record<string, string|undefined>}
*/
function getEnvParameters () {
const env = getProcessEnv()
return Object.keys(env)
.filter(key => ENV_TEST_PARAMETERS.includes(key))
.reduce((/** @type {Record<string, string|undefined>} */params, key) => {
params[key] = env[key]
return params
}, {})
}
GlenDC marked this conversation as resolved.
Show resolved Hide resolved

module.exports = {
getEnvParameters
}
35 changes: 35 additions & 0 deletions src/env/runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

const ENV_TEST_BRANCH = 'TEST_BRANCH'
const ENV_TEST_CASE = 'TEST_CASE'
const ENV_TEST_GROUP_ID = 'TEST_GROUP_ID'
const ENV_TEST_GROUP_INSTANCE_COUNT = 'TEST_GROUP_INSTANCE_COUNT'
const ENV_TEST_INSTANCE_COUNT = 'TEST_INSTANCE_COUNT'
const ENV_TEST_INSTANCE_PARAMS = 'TEST_INSTANCE_PARAMS'
const ENV_TEST_INSTANCE_ROLE = 'TEST_INSTANCE_ROLE'
const ENV_TEST_OUTPUTS_PATH = 'TEST_OUTPUTS_PATH'
const ENV_TEST_PLAN = 'TEST_PLAN'
const ENV_TEST_REPO = 'TEST_REPO'
const ENV_TEST_RUN = 'TEST_RUN'
const ENV_TEST_SIDECAR = 'TEST_SIDECAR'
const ENV_TEST_START_TIME = 'TEST_START_TIME'
const ENV_TEST_SUBNET = 'TEST_SUBNET'
const ENV_TEST_TAG = 'TEST_TAG'

module.exports = {
ENV_TEST_BRANCH,
ENV_TEST_CASE,
ENV_TEST_GROUP_ID,
ENV_TEST_GROUP_INSTANCE_COUNT,
ENV_TEST_INSTANCE_COUNT,
ENV_TEST_INSTANCE_PARAMS,
ENV_TEST_INSTANCE_ROLE,
ENV_TEST_OUTPUTS_PATH,
ENV_TEST_PLAN,
ENV_TEST_REPO,
ENV_TEST_RUN,
ENV_TEST_SIDECAR,
ENV_TEST_START_TIME,
ENV_TEST_SUBNET,
ENV_TEST_TAG
}
9 changes: 9 additions & 0 deletions src/env/sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

const ENV_SYNC_SERVICE_HOST = 'SYNC_SERVICE_HOST'
const ENV_SYNC_SERVICE_PORT = 'SYNC_SERVICE_PORT'

module.exports = {
ENV_SYNC_SERVICE_HOST,
ENV_SYNC_SERVICE_PORT
}
27 changes: 25 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
const runtime = require('./runtime')
const sync = require('./sync')
const network = require('./network')
const env = require('./env')

const { registerTestcaseResult } = require('./env/env')

/** @typedef {import('./runtime').RunEnv} RunEnv */
/** @typedef {import('./sync').SyncClient} SyncClient */
Expand All @@ -17,12 +20,28 @@ async function invokeMap (cases) {
const runenv = runtime.currentRunEnv()

if (cases[runenv.testCase]) {
await invokeHelper(runenv, cases[runenv.testCase])
try {
await invokeHelper(runenv, cases[runenv.testCase])
} catch (err) {
registerAndMessageTestcaseResult(err, runenv)
throw err
}
} else {
throw new Error(`unrecognized test case: ${runenv.testCase}`)
const err = new Error(`unrecognized test case: ${runenv.testCase}`)
registerAndMessageTestcaseResult(err, runenv)
throw err
}
}

/**
* @param {unknown} result
* @param {RunEnv} runenv
*/
function registerAndMessageTestcaseResult (result, runenv) {
runenv.recordMessage(`registerTestcaseResult: ${result}`)
registerTestcaseResult(result)
}

/**
* Runs the passed test-case and reports the result.
*
Expand All @@ -47,22 +66,26 @@ async function invokeHelper (runenv, fn) {

await runenv.recordStart()

let /** @type {unknown} */ testResult = true
try {
await fn(runenv, client)
await runenv.recordSuccess()
} catch (err) {
await runenv.recordFailure(err)
testResult = err
} finally {
if (client) {
client.close()
}
registerAndMessageTestcaseResult(testResult, runenv)
}
}

module.exports = {
invoke,
invokeMap,

env,
network,
runtime,
sync
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function newEvents (runParams, logger, getSignalEmitter) {

logger.info('', { event })
return emitEvent(event)
// eslint-disable-next-line no-warning-comments
// TODO(metrics): re.metrics.recordEvent(&evt)
},
recordSuccess: () => {
Expand All @@ -58,6 +59,7 @@ function newEvents (runParams, logger, getSignalEmitter) {

logger.info('', { event })
return emitEvent(event)
// eslint-disable-next-line no-warning-comments
// TODO(metrics): re.metrics.recordEvent(&evt)
},
recordFailure: (err) => {
Expand All @@ -70,6 +72,7 @@ function newEvents (runParams, logger, getSignalEmitter) {

logger.info('', { event })
return emitEvent(event)
// eslint-disable-next-line no-warning-comments
// TODO(metrics): re.metrics.recordEvent(&evt)
},
recordCrash: (err) => {
Expand All @@ -83,6 +86,7 @@ function newEvents (runParams, logger, getSignalEmitter) {

logger.info('', { event })
return emitEvent(event)
// eslint-disable-next-line no-warning-comments
// TODO(metrics): re.metrics.recordEvent(&evt)
GlenDC marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { getLogger } = require('./logger')
const { newEvents } = require('./events')
const { parseRunParams } = require('./params')
const { getEnvParameters } = require('../env')

/** @typedef {import('./types').RunParams} RunParams */
/** @typedef {import('./types').SignalEmitter} SignalEmitter */
Expand All @@ -14,7 +15,8 @@ const { parseRunParams } = require('./params')
* @returns {RunEnv}
*/
function currentRunEnv () {
return parseRunEnv(process.env)
const env = getEnvParameters()
return parseRunEnv(env)
}

/**
Expand Down Expand Up @@ -52,5 +54,6 @@ function newRunEnv (params) {
module.exports = {
newRunEnv,
currentRunEnv,
parseRunEnv
parseRunEnv,
getEnvParameters
}
34 changes: 18 additions & 16 deletions src/runtime/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@

const ipaddr = require('ipaddr.js')

/** @typedef {import('./types').RunParams} RunParams */
const {
ENV_TEST_BRANCH,
ENV_TEST_CASE,
ENV_TEST_GROUP_ID,
ENV_TEST_GROUP_INSTANCE_COUNT,
ENV_TEST_INSTANCE_COUNT,
ENV_TEST_INSTANCE_PARAMS,
ENV_TEST_INSTANCE_ROLE,
ENV_TEST_OUTPUTS_PATH,
ENV_TEST_PLAN,
ENV_TEST_REPO,
ENV_TEST_RUN,
ENV_TEST_SIDECAR,
ENV_TEST_START_TIME,
ENV_TEST_SUBNET,
ENV_TEST_TAG
} = require('../env/runtime')

const ENV_TEST_BRANCH = 'TEST_BRANCH'
const ENV_TEST_CASE = 'TEST_CASE'
const ENV_TEST_GROUP_ID = 'TEST_GROUP_ID'
const ENV_TEST_GROUP_INSTANCE_COUNT = 'TEST_GROUP_INSTANCE_COUNT'
const ENV_TEST_INSTANCE_COUNT = 'TEST_INSTANCE_COUNT'
const ENV_TEST_INSTANCE_PARAMS = 'TEST_INSTANCE_PARAMS'
const ENV_TEST_INSTANCE_ROLE = 'TEST_INSTANCE_ROLE'
const ENV_TEST_OUTPUTS_PATH = 'TEST_OUTPUTS_PATH'
const ENV_TEST_PLAN = 'TEST_PLAN'
const ENV_TEST_REPO = 'TEST_REPO'
const ENV_TEST_RUN = 'TEST_RUN'
const ENV_TEST_SIDECAR = 'TEST_SIDECAR'
const ENV_TEST_START_TIME = 'TEST_START_TIME'
const ENV_TEST_SUBNET = 'TEST_SUBNET'
const ENV_TEST_TAG = 'TEST_TAG'
/** @typedef {import('./types').RunParams} RunParams */

/**
* @param {Record<string, string|undefined>} env
Expand Down
29 changes: 29 additions & 0 deletions src/shims/env/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

/**
* Gets the environment that can be used by the environment
* to create the runtime.
*
* @returns {Record<string, string|undefined>}
*/
function getProcessEnv () {
// @ts-ignore
return (window.testground || {}).env
}
/**
* @param {unknown} result
*/
function registerTestcaseResult (result) {
// @ts-ignore
if (!window.testground) {
// @ts-ignore
window.testground = {}
}
// @ts-ignore
window.testground.result = result
}

module.exports = {
getProcessEnv,
registerTestcaseResult
}
20 changes: 20 additions & 0 deletions src/shims/os.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

/**
* @returns {any[]}
*/
function networkInterfaces () {
return []
}

/**
* @returns {string}
*/
function hostname () {
return 'browser'
}

module.exports = {
networkInterfaces,
hostname
}
Loading