From 7ad630ab825a56b7d7119fab33d39bd9aabb23be Mon Sep 17 00:00:00 2001 From: Peter Somogyvari Date: Mon, 14 Oct 2024 16:57:34 -0700 Subject: [PATCH] test(cmd-api-server): jestify grpc-proto-gen-ts-client-healthcheck test 1. Also fixing a minor bug in config/config-service-example-config-validity.test.ts where the path of the temporary files generated by the test were being saved in the wrong directory instead of $PROJECT_ROOT/.tmp/... Signed-off-by: Peter Somogyvari --- .github/workflows/ci.yaml | 2 +- .taprc | 1 - jest.config.js | 1 - ...ig-service-example-config-validity.test.ts | 6 +- ...pc-proto-gen-ts-client-healthcheck.test.ts | 167 ++++++++++-------- 5 files changed, 94 insertions(+), 83 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index c87fe815255..b84d6399501 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -437,7 +437,7 @@ jobs: JEST_TEST_COVERAGE_PATH: ./code-coverage-ts/cactus-cmd-api-server JEST_TEST_CODE_COVERAGE_ENABLED: true TAPE_TEST_PATTERN: >- - --files={./packages/cactus-cmd-api-server/src/test/typescript/unit/config/self-signed-certificate-generator/certificates-work-for-mutual-tls.test.ts,./packages/cactus-cmd-api-server/src/test/typescript/unit/config/self-signed-certificate-generator/generates-working-certificates.test.ts,./packages/cactus-cmd-api-server/src/test/typescript/unit/grpc-js-proto-loader-client-healthcheck.test.ts,./packages/cactus-cmd-api-server/src/test/typescript/unit/grpc-proto-gen-ts-client-healthcheck.test.ts,./packages/cactus-cmd-api-server/src/test/typescript/unit/grpc-proto-gen-ts-client-m-tls-enabled.test.ts,./packages/cactus-cmd-api-server/src/test/typescript/unit/plugins/install-basic-plugin-ledger-connector-fabric-0-7-0.test.ts} + --files={./packages/cactus-cmd-api-server/src/test/typescript/unit/config/self-signed-certificate-generator/certificates-work-for-mutual-tls.test.ts,./packages/cactus-cmd-api-server/src/test/typescript/unit/config/self-signed-certificate-generator/generates-working-certificates.test.ts,./packages/cactus-cmd-api-server/src/test/typescript/unit/grpc-js-proto-loader-client-healthcheck.test.ts,./packages/cactus-cmd-api-server/src/test/typescript/unit/grpc-proto-gen-ts-client-m-tls-enabled.test.ts,./packages/cactus-cmd-api-server/src/test/typescript/unit/plugins/install-basic-plugin-ledger-connector-fabric-0-7-0.test.ts} TAPE_TEST_RUNNER_DISABLED: false runs-on: ubuntu-22.04 steps: diff --git a/.taprc b/.taprc index 21648be8861..3c7afc44cf1 100644 --- a/.taprc +++ b/.taprc @@ -49,7 +49,6 @@ files: - ./packages/cactus-cmd-api-server/src/test/typescript/unit/plugins/install-basic-plugin-ledger-connector-fabric-0-7-0.test.ts - ./packages/cactus-cmd-api-server/src/test/typescript/unit/config/self-signed-certificate-generator/generates-working-certificates.test.ts - ./packages/cactus-cmd-api-server/src/test/typescript/unit/config/self-signed-certificate-generator/certificates-work-for-mutual-tls.test.ts - - ./packages/cactus-cmd-api-server/src/test/typescript/unit/grpc-proto-gen-ts-client-healthcheck.test.ts - ./packages/cactus-cmd-api-server/src/test/typescript/unit/grpc-js-proto-loader-client-healthcheck.test.ts - ./packages/cactus-cmd-api-server/src/test/typescript/unit/grpc-proto-gen-ts-client-m-tls-enabled.test.ts - ./extensions/cactus-plugin-object-store-ipfs/src/test/typescript/integration/plugin-object-store-ipfs.test.ts diff --git a/jest.config.js b/jest.config.js index 4639041b3d5..d803057a7a6 100644 --- a/jest.config.js +++ b/jest.config.js @@ -62,7 +62,6 @@ module.exports = { `./packages/cactus-cmd-api-server/src/test/typescript/unit/plugins/install-basic-plugin-ledger-connector-fabric-0-7-0.test.ts`, `./packages/cactus-cmd-api-server/src/test/typescript/unit/config/self-signed-certificate-generator/generates-working-certificates.test.ts`, `./packages/cactus-cmd-api-server/src/test/typescript/unit/config/self-signed-certificate-generator/certificates-work-for-mutual-tls.test.ts`, - `./packages/cactus-cmd-api-server/src/test/typescript/unit/grpc-proto-gen-ts-client-healthcheck.test.ts`, `./packages/cactus-cmd-api-server/src/test/typescript/unit/grpc-js-proto-loader-client-healthcheck.test.ts`, `./packages/cactus-cmd-api-server/src/test/typescript/unit/grpc-proto-gen-ts-client-m-tls-enabled.test.ts`, `./extensions/cactus-plugin-object-store-ipfs/src/test/typescript/integration/plugin-object-store-ipfs.test.ts`, diff --git a/packages/cactus-cmd-api-server/src/test/typescript/unit/config/config-service-example-config-validity.test.ts b/packages/cactus-cmd-api-server/src/test/typescript/unit/config/config-service-example-config-validity.test.ts index b4954e2d273..fc005f46a88 100644 --- a/packages/cactus-cmd-api-server/src/test/typescript/unit/config/config-service-example-config-validity.test.ts +++ b/packages/cactus-cmd-api-server/src/test/typescript/unit/config/config-service-example-config-validity.test.ts @@ -11,9 +11,7 @@ import { import { ApiServer } from "../../../../main/typescript/public-api"; import { ConfigService } from "../../../../main/typescript/public-api"; -const testcase = ""; - -describe(testcase, () => { +describe("ConfigService", () => { const configService = new ConfigService(); let apiServer: ApiServer, exampleConfig: ICactusApiServerOptions, @@ -24,7 +22,7 @@ describe(testcase, () => { exampleConfig = await configService.newExampleConfig(); const pluginsPath = path.join( __dirname, - "../../../../../../", // walk back up to the project root + "../../../../../../../", // walk back up to the project root ".tmp/test/test-cmd-api-server/config-service-example-config-validity_test/", // the dir path from the root uuidv4(), // then a random directory to ensure proper isolation ); diff --git a/packages/cactus-cmd-api-server/src/test/typescript/unit/grpc-proto-gen-ts-client-healthcheck.test.ts b/packages/cactus-cmd-api-server/src/test/typescript/unit/grpc-proto-gen-ts-client-healthcheck.test.ts index b996e296a62..bdb0e452a76 100644 --- a/packages/cactus-cmd-api-server/src/test/typescript/unit/grpc-proto-gen-ts-client-healthcheck.test.ts +++ b/packages/cactus-cmd-api-server/src/test/typescript/unit/grpc-proto-gen-ts-client-healthcheck.test.ts @@ -1,6 +1,6 @@ -import test, { Test } from "tape-promise/tape"; - +import "jest-extended"; import * as grpc from "@grpc/grpc-js"; +import { RuntimeError } from "run-time-error-cjs"; import { LogLevelDesc } from "@hyperledger/cactus-common"; @@ -9,80 +9,95 @@ import { AuthorizationProtocol } from "../../../main/typescript/public-api"; import { default_service } from "../../../main/typescript/public-api"; import { health_check_response_pb } from "../../../main/typescript/public-api"; import { empty } from "../../../main/typescript/public-api"; -import { RuntimeError } from "run-time-error-cjs"; -const testCase = "API server: runs gRPC TS-proto web services"; -const logLevel: LogLevelDesc = "TRACE"; - -test(testCase, async (t: Test) => { - const configService = new ConfigService(); - const apiSrvOpts = await configService.newExampleConfig(); - apiSrvOpts.authorizationProtocol = AuthorizationProtocol.NONE; - apiSrvOpts.configFile = ""; - apiSrvOpts.logLevel = logLevel; - apiSrvOpts.apiCorsDomainCsv = "*"; - apiSrvOpts.apiPort = 0; - apiSrvOpts.grpcPort = 0; - apiSrvOpts.crpcPort = 0; - apiSrvOpts.cockpitPort = 0; - apiSrvOpts.grpcMtlsEnabled = false; - apiSrvOpts.apiTlsEnabled = false; - apiSrvOpts.plugins = []; - const config = await configService.newExampleConfigConvict(apiSrvOpts); - - const apiServer = new ApiServer({ - config: config.getProperties(), +const logLevel: LogLevelDesc = "INFO"; + +describe("ApiServer", () => { + let apiServer: ApiServer; + + afterAll(async () => { + await apiServer.shutdown(); + }); + + test("runs gRPC TS-proto web services", async () => { + const configService = new ConfigService(); + const apiSrvOpts = await configService.newExampleConfig(); + apiSrvOpts.authorizationProtocol = AuthorizationProtocol.NONE; + apiSrvOpts.configFile = ""; + apiSrvOpts.logLevel = logLevel; + apiSrvOpts.apiCorsDomainCsv = "*"; + apiSrvOpts.apiPort = 0; + apiSrvOpts.grpcPort = 0; + apiSrvOpts.crpcPort = 0; + apiSrvOpts.cockpitPort = 0; + apiSrvOpts.grpcMtlsEnabled = false; + apiSrvOpts.apiTlsEnabled = false; + apiSrvOpts.plugins = []; + const config = await configService.newExampleConfigConvict(apiSrvOpts); + + apiServer = new ApiServer({ + config: config.getProperties(), + }); + + const startResponse = apiServer.start(); + expect(startResponse).toResolve(); + + const addressInfoGrpc = (await startResponse).addressInfoGrpc; + const { address, port } = addressInfoGrpc; + const grpcHostAndPort = `${address}:${port}`; + + const apiClient = + new default_service.org.hyperledger.cactus.cmd_api_server.DefaultServiceClient( + grpcHostAndPort, + grpc.credentials.createInsecure(), + ); + expect(apiClient).toBeTruthy(); + + const responsePromise = + new Promise( + (resolve, reject) => { + apiClient.GetHealthCheckV1( + new empty.google.protobuf.Empty(), + ( + error: grpc.ServiceError | null, + response?: health_check_response_pb.org.hyperledger.cactus.cmd_api_server.HealthCheckResponsePB, + ) => { + if (error) { + reject(error); + } else if (response) { + resolve(response); + } else { + throw new RuntimeError("No error, nor response received."); + } + }, + ); + }, + ); + + await expect(responsePromise).resolves.toMatchObject({ + toObject: expect.toBeFunction(), + }); + + const resPb = await responsePromise; + const res = resPb.toObject(); + + expect(res).toMatchObject({ + createdAt: expect.toBeDateString(), + memoryUsage: expect.objectContaining({ + rss: expect.toBeNumber(), + }), + success: true, + }); + // await t.doesNotReject(responsePromise, "No error in healthcheck OK"); + // const res = await responsePromise; + + // const resHc = res?.toObject(); + + // t.ok(resHc, `healthcheck response truthy OK`); + // t.ok(resHc?.createdAt, `resHc.createdAt truthy OK`); + // t.ok(resHc?.memoryUsage, `resHc.memoryUsage truthy OK`); + // t.ok(resHc?.memoryUsage?.rss, `resHc.memoryUsage.rss truthy OK`); + // t.ok(resHc?.success, `resHc.success truthy OK`); + // t.end(); }); - test.onFinish(async () => await apiServer.shutdown()); - - const startResponse = apiServer.start(); - await t.doesNotReject( - startResponse, - "failed to start API server with dynamic plugin imports configured for it...", - ); - t.ok(startResponse, "startResponse truthy OK"); - - const addressInfoGrpc = (await startResponse).addressInfoGrpc; - const { address, port } = addressInfoGrpc; - const grpcHostAndPort = `${address}:${port}`; - - const apiClient = - new default_service.org.hyperledger.cactus.cmd_api_server.DefaultServiceClient( - grpcHostAndPort, - grpc.credentials.createInsecure(), - ); - t.ok(apiClient, "apiClient truthy OK"); - - const responsePromise = - new Promise( - (resolve, reject) => { - apiClient.GetHealthCheckV1( - new empty.google.protobuf.Empty(), - ( - error: grpc.ServiceError | null, - response?: health_check_response_pb.org.hyperledger.cactus.cmd_api_server.HealthCheckResponsePB, - ) => { - if (error) { - reject(error); - } else if (response) { - resolve(response); - } else { - throw new RuntimeError("No error, nor response received."); - } - }, - ); - }, - ); - - await t.doesNotReject(responsePromise, "No error in healthcheck OK"); - const res = await responsePromise; - - const resHc = res?.toObject(); - - t.ok(resHc, `healthcheck response truthy OK`); - t.ok(resHc?.createdAt, `resHc.createdAt truthy OK`); - t.ok(resHc?.memoryUsage, `resHc.memoryUsage truthy OK`); - t.ok(resHc?.memoryUsage?.rss, `resHc.memoryUsage.rss truthy OK`); - t.ok(resHc?.success, `resHc.success truthy OK`); - t.end(); });