From c87139ffa948bba9a2253c1fd689791627ddeb99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Fri, 25 Apr 2025 17:07:12 +0200 Subject: [PATCH] do not use backend-common in the integration test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .../src/tests/integration.test.ts | 65 ++++++++++++------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/plugins/catalog-backend/src/tests/integration.test.ts b/plugins/catalog-backend/src/tests/integration.test.ts index aa6d241d23..76d6af1f0d 100644 --- a/plugins/catalog-backend/src/tests/integration.test.ts +++ b/plugins/catalog-backend/src/tests/integration.test.ts @@ -52,9 +52,8 @@ import { DefaultEntitiesCatalog } from '../service/DefaultEntitiesCatalog'; import { DefaultRefreshService } from '../service/DefaultRefreshService'; import { RefreshOptions, RefreshService } from '../service/types'; import { DefaultStitcher } from '../stitching/DefaultStitcher'; -import { mockServices } from '@backstage/backend-test-utils'; +import { mockServices, TestDatabases } from '@backstage/backend-test-utils'; import { LoggerService } from '@backstage/backend-plugin-api'; -import { DatabaseManager } from '@backstage/backend-common'; import { entitiesResponseToObjects } from '../service/response'; import { deleteOrphanedEntities } from '../database/operations/util/deleteOrphanedEntities'; @@ -205,10 +204,10 @@ class TestHarness { readonly #proxyProgressTracker: ProxyProgressTracker; readonly #db: Knex; - static async create(options?: { + static async create(options: { disableRelationsCompatibility?: boolean; logger?: LoggerService; - db?: Knex; + db: Knex; permissions?: PermissionEvaluator; additionalProviders?: EntityProvider[]; processEntity?( @@ -231,24 +230,19 @@ class TestHarness { }, }); const logger = options?.logger ?? mockServices.logger.mock(); - const db = - options?.db ?? - (await DatabaseManager.fromConfig(config, { logger }) - .forPlugin('catalog') - .getClient()); - await applyDatabaseMigrations(db); + await applyDatabaseMigrations(options.db); const catalogDatabase = new DefaultCatalogDatabase({ - database: db, + database: options.db, logger, }); const providerDatabase = new DefaultProviderDatabase({ - database: db, + database: options.db, logger, }); const processingDatabase = new DefaultProcessingDatabase({ - database: db, + database: options.db, logger, refreshInterval: () => 0.05, }); @@ -281,9 +275,12 @@ class TestHarness { policy: EntityPolicies.allOf([]), legacySingleProcessorValidation: false, }); - const stitcher = DefaultStitcher.fromConfig(config, { knex: db, logger }); + const stitcher = DefaultStitcher.fromConfig(config, { + knex: options.db, + logger, + }); const catalog = new DefaultEntitiesCatalog({ - database: db, + database: options.db, logger, stitcher, disableRelationsCompatibility: options?.disableRelationsCompatibility, @@ -296,7 +293,7 @@ class TestHarness { config: new ConfigReader({}), logger, processingDatabase, - knex: db, + knex: options.db, orchestrator, stitcher, createHash: () => createHash('sha1'), @@ -333,7 +330,7 @@ class TestHarness { refresh, provider, proxyProgressTracker, - db, + options.db, ); } @@ -444,10 +441,13 @@ class TestHarness { } describe('Catalog Backend Integration', () => { + const databases = TestDatabases.create({ ids: ['SQLITE_3'] }); + it('should add entities and update errors', async () => { let triggerError = false; const harness = await TestHarness.create({ + db: await databases.init('SQLITE_3'), async processEntity(entity: Entity) { if (triggerError) { throw new Error('NOPE'); @@ -525,6 +525,7 @@ describe('Catalog Backend Integration', () => { const generatedApis = ['api-1', 'api-2']; const harness = await TestHarness.create({ + db: await databases.init('SQLITE_3'), async processEntity( entity: Entity, location: LocationSpec, @@ -609,7 +610,9 @@ describe('Catalog Backend Integration', () => { }); it('should not replace matching provided entities', async () => { - const harness = await TestHarness.create(); + const harness = await TestHarness.create({ + db: await databases.init('SQLITE_3'), + }); const entityA = { apiVersion: 'backstage.io/v1alpha1', @@ -669,6 +672,7 @@ describe('Catalog Backend Integration', () => { } const harness = await TestHarness.create({ + db: await databases.init('SQLITE_3'), async processEntity( entity: Entity, location: LocationSpec, @@ -788,7 +792,9 @@ describe('Catalog Backend Integration', () => { }); it('should reject insecure URLs', async () => { - const harness = await TestHarness.create(); + const harness = await TestHarness.create({ + db: await databases.init('SQLITE_3'), + }); await harness.setInputEntities([ { @@ -827,7 +833,9 @@ describe('Catalog Backend Integration', () => { }); it('should reject insecure location URLs', async () => { - const harness = await TestHarness.create(); + const harness = await TestHarness.create({ + db: await databases.init('SQLITE_3'), + }); await harness.setInputEntities([ { @@ -854,6 +862,7 @@ describe('Catalog Backend Integration', () => { it('should return valid responses in raw JSON mode', async () => { const harness = await TestHarness.create({ + db: await databases.init('SQLITE_3'), disableRelationsCompatibility: true, }); @@ -910,6 +919,7 @@ describe('Catalog Backend Integration', () => { const secondProvider = new TestProvider('second'); const harness = await TestHarness.create({ + db: await databases.init('SQLITE_3'), additionalProviders: [firstProvider, secondProvider], }); @@ -1066,7 +1076,10 @@ describe('Catalog Backend Integration', () => { _emit: CatalogProcessorEmit, ) => entity, ); - const harness = await TestHarness.create({ processEntity }); + const harness = await TestHarness.create({ + db: await databases.init('SQLITE_3'), + processEntity, + }); processEntity.mockImplementation(async (entity, location, emit) => { if (entity.metadata.name === entityA.metadata.name) { @@ -1189,7 +1202,10 @@ describe('Catalog Backend Integration', () => { _emit: CatalogProcessorEmit, ) => entity, ); - const harness = await TestHarness.create({ processEntity }); + const harness = await TestHarness.create({ + db: await databases.init('SQLITE_3'), + processEntity, + }); processEntity.mockImplementation(async (entity, location, emit) => { if (entity.metadata.name === entityA.metadata.name) { @@ -1246,7 +1262,10 @@ describe('Catalog Backend Integration', () => { _emit: CatalogProcessorEmit, ) => entity, ); - const harness = await TestHarness.create({ processEntity }); + const harness = await TestHarness.create({ + db: await databases.init('SQLITE_3'), + processEntity, + }); processEntity.mockImplementation(async (entity, location, emit) => { if (entity.metadata.name === entityA.metadata.name) {