From 20727a60cdb4c9618b18c440a67f17969b504930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Tue, 12 Jan 2021 18:17:32 +0000 Subject: [PATCH] feat: idle machine polling --- .gitignore | 2 + test/stress/index.js | 68 ++++++++++++++++++++++++++ test/stress/scripts/create-machines.sh | 61 +++++++++++++++++++++++ test/stress/scripts/index.js | 22 +++++++++ test/stress/utils/init-cert.js | 12 +++++ test/stress/utils/save-config.js | 28 +++++++++++ test/stress/utils/variables.js | 5 ++ 7 files changed, 198 insertions(+) create mode 100644 test/stress/index.js create mode 100644 test/stress/scripts/create-machines.sh create mode 100644 test/stress/scripts/index.js create mode 100644 test/stress/utils/init-cert.js create mode 100644 test/stress/utils/save-config.js create mode 100644 test/stress/utils/variables.js diff --git a/.gitignore b/.gitignore index 041f860c..78852c81 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,8 @@ scratch/ seeds/ mnemonics/ certs/ +test/stress/machines +test/stress/config.json lamassu.json terraform.* diff --git a/test/stress/index.js b/test/stress/index.js new file mode 100644 index 00000000..ca566a40 --- /dev/null +++ b/test/stress/index.js @@ -0,0 +1,68 @@ +const https = require('https') +const path = require('path') +const fs = require('fs') +const uuid = require('uuid') +const cmd = require('./scripts') +const variables = require('./utils/variables') + +async function createMachines () { + await cmd.execCommand( + `bash ./scripts/create-machines.sh ${variables.NUMBER_OF_MACHINES} ${variables.SERVER_CERT_PATH} ${variables.MACHINE_PATH}` + ) +} + +function getCert (machineIndex) { + try { + return { + key: fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.key')), + cert: fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.pem')) + } + } catch (e) { + return null + } +} + +function connectionInfo (machineIndex) { + console.log(machineIndex) + try { + return JSON.parse(fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'connection_info.json'))) + } catch (e) { + return null + } +} + +let index = 0 + +function request (machineIndex, pid) { + https.get({ + hostname: 'localhost', + port: 3000, + path: '/poll?state=chooseCoin&model=unknown&version=7.5.0-beta.0&idle=true&pid=' + pid + '&sn=' + index, + method: 'GET', + key: getCert(machineIndex).key, + cert: getCert(machineIndex).cert, + ca: connectionInfo(machineIndex).ca, + headers: { + date: new Date().toISOString(), + 'request-id': uuid.v4() + } + }, res => { + res.on('data', (d) => { + console.log(Buffer.from(d).toString()) + }) + }) + + index++ +} + +function run () { + createMachines().then(() => { + for (let i = 1; i <= variables.NUMBER_OF_MACHINES; i++) { + const pid = uuid.v4() + request(i, pid) + setInterval(() => request(i, pid), 5000) + } + }) +} + +run() diff --git a/test/stress/scripts/create-machines.sh b/test/stress/scripts/create-machines.sh new file mode 100644 index 00000000..285a4178 --- /dev/null +++ b/test/stress/scripts/create-machines.sh @@ -0,0 +1,61 @@ +#!/bin/bash +set -e + +if [ $# -eq 0 ] + then + echo "usage: ./build-machines [number_of_machines] /path/to/server/cert/lamassu_op_root_ca.pem /path/to/machine/" && exit 1 +fi + +case $1 in + ''|*[!0-9]*) echo "usage: ./build-machines [number_of_machines] /path/to/server/cert/lamassu_op_root_ca.pem /path/to/machine/" && exit 1;; +esac + +SERVER_CERT=$(perl -pe 's/\n/\\n/' < $2) +if [ -z "$SERVER_CERT" ] + then + echo "Lamassu-op-root-ca.pem is empty" && exit 1 +fi + +# Remove old folders +rm -rf ./machines/* + +# Create stress database +# sudo -u postgres psql postgres -c "drop database if exists lamassu_stress" +# sudo -u postgres psql postgres -c "create database lamassu_stress with template lamassu" + +START=1 +END=$1 +for (( c=$START; c<=$END; c++ )) +do + NUMBER=$c + mkdir -p ./machines/$NUMBER/ + cp "$3"/data/client.sample.pem ./machines/$NUMBER/ + cp "$3"/data/client.sample.key ./machines/$NUMBER/ + + + cat > ./machines/$NUMBER/connection_info.json << EOL + {"host":"localhost","ca":"$SERVER_CERT"} +EOL + + echo 'Generating certs...' + node ./utils/init-cert.js $NUMBER + + # Get device_id + DEVICE_ID=`openssl x509 -outform der -in ./machines/$NUMBER/client.pem | sha256sum | cut -d " " -f 1` + + # Update db config + sudo -u postgres psql -t -d lamassu -c "select data from user_config where type='config' order by id desc limit 1" > config.json + NEW_CONFIG=$(node ./utils/save-config.js $NUMBER $DEVICE_ID) + sudo -u postgres psql "lamassu" << EOF + insert into user_config(type, data, created, valid) + values('config', '$NEW_CONFIG', now(), 't') +EOF + + # Add device on db + sudo -u postgres psql "lamassu" << EOF + insert into devices(device_id, cashbox, cassette1, cassette2, paired, display, created, name, last_online, location) + values ('$DEVICE_ID', 0, 0, 0, 't', 't', now(), $NUMBER, now(), '{}'::json) +EOF +done + +echo "Done!" diff --git a/test/stress/scripts/index.js b/test/stress/scripts/index.js new file mode 100644 index 00000000..be7080af --- /dev/null +++ b/test/stress/scripts/index.js @@ -0,0 +1,22 @@ +const exec = require('child_process') + +/** + * Execute simple shell command (async wrapper). + * @param {String} cmd + * @return {Object} { stdout: String, stderr: String } + */ +async function execCommand (cmd) { + return new Promise(function (resolve, reject) { + exec.exec(cmd, (err, stdout, stderr) => { + if (err) { + reject(err) + } else { + console.log(stdout) + console.error(stderr) + // resolve({ stdout, stderr }) + } + }) + }) +} + +module.exports = { execCommand } diff --git a/test/stress/utils/init-cert.js b/test/stress/utils/init-cert.js new file mode 100644 index 00000000..cbfef975 --- /dev/null +++ b/test/stress/utils/init-cert.js @@ -0,0 +1,12 @@ +const path = require('path') +const variables = require('./variables') +const { init } = require(`../${variables.MACHINE_PATH}/lib/pairing`) + +const number = process.argv[2] + +const certPath = { + cert: path.resolve(process.cwd(), 'machines', number, 'client.pem'), + key: path.resolve(process.cwd(), 'machines', number, 'client.key') +} + +init(certPath) diff --git a/test/stress/utils/save-config.js b/test/stress/utils/save-config.js new file mode 100644 index 00000000..8c36f52d --- /dev/null +++ b/test/stress/utils/save-config.js @@ -0,0 +1,28 @@ +const number = process.argv[2] +const machine = process.argv[3] +const old = require('../config.json') + +function configAddField (scope, fieldCode, fieldType, fieldClass, value) { + return { + fieldLocator: { + fieldScope: { + crypto: scope.crypto, + machine: scope.machine + }, + code: fieldCode, + fieldType, + fieldClass + }, + fieldValue: { fieldType, value } + } +} + +const scope = { crypto: 'global', machine } + +const newFields = [ + configAddField(scope, 'cashOutEnabled', 'onOff', null, false), + configAddField(scope, 'machineName', 'string', null, number), + configAddField(scope, 'machineModel', 'string', null, 'Linux') +] +const data = { config: newFields.concat(old.config) } +console.log(JSON.stringify(data)) diff --git a/test/stress/utils/variables.js b/test/stress/utils/variables.js new file mode 100644 index 00000000..487ed8fc --- /dev/null +++ b/test/stress/utils/variables.js @@ -0,0 +1,5 @@ +const NUMBER_OF_MACHINES = 4 +const SERVER_CERT_PATH = `../../certs/Lamassu_OP_Root_CA.pem` +const MACHINE_PATH = `../../../lamassu-machine` + +module.exports = { NUMBER_OF_MACHINES, SERVER_CERT_PATH, MACHINE_PATH }