Skip to content

Commit

Permalink
chore: improve test logging
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Meier <[email protected]>
  • Loading branch information
n0izn0iz committed Jan 20, 2024
1 parent 51d92fc commit 682eed9
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 55 deletions.
48 changes: 28 additions & 20 deletions packages/scripts/integration-testing/cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import fs from "fs/promises";
import { Listr } from "listr2";
import { tmpdir } from "os";
import path from "path";
import util from "util";

import { cloneRepo } from "./git";
import { zodTryParseJSON } from "../../utils/sanitize";
import {
execPromise,
killProcess,
mustGetAttr,
replaceInFile,
sleep,
Expand All @@ -22,12 +22,17 @@ export const startCosmosLocalnet = async (
home?: string;
},
) => {
let height = opts?.height;
if (!height) {
height = BigInt(0);
}
console.log("🧱 Starting localnet at height " + height);
let home = opts?.home;
if (!home) {
home = await fs.mkdtemp(path.join(tmpdir(), "cosmos-home-"));
// run teritorid init --chain-id=testing testing --home=$HOME/.teritorid
const initCmd = `${binaryPath} init testing --chain-id testing --home ${home}`;
console.log("> " + initCmd);
console.log("⚙️ " + initCmd);
await execPromise(initCmd);

await replaceInFile(
Expand All @@ -38,51 +43,49 @@ export const startCosmosLocalnet = async (

// run teritorid keys add validator --keyring-backend=test --home=$HOME/.teritorid
const addValidatorCmd = `${binaryPath} keys add validator --keyring-backend=test --home ${home}`;
console.log("> " + addValidatorCmd);
console.log("⚙️ " + addValidatorCmd);
await execPromise(addValidatorCmd);
// run teritorid add-genesis-account $(teritorid keys show validator -a --keyring-backend=test --home=$HOME/.teritorid) 100000000000utori,100000000000stake --home=$HOME/.teritorid
const addGenesisAccountCmd = `${binaryPath} add-genesis-account $(${binaryPath} keys show validator -a --keyring-backend=test --home ${home}) 100000000000000000utori,100000000000000000stake --home ${home}`;
console.log("> " + addGenesisAccountCmd);
console.log("⚙️ " + addGenesisAccountCmd);
await execPromise(addGenesisAccountCmd);
// run teritorid keys add testnet-adm --keyring-backend=test --home=$HOME/.teritorid
const addTestnetAdmCmd = `${binaryPath} keys add testnet-adm --keyring-backend=test --home ${home}`;
console.log("> " + addTestnetAdmCmd);
console.log("⚙️ " + addTestnetAdmCmd);
await execPromise(addTestnetAdmCmd);
// run teritorid add-genesis-account $(teritorid keys show validator -a --keyring-backend=test --home=$HOME/.teritorid) 100000000000utori,100000000000stake --home=$HOME/.teritorid
const addTestnetAdmGenesisAccountCmd = `${binaryPath} add-genesis-account $(${binaryPath} keys show testnet-adm -a --keyring-backend=test --home ${home}) 100000000000000000utori,100000000000000000stake --home ${home}`;
console.log("> " + addTestnetAdmGenesisAccountCmd);
console.log("⚙️ " + addTestnetAdmGenesisAccountCmd);
await execPromise(addTestnetAdmGenesisAccountCmd);
// run teritorid gentx validator 500000000stake --keyring-backend=test --home=$HOME/.teritorid --chain-id=testing
const gentxCmd = `${binaryPath} gentx validator 500000000stake --keyring-backend=test --home ${home} --chain-id=testing`;
console.log("> " + gentxCmd);
console.log("⚙️ " + gentxCmd);
await execPromise(gentxCmd);
// run teritorid collect-gentxs --home=$HOME/.teritorid
const collectGentxsCmd = `${binaryPath} collect-gentxs --home ${home}`;
console.log("> " + collectGentxsCmd);
console.log("⚙️ " + collectGentxsCmd);
await execPromise(collectGentxsCmd);
}
const cmd = `${binaryPath} start --home ${home}`;
console.log("> " + cmd);
console.log("⚙️ " + cmd);

const result = util.promisify(child_process.exec)(cmd, {
const result = execPromise(cmd, {
encoding: "utf-8",
});
let height = opts?.height;
if (!height) {
height = BigInt(0);
}
await waitForHeight(26657, height + BigInt(1));
console.log("🧱 Started localnet at height " + height);
return {
home,
validatorWalletName: "validator",
result,
process: result.child,
kill: () => killProcess(result.child, result),
};
};

const waitForHeight = async (port: number, targetHeight: bigint) => {
const statusCmd = `curl -s http://localhost:${port}/status`;
console.log("> " + statusCmd);
console.log("⏳ Waiting for height " + targetHeight);
while (true) {
try {
const result = await execPromise(statusCmd);
Expand All @@ -97,6 +100,7 @@ const waitForHeight = async (port: number, targetHeight: bigint) => {
} catch {}
await sleep(1000);
}
console.log("Reached height " + targetHeight);
};

export const upgradeCosmosLocalnet = async (
Expand All @@ -105,9 +109,11 @@ export const upgradeCosmosLocalnet = async (
validatorWalletName: string,
home: string,
) => {
console.log("⬆️ Upgrading chain to " + newVersion);

// get height
const statusCmd = `curl -s http://localhost:26657/status`;
console.log("> " + statusCmd);
console.log("⚙️ " + statusCmd);
// eslint-disable-next-line no-restricted-syntax
const status = JSON.parse(
(await execPromise(statusCmd, { encoding: "utf-8" })).stdout,
Expand All @@ -122,7 +128,7 @@ export const upgradeCosmosLocalnet = async (
--chain-id=testing --home=$HOME/.teritorid --yes -b block --deposit="100000000stake"
*/
const cmd = `${oldBinaryPath} tx gov submit-proposal software-upgrade "${newVersion}" --upgrade-height=${height} --title="Upgrade to ${newVersion}" --description="Upgrade to ${newVersion}" --from=${validatorWalletName} --keyring-backend=test --chain-id=testing --home=${home} --yes -b block --deposit="500000000stake" --node http://127.0.0.1:26657 -o json`;
console.log("> " + cmd);
console.log("⚙️ " + cmd);
const { stdout: out } = await execPromise(cmd, { encoding: "utf-8" });
const outObj = zodTryParseJSON(zodTxResult, out);
if (!outObj) {
Expand All @@ -134,14 +140,14 @@ export const upgradeCosmosLocalnet = async (
--home $HOME/.teritorid -b block -y --keyring-backend test
*/
const voteCmd = `${oldBinaryPath} tx gov vote ${proposalId} yes --from ${validatorWalletName} --chain-id testing --home ${home} -b block -y --keyring-backend test --node http://127.0.0.1:26657`;
console.log("> " + voteCmd);
console.log("⚙️ " + voteCmd);
await execPromise(voteCmd);

await waitForHeight(26657, height);

// check proposal status
const proposalCmd = `${oldBinaryPath} query gov proposal ${proposalId} --chain-id testing --home ${home} -o json --node http://127.0.0.1:26657`;
console.log("> " + proposalCmd);
console.log("⚙️ " + proposalCmd);
const { stdout: proposalOut } = await execPromise(proposalCmd, {
encoding: "utf-8",
});
Expand All @@ -154,6 +160,8 @@ export const upgradeCosmosLocalnet = async (
throw new Error("Proposal not passed");
}

console.log("🆙 Upgraded chain to " + newVersion);

return height;
};

Expand All @@ -162,7 +170,7 @@ export const buildCosmos = async (
binaryName: string,
): Promise<string> => {
const cmd = `cd ${repoPath} && make build`;
// console.log("> " + cmd);
// console.log("⚙️ " + cmd);
await new Promise((resolve, reject) => {
child_process.exec(cmd, (err, stdout, stderr) => {
if (err) {
Expand Down
2 changes: 1 addition & 1 deletion packages/scripts/integration-testing/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const cloneRepo = async (
): Promise<string> => {
const repoPath = await fs.mkdtemp(path.join(tmpdir(), "git-repo-"));
const cmd = `git clone ${repoURL} ${repoPath} && cd ${repoPath} && git checkout ${ref}`;
// console.log("> " + cmd);
// console.log("⚙️ " + cmd);
await new Promise((resolve, reject) => {
child_process.exec(cmd, (err, stdout, stderr) => {
if (err) {
Expand Down
4 changes: 2 additions & 2 deletions packages/scripts/integration-testing/simpleTest204.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from "fs/promises";

import { buildBinaries, startCosmosLocalnet } from "./cosmos";
import { teritoriLocalnetNetwork } from "../../networks/teritori-localnet";
import { killProcess } from "../lib";
import { deployTeritoriEcosystem } from "../network-setup/deployLib";

const repoURL = "https://github.com/TERITORI/teritori-chain.git";
Expand All @@ -24,8 +25,7 @@ const main = async () => {
);

// stop
v204Process.kill();
await v204Result;
killProcess(v204Process, v204Result);

await fs.rm(home, { recursive: true, force: true });
};
Expand Down
23 changes: 9 additions & 14 deletions packages/scripts/integration-testing/upgradeTest120to204.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ const main = async () => {

const {
home,
result: v120Result,
process: v120Process,
validatorWalletName,
kill: killv120,
} = await startCosmosLocalnet(binaries["v1.2.0"]);

await deployTeritoriEcosystem(
Expand All @@ -40,8 +39,7 @@ const main = async () => {
validatorWalletName,
home,
);
v120Process.kill();
await v120Result;
await killv120();

upgradeHeight = await runUpgrade(
binaries["v1.3.0"],
Expand Down Expand Up @@ -73,11 +71,10 @@ const main = async () => {
`minimum-gas-prices = "0stake"`,
);

const { process: v204Process, result: v204Result } =
await startCosmosLocalnet(binaries["v2.0.4"], {
home,
height: upgradeHeight,
});
const { kill: killv204 } = await startCosmosLocalnet(binaries["v2.0.4"], {
home,
height: upgradeHeight,
});

// test cosmwasm
await deployTeritoriEcosystem(
Expand All @@ -86,8 +83,7 @@ const main = async () => {
"testnet-adm",
);

v204Process.kill();
await v204Result;
await killv204();

await fs.rm(home, { recursive: true, force: true });
};
Expand All @@ -99,7 +95,7 @@ const runUpgrade = async (
version: string,
validatorWalletName: string,
) => {
const { result, process } = await startCosmosLocalnet(binaryPath, {
const { kill } = await startCosmosLocalnet(binaryPath, {
home,
height,
});
Expand All @@ -109,8 +105,7 @@ const runUpgrade = async (
validatorWalletName,
home,
);
process.kill();
await result;
await kill();
return upgradeHeight;
};

Expand Down
20 changes: 7 additions & 13 deletions packages/scripts/integration-testing/upgradeTest142to204.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ const main = async () => {

const {
home,
result: v142Result,
process: v142Process,
kill: killv142,
validatorWalletName,
} = await startCosmosLocalnet(binaries["v1.4.2"]);

Expand All @@ -37,8 +36,7 @@ const main = async () => {
validatorWalletName,
home,
);
v142Process.kill();
await v142Result;
await killv142();

await replaceInFile(
path.join(home, "config/app.toml"),
Expand All @@ -47,13 +45,10 @@ const main = async () => {
);

// start next version
const { result, process: v204Process } = await startCosmosLocalnet(
binaries["v2.0.4"],
{
home,
height: upgradeHeight,
},
);
const { kill: killv204 } = await startCosmosLocalnet(binaries["v2.0.4"], {
home,
height: upgradeHeight,
});

// test cosmwasm
await deployTeritoriEcosystem(
Expand All @@ -63,8 +58,7 @@ const main = async () => {
);

// stop
v204Process.kill();
await result;
await killv204();

await fs.rm(home, { recursive: true, force: true });
};
Expand Down
23 changes: 22 additions & 1 deletion packages/scripts/lib.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NodeHttpTransport } from "@improbable-eng/grpc-web-node-http-transport";
import child_process from "child_process";
import child_process, { ChildProcess, PromiseWithChild } from "child_process";
import fs from "fs/promises";
import util from "util";
import { z } from "zod";
Expand Down Expand Up @@ -109,3 +109,24 @@ export const replaceInFile = async (
};

export const execPromise = util.promisify(child_process.exec);

export const killProcess = async (
p: ChildProcess,
r: PromiseWithChild<{
stdout: string;
stderr: string;
}>,
timeout?: number,
) => {
console.log("🔪 Killing process");
const innerKillProcess = async () => {
p.kill();
await r;
};
const startTimeout = async () => {
await sleep(timeout || 5000);
throw new Error("Timed out waiting for process to terminate");
};
await Promise.race([startTimeout(), innerKillProcess()]);
console.log("🔪 Process terminated");
};
8 changes: 5 additions & 3 deletions packages/scripts/network-setup/deployLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ const instantiateNameService = async (
} --node ${injectRPCPort(
network.rpcEndpoint,
)} --yes --keyring-backend test -o json --home ${opts.home}`;
console.log("> " + cmd);
console.log("⚙️ " + cmd);
let { stdout: out } = await retry(5, async () => {
return await execPromise(cmd, {
encoding: "utf-8",
Expand Down Expand Up @@ -224,7 +224,7 @@ const instantiateContract = async (
)} --yes --keyring-backend test -o json --label ${sqh(
label,
)} --admin ${admin} --home ${opts.home}`;
console.log("> " + cmd);
console.log("⚙️ " + cmd);
let { stdout: out } = await retry(
5,
async () =>
Expand Down Expand Up @@ -257,7 +257,7 @@ const storeWASM = async (
} --node ${injectRPCPort(
network.rpcEndpoint,
)} --yes --keyring-backend test -o json --home ${opts.home}`;
console.log("> " + cmd);
console.log("⚙️ " + cmd);
let { stdout: out } = await retry(5, async () => {
return await execPromise(cmd, {
encoding: "utf-8",
Expand Down Expand Up @@ -292,6 +292,7 @@ const getTx = async (networkId: string, txhash: string, timeout?: number) => {
await sleep(timeout || 20000);
return undefined;
};
console.log("⏳ Waiting for tx '" + txhash + "'");
const tx = await Promise.race([startTimeout(), innerGetTx()]);
if (!tx) {
throw new Error("Timed out waiting for tx '" + txhash + "'");
Expand All @@ -306,6 +307,7 @@ const getTx = async (networkId: string, txhash: string, timeout?: number) => {
JSON.stringify(tx, null, 2),
);
}
console.log("Tx '" + txhash + "' found");
return tx;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/scripts/network-setup/restoreContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const main = async () => {
} --node ${injectRPCPort(destinationNetwork.rpcEndpoint)} -o json${
keyringBackend ? ` --keyring-backend ${keyringBackend}` : ""
}`;
console.log("> " + cmd);
console.log("⚙️ " + cmd);
const out = child_process.execSync(cmd, {
stdio: ["inherit", "pipe", "inherit"],
encoding: "utf-8",
Expand Down

0 comments on commit 682eed9

Please sign in to comment.