backend-test-utils: move the rest of the mock services to new structure
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -5,14 +5,27 @@
|
||||
```ts
|
||||
import { Backend } from '@backstage/backend-app-api';
|
||||
import { BackendFeature } from '@backstage/backend-plugin-api';
|
||||
import { CacheService } from '@backstage/backend-plugin-api';
|
||||
import { ConfigReader } from '@backstage/config';
|
||||
import { ConfigService } from '@backstage/backend-plugin-api';
|
||||
import { DatabaseService } from '@backstage/backend-plugin-api';
|
||||
import { ExtendedHttpServer } from '@backstage/backend-app-api';
|
||||
import { ExtensionPoint } from '@backstage/backend-plugin-api';
|
||||
import { HttpRouterFactoryOptions } from '@backstage/backend-app-api';
|
||||
import { HttpRouterService } from '@backstage/backend-plugin-api';
|
||||
import { IdentityService } from '@backstage/backend-plugin-api';
|
||||
import { JsonObject } from '@backstage/types';
|
||||
import { Knex } from 'knex';
|
||||
import { LifecycleService } from '@backstage/backend-plugin-api';
|
||||
import { LoggerService } from '@backstage/backend-plugin-api';
|
||||
import { PermissionsService } from '@backstage/backend-plugin-api';
|
||||
import { RootLifecycleService } from '@backstage/backend-plugin-api';
|
||||
import { RootLoggerService } from '@backstage/backend-plugin-api';
|
||||
import { SchedulerService } from '@backstage/backend-plugin-api';
|
||||
import { ServiceFactory } from '@backstage/backend-plugin-api';
|
||||
import { ServiceRef } from '@backstage/backend-plugin-api';
|
||||
import { TokenManagerService } from '@backstage/backend-plugin-api';
|
||||
import { UrlReaderService } from '@backstage/backend-plugin-api';
|
||||
|
||||
// @public (undocumented)
|
||||
export function isDockerDisabledForTests(): boolean;
|
||||
@@ -23,6 +36,34 @@ export namespace mockFactories {
|
||||
config: (
|
||||
options?: mockServices.config.Options | undefined,
|
||||
) => ServiceFactory<ConfigService>;
|
||||
const // (undocumented)
|
||||
rootLogger: (
|
||||
options?: mockServices.rootLogger.Options | undefined,
|
||||
) => ServiceFactory<RootLoggerService>;
|
||||
const // (undocumented)
|
||||
tokenManager: () => ServiceFactory<TokenManagerService>;
|
||||
const // (undocumented)
|
||||
identity: () => ServiceFactory<IdentityService>;
|
||||
const // (undocumented)
|
||||
cache: () => ServiceFactory<CacheService>;
|
||||
const // (undocumented)
|
||||
database: () => ServiceFactory<DatabaseService>;
|
||||
const // (undocumented)
|
||||
httpRouter: (
|
||||
options?: HttpRouterFactoryOptions | undefined,
|
||||
) => ServiceFactory<HttpRouterService>;
|
||||
const // (undocumented)
|
||||
lifecycle: () => ServiceFactory<LifecycleService>;
|
||||
const // (undocumented)
|
||||
logger: () => ServiceFactory<LoggerService>;
|
||||
const // (undocumented)
|
||||
permissions: () => ServiceFactory<PermissionsService>;
|
||||
const // (undocumented)
|
||||
rootLifecycle: () => ServiceFactory<RootLifecycleService>;
|
||||
const // (undocumented)
|
||||
scheduler: () => ServiceFactory<SchedulerService>;
|
||||
const // (undocumented)
|
||||
urlReader: () => ServiceFactory<UrlReaderService>;
|
||||
}
|
||||
|
||||
// @alpha (undocumented)
|
||||
@@ -36,6 +77,26 @@ export namespace mockServices {
|
||||
}
|
||||
// (undocumented)
|
||||
export function config(options?: config.Options): ConfigReader;
|
||||
// (undocumented)
|
||||
export function identity(): IdentityService;
|
||||
// (undocumented)
|
||||
export namespace rootLogger {
|
||||
// (undocumented)
|
||||
export type Options = {
|
||||
levels:
|
||||
| boolean
|
||||
| {
|
||||
error: boolean;
|
||||
warn: boolean;
|
||||
info: boolean;
|
||||
debug: boolean;
|
||||
};
|
||||
};
|
||||
}
|
||||
// (undocumented)
|
||||
export function rootLogger(options?: rootLogger.Options): LoggerService;
|
||||
// (undocumented)
|
||||
export function tokenManager(): TokenManagerService;
|
||||
}
|
||||
|
||||
// @public
|
||||
|
||||
@@ -14,6 +14,17 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
cacheFactory,
|
||||
databaseFactory,
|
||||
httpRouterFactory,
|
||||
lifecycleFactory,
|
||||
loggerFactory,
|
||||
permissionsFactory,
|
||||
rootLifecycleFactory,
|
||||
schedulerFactory,
|
||||
urlReaderFactory,
|
||||
} from '@backstage/backend-app-api';
|
||||
import {
|
||||
coreServices,
|
||||
createServiceFactory,
|
||||
@@ -33,4 +44,41 @@ export namespace mockFactories {
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
export const rootLogger = createServiceFactory(
|
||||
(options?: mockServices.rootLogger.Options) => ({
|
||||
service: coreServices.rootLogger,
|
||||
deps: {},
|
||||
async factory() {
|
||||
return mockServices.rootLogger(options);
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
export const tokenManager = createServiceFactory({
|
||||
service: coreServices.tokenManager,
|
||||
deps: {},
|
||||
async factory() {
|
||||
return mockServices.tokenManager();
|
||||
},
|
||||
});
|
||||
|
||||
export const identity = createServiceFactory({
|
||||
service: coreServices.identity,
|
||||
deps: {},
|
||||
async factory() {
|
||||
return mockServices.identity();
|
||||
},
|
||||
});
|
||||
|
||||
// For all other services, we just use the default implementations
|
||||
export const cache = cacheFactory;
|
||||
export const database = databaseFactory;
|
||||
export const httpRouter = httpRouterFactory;
|
||||
export const lifecycle = lifecycleFactory;
|
||||
export const logger = loggerFactory;
|
||||
export const permissions = permissionsFactory;
|
||||
export const rootLifecycle = rootLifecycleFactory;
|
||||
export const scheduler = schedulerFactory;
|
||||
export const urlReader = urlReaderFactory;
|
||||
}
|
||||
|
||||
@@ -13,17 +13,13 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import {
|
||||
coreServices,
|
||||
createServiceFactory,
|
||||
IdentityService,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import { IdentityService } from '@backstage/backend-plugin-api';
|
||||
import {
|
||||
IdentityApiGetIdentityRequest,
|
||||
BackstageIdentityResponse,
|
||||
} from '@backstage/plugin-auth-node';
|
||||
|
||||
class MockIdentityServiceImpl implements IdentityService {
|
||||
export class MockIdentityService implements IdentityService {
|
||||
getIdentity(
|
||||
_options: IdentityApiGetIdentityRequest,
|
||||
): Promise<BackstageIdentityResponse | undefined> {
|
||||
@@ -37,11 +33,3 @@ class MockIdentityServiceImpl implements IdentityService {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const mockIdentityFactory = createServiceFactory({
|
||||
service: coreServices.identity,
|
||||
deps: {},
|
||||
async factory() {
|
||||
return new MockIdentityServiceImpl();
|
||||
},
|
||||
});
|
||||
|
||||
@@ -15,21 +15,14 @@
|
||||
*/
|
||||
|
||||
import {
|
||||
coreServices,
|
||||
createServiceFactory,
|
||||
LoggerService,
|
||||
LogMeta,
|
||||
RootLoggerService,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import type { mockServices } from './mockServices';
|
||||
|
||||
interface MockLoggerOptions {
|
||||
levels:
|
||||
| boolean
|
||||
| { error: boolean; warn: boolean; info: boolean; debug: boolean };
|
||||
}
|
||||
|
||||
class MockLogger implements RootLoggerService {
|
||||
#levels: Exclude<MockLoggerOptions['levels'], boolean>;
|
||||
export class MockLogger implements RootLoggerService {
|
||||
#levels: Exclude<mockServices.rootLogger.Options['levels'], boolean>;
|
||||
#meta: LogMeta;
|
||||
|
||||
error(message: string, meta?: LogMeta | Error | undefined): void {
|
||||
@@ -52,7 +45,10 @@ class MockLogger implements RootLoggerService {
|
||||
return new MockLogger(this.#levels, { ...this.#meta, ...meta });
|
||||
}
|
||||
|
||||
constructor(levels: MockLoggerOptions['levels'], meta: LogMeta) {
|
||||
constructor(
|
||||
levels: mockServices.rootLogger.Options['levels'],
|
||||
meta: LogMeta,
|
||||
) {
|
||||
if (typeof levels === 'boolean') {
|
||||
this.#levels = {
|
||||
error: levels,
|
||||
@@ -79,12 +75,3 @@ class MockLogger implements RootLoggerService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** @alpha */
|
||||
export const mockRootLoggerService = createServiceFactory({
|
||||
service: coreServices.rootLogger,
|
||||
deps: {},
|
||||
async factory(_deps) {
|
||||
return new MockLogger(false, {});
|
||||
},
|
||||
});
|
||||
|
||||
@@ -14,8 +14,15 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
IdentityService,
|
||||
LoggerService,
|
||||
TokenManagerService,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import { ConfigReader } from '@backstage/config';
|
||||
import { JsonObject } from '@backstage/types';
|
||||
import { MockIdentityService } from './mockIdentityService';
|
||||
import { MockLogger } from './mockRootLoggerService';
|
||||
|
||||
/**
|
||||
* @alpha
|
||||
@@ -24,8 +31,39 @@ export namespace mockServices {
|
||||
export namespace config {
|
||||
export type Options = { data?: JsonObject };
|
||||
}
|
||||
|
||||
export function config(options?: config.Options) {
|
||||
return new ConfigReader(options?.data, 'mock-config');
|
||||
}
|
||||
|
||||
export namespace rootLogger {
|
||||
export type Options = {
|
||||
levels:
|
||||
| boolean
|
||||
| { error: boolean; warn: boolean; info: boolean; debug: boolean };
|
||||
};
|
||||
}
|
||||
export function rootLogger(options?: rootLogger.Options): LoggerService {
|
||||
return new MockLogger(options?.levels ?? false, {});
|
||||
}
|
||||
|
||||
export function tokenManager(): TokenManagerService {
|
||||
return {
|
||||
async getToken(): Promise<{ token: string }> {
|
||||
return { token: 'mock-token' };
|
||||
},
|
||||
async authenticate(token: string): Promise<void> {
|
||||
if (token !== 'mock-token') {
|
||||
throw new Error('Invalid token');
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function identity(): IdentityService {
|
||||
return new MockIdentityService();
|
||||
}
|
||||
|
||||
// TODO(Rugvip): Not all core services have implementations available here yet.
|
||||
// some may need a bit more refactoring for it to be simpler to
|
||||
// re-implement functioning mock versions here.
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023 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 { TokenManager } from '@backstage/backend-common';
|
||||
import {
|
||||
coreServices,
|
||||
createServiceFactory,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
|
||||
class TokenManagerMock implements TokenManager {
|
||||
async getToken(): Promise<{ token: string }> {
|
||||
return { token: 'mock-token' };
|
||||
}
|
||||
async authenticate(token: string): Promise<void> {
|
||||
if (token !== 'mock-token') {
|
||||
throw new Error('Invalid token');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** @alpha */
|
||||
export const mockTokenManagerFactory = createServiceFactory({
|
||||
service: coreServices.tokenManager,
|
||||
deps: {},
|
||||
async factory() {
|
||||
return new TokenManagerMock();
|
||||
},
|
||||
});
|
||||
@@ -17,15 +17,6 @@
|
||||
import {
|
||||
Backend,
|
||||
createSpecializedBackend,
|
||||
lifecycleFactory,
|
||||
rootLifecycleFactory,
|
||||
loggerFactory,
|
||||
cacheFactory,
|
||||
permissionsFactory,
|
||||
schedulerFactory,
|
||||
urlReaderFactory,
|
||||
databaseFactory,
|
||||
httpRouterFactory,
|
||||
MiddlewareFactory,
|
||||
createHttpServer,
|
||||
ExtendedHttpServer,
|
||||
@@ -42,11 +33,8 @@ import {
|
||||
} from '@backstage/backend-plugin-api';
|
||||
|
||||
import { mockFactories } from '../implementations';
|
||||
import { mockRootLoggerService } from '../implementations/mockRootLoggerService';
|
||||
import { mockTokenManagerFactory } from '../implementations/mockTokenManagerService';
|
||||
import { ConfigReader } from '@backstage/config';
|
||||
import express from 'express';
|
||||
import { mockIdentityFactory } from '../implementations/mockIdentityService';
|
||||
|
||||
/** @alpha */
|
||||
export interface TestBackendOptions<
|
||||
@@ -84,19 +72,18 @@ export interface TestBackend extends Backend {
|
||||
}
|
||||
|
||||
const defaultServiceFactories = [
|
||||
cacheFactory(),
|
||||
databaseFactory(),
|
||||
httpRouterFactory(),
|
||||
lifecycleFactory(),
|
||||
loggerFactory(),
|
||||
mockFactories.cache(),
|
||||
mockFactories.database(),
|
||||
mockFactories.httpRouter(),
|
||||
mockFactories.lifecycle(),
|
||||
mockFactories.logger(),
|
||||
mockFactories.config(),
|
||||
mockRootLoggerService(),
|
||||
mockIdentityFactory(),
|
||||
mockTokenManagerFactory(),
|
||||
permissionsFactory(),
|
||||
rootLifecycleFactory(),
|
||||
schedulerFactory(),
|
||||
urlReaderFactory(),
|
||||
mockFactories.identity(),
|
||||
mockFactories.tokenManager(),
|
||||
mockFactories.permissions(),
|
||||
mockFactories.rootLifecycle(),
|
||||
mockFactories.scheduler(),
|
||||
mockFactories.urlReader(),
|
||||
];
|
||||
|
||||
const backendInstancesToCleanUp = new Array<Backend>();
|
||||
|
||||
Reference in New Issue
Block a user