Skip to content

Commit 8ee9c62

Browse files
krriscodepetermetz
authored andcommitted
test(test-plugin-ledger-connector-besu): jestify get-block-endpoint
Migration of get-block-endpoint.ts from tape-promise to Jest. Update tests for improved reliability and performance. ``` packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/ integration/plugin-validator-besu/get-block-endpoint.test.ts ``` Fixes: #3500 Co-authored-by: Peter Somogyvari <[email protected]> Signed-off-by: krriscode <[email protected]> Signed-off-by: Peter Somogyvari <[email protected]>
1 parent 2cd5413 commit 8ee9c62

File tree

5 files changed

+108
-263
lines changed

5 files changed

+108
-263
lines changed

.github/workflows/ci.yaml

+1-3
Original file line numberDiff line numberDiff line change
@@ -2048,9 +2048,7 @@ jobs:
20482048
JEST_TEST_RUNNER_DISABLED: false
20492049
JEST_TEST_COVERAGE_PATH: ./code-coverage-ts/ctp-ledger-connector-besu
20502050
JEST_TEST_CODE_COVERAGE_ENABLED: true
2051-
TAPE_TEST_PATTERN: >-
2052-
--files={./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-block-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-block-endpoint.test.ts}
2053-
TAPE_TEST_RUNNER_DISABLED: false
2051+
TAPE_TEST_RUNNER_DISABLED: true
20542052
runs-on: ubuntu-22.04
20552053
steps:
20562054
- name: Use Node.js ${{ env.NODEJS_VERSION }}

.taprc

-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ files:
2121
- ./packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/openapi/openapi-validation.test.ts
2222
- ./packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/openapi/openapi-validation-no-keychain.test.ts
2323
- ./packages/cactus-common/src/test/typescript/unit/logging/logger.test.ts
24-
- ./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-block-endpoint.test.ts
25-
- ./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-block-endpoint.test.ts
2624
- ./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-sign-transaction-endpoint.test.ts
2725
- ./packages/cactus-plugin-keychain-vault/src/test/typescript/integration/cactus-keychain-vault-server.test.ts
2826
- ./packages/cactus-plugin-keychain-vault/src/test/typescript/integration/plugin-keychain-vault.test.ts

jest.config.js

-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ module.exports = {
3434
`./packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/openapi/openapi-validation.test.ts`,
3535
`./packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/openapi/openapi-validation-no-keychain.test.ts`,
3636
`./packages/cactus-common/src/test/typescript/unit/logging/logger.test.ts`,
37-
`./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-block-endpoint.test.ts`,
38-
`./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-block-endpoint.test.ts`,
3937
`./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-sign-transaction-endpoint.test.ts`,
4038
`./packages/cactus-plugin-keychain-vault/src/test/typescript/integration/cactus-keychain-vault-server.test.ts`,
4139
`./packages/cactus-plugin-keychain-vault/src/test/typescript/integration/plugin-keychain-vault.test.ts`,
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import test, { Test } from "tape-promise/tape";
1+
import "jest-extended";
22

33
import { v4 as uuidv4 } from "uuid";
44
import { createServer } from "http";
@@ -33,117 +33,119 @@ import { PluginRegistry } from "@hyperledger/cactus-core";
3333

3434
import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory";
3535

36-
const testCase = "Test sign transaction endpoint";
37-
const logLevel: LogLevelDesc = "TRACE";
36+
const testCase = "Test get block endpoint";
37+
const logLevel: LogLevelDesc = "INFO";
3838

39-
test("BEFORE " + testCase, async (t: Test) => {
40-
const pruning = pruneDockerAllIfGithubAction({ logLevel });
41-
await t.doesNotReject(pruning, "Pruning didn't throw OK");
42-
t.end();
43-
});
44-
45-
test(testCase, async (t: Test) => {
46-
const keyEncoder: KeyEncoder = new KeyEncoder("secp256k1");
47-
const keychainId = uuidv4();
48-
const keychainRef = uuidv4();
49-
50-
const { privateKey } = Secp256k1Keys.generateKeyPairsBuffer();
51-
const keyHex = privateKey.toString("hex");
52-
const pem = keyEncoder.encodePrivate(keyHex, KeyFormat.Raw, KeyFormat.PEM);
53-
54-
const keychain = new PluginKeychainMemory({
55-
backend: new Map([[keychainRef, pem]]),
56-
keychainId,
57-
logLevel,
58-
instanceId: uuidv4(),
59-
});
39+
describe(testCase, () => {
40+
const besuTestLedger = new BesuTestLedger();
41+
let apiServer: ApiServer;
6042

61-
const httpServer1 = createServer();
62-
await new Promise((resolve, reject) => {
63-
httpServer1.once("error", reject);
64-
httpServer1.once("listening", resolve);
65-
httpServer1.listen(0, "127.0.0.1");
43+
beforeAll(async () => {
44+
const pruning = pruneDockerAllIfGithubAction({ logLevel });
45+
await expect(pruning).resolves.toBeTruthy();
46+
await besuTestLedger.start();
6647
});
67-
const addressInfo1 = httpServer1.address() as AddressInfo;
68-
t.comment(`HttpServer1 AddressInfo: ${JSON.stringify(addressInfo1)}`);
69-
const node1Host = `http://${addressInfo1.address}:${addressInfo1.port}`;
70-
t.comment(`Cactus Node 1 Host: ${node1Host}`);
71-
72-
const besuTestLedger = new BesuTestLedger();
73-
await besuTestLedger.start();
7448

75-
const tearDown = async () => {
49+
afterAll(async () => {
7650
await besuTestLedger.stop();
7751
await besuTestLedger.destroy();
78-
};
79-
80-
test.onFinish(tearDown);
81-
82-
const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost();
83-
const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost();
84-
85-
// 2. Instantiate plugin registry which will provide the web service plugin with the key value storage plugin
86-
const pluginRegistry = new PluginRegistry({ plugins: [keychain] });
87-
88-
// 3. Instantiate the web service consortium plugin
89-
const options: IPluginLedgerConnectorBesuOptions = {
90-
instanceId: uuidv4(),
91-
rpcApiHttpHost,
92-
rpcApiWsHost,
93-
pluginRegistry,
94-
logLevel,
95-
};
96-
const pluginValidatorBesu = new PluginLedgerConnectorBesu(options);
97-
98-
// 4. Create the API Server object that we embed in this test
99-
const configService = new ConfigService();
100-
const apiServerOptions = await configService.newExampleConfig();
101-
apiServerOptions.authorizationProtocol = AuthorizationProtocol.NONE;
102-
apiServerOptions.configFile = "";
103-
apiServerOptions.apiCorsDomainCsv = "*";
104-
apiServerOptions.apiPort = addressInfo1.port;
105-
apiServerOptions.cockpitPort = 0;
106-
apiServerOptions.grpcPort = 0;
107-
apiServerOptions.crpcPort = 0;
108-
apiServerOptions.apiTlsEnabled = false;
109-
const config = await configService.newExampleConfigConvict(apiServerOptions);
110-
111-
pluginRegistry.add(pluginValidatorBesu);
112-
113-
const apiServer = new ApiServer({
114-
httpServerApi: httpServer1,
115-
config: config.getProperties(),
116-
pluginRegistry,
52+
await apiServer.shutdown();
11753
});
11854

119-
// 5. make sure the API server is shut down when the testing if finished.
120-
test.onFinish(() => apiServer.shutdown());
121-
122-
// 6. Start the API server which is now listening on port A and it's healthcheck works through the main SDK
123-
await apiServer.start();
124-
125-
// 7. Instantiate the main SDK dynamically with whatever port the API server ended up bound to (port 0)
126-
t.comment(`AddressInfo: ${JSON.stringify(addressInfo1)}`);
127-
128-
const request: GetBlockV1Request = {
129-
blockHashOrBlockNumber: 0,
130-
};
131-
132-
const configuration = new BesuApiClientOptions({ basePath: node1Host });
133-
const api = new BesuApiClient(configuration);
134-
135-
// Test for 200 valid response test case
136-
const res = await api.getBlockV1(request);
137-
138-
const { status, data } = res;
139-
t.true(status >= 200, "status GTE 200 OK");
140-
t.true(status < 300, "status LT 300 OK");
141-
t.ok(data, "GetBlockResponse Truthy OK");
142-
t.true(typeof data.block === "object", "Response data is OK");
143-
});
55+
afterAll(async () => {
56+
const pruning = pruneDockerAllIfGithubAction({ logLevel });
57+
await expect(pruning).resolves.toBeTruthy();
58+
});
14459

145-
test("AFTER " + testCase, async (t: Test) => {
146-
const pruning = pruneDockerAllIfGithubAction({ logLevel });
147-
await t.doesNotReject(pruning, "Pruning didn't throw OK");
148-
t.end();
60+
test(testCase, async () => {
61+
const keyEncoder: KeyEncoder = new KeyEncoder("secp256k1");
62+
const keychainId = uuidv4();
63+
const keychainRef = uuidv4();
64+
65+
const { privateKey } = Secp256k1Keys.generateKeyPairsBuffer();
66+
const keyHex = privateKey.toString("hex");
67+
const pem = keyEncoder.encodePrivate(keyHex, KeyFormat.Raw, KeyFormat.PEM);
68+
69+
const keychain = new PluginKeychainMemory({
70+
backend: new Map([[keychainRef, pem]]),
71+
keychainId,
72+
logLevel,
73+
instanceId: uuidv4(),
74+
});
75+
76+
const httpServer1 = createServer();
77+
await new Promise((resolve, reject) => {
78+
httpServer1.once("error", reject);
79+
httpServer1.once("listening", resolve);
80+
httpServer1.listen(0, "127.0.0.1");
81+
});
82+
const addressInfo1 = httpServer1.address() as AddressInfo;
83+
console.log(`HttpServer1 AddressInfo: ${JSON.stringify(addressInfo1)}`);
84+
85+
const node1Host = `http://${addressInfo1.address}:${addressInfo1.port}`;
86+
console.log(`Cactus Node 1 Host: ${node1Host}`);
87+
88+
const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost();
89+
const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost();
90+
91+
// 2. Instantiate plugin registry which will provide the web service plugin with the key value storage plugin
92+
const pluginRegistry = new PluginRegistry({ plugins: [keychain] });
93+
94+
// 3. Instantiate the web service consortium plugin
95+
const options: IPluginLedgerConnectorBesuOptions = {
96+
instanceId: uuidv4(),
97+
rpcApiHttpHost,
98+
rpcApiWsHost,
99+
pluginRegistry,
100+
logLevel,
101+
};
102+
const pluginValidatorBesu = new PluginLedgerConnectorBesu(options);
103+
104+
// 4. Create the API Server object that we embed in this test
105+
const configService = new ConfigService();
106+
const apiServerOptions = await configService.newExampleConfig();
107+
apiServerOptions.authorizationProtocol = AuthorizationProtocol.NONE;
108+
apiServerOptions.configFile = "";
109+
apiServerOptions.apiCorsDomainCsv = "*";
110+
apiServerOptions.apiPort = addressInfo1.port;
111+
apiServerOptions.cockpitPort = 0;
112+
apiServerOptions.grpcPort = 0;
113+
apiServerOptions.crpcPort = 0;
114+
apiServerOptions.apiTlsEnabled = false;
115+
const config =
116+
await configService.newExampleConfigConvict(apiServerOptions);
117+
118+
pluginRegistry.add(pluginValidatorBesu);
119+
120+
apiServer = new ApiServer({
121+
httpServerApi: httpServer1,
122+
config: config.getProperties(),
123+
pluginRegistry,
124+
});
125+
126+
// 5. make sure the API server is shut down when the testing if finished.
127+
128+
// 6. Start the API server which is now listening on port A and it's healthcheck works through the main SDK
129+
await apiServer.start();
130+
131+
// 7. Instantiate the main SDK dynamically with whatever port the API server ended up bound to (port 0)
132+
console.log(`AddressInfo: ${JSON.stringify(addressInfo1)}`);
133+
134+
const request: GetBlockV1Request = {
135+
blockHashOrBlockNumber: 0,
136+
};
137+
138+
const configuration = new BesuApiClientOptions({ basePath: node1Host });
139+
const api = new BesuApiClient(configuration);
140+
141+
// Test for 200 valid response test case
142+
const res = await api.getBlockV1(request);
143+
144+
const { status, data } = res;
145+
146+
expect(status).toBeGreaterThanOrEqual(200);
147+
expect(status).toBeLessThan(300);
148+
expect(data).toBeTruthy();
149+
expect(data.block).toBeObject();
150+
});
149151
});

0 commit comments

Comments
 (0)