Skip to content

Commit

Permalink
Drone e2e backend populate with data
Browse files Browse the repository at this point in the history
  • Loading branch information
lndrtrbn committed May 2, 2024
1 parent 498c275 commit 2474035
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ services:
- BRANCH=$(echo $DRONE_COMMIT_BRANCH | cut -d "/" -f 2)
- CLIENT_PYTHON_BRANCH=$([[ "$(echo "$(git ls-remote --heads https://github.com/OpenCTI-Platform/client-python.git opencti/$BRANCH)")" != '' ]] && echo opencti/$BRANCH || echo 'master')
- pip3 install --upgrade --force git+https://github.com/OpenCTI-Platform/client-python@$CLIENT_PYTHON_BRANCH
- yarn build:dev
- yarn wait-api && yarn insert:dev &
- NODE_OPTIONS=--max_old_space_size=8192 yarn start

volumes:
Expand Down
3 changes: 2 additions & 1 deletion opencti-platform/opencti-graphql/builder/dev/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ esbuild.build({
entryPoints: [
'src/back.js',
'script/script-clean-relations.js',
'script/script-insert-dataset.js'
'script/script-insert-dataset.js',
'script/script-wait-for-api.js',
],
entryNames: "[name]",
bundle: true,
Expand Down
3 changes: 2 additions & 1 deletion opencti-platform/opencti-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"test:dev": "vitest run --config vitest.config.dev.ts",
"test:watch:resume": "SKIP_CLEANUP_PLATFORM=true vitest watch --config vitest.config.dev.ts",
"test:watch": "vitest watch --config vitest.config.dev.ts",
"test": "vitest --silent run --config vitest.config.test.ts --coverage"
"test": "vitest --silent run --config vitest.config.test.ts --coverage",
"wait-api": "node build/script-wait-for-api.js"
},
"pkg": {
"scripts": "dist/**/*.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const getStartingHandler = () => {
process.exit();
}
};
return fetch(API_URI, {}).then(() => manualStartHandler).catch(() => autoStartHandler);
return fetch(`${API_URI}/health`, {}).then(() => manualStartHandler).catch(() => autoStartHandler);
};

// noinspection JSIgnoredPromiseFromCall
Expand Down
40 changes: 40 additions & 0 deletions opencti-platform/opencti-graphql/script/script-wait-for-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import fetch from 'node-fetch';
import conf, { logApp } from '../src/config/conf';

const API_URI = `http://localhost:${conf.get('app:port')}`;

const MAX_ATTEMPTS = 20;
const DELAY_BETWEEN_ATTEMPTS = 5000;

const checkApiAvailability = async () => {
try {
await fetch(`${API_URI}/health`, {});
return true;
} catch (e) {
logApp.info('[WAIT-FOR-API] API unavailable');
return false;
}
};

const waitForApi = async () => {
const delay = (ms) => new Promise((resolve) => { setTimeout(resolve, ms); });
logApp.info('[WAIT-FOR-API] Start waiting for API');

let available = false;
let attempt = 0;
while (!available && attempt < MAX_ATTEMPTS) {
if (attempt > 0) {
await delay(DELAY_BETWEEN_ATTEMPTS);
}
attempt += 1;
available = await checkApiAvailability();
}
if (!available) {
logApp.error('[WAIT-FOR-API] API not reachable');
process.exit(1);
}
logApp.info('[WAIT-FOR-API] Success - API reachable');
};

// noinspection JSIgnoredPromiseFromCall
waitForApi();

0 comments on commit 2474035

Please sign in to comment.