From c57b65b8f0da2407d2593630d9bd77dc95c75155 Mon Sep 17 00:00:00 2001 From: Giovanni Baratta Date: Mon, 11 Mar 2024 22:09:27 +0000 Subject: [PATCH] refactor: create an isolated test env for each test instead of test suite The reason behind these change is that test were failing on a faster machine (probably due to the implementation of KafkaJS and Kafka itself). The clean up of topics were failing and consequently also the tests. An easier solution is not to clean up the environment but to have a true isolated environment for each test. --- service/libs/testing/src/kafka.ts | 33 +------------------ .../plan.controller.integration.test.ts | 19 ++++------- .../run.controller.integration.test.ts | 27 +++++---------- ...source-code.controller.integration.test.ts | 25 +++++--------- 4 files changed, 25 insertions(+), 79 deletions(-) diff --git a/service/libs/testing/src/kafka.ts b/service/libs/testing/src/kafka.ts index 0fe58f7..aa4c7a4 100644 --- a/service/libs/testing/src/kafka.ts +++ b/service/libs/testing/src/kafka.ts @@ -2,7 +2,7 @@ import {KafkaConfig} from "@libs/external/config/config" import {randomUUID} from "crypto" import {Kafka, logLevel} from "kafkajs" -export async function prepareKafka(): Promise { +export async function generateIsolatedKafkaConfig(): Promise { const rawKafkaBrokers = process.env.KAFKA_BROKERS if (rawKafkaBrokers === undefined) @@ -10,8 +10,6 @@ export async function prepareKafka(): Promise { const rawKafkaTopicRunStatusChanged = `run-status-changed-${randomUUID()}` - await createTopic(rawKafkaBrokers, rawKafkaTopicRunStatusChanged) - return { brokers: rawKafkaBrokers.split(","), topics: { @@ -20,35 +18,6 @@ export async function prepareKafka(): Promise { } } -async function createTopic(brokers: string, topic: string) { - const client = new Kafka({ - brokers: brokers.split(",") - }) - - const admin = client.admin() - await admin.connect() - await admin.createTopics({ - topics: [{topic}], - waitForLeaders: true - }) - await admin.disconnect() -} - -export async function cleanKafka(config: KafkaConfig): Promise { - const client = new Kafka({ - brokers: config.brokers, - logLevel: logLevel.ERROR - }) - - const admin = client.admin() - await admin.connect() - await admin.deleteTopics({ - topics: [config.topics.runStatusChanged] - }) - await createTopic(config.brokers.join(","), config.topics.runStatusChanged) - await admin.disconnect() -} - export async function readMessagesFromKafka( config: KafkaConfig, topic: string diff --git a/service/main/test/controller/plan.controller.integration.test.ts b/service/main/test/controller/plan.controller.integration.test.ts index e5f584c..054e4c3 100644 --- a/service/main/test/controller/plan.controller.integration.test.ts +++ b/service/main/test/controller/plan.controller.integration.test.ts @@ -3,14 +3,14 @@ import * as request from "supertest" import {NestApplication} from "@nestjs/core" // eslint-disable-next-line node/no-unpublished-import -import {Test, TestingModule} from "@nestjs/testing" import {AppModule} from "@app/app.module" -import {PrismaClient} from "@prisma/client" import {CreatePlanRefRequestBody} from "@app/controller/plan-models" -import {cleanDatabase, prepareDatabase} from "@libs/testing/database" import {Config, KafkaConfig} from "@libs/external/config/config" import {DatabaseClient} from "@libs/external/db/database-client" -import {cleanKafka, prepareKafka} from "@libs/testing/kafka" +import {prepareDatabase} from "@libs/testing/database" +import {generateIsolatedKafkaConfig} from "@libs/testing/kafka" +import {Test, TestingModule} from "@nestjs/testing" +import {PrismaClient} from "@prisma/client" describe("POST /plan-refs", () => { let app: NestApplication @@ -18,9 +18,9 @@ describe("POST /plan-refs", () => { let prisma: PrismaClient let kafkaConfig: KafkaConfig - beforeAll(async () => { + beforeEach(async () => { const isolatedDb = await prepareDatabase() - kafkaConfig = await prepareKafka() + kafkaConfig = await generateIsolatedKafkaConfig() const module: TestingModule = await Test.createTestingModule({ imports: [AppModule] @@ -38,11 +38,6 @@ describe("POST /plan-refs", () => { prisma = module.get(DatabaseClient) }) - beforeEach(async () => { - await cleanKafka(kafkaConfig) - await cleanDatabase(prisma) - }) - it("should create a record in the plan table and return the uuid", async () => { // Given const requestBody: CreatePlanRefRequestBody = { @@ -116,8 +111,6 @@ describe("POST /plan-refs", () => { }) afterAll(async () => { - await cleanKafka(kafkaConfig) - await cleanDatabase(prisma) await prisma.$disconnect() await app.close() }) diff --git a/service/main/test/controller/run.controller.integration.test.ts b/service/main/test/controller/run.controller.integration.test.ts index a9e34ff..69f0f2d 100644 --- a/service/main/test/controller/run.controller.integration.test.ts +++ b/service/main/test/controller/run.controller.integration.test.ts @@ -2,23 +2,21 @@ import {AppModule} from "@app/app.module" import {CreateRunRequestBody} from "@app/controller/run-models" import {Config, KafkaConfig} from "@libs/external/config/config" import {DatabaseClient} from "@libs/external/db/database-client" -import {cleanDatabase, prepareDatabase} from "@libs/testing/database" -import {persistPlanMock} from "@libs/testing" -import {persistSourceCodeMock} from "@libs/testing" +import {persistPlanMock, persistSourceCodeMock} from "@libs/testing" +import {prepareDatabase} from "@libs/testing/database" import {NestApplication} from "@nestjs/core" -import {TestingModule, Test} from "@nestjs/testing" +import {Test, TestingModule} from "@nestjs/testing" import {PrismaClient} from "@prisma/client" // eslint-disable-next-line node/no-unpublished-import -import * as request from "supertest" -import "expect-more-jest" -import {randomUUID} from "crypto" import {globalValidationPipe} from "@app/validation-pipe" import { - cleanKafka, - prepareKafka, + generateIsolatedKafkaConfig, readMessagesFromKafka } from "@libs/testing/kafka" import {HttpStatus} from "@nestjs/common" +import {randomUUID} from "crypto" +import "expect-more-jest" +import * as request from "supertest" describe("POST /runs", () => { let app: NestApplication @@ -27,9 +25,9 @@ describe("POST /runs", () => { const endpoint = "/runs" - beforeAll(async () => { + beforeEach(async () => { const isolatedDb = await prepareDatabase() - kafkaConfig = await prepareKafka() + kafkaConfig = await generateIsolatedKafkaConfig() const module: TestingModule = await Test.createTestingModule({ imports: [AppModule] @@ -47,11 +45,6 @@ describe("POST /runs", () => { prisma = module.get(DatabaseClient) }) - beforeEach(async () => { - await cleanKafka(kafkaConfig) - await cleanDatabase(prisma) - }) - it("should persist the run, emit a run-status-changed event and return the uuid", async () => { // Given const sourceCode = await persistSourceCodeMock(prisma) @@ -200,8 +193,6 @@ describe("POST /runs", () => { }) afterAll(async () => { - await cleanKafka(kafkaConfig) - await cleanDatabase(prisma) await prisma.$disconnect() await app.close() }) diff --git a/service/main/test/controller/source-code.controller.integration.test.ts b/service/main/test/controller/source-code.controller.integration.test.ts index 175654e..f5ae52b 100644 --- a/service/main/test/controller/source-code.controller.integration.test.ts +++ b/service/main/test/controller/source-code.controller.integration.test.ts @@ -1,24 +1,24 @@ // eslint-disable-next-line node/no-unpublished-import -import * as request from "supertest" import {NestApplication} from "@nestjs/core" +import * as request from "supertest" // eslint-disable-next-line node/no-unpublished-import -import {Test, TestingModule} from "@nestjs/testing" -import {AppModule} from "@app/app.module" import {operations} from "@apis/apis" -import {PrismaClient} from "@prisma/client" -import {cleanDatabase, prepareDatabase} from "@libs/testing/database" +import {AppModule} from "@app/app.module" import {Config, KafkaConfig} from "@libs/external/config/config" import {DatabaseClient} from "@libs/external/db/database-client" -import {cleanKafka, prepareKafka} from "@libs/testing/kafka" +import {prepareDatabase} from "@libs/testing/database" +import {generateIsolatedKafkaConfig} from "@libs/testing/kafka" +import {Test, TestingModule} from "@nestjs/testing" +import {PrismaClient} from "@prisma/client" describe("POST /source-code-refs", () => { let kafkaConfig: KafkaConfig let app: NestApplication let prisma: PrismaClient - beforeAll(async () => { + beforeEach(async () => { const isolatedDb = await prepareDatabase() - kafkaConfig = await prepareKafka() + kafkaConfig = await generateIsolatedKafkaConfig() const module: TestingModule = await Test.createTestingModule({ imports: [AppModule] @@ -36,11 +36,6 @@ describe("POST /source-code-refs", () => { prisma = module.get(DatabaseClient) }) - beforeEach(async () => { - await cleanKafka(kafkaConfig) - await cleanDatabase(prisma) - }) - it("should create a record in the SourceCode table and return the uuid", async () => { // Given const requestBody: operations["createSourceCodeRef"]["requestBody"]["content"]["application/json"] = @@ -116,9 +111,7 @@ describe("POST /source-code-refs", () => { }) }) - afterAll(async () => { - await cleanKafka(kafkaConfig) - await cleanDatabase(prisma) + afterEach(async () => { await prisma.$disconnect() await app.close() })