From 9d896091329c896324ffdd72d72d3e35ca8e4c70 Mon Sep 17 00:00:00 2001 From: m5r Date: Thu, 27 Jun 2024 16:35:23 +0200 Subject: [PATCH] orchestrate with mocha hooks --- .gitignore | 1 + test/e2e/.mocharc.js | 2 +- test/e2e/hooks.js | 63 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 2c86ae6e4..39c909fa0 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ coverage .nyc_output .DS_Store test/.DS_Store +test/e2e/.cht-docker-helper diff --git a/test/e2e/.mocharc.js b/test/e2e/.mocharc.js index 6ad8a1f9d..dd3a5a76a 100644 --- a/test/e2e/.mocharc.js +++ b/test/e2e/.mocharc.js @@ -13,7 +13,7 @@ module.exports = { spec: require('./specs').all, timeout: 200 * 1000, //API takes a little long to start up reporter: 'spec', - file: [ 'test/e2e/hooks.js' ], + file: ['test/e2e/hooks.js'], captureFile: 'test/e2e/results.txt', exit: true, recursive: true, diff --git a/test/e2e/hooks.js b/test/e2e/hooks.js index a751b6e39..bebc677ff 100644 --- a/test/e2e/hooks.js +++ b/test/e2e/hooks.js @@ -1,9 +1,66 @@ -before(() => { +const path = require('path'); +const fs = require('fs'); +const https = require('https'); +const { spawn } = require('child_process'); + +const projectName = 'cht_conf_e2e_tests'; +const dockerHelperDirectory = path.resolve(__dirname, '.cht-docker-helper'); +const dockerHelperScript = path.resolve(dockerHelperDirectory, './cht-docker-compose.sh'); + +const downloadDockerHelperScript = () => new Promise((resolve, reject) => { + const file = fs.createWriteStream(dockerHelperScript, { mode: 0o755 }); + https + .get('https://raw.githubusercontent.com/medic/cht-core/master/scripts/docker-helper-4.x/cht-docker-compose.sh', (response) => { + response.pipe(file); + file.on('finish', () => file.close(resolve)); + file.on('error', () => file.close(reject)); + }) + .on('error', () => { + fs.unlinkSync(file.path); + file.close(() => reject('Failed to download CHT Docker Helper script "cht-docker-compose.sh"')); + }); +}); + +const spinUpCHT = () => new Promise((resolve, reject) => { + const childProcess = spawn(dockerHelperScript, { stdio: 'pipe', cwd: dockerHelperDirectory }); + childProcess.on('error', reject); + childProcess.on('close', resolve); + + const configFile = path.resolve(dockerHelperDirectory, `${projectName}.env`); + if (fs.existsSync(configFile)) { + childProcess.stdin.write('n\n'); + childProcess.stdin.write('1\n'); + } else { + childProcess.stdin.write('y\n'); + childProcess.stdin.write('y\n'); + childProcess.stdin.write(`${projectName}\n`); + } +}); + +const takeDownCHT = () => new Promise((resolve, reject) => { + const childProcess = spawn(dockerHelperScript, [`${projectName}.env`, 'stop'], { cwd: dockerHelperDirectory }); + childProcess.on('error', reject); + childProcess.on('close', resolve); +}); + +before(async () => { console.log('before'); + + if (!fs.existsSync(dockerHelperDirectory)) { + fs.mkdirSync(dockerHelperDirectory); + } + + if (!fs.existsSync(dockerHelperScript)) { + await downloadDockerHelperScript(); + } + + await spinUpCHT(); }); -after(() => { +after(async () => { console.log('after'); + + await takeDownCHT(); }); beforeEach(() => { @@ -12,4 +69,4 @@ beforeEach(() => { afterEach(() => { console.log('afterEach'); -}); \ No newline at end of file +});