Skip to content

Commit

Permalink
add function to convert the network config to go version
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
  • Loading branch information
hacdias committed Oct 9, 2020
1 parent 9ca2c18 commit 456bea1
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,44 @@ function waitNetworkInitialized ({ client, runenv }) {

function configureNetwork ({ client, runenv }) {
return async (config) => {
if (!runenv.TestSidecar) {
if (!runenv.testSidecar) {
runenv.logger.warn('ignoring network change request; running in a sidecar-less environment')
return
}

if (!config.State) {
if (!config.callbackState) {
throw new Error('failed to configure network; no callback state provided')
}

const hostname = os.hostname()
const topic = `network:${hostname}`
const target = config.CallbackTarget === 0
? config.TestInstanceCount // Fall back to instance count on zero value.
: config.CallbackTarget
const target = config.callbackTarget === 0
? config.testInstanceCount // Fall back to instance count on zero value.
: config.callbackTarget

await client.publishAndWait(topic, fixConfig(config), config.callbackState, target)
}
}

// fixConfig converts the configuration from the JavaScript lowerCamelCase version to the
// Go CammelCase version. More about this is mentioned at https://github.com/testground/sdk-go/pull/34
function fixConfig (config) {
if (typeof config !== 'object') {
return config
}

await client.publishAndWait(topic, config, config.State, target)
if (Array.isArray(config)) {
return config.map(fixConfig)
}

const parsed = {}

for (let [key, value] of Object.entries(config)) {
if (key === 'callbackState') key = 'State'
parsed[key.charAt(0).toUpperCase() + key.slice(1)] = value
}

return parsed
}

function getDataNetworkIP ({ client, runenv }) {
Expand Down

0 comments on commit 456bea1

Please sign in to comment.