diff --git a/.changeset/angry-ducks-shake.md b/.changeset/angry-ducks-shake.md new file mode 100644 index 0000000000..58975123b4 --- /dev/null +++ b/.changeset/angry-ducks-shake.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-test-utils': patch +--- + +Allow specifying custom Docker registry for database tests diff --git a/packages/backend-test-utils/README.md b/packages/backend-test-utils/README.md index d0f52388bb..f22cb1bd3c 100644 --- a/packages/backend-test-utils/README.md +++ b/packages/backend-test-utils/README.md @@ -12,6 +12,26 @@ cd plugins/my-plugin-backend yarn add --dev @backstage/backend-test-utils ``` +## Environment variables + +- `BACKSTAGE_TEST_DISABLE_DOCKER` + - Setting the value to `1` disables Docker for tests +- `CI` + - Setting the value to `1` enables long-running tests, including the ones utilizing Docker +- `BACKSTAGE_TEST_DOCKER_REGISTRY` + - Docker registry mirror address where to pull images for tests, for example `mycompany.docker.io/mirror` + - See [documentation](https://node.testcontainers.org/configuration/) for information + about authentication (`DOCKER_AUTH_CONFIG`) + +Connection strings for different databases that are used for testing. The value of the +string should point to the running instance of the database. + +- `BACKSTAGE_TEST_DATABASE_POSTGRES13_CONNECTION_STRING` +- `BACKSTAGE_TEST_DATABASE_POSTGRES12_CONNECTION_STRING` +- `BACKSTAGE_TEST_DATABASE_POSTGRES11_CONNECTION_STRING` +- `BACKSTAGE_TEST_DATABASE_POSTGRES9_CONNECTION_STRING` +- `BACKSTAGE_TEST_DATABASE_MYSQL8_CONNECTION_STRING` + ## Documentation - [Backstage Readme](https://github.com/backstage/backstage/blob/master/README.md) diff --git a/packages/backend-test-utils/api-report.md b/packages/backend-test-utils/api-report.md index 358b51071f..1ff3521869 100644 --- a/packages/backend-test-utils/api-report.md +++ b/packages/backend-test-utils/api-report.md @@ -25,6 +25,9 @@ import { ServiceRef } from '@backstage/backend-plugin-api'; import { TokenManagerService } from '@backstage/backend-plugin-api'; import { UrlReaderService } from '@backstage/backend-plugin-api'; +// @public (undocumented) +export const getDockerImageForName: (name: string) => string; + // @public (undocumented) export function isDockerDisabledForTests(): boolean; diff --git a/packages/backend-test-utils/src/database/types.ts b/packages/backend-test-utils/src/database/types.ts index 89ec21e7cc..c35d23c571 100644 --- a/packages/backend-test-utils/src/database/types.ts +++ b/packages/backend-test-utils/src/database/types.ts @@ -16,6 +16,7 @@ import { DatabaseManager } from '@backstage/backend-common'; import { Knex } from 'knex'; +import { getDockerImageForName } from '../util'; /** * The possible databases to test against. @@ -48,35 +49,35 @@ export const allDatabases: Record = POSTGRES_13: { name: 'Postgres 13.x', driver: 'pg', - dockerImageName: 'postgres:13', + dockerImageName: getDockerImageForName('postgres:13'), connectionStringEnvironmentVariableName: 'BACKSTAGE_TEST_DATABASE_POSTGRES13_CONNECTION_STRING', }, POSTGRES_12: { name: 'Postgres 12.x', driver: 'pg', - dockerImageName: 'postgres:12', + dockerImageName: getDockerImageForName('postgres:12'), connectionStringEnvironmentVariableName: 'BACKSTAGE_TEST_DATABASE_POSTGRES12_CONNECTION_STRING', }, POSTGRES_11: { name: 'Postgres 11.x', driver: 'pg', - dockerImageName: 'postgres:11', + dockerImageName: getDockerImageForName('postgres:11'), connectionStringEnvironmentVariableName: 'BACKSTAGE_TEST_DATABASE_POSTGRES11_CONNECTION_STRING', }, POSTGRES_9: { name: 'Postgres 9.x', driver: 'pg', - dockerImageName: 'postgres:9', + dockerImageName: getDockerImageForName('postgres:9'), connectionStringEnvironmentVariableName: 'BACKSTAGE_TEST_DATABASE_POSTGRES9_CONNECTION_STRING', }, MYSQL_8: { name: 'MySQL 8.x', driver: 'mysql2', - dockerImageName: 'mysql:8', + dockerImageName: getDockerImageForName('mysql:8'), connectionStringEnvironmentVariableName: 'BACKSTAGE_TEST_DATABASE_MYSQL8_CONNECTION_STRING', }, diff --git a/packages/backend-test-utils/src/util/getDockerImageForName.ts b/packages/backend-test-utils/src/util/getDockerImageForName.ts new file mode 100644 index 0000000000..a3e1958bf6 --- /dev/null +++ b/packages/backend-test-utils/src/util/getDockerImageForName.ts @@ -0,0 +1,22 @@ +/* + * Copyright 2021 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. + */ + +/** @public */ +export const getDockerImageForName = (name: string) => { + return process.env.BACKSTAGE_TEST_DOCKER_REGISTRY + ? `${process.env.BACKSTAGE_TEST_DOCKER_REGISTRY}/${name}` + : name; +}; diff --git a/packages/backend-test-utils/src/util/index.ts b/packages/backend-test-utils/src/util/index.ts index a6cdc621d4..837af39028 100644 --- a/packages/backend-test-utils/src/util/index.ts +++ b/packages/backend-test-utils/src/util/index.ts @@ -15,3 +15,4 @@ */ export { isDockerDisabledForTests } from './isDockerDisabledForTests'; +export { getDockerImageForName } from './getDockerImageForName';