datahaven/test/utils/parameters.ts

87 lines
3.1 KiB
TypeScript
Raw Permalink Normal View History

import path from "node:path";
import { $ } from "bun";
test: 🏗️ Setup e2e testing framework (#104) ## Implement E2E Testing Framework with Isolated Networks ### Summary Refactors the existing E2E testing infrastructure to provide isolated test environments with parallel execution support. Each test suite now runs in its own network namespace, preventing resource conflicts. ### Key Changes - **New Testing Framework** (`test/framework/`): Base classes for test lifecycle management with automatic setup/teardown - **Launcher Module** (`test/launcher/`): Extracted network orchestration logic from CLI handlers for reusability - **Parallel Execution**: Added `test-parallel.ts` script with concurrency limits to prevent resource exhaustion - **Test Isolation**: Each suite gets unique network IDs (format: `suiteName-timestamp`) and Docker networks - **Improved Test Organization**: Migrated tests to new framework, deprecated old test structure ### Test Improvements - Added 4 new test suites demonstrating framework usage. : - `contracts.test.ts` - Smart contract deployment/interaction - `datahaven-substrate.test.ts` - Substrate API operations - `cross-chain.test.ts` - Snowbridge cross-chain messaging - `ethereum-basic.test.ts` - Ethereum network operations > [!WARNING] The test suites themselves are bad and shouldn't be consider examples of good tests. They were AI generated just to test the concurrency of test runners ### Documentation - Added comprehensive framework overview (`E2E_FRAMEWORK_OVERVIEW.md`) - Updated README with parallel testing commands - Added test patterns and best practices ### Breaking Changes - Old test suites moved to `e2e - DEPRECATED/` directory - Test execution now requires extending `BaseTestSuite` class ### Testing Run tests with: `bun test:e2e` or `bun test:e2e:parallel` (with concurrency limits) ### TODO - [ ] Implement good test examples. - [ ] Implement useful test utils (like waiting for an event to show up in DataHaven or Ethereum). - [ ] Enforce tests with CI (currently cannot be done due to intermittent error when sending a transaction with PAPI). --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: undercover-cactus <lola@moonsonglabs.com>
2025-07-16 16:51:07 +00:00
import { logger } from "./logger";
import type { ParsedDataHavenParameter } from "./types";
// Constants for paths
export const PARAMETERS_TEMPLATE_PATH = "configs/parameters/datahaven-parameters.json";
export const PARAMETERS_OUTPUT_DIR = "tmp/configs";
export const PARAMETERS_OUTPUT_FILE = "datahaven-parameters.json";
export const PARAMETERS_OUTPUT_PATH = path.join(PARAMETERS_OUTPUT_DIR, PARAMETERS_OUTPUT_FILE);
/**
* A collection of parameters to be set in the DataHaven runtime.
* This class is used to collect parameters from different steps of the launch process
* and then generate a JSON file to be used by the setDataHavenParameters script.
*/
export class ParameterCollection {
private parameters: ParsedDataHavenParameter[] = [];
/**
* Adds a parameter to the collection
* @param param The parameter to add
*/
public addParameter(param: ParsedDataHavenParameter): void {
// Check if parameter with same name already exists
const existingIndex = this.parameters.findIndex((p) => p.name === param.name);
if (existingIndex !== -1) {
// Replace existing parameter
this.parameters[existingIndex] = param;
logger.debug(`Updated parameter: ${String(param.name)} = ${JSON.stringify(param.value)}`);
} else {
// Add new parameter
this.parameters.push(param);
logger.debug(`Added parameter: ${String(param.name)} = ${JSON.stringify(param.value)}`);
}
}
/**
* Returns the current parameters
*/
public getParameters(): ParsedDataHavenParameter[] {
return [...this.parameters];
}
/**
* Generates a JSON file with the parameters collected so far
*/
public async generateParametersFile(): Promise<string> {
logger.debug(`Ensuring output directory exists: ${PARAMETERS_OUTPUT_DIR}`);
await $`mkdir -p ${PARAMETERS_OUTPUT_DIR}`.quiet();
// If we have no parameters, load the template to get the structure
if (this.parameters.length === 0) {
logger.debug(`No parameters collected, loading template from ${PARAMETERS_TEMPLATE_PATH}`);
const templateFile = Bun.file(PARAMETERS_TEMPLATE_PATH);
if (!(await templateFile.exists())) {
throw new Error(`Template file ${PARAMETERS_TEMPLATE_PATH} does not exist`);
}
this.parameters = await templateFile.json();
}
// Write the parameters to a file
logger.debug(`Writing parameters to ${PARAMETERS_OUTPUT_PATH}`);
await Bun.write(PARAMETERS_OUTPUT_PATH, JSON.stringify(this.parameters, null, 2));
logger.debug(`Parameters file generated at ${PARAMETERS_OUTPUT_PATH}`);
return PARAMETERS_OUTPUT_PATH;
}
}
/**
* Creates a new ParameterCollection, pre-loaded with template parameters if available
*/
feat: 🚀 Add `deploy` command to CLI (#87) ### New Features 1. Add the `deploy` command to our CLI. 1. Conditionally deploys kurtosis eth network if we're in `stagenet` environment. 2. Deploys DH nodes. 3. Deploys contracts (all of them). In `mainnet` and `testnet` it shouldn't deploy EL contracts, but for now that's not implemented. 4. Configures parameters, validators and relayers in the same way as `launch`. 5. Currently, it only deploys `beefy` and `beacon` relayers, `execution` and `solochain` relayers are pending for a subsequent PR. 2. Add `waitFor` utility function that receives a lambda. ### Refactors 1. Several common functionalities used both by the `launch` and `deploy` command have been moved to the `cli/handlers/common` directory, from where both commands use them. These include 1. Checks for installed dependencies. 2. Common constants. 3. The `LaunchedNetwork` class has been moved to this directory. 4. DataHaven nodes common functions. 5. Kurtosis common functions. 6. Relayer common functions. 7. Kubernetes functions (although only used by `deploy`, it seemed fitting to have it here still). 8. Remove CLI questions and separator prints from `deploy-contracts.ts` and `set-datahaven-parameters.ts` scripts. These things should be in the `cli/launch` folder, which consumes the functions in these scripts. 9. Remove `setParametersFromCollection` from `utils` folder and put it in `cli`. 10. Create base snowbridge relayer configs for `local` and `stagenet` as two separate directories. ### Fixes 1. Sets the default time of the `deploy` command to 6s as Lodestar is slower than Lighthouse. 2. In `runShellCommandWithLogger` only print `stderr` if the command fails. ### Additional Minor Changes 1. K8s secret key names changed from `dh-beefy-relay-eth-key` to `dh-beefy-relay-ethereum-key` and `dh-beacon-relay-sub-key` to `dh-beacon-relay-substrate-key`, for simplicity in the deployment script. 11. Update suggested configs for `.vscode` configs. --------- Co-authored-by: Steve Degosserie <723552+stiiifff@users.noreply.github.com>
2025-06-12 08:24:03 +00:00
export const createParameterCollection = async (): Promise<ParameterCollection> => {
const collection = new ParameterCollection();
const templateFile = Bun.file(PARAMETERS_TEMPLATE_PATH);
if (await templateFile.exists()) {
const templateParams = await templateFile.json();
for (const param of templateParams) {
collection.addParameter(param);
}
}
return collection;
feat: 🚀 Add `deploy` command to CLI (#87) ### New Features 1. Add the `deploy` command to our CLI. 1. Conditionally deploys kurtosis eth network if we're in `stagenet` environment. 2. Deploys DH nodes. 3. Deploys contracts (all of them). In `mainnet` and `testnet` it shouldn't deploy EL contracts, but for now that's not implemented. 4. Configures parameters, validators and relayers in the same way as `launch`. 5. Currently, it only deploys `beefy` and `beacon` relayers, `execution` and `solochain` relayers are pending for a subsequent PR. 2. Add `waitFor` utility function that receives a lambda. ### Refactors 1. Several common functionalities used both by the `launch` and `deploy` command have been moved to the `cli/handlers/common` directory, from where both commands use them. These include 1. Checks for installed dependencies. 2. Common constants. 3. The `LaunchedNetwork` class has been moved to this directory. 4. DataHaven nodes common functions. 5. Kurtosis common functions. 6. Relayer common functions. 7. Kubernetes functions (although only used by `deploy`, it seemed fitting to have it here still). 8. Remove CLI questions and separator prints from `deploy-contracts.ts` and `set-datahaven-parameters.ts` scripts. These things should be in the `cli/launch` folder, which consumes the functions in these scripts. 9. Remove `setParametersFromCollection` from `utils` folder and put it in `cli`. 10. Create base snowbridge relayer configs for `local` and `stagenet` as two separate directories. ### Fixes 1. Sets the default time of the `deploy` command to 6s as Lodestar is slower than Lighthouse. 2. In `runShellCommandWithLogger` only print `stderr` if the command fails. ### Additional Minor Changes 1. K8s secret key names changed from `dh-beefy-relay-eth-key` to `dh-beefy-relay-ethereum-key` and `dh-beacon-relay-sub-key` to `dh-beacon-relay-substrate-key`, for simplicity in the deployment script. 11. Update suggested configs for `.vscode` configs. --------- Co-authored-by: Steve Degosserie <723552+stiiifff@users.noreply.github.com>
2025-06-12 08:24:03 +00:00
};