Skip to content

Commit

Permalink
Merge pull request #314 from dappnode/pablo/switch-stakers-test
Browse files Browse the repository at this point in the history
Check network for staker packages tests
  • Loading branch information
pablomendezroyo authored Apr 20, 2023
2 parents bbb5813 + 8cda431 commit d2ae252
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ import chalk from "chalk";
import { DappmanagerTestApi } from "./dappmanagerTestApi.js";
import {
nonStakerPackagesSetup,
stakerGnosisConfig,
stakerMainnetConfig,
stakerPraterConfig,
getStakerConfigByNetwork,
packagesToKeep
} from "./params.js";
import { IpfsClientTarget } from "./types.js";
import { IpfsClientTarget, Network } from "./types.js";
import got from "got";

/**
* Ensure that the DAppNode environment is ready to run the integration tests
*/
export async function ensureDappnodeEnvironment({
dappmanagerTestApi
dappmanagerTestApi,
network
}: {
dappmanagerTestApi: DappmanagerTestApi;
network?: Network;
}): Promise<void> {
// Check the Bind container IP address is in the /etc/resolv.conf file
await ensureDockerAliasesResolveFromHost();
// Check dappmanager is running
await dappmanagerTestApi.healthCheck();
// Make sure extra pkgs are removed
await ensureOnlyDefaultPkgsInstalled(dappmanagerTestApi);
await ensureOnlyDefaultPkgsInstalled(dappmanagerTestApi, network);
// Ensure that the Staker configurations are persisted
await persistStakerConfigs(dappmanagerTestApi);
if (network) await persistStakerConfigs(dappmanagerTestApi, network);
// Ensure that the Staker packages are installed
await ensureNonStakerPkgsAreInstalled(dappmanagerTestApi);
// Ensure IPFS is running and IPFS repository is in local mode
Expand All @@ -50,23 +50,33 @@ async function ensureDockerAliasesResolveFromHost(): Promise<void> {
* Ensure that the Staker configurations are persisted
*/
async function persistStakerConfigs(
dappmanagerTestApi: DappmanagerTestApi
dappmanagerTestApi: DappmanagerTestApi,
network: Network
): Promise<void> {
//await dappmanagerTestApi.stakerConfigSet(stakerMainnetConfig);
//await dappmanagerTestApi.stakerConfigSet(stakerGnosisConfig);
await dappmanagerTestApi.stakerConfigSet(stakerPraterConfig);
const stakerConfig = getStakerConfigByNetwork(network);
if (network === "prater") {
console.log("persisting prater staker configuration");
await dappmanagerTestApi.stakerConfigSet(stakerConfig);
} else if (network === "mainnet") {
console.log("persisting mainnet staker configuration");
await dappmanagerTestApi.stakerConfigSet(stakerConfig);
} else if (network === "gnosis") {
console.log("persisting gnosis staker configuration");
await dappmanagerTestApi.stakerConfigSet(stakerConfig);
}
}

/**
* Ensure only required packages are installed (Staker configs from prater mainnet and gnosis)
*/
async function ensureOnlyDefaultPkgsInstalled(
dappmanagerTestApi: DappmanagerTestApi
dappmanagerTestApi: DappmanagerTestApi,
network?: Network
): Promise<void> {
const installedPackages = await dappmanagerTestApi.packagesGet();

for (const installedPackage of installedPackages) {
if (!packagesToKeep.includes(installedPackage.dnpName)) {
if (!packagesToKeep(network).includes(installedPackage.dnpName)) {
console.log(
chalk.dim(
` - Removing package ${installedPackage.dnpName} from the DAppNode environment`
Expand Down
122 changes: 92 additions & 30 deletions src/commands/githubActions/endToEndTest/executeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@ import { Manifest } from "../../../types.js";
import { DappmanagerTestApi } from "./dappmanagerTestApi.js";
import Docker from "dockerode";
import chalk from "chalk";
import { getStakerConfigByNetwork } from "./params.js";
import { getIsStakerPkg } from "./utils.js";
import {
stakerPkgsToKeep,
stakerGnosisConfig,
stakerMainnetConfig,
stakerPraterConfig
} from "./params.js";
import { getStakerPkgNetwork } from "./utils.js";
ConsensusClientGnosis,
ConsensusClientMainnet,
ConsensusClientPrater,
ExecutionClientGnosis,
ExecutionClientMainnet,
ExecutionClientPrater,
Network,
consensusClientsGnosis,
consensusClientsMainnet,
consensusClientsPrater,
executionClientsGnosis,
executionClientsMainnet,
executionClientsPrater
} from "./types.js";

/**
* Execute the tests for the integration test workflow. These tests require
Expand All @@ -28,7 +38,8 @@ export async function executePackageInstallAndUpdateTest({
compose,
errorLogsTimeout,
healthCheckUrl,
environmentByService
environmentByService,
network
}: {
dappmanagerTestApi: DappmanagerTestApi;
releaseMultiHash: string;
Expand All @@ -37,26 +48,25 @@ export async function executePackageInstallAndUpdateTest({
errorLogsTimeout: number;
healthCheckUrl?: string;
environmentByService?: Record<string, string>;
network?: Network;
}): Promise<void> {
// Test Install package from scratch
console.log(chalk.dim("\nTEST: Installing pkg from scratch..."));
const { name } = manifest;
const isStakerPkg = getIsStakerPkg(name);

// TEST: Install package from scratch
console.log(chalk.dim("\nTEST: Installing pkg from scratch"));
// Remove package, if not found it will throw an error but it's ok
await dappmanagerTestApi
.packageInstall({
dnpName: manifest.name,
version: releaseMultiHash,
userSettings: { environment: environmentByService || {} }
})
.then(() => {
// If its a staker pkg then the stakerConfigSet must be called
if (stakerPkgsToKeep.includes(manifest.name)) {
const network = getStakerPkgNetwork(manifest.name);
network === "mainnet"
? dappmanagerTestApi.stakerConfigSet(stakerMainnetConfig)
: network === "gnosis"
? dappmanagerTestApi.stakerConfigSet(stakerGnosisConfig)
: dappmanagerTestApi.stakerConfigSet(stakerPraterConfig);
}
});
.packageRemove({ dnpName: name })
.catch(() => console.log("Package already removed"));
await dappmanagerTestApi.packageInstall({
dnpName: name,
version: releaseMultiHash,
userSettings: { environment: environmentByService || {} }
});
// Set staker config if staker package
if (isStakerPkg && network)
await setStakerConfig(name, dappmanagerTestApi, network);
await executeTestCheckers({
dnpName: manifest.name,
compose,
Expand All @@ -67,21 +77,23 @@ export async function executePackageInstallAndUpdateTest({
// Skip update test if running in test environment, dappnodesdk package name is not published
if (process.env.ENVIRONMENT === "TEST") return;

// Remove package
await dappmanagerTestApi.packageRemove({ dnpName: manifest.name });

// Update package to the given hash
console.log(chalk.dim("\nTEST: Install production pkg and update"));
// Remove package, if not found it will throw an error but it's ok
await dappmanagerTestApi.packageRemove({ dnpName: manifest.name });
await dappmanagerTestApi.packageInstall({
dnpName: manifest.name,
version: "latest", // Install production version
userSettings: { environment: environmentByService }
});
await dappmanagerTestApi.packageInstall({
dnpName: manifest.name,
version: releaseMultiHash,
version: releaseMultiHash, // Install test version
userSettings: { environment: environmentByService }
});
// Set staker config if staker package
if (isStakerPkg && network)
await setStakerConfig(name, dappmanagerTestApi, network);
await executeTestCheckers({
dnpName: manifest.name,
compose,
Expand All @@ -94,12 +106,14 @@ async function executeTestCheckers({
dnpName,
compose,
errorLogsTimeout,
healthCheckUrl
healthCheckUrl,
network
}: {
dnpName: string;
compose: Compose;
errorLogsTimeout: number;
healthCheckUrl?: string;
network?: Network;
}): Promise<void> {
const docker = new Docker();
for (const service of Object.keys(compose.services)) {
Expand All @@ -119,6 +133,10 @@ async function executeTestCheckers({
await ensureHealthCheck(healthCheckUrl).then(() =>
console.log(chalk.green(` ✓ Healthcheck endpoint returned 200`))
);
if (network)
await attestanceProof(network).then(() =>
console.log(chalk.green(` ✓ Attestation proof`))
);
}

async function ensureContainerStatus(
Expand Down Expand Up @@ -176,3 +194,47 @@ async function ensureNoErrorLogs(
throw Error(errorMessage);
}
}

/**
* Test that the validators are attesting after a peri
*/
async function attestanceProof(network: Network): Promise<void> {
if (network === "mainnet") return;
// TODO
return;
}

async function setStakerConfig(
dnpName: string,
dappmanagerTestApi: DappmanagerTestApi,
network: Network
): Promise<void> {
const stakerConfig = getStakerConfigByNetwork(network);
switch (network) {
case "mainnet":
if (executionClientsMainnet.includes(dnpName as any))
stakerConfig.executionClient.dnpName = dnpName as ExecutionClientMainnet;
else if (consensusClientsMainnet.includes(dnpName as any))
stakerConfig.consensusClient.dnpName = dnpName as ConsensusClientMainnet;

break;
case "gnosis":
if (executionClientsGnosis.includes(dnpName as any))
stakerConfig.executionClient.dnpName = dnpName as ExecutionClientGnosis;
else if (consensusClientsGnosis.includes(dnpName as any))
stakerConfig.consensusClient.dnpName = dnpName as ConsensusClientGnosis;
await dappmanagerTestApi.stakerConfigSet(stakerConfig);
break;
case "prater":
if (executionClientsPrater.includes(dnpName as any))
stakerConfig.executionClient.dnpName = dnpName as ExecutionClientPrater;
else if (consensusClientsPrater.includes(dnpName as any))
stakerConfig.consensusClient.dnpName = dnpName as ConsensusClientPrater;
await dappmanagerTestApi.stakerConfigSet(stakerConfig);
break;

default:
throw Error("unknown network");
}
await dappmanagerTestApi.stakerConfigSet(stakerConfig);
}
25 changes: 22 additions & 3 deletions src/commands/githubActions/endToEndTest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import { executePackageInstallAndUpdateTest } from "./executeTests.js";
import { DappmanagerTestApi } from "./dappmanagerTestApi.js";
import { localDappmanagerTestApiUrl, localIpfsApiUrl } from "./params.js";
import chalk from "chalk";
import { Network } from "./types.js";

interface CliCommandOptions extends CliGlobalOptions {
healthCheckUrl?: string;
errorLogsTimeout: number;
environmentByService?: string;
network?: string;
}

export const endToEndTest: CommandModule<
Expand All @@ -32,6 +34,11 @@ export const endToEndTest: CommandModule<
type: "number",
default: 30
},
network: {
describe:
"Network to use for the test if any. Available values are mainnet, prater, gnosis",
type: "string"
},
environmentByService: {
describe:
"Environments by service to install the package with. JSON format",
Expand All @@ -47,8 +54,18 @@ export async function gaTestEndToEndHandler({
dir,
healthCheckUrl,
errorLogsTimeout,
environmentByService
environmentByService,
network
}: CliCommandOptions): Promise<void> {
if (
network &&
network !== "mainnet" &&
network !== "prater" &&
network !== "gnosis"
)
throw Error(
`Invalid network ${network}. Available values are mainnet, prater, gnosis`
);
const dappmanagerTestApi = new DappmanagerTestApi(localDappmanagerTestApiUrl);
const compose = readCompose({ dir });
const { manifest } = readManifest({ dir });
Expand All @@ -72,7 +89,8 @@ export async function gaTestEndToEndHandler({
chalk.dim("\nCleaning test-integration environment before starting")
);
await ensureDappnodeEnvironment({
dappmanagerTestApi
dappmanagerTestApi,
network: network as Network
});

await executePackageInstallAndUpdateTest({
Expand All @@ -82,7 +100,8 @@ export async function gaTestEndToEndHandler({
compose,
healthCheckUrl,
errorLogsTimeout,
environmentByService: environmentByServiceParsed
environmentByService: environmentByServiceParsed,
network: network as Network
});
} catch (e) {
throw Error(`Error on test-integration: ${e}`);
Expand Down
Loading

0 comments on commit d2ae252

Please sign in to comment.