Skip to content

Commit

Permalink
test(connector-fabric): combine 3 tests into connector-fabric-baseline
Browse files Browse the repository at this point in the history
1. This is making the test case harder to read but shaves off easily
10 to 15 minutes from one of our slowest CI jobs which can take up to an
hour to run when the GitHub runners are feeling lazy.
2. That above is my only justification for it. The test cases I'm consolidating
are relatively stable at this point (took us years to get here but now they
are passing with a high ratio and the false negatives have pretty much
disappeared).
3. We are downloading and launching the fabirc AIO ledger 10+ times which
is very resource intensive and this could help make a dent in it.

Running this test right now looks like this on my machine:

 PASS  packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/
 integration/fabric-v2-2-x/connector-fabric-baseline.test.ts (277.062 s, 638 MB heap size)

  PluginLedgerConnectorFabric
    ✓ getBlockV1() -Get first block by it's number - decoded. (1216 ms)
    ✓ getBlockV1() - Get first block by it's number - encoded. (1084 ms)
    ✓ getBlockV1() - Get a block by transactionId it contains (4534 ms)
    ✓ getBlockV1() - Get a block by transactionId it contains - cacti transactions (4535 ms)
    ✓ getBlockV1() - Get a block by transactionId it contains - cacti full block (4559 ms)
    ✓ getBlockV1() - Get block by it's hash. (6727 ms)
    ✓ getBlockV1() - Reading block with invalid number returns an error. (1 ms)
    ✓ GetChainInfoV1() - Get test ledger chain info. (2134 ms)
    ✓ deployContractV1() - deploys Fabric 2.x contract from go source (38351 ms)
    ✓ deployContractV1() - deploys contract and performs transactions (40840 ms)

Test Suites: 1 passed, 1 total
Tests:       10 passed, 10 total
Snapshots:   0 total
Time:        277.117 s

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
  • Loading branch information
petermetz committed Sep 16, 2024
1 parent 4de8069 commit 43209df
Show file tree
Hide file tree
Showing 8 changed files with 996 additions and 1,232 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
examples/cactus-example-carbon-accounting-frontend/www/
examples/cactus-example-supply-chain-frontend/www/
weaver/core/identity-management/iin-agent/out/
weaver/common/protos-js/build/

**/src/main/typescript/generated/proto/**
**/src/main/typescript/generated/wasm-pack/**
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1458,14 +1458,13 @@ jobs:
continue-on-error: false
env:
CACTI_NPM_PACKAGE_NAME: "@hyperledger/cactus-plugin-ledger-connector-fabric"
HFC_LOGGING: '{"debug":"console","info":"console","warn": "console","error":"console"}'
HFC_LOGGING: ''
FULL_BUILD_DISABLED: true
FREE_UP_GITHUB_RUNNER_DISK_SPACE_DISABLED: false
JEST_TEST_PATTERN: packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/(unit|integration|benchmark)/.*/*.test.ts
JEST_TEST_RUNNER_DISABLED: false
JEST_TEST_COVERAGE_PATH: ./code-coverage-ts/plc-fabric-0
JEST_TEST_CODE_COVERAGE_ENABLED: true
TAPE_TEST_PATTERN: ""
TAPE_TEST_RUNNER_DISABLED: true
runs-on: ubuntu-22.04
steps:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { Logger } from "@hyperledger/cactus-common";
import {
GatewayOptions,
GetBlockRequestV1Query,
GetBlockResponseTypeV1,
DefaultApi as FabricApi,
} from "../../../main/typescript/generated/openapi/typescript-axios/api";

/**
* Run get block endpoint using a query, do basic response checks.
* Can be reused throughout the tests.
*
* @param query how to find requested block
* @param responseType response type requested
*
* @returns block object / block buffer
*/
export async function getBlock(opts: {
readonly query: GetBlockRequestV1Query;
readonly responseType?: GetBlockResponseTypeV1;
readonly gatewayOptions: GatewayOptions;
readonly log: Logger;
readonly apiClient: FabricApi;
readonly ledgerChannelName: string;
}): Promise<any> {
const {
responseType = GetBlockResponseTypeV1.Full,
ledgerChannelName,
gatewayOptions,
query,
log,
apiClient,
} = opts;

const getBlockReq = {
channelName: ledgerChannelName,
gatewayOptions,
query,
responseType,
};

const getBlockResponse = await apiClient.getBlockV1(getBlockReq);
log.debug(
"getBlockResponse = ",
getBlockResponse.status,
getBlockResponse.data,
);

expect(getBlockResponse).toBeTruthy();
expect(getBlockResponse.status).toEqual(200);
expect(getBlockResponse.data).toBeTruthy();

switch (responseType) {
case GetBlockResponseTypeV1.Full:
if (!("decodedBlock" in getBlockResponse.data)) {
throw new Error(
`Wrong response received - expected decoded, got: ${getBlockResponse.data}`,
);
}
expect(getBlockResponse.data.decodedBlock).toBeTruthy();
return getBlockResponse.data.decodedBlock;
case GetBlockResponseTypeV1.Encoded:
if (!("encodedBlock" in getBlockResponse.data)) {
throw new Error(
`Wrong response received - expected encoded, got: ${getBlockResponse.data}`,
);
}
expect(getBlockResponse.data.encodedBlock).toBeTruthy();
return getBlockResponse.data.encodedBlock;
case GetBlockResponseTypeV1.CactiTransactions:
if (!("cactiTransactionsEvents" in getBlockResponse.data)) {
throw new Error(
`Wrong response received - expected CactiTransactions, got: ${getBlockResponse.data}`,
);
}
expect(getBlockResponse.data.cactiTransactionsEvents).toBeTruthy();
return getBlockResponse.data.cactiTransactionsEvents;
case GetBlockResponseTypeV1.CactiFullBlock:
if (!("cactiFullEvents" in getBlockResponse.data)) {
throw new Error(
`Wrong response received - expected CactiFullBlock, got: ${getBlockResponse.data}`,
);
}
expect(getBlockResponse.data.cactiFullEvents).toBeTruthy();
return getBlockResponse.data.cactiFullEvents;
default:
// Will not compile if any type was not handled by above switch.
const unknownType: never = responseType;
const validTypes = Object.keys(GetBlockResponseTypeV1).join(";");
const errorMessage = `Unknown get block response type '${unknownType}'. Accepted types for GetBlockResponseTypeV1 are: [${validTypes}]`;
throw new Error(errorMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Logger } from "@hyperledger/cactus-common";

import { FabricSigningCredential } from "../../../main/typescript/generated/openapi/typescript-axios/api";
import { FabricContractInvocationType } from "../../../main/typescript/generated/openapi/typescript-axios/api";
import { GatewayOptions } from "../../../main/typescript/generated/openapi/typescript-axios/api";
import { DefaultApi as FabricApi } from "../../../main/typescript/generated/openapi/typescript-axios/api";

/**
* Create new asset on the ledger to trigger new transaction creation.
*
* @param assetName unique asset name to create
* @returns committed transaction id.
*/
export async function sendTransactionOnFabric(opts: {
readonly gatewayOptions: GatewayOptions;
readonly log: Logger;
readonly apiClient: FabricApi;
readonly assetName: string;
readonly ledgerChannelName: string;
readonly ledgerContractName: string;
}) {
const fn = "sendTransactionOnFabric()";

if (!opts) {
throw new TypeError(`${fn} arg opts cannot be falsy.`);
}
const { log, apiClient, gatewayOptions, assetName } = opts;
const { ledgerContractName, ledgerChannelName } = opts;

if (!opts.gatewayOptions) {
throw new TypeError(`${fn} arg opts.gatewayOptions cannot be falsy.`);
}
if (!gatewayOptions.wallet) {
throw new TypeError(`${fn} arg opts.gatewayOptions.wallet cannot be falsy`);
}

const createAssetResponse = await apiClient.runTransactionV1({
signingCredential: gatewayOptions.wallet
.keychain as FabricSigningCredential,
channelName: ledgerChannelName,
invocationType: FabricContractInvocationType.Send,
contractName: ledgerContractName,
methodName: "CreateAsset",
params: [assetName, "green", "111", "someOwner", "299"],
});
expect(createAssetResponse).toBeTruthy();
expect(createAssetResponse.status).toEqual(200);
expect(createAssetResponse.data).toBeTruthy();
const txId = createAssetResponse.data.transactionId;
expect(txId).toBeTruthy();

log.debug("Crated new transaction, txId:", txId);
return txId;
}
Loading

0 comments on commit 43209df

Please sign in to comment.