Skip to content

Commit

Permalink
chore(CI): improve the stability of e2e_test workflow (#1572)
Browse files Browse the repository at this point in the history
* refactor: remove useless code in tests/e2e/config

* chore(CI): refactor `e2e_test` workflow

* chore(CI): rm `sleep 10` in workflows/axon-start-test

* Update .github/workflows/e2e_test.yml
  • Loading branch information
Flouse authored Nov 17, 2023
1 parent d7e50cc commit 386fa3a
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 207 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/axon-start-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ jobs:
--config devtools/chain/config.toml \
| tee -a ${{ env.LOG_FILE }} &
sleep 10
npx zx <<'EOF'
import { waitXBlocksPassed } from './devtools/ci/scripts/helper.js'
await retry(3, '6s', () => waitXBlocksPassed('http://127.0.0.1:8000', 2))
Expand Down
40 changes: 32 additions & 8 deletions .github/workflows/e2e_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,29 @@ jobs:
restore-keys: |
${{ matrix.os }}-${{ runner.os }}-${{ runner.arch }}-cargo-build
- uses: lyricwulf/abc@v1
with:
# https://www.gnu.org/software/m4/
linux: m4
- name: Start a single Axon node
env:
LOG_FILE: ${{ runner.temp }}/${{ matrix.os }}-single-axon-node.log
run: |
target/debug/axon --version | tee ${{ env.LOG_FILE }}
target/debug/axon init \
--config devtools/chain/config.toml \
--chain-spec devtools/chain/specs/single_node/chain-spec.toml \
| tee -a ${{ env.LOG_FILE }}
target/debug/axon run \
--config devtools/chain/config.toml \
| tee -a ${{ env.LOG_FILE }} &
npx zx <<'EOF'
import { waitXBlocksPassed } from './devtools/ci/scripts/helper.js'
await retry(3, '6s', () => waitXBlocksPassed('http://127.0.0.1:8000', 2))
EOF
timeout-minutes: 1

# TODO: use Node.js 18
- name: Use Node.js 16
uses: actions/setup-node@v4
with:
node-version: "16"
node-version: 16
- name: Get yarn cache directory
id: yarn-cache-dir
run: echo "dir=$(yarn cache dir)" >> ${GITHUB_OUTPUT}
Expand All @@ -67,6 +80,17 @@ jobs:
${{ runner.os }}-node_modules-
- name: E2E Tests Linting in tests/e2e
run: make e2e-test-lint
working-directory: tests/e2e
run: yarn && yarn lint

- name: Serve files in tests/e2e/src by http-server
working-directory: tests/e2e
run: echo '\n' | yarn http-server

- name: E2E Tests in tests/e2e
run: make e2e-test-ci
working-directory: tests/e2e
run: |
npx zx <<'EOF'
await retry(3, expBackoff(), () => $`HEADLESS=new yarn test`)
EOF
timeout-minutes: 6
25 changes: 0 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,6 @@ info:
pwd
env

e2e-test-lint:
cd tests/e2e && yarn && yarn lint

e2e-test:
cargo build
rm -rf ./devtools/chain/data
./target/debug/axon init \
--config devtools/chain/config.toml \
--chain-spec devtools/chain/specs/single_node/chain-spec.toml \
> /tmp/log 2>&1
./target/debug/axon run \
--config devtools/chain/config.toml \
>> /tmp/log 2>&1 &
cd tests/e2e && yarn
cd tests/e2e/src && yarn exec http-server &
cd tests/e2e && yarn exec wait-on -t 5000 tcp:8000 && yarn exec wait-on -t 5000 tcp:8080 && HEADLESS="$(HEADLESS)" yarn test
pkill -2 axon
pkill -2 http-server

e2e-test-ci: HEADLESS=true
e2e-test-ci: e2e-test

e2e-test-via-docker:
docker-compose -f tests/e2e/docker-compose-e2e-test.yaml up --exit-code-from e2e-test --force-recreate

# For counting lines of code
stats:
@cargo count --version || cargo +nightly install --git https://github.com/kbknapp/cargo-count
Expand Down
72 changes: 5 additions & 67 deletions tests/e2e/config.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,18 @@
import configSetting from "./config.json";

function makeRequest(method, url) {
return new Promise((resolve, reject) => {
const XMLHttpRequest = require("xhr2");// eslint-disable-line global-require
const xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader("Content-type", "application/json");
xhr.send(JSON.stringify(
{
id: 1,
jsonrpc: "2.0",
method: "eth_chainId",
params: [

],
},
));
xhr.onload = function load_() {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject(new Error("makeRequest fail"));
}
};
xhr.onerror = function error_() {
// reject({
// status: this.status,
// statusText: xhr.statusText,
// });
reject(new Error("makeRequest fail"));
};
// xhr.send();
});
}

async function doAjaxThings() {
const result = await makeRequest("POST", configSetting.axonRpc.url);

return new Promise((resolve) => {
resolve(parseInt(JSON.parse(result).result, 16));
});
}

export default class Config {
constructor() {
this.axonRpc = { url: "", netWorkName: "", chainId: "" };
this.acount1 = "";
this.acount2 = "";
this.httpServer = "";
this.hexPrivateKey = "";
try {
// eslint-disable-next-line no-console
console.log(configSetting.axonRpc);
this.axonRpc = configSetting.axonRpc;
this.httpServer = configSetting.httpServer;
this.hexPrivateKey = configSetting.hexPrivateKey;
this.acount1 = configSetting.acount1;
this.acount2 = configSetting.acount2;
} catch (err) {
// eslint-disable-next-line no-console
console.log(err);
throw err;
}
}

async initialize() {
this.axonRpc.chainId = await doAjaxThings();
this.axonRpc = configSetting.axonRpc;
this.httpServer = configSetting.httpServer;
this.hexPrivateKey = configSetting.hexPrivateKey;
this.account1 = configSetting.account1;
this.account2 = configSetting.account2;
}

static getIns() {
if (!Config.ins) {
Config.ins = new Config();
}
(async function init_() {
await Config.ins.initialize();
}());
return Config.ins;
}
}
6 changes: 3 additions & 3 deletions tests/e2e/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"axonRpc": {
"netWorkName": "axon",
"url": "http://localhost:8000",
"chainId": 0
"chainId": 1098411886
},
"hexPrivateKey": "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
"acount1": "0x8ab0CF264DF99D83525e9E11c7e4db01558AE1b1",
"acount2": "0xF386573563C3a75dBbd269FCe9782620826dDAc2"
"account1": "0x8ab0CF264DF99D83525e9E11c7e4db01558AE1b1",
"account2": "0xF386573563C3a75dBbd269FCe9782620826dDAc2"
}
32 changes: 23 additions & 9 deletions tests/e2e/jest/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,49 @@ import createTransactionData from "../src/create_test_data/createTestDataManage"
export const MetaMaskOptions = {
metaMaskVersion: RECOMMENDED_METAMASK_VERSION,
automation: "puppeteer",
headless: process.env.HEADLESS ? true : false,
// https://developer.chrome.com/articles/new-headless/
headless: process.env.HEADLESS ? 'new' : false,
metaMaskFlask: false,
args: [
process.env.WSL ? "-no-sandbox" : "",
]
};

export default async function setup() {
console.log('Setup Start...');

const { metaMask, browser } = await bootstrap(MetaMaskOptions);
let hostPage;
try {
await createTransactionData.resetTestTmpFiles();
await createTransactionData.createTransactionData(); // create test data

process.env.PUPPETEER_WS_ENDPOINT = browser.wsEndpoint();
global.browser = browser;
global.metamask = metaMask;

console.log(`browser.newPage()`);
hostPage = await browser.newPage().catch(err => {
console.error("browser.newPage() failed");
throw err;
});
} catch (error) {
// eslint-disable-next-line no-console
console.log(error);
throw error;
}

process.env.PUPPETEER_WS_ENDPOINT = browser.wsEndpoint();
global.browser = browser;
global.metamask = metaMask;

const hostPage = await browser.newPage();
await Config.getIns().initialize();
console.log(`goto httpServer: ${Config.getIns().httpServer}`);
await hostPage.goto(Config.getIns().httpServer);

const configParams = {
networkName: Config.getIns().axonRpc.netWorkName,
rpc: Config.getIns().axonRpc.url,
rpc: process.env.AXON_RPC_URL || Config.getIns().axonRpc.url,
chainId: "0x" + Config.getIns().axonRpc.chainId.toString(16),
symbol: "AXON"
}
console.log("MetaMask configs:", configParams);

// add custom network to a MetaMask
await hostPage.evaluate((cfparams) => {
window.ethereum.request({
Expand All @@ -58,7 +70,9 @@ export default async function setup() {
}, configParams);
await metaMask.acceptAddNetwork(false);
await metaMask.switchNetwork("Axon");

await hostPage.bringToFront();
global.page = hostPage.page;

console.log('Setup End.');
}
8 changes: 5 additions & 3 deletions tests/e2e/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"name": "axon-e2e-tests",
"scripts": {
"test": "jest -i",
"http-server": "cd src && http-server &",
"test": "jest --runInBand",
"test-single": "jest",
"posttest": "pkill -2 http-server || echo no http-server running",
"lint": "eslint src/*.js ./*.js"
},
"license": "MIT",
"dependencies": {
"@chainsafe/dappeteer": "^5.2.0",
"@ethereumjs/common": "^3.1.1",
Expand All @@ -20,8 +23,7 @@
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-sonarjs": "^0.19.0",
"http-server": "^14.1.1",
"jest": "^29.5.0",
"wait-on": "^7.0.1"
"jest": "^29.5.0"
},
"jest": {
"preset": "@chainsafe/dappeteer",
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/src/create_test_data/createTestDataManage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Web3 from "web3";
import fs from "fs";
import Web3 from "web3";
import Config from "../../config";
import erc20 from "./ERC20.json";

Expand Down Expand Up @@ -116,7 +116,7 @@ const createTestDataMange = {
},

async sendRawTestTx() {
const toAddress = Config.getIns().acount2;
const toAddress = Config.getIns().account2;
const nonce = (await web3.eth.getTransactionCount(accountFrom.address)) + 1;
const txObject = {
nonce: web3.utils.toHex(nonce),
Expand Down
Loading

0 comments on commit 386fa3a

Please sign in to comment.