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

[backend] extend our vitest config for easier dev process #5796

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[backend] extend our vitest config for easier dev process
  • Loading branch information
labo-flg committed Feb 29, 2024
commit e30b1b740b3d9f687b60df951cfc67e62e6e0580
2 changes: 2 additions & 0 deletions opencti-platform/opencti-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"serv": "node build/back.js",
"insert:dev": "node build/script-insert-dataset.js",
"test:dev": "vitest watch --config vitest.config.dev.ts",
labo-flg marked this conversation as resolved.
Show resolved Hide resolved
"test:dev-init-only": "ONLY_PLATFORM_INIT=1 vitest run --config vitest.config.dev.ts tests/02-integration/00-inject/loader-test.js",
labo-flg marked this conversation as resolved.
Show resolved Hide resolved
"test:dev-no-cleanup": "SKIP_CLEANUP_PLATFORM_AT_START=1 vitest watch --config vitest.config.dev.ts",
"test": "vitest --silent run --config vitest.config.test.ts --coverage"
},
"pkg": {
Expand Down
78 changes: 64 additions & 14 deletions opencti-platform/opencti-graphql/tests/utils/globalSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,67 @@ import { executionContext } from '../../src/utils/access';
import { initializeData } from '../../src/database/data-initialization';
import { shutdownModules, startModules } from '../../src/managers';

const testPlatformStart = async () => {
/**
* Vitest setup is configurable with environment variables, as you can see in our package.json scripts
* ONLY_PLATFORM_INIT=1 > cleanup the test platform, removing elastic indices, and setup it again
* SKIP_CLEANUP_PLATFORM_AT_START=1 > skip cleanup, and directly start the platform
*
* run yarn test:dev-init-only to cleanup and reinit a test platform (it also provision the data)
* run yarn test:dev-no-cleanup <file-pattern> to run directly some tests without cleanup and init of the test platform
*/

const { ONLY_PLATFORM_INIT, SKIP_CLEANUP_PLATFORM_AT_START } = process.env;

const initializePlatform = async () => {
const context = executionContext('platform_test_initialization');
logApp.info('[OPENCTI] Starting platform');
console.log('🚀 [vitest-global-setup] initializing platform');
labo-flg marked this conversation as resolved.
Show resolved Hide resolved
const stopTime = new Date().getTime();

await initializeInternalQueues();
await initializeBucket();
await initializeSchema();
await initializeData(context, true);
await initializeAdminUser(context);
await initDefaultNotifiers(context);
console.log(`🚀 [vitest-global-setup] Platform initialized in ${new Date().getTime() - stopTime} ms`);
};

const testPlatformStart = async () => {
const stopTime = new Date().getTime();
console.log('🚀 [vitest-global-setup] Starting platform');
try {
// Check all dependencies access
await searchEngineInit();
// Init the cache manager
await cacheManager.start();
// Init the platform default
await initializeInternalQueues();
await initializeBucket();
await initializeSchema();
await initializeData(context, true);
await initializeAdminUser(context);
await initDefaultNotifiers(context);
// Init the platform default if it was cleaned up
if (!SKIP_CLEANUP_PLATFORM_AT_START) {
await initializePlatform();
}
// Init the modules
await startModules();
console.log(`🚀 [vitest-global-setup] Platform started in ${new Date().getTime() - stopTime} ms`);
} catch (e) {
logApp.error(e);
process.exit(1);
}
};

const testPlatformStop = async () => {
console.log('🚀 [vitest-global-setup] stopping platform');
const stopTime = new Date().getTime();
// Shutdown the cache manager
await cacheManager.shutdown();
// Destroy the modules
await shutdownModules();
// Shutdown the redis clients
await shutdownRedisClients();
logApp.info(`[OPENCTI] Platform stopped ${new Date().getTime() - stopTime} ms`);
console.log(`🚀 [vitest-global-setup] Platform stopped in ${new Date().getTime() - stopTime} ms`);
};

const platformClean = async () => {
console.log('🚀 [vitest-global-setup] cleaning up platform');
const stopTime = new Date().getTime();
// Delete the bucket
await deleteBucket();
// Delete all rabbitmq queues
Expand All @@ -63,15 +90,38 @@ const platformClean = async () => {
const testRedisClient = createRedisClient('reset');
await testRedisClient.del('stream.opencti');
testRedisClient.disconnect();
console.log(`🚀 [vitest-global-setup] Platform cleaned up in ${new Date().getTime() - stopTime} ms`);
};

export async function setup() {
// Platform cleanup before executing tests
await platformClean();
if (ONLY_PLATFORM_INIT) {
console.log('🚀 [vitest-global-setup] only running test platform initialization');
const stopTime = new Date().getTime();
await platformClean();
await testPlatformStart();
await wait(15000); // Wait 15 secs for complete platform start
labo-flg marked this conversation as resolved.
Show resolved Hide resolved
console.log('🚀 [vitest-global-setup] creating test users');
await createTestUsers();
console.log(`🚀 [vitest-global-setup] Test Platform initialization done in ${new Date().getTime() - stopTime} ms`);
return;
}

if (!SKIP_CLEANUP_PLATFORM_AT_START) {
// Platform cleanup before executing tests
console.log('🚀 [vitest-global-setup] Cleaning up test platform...');
await platformClean();
} else {
console.log('🚀 [vitest-global-setup] ⚠️ skipping platform cleanup and setup - database state is the same as your last run ⚠️');
}
// Start the platform
await testPlatformStart();
await wait(15000); // Wait 15 secs for complete platform start
await createTestUsers();

// setup tests users
if (!SKIP_CLEANUP_PLATFORM_AT_START) {
await wait(15000); // Wait 15 secs for complete platform start
console.log('🚀 [vitest-global-setup] Creating test users...');
await createTestUsers();
}
}

export async function teardown() {
Expand Down
1 change: 0 additions & 1 deletion opencti-platform/opencti-graphql/vitest.config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@ export const buildTestConfig = (include: string[]) => defineConfig({
});

export default buildTestConfig(['tests/**/*-test.{js,mjs,cjs,ts,mts,cts,jsx,tsx}']);
// export default buildTestConfig(['tests/(02)-*/**/(loader|filterGroup|workspace)*-test.{js,mjs,cjs,ts,mts,cts,jsx,tsx}']);