From a651e04a4e8e7cd3c228d09007d4406a4ed4b500 Mon Sep 17 00:00:00 2001 From: aramissennyeydd Date: Tue, 28 Jan 2025 20:42:57 -0500 Subject: [PATCH] add test case Signed-off-by: aramissennyeydd --- packages/backend-defaults/package.json | 5 +- .../SystemMetadataService.test.ts | 139 ++++++++++++++++++ yarn.lock | 1 + 3 files changed, 141 insertions(+), 4 deletions(-) create mode 100644 packages/backend-defaults/src/entrypoints/systemMetadata/SystemMetadataService.test.ts diff --git a/packages/backend-defaults/package.json b/packages/backend-defaults/package.json index bae8dbf04f..042b9497b9 100644 --- a/packages/backend-defaults/package.json +++ b/packages/backend-defaults/package.json @@ -21,7 +21,6 @@ "exports": { ".": "./src/index.ts", "./auditor": "./src/entrypoints/auditor/index.ts", - "./alpha": "./src/entrypoints/alpha.ts", "./auth": "./src/entrypoints/auth/index.ts", "./cache": "./src/entrypoints/cache/index.ts", "./database": "./src/entrypoints/database/index.ts", @@ -50,9 +49,6 @@ "auditor": [ "src/entrypoints/auditor/index.ts" ], - "alpha": [ - "src/entrypoints/alpha.ts" - ], "auth": [ "src/entrypoints/auth/index.ts" ], @@ -218,6 +214,7 @@ "@types/yauzl": "^2.10.0", "aws-sdk-client-mock": "^4.0.0", "better-sqlite3": "^12.0.0", + "get-port": "^5.1.1", "http-errors": "^2.0.0", "msw": "^1.0.0", "node-mocks-http": "^1.0.0", diff --git a/packages/backend-defaults/src/entrypoints/systemMetadata/SystemMetadataService.test.ts b/packages/backend-defaults/src/entrypoints/systemMetadata/SystemMetadataService.test.ts new file mode 100644 index 0000000000..93b0d2dfb5 --- /dev/null +++ b/packages/backend-defaults/src/entrypoints/systemMetadata/SystemMetadataService.test.ts @@ -0,0 +1,139 @@ +/* + * Copyright 2025 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { createSpecializedBackend } from '@backstage/backend-app-api'; +import { systemMetadataServiceFactory } from './systemMetadataServiceFactory'; +import { mockServices } from '@backstage/backend-test-utils'; +import getPort from 'get-port'; +import { createBackendPlugin } from '@backstage/backend-plugin-api'; + +const baseFactories = [ + mockServices.rootHealth.factory(), + mockServices.rootLogger.factory(), + mockServices.rootLifecycle.factory(), + mockServices.rootHttpRouter.factory(), + mockServices.lifecycle.factory(), + mockServices.logger.factory(), +]; + +describe('SystemMetadataService', () => { + it('should list plugins across instances', async () => { + const instance1HttpPort = await getPort(); + const instance2HttpPort = await getPort(); + const instance1 = createSpecializedBackend({ + defaultServiceFactories: [ + ...baseFactories, + systemMetadataServiceFactory, + mockServices.rootConfig.factory({ + data: { + backend: { + listen: { + port: instance1HttpPort, + }, + }, + discovery: { + instances: [ + { + baseUrl: `http://localhost:${instance1HttpPort}`, + }, + { + baseUrl: `http://localhost:${instance2HttpPort}`, + }, + ], + }, + }, + }), + ], + }); + + const instance2 = createSpecializedBackend({ + defaultServiceFactories: [ + ...baseFactories, + systemMetadataServiceFactory, + mockServices.rootConfig.factory({ + data: { + backend: { + listen: { + port: instance2HttpPort, + }, + }, + discovery: { + instances: [ + { + baseUrl: `http://localhost:${instance1HttpPort}`, + }, + { + baseUrl: `http://localhost:${instance2HttpPort}`, + }, + ], + }, + }, + }), + ], + }); + + instance1.add( + createBackendPlugin({ + pluginId: 'test', + register(reg) { + reg.registerInit({ + deps: {}, + async init() { + // do nothing + }, + }); + }, + }), + ); + + instance2.add( + createBackendPlugin({ + pluginId: 'test-other', + register(reg) { + reg.registerInit({ + deps: {}, + async init() { + // do nothing + }, + }); + }, + }), + ); + + await instance1.start(); + await instance2.start(); + + const installedFeatures = await fetch( + `http://localhost:${instance1HttpPort}/.backstage/systemMetadata/v1/features/installed`, + ); + + expect(installedFeatures.status).toBe(200); + + await expect(installedFeatures.json()).resolves.toMatchObject({ + test: [ + { + externalUrl: `http://localhost:${instance1HttpPort}`, + internalUrl: `http://localhost:${instance1HttpPort}`, + }, + ], + 'test-other': [ + { + externalUrl: `http://localhost:${instance2HttpPort}`, + internalUrl: `http://localhost:${instance2HttpPort}`, + }, + ], + }); + }); +}); diff --git a/yarn.lock b/yarn.lock index ac53d6c016..eec211526d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2915,6 +2915,7 @@ __metadata: express-promise-router: "npm:^4.1.0" express-rate-limit: "npm:^7.5.0" fs-extra: "npm:^11.2.0" + get-port: "npm:^5.1.1" git-url-parse: "npm:^15.0.0" helmet: "npm:^6.0.0" http-errors: "npm:^2.0.0"