Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure test kit migrators are run only once #154446

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import { type TestElasticsearchUtils } from '@kbn/core-test-helpers-kbn-server';
import { ElasticsearchClient } from '@kbn/core-elasticsearch-server';
import type { MigrationResult } from '@kbn/core-saved-objects-base-server-internal';
import {
readLog,
clearLog,
Expand All @@ -19,13 +18,14 @@ import {
getIdenticalMappingsMigrator,
getIncompatibleMappingsMigrator,
startElasticsearch,
KibanaMigratorTestKit,
} from '../kibana_migrator_test_kit';
import { delay } from '../test_utils';

describe('when migrating to a new version', () => {
let esServer: TestElasticsearchUtils['es'];
let esClient: ElasticsearchClient;
let runMigrations: (rerun?: boolean | undefined) => Promise<MigrationResult[]>;
let migratorTestKitFactory: () => Promise<KibanaMigratorTestKit>;

beforeAll(async () => {
esServer = await startElasticsearch();
Expand All @@ -39,8 +39,9 @@ describe('when migrating to a new version', () => {
describe('and the mappings remain the same', () => {
it('the migrator skips reindexing', async () => {
// we run the migrator with the same identic baseline types
runMigrations = (await getIdenticalMappingsMigrator()).runMigrations;
await runMigrations();
migratorTestKitFactory = () => getIdenticalMappingsMigrator();
const testKit = await migratorTestKitFactory();
await testKit.runMigrations();

const logs = await readLog();
expect(logs).toMatch('INIT -> WAIT_FOR_YELLOW_SOURCE.');
Expand All @@ -67,8 +68,9 @@ describe('when migrating to a new version', () => {
describe("and the mappings' changes are still compatible", () => {
it('the migrator skips reindexing', async () => {
// we run the migrator with altered, compatible mappings
runMigrations = (await getCompatibleMappingsMigrator()).runMigrations;
await runMigrations();
migratorTestKitFactory = () => getCompatibleMappingsMigrator();
const testKit = await migratorTestKitFactory();
await testKit.runMigrations();

const logs = await readLog();
expect(logs).toMatch('INIT -> WAIT_FOR_YELLOW_SOURCE.');
Expand All @@ -94,9 +96,10 @@ describe('when migrating to a new version', () => {

describe("and the mappings' changes are NOT compatible", () => {
it('the migrator reindexes documents to a new index', async () => {
// we run the migrator with altered, compatible mappings
runMigrations = (await getIncompatibleMappingsMigrator()).runMigrations;
await runMigrations();
// we run the migrator with incompatible mappings
migratorTestKitFactory = () => getIncompatibleMappingsMigrator();
const testKit = await migratorTestKitFactory();
await testKit.runMigrations();

const logs = await readLog();
expect(logs).toMatch('INIT -> WAIT_FOR_YELLOW_SOURCE.');
Expand All @@ -115,8 +118,9 @@ describe('when migrating to a new version', () => {

afterEach(async () => {
// we run the migrator again to ensure that the next time state is loaded everything still works as expected
const migratorTestKit = await migratorTestKitFactory();
await clearLog();
await runMigrations(true);
await migratorTestKit.runMigrations();

const logs = await readLog();
expect(logs).toMatch('INIT -> WAIT_FOR_YELLOW_SOURCE.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type { ISavedObjectsRepository } from '@kbn/core-saved-objects-api-server
import { getDocLinks, getDocLinksMeta } from '@kbn/doc-links';
import type { DocLinksServiceStart } from '@kbn/core-doc-links-server';
import { baselineDocuments, baselineTypes } from './kibana_migrator_test_kit.fixtures';
import { delay } from './test_utils';

export const defaultLogFilePath = Path.join(__dirname, 'kibana_migrator_test_kit.log');

Expand Down Expand Up @@ -121,6 +122,7 @@ export const getKibanaMigratorTestKit = async ({
types = [],
logFilePath = defaultLogFilePath,
}: KibanaMigratorTestKitParams = {}): Promise<KibanaMigratorTestKit> => {
let hasRun = false;
const loggingSystem = new LoggingSystem();
const loggerFactory = loggingSystem.asLoggerFactory();

Expand All @@ -147,9 +149,13 @@ export const getKibanaMigratorTestKit = async ({
kibanaBranch
);

const runMigrations = async (rerun?: boolean) => {
const runMigrations = async () => {
if (hasRun) {
throw new Error('The test kit migrator can only be run once. Please instantiate it again.');
}
hasRun = true;
migrator.prepareMigrations();
const migrationResults = await migrator.runMigrations({ rerun });
const migrationResults = await migrator.runMigrations();
await loggingSystem.stop();
return migrationResults;
};
Expand Down Expand Up @@ -385,6 +391,7 @@ export const getIncompatibleMappingsMigrator = async ({
};

export const readLog = async (logFilePath: string = defaultLogFilePath): Promise<string> => {
await delay(0.1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:i_see_what_you_did_there:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙈

return await fs.readFile(logFilePath, 'utf-8');
};

Expand Down