Skip to content

Commit

Permalink
[Kibana migrator test kit] Flush logger after running migrations (ela…
Browse files Browse the repository at this point in the history
…stic#152818)

This PR addresses flakiness of:

- elastic#152472
- elastic#152448

These tests fail because we have a race condition. We are checking
certain conditions in the log files, and sometimes we do it before the
logs are actually written.

With the Kibana migrator test kit, we can actually flush + wait for all
the logging appenders. This way, we are sure that the logs are complete
when we inspect them in the tests.

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
2 people authored and bmorelli25 committed Mar 10, 2023
1 parent a53c6d2 commit 17a4ede
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ describe('migration v2', () => {
});

it('clean ups if migration fails', async () => {
const { migrator, client } = await setupNextMinor();
migrator.prepareMigrations();
const { runMigrations, client } = await setupNextMinor();

await expect(migrator.runMigrations()).rejects.toThrowErrorMatchingInlineSnapshot(`
await expect(runMigrations()).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to complete saved object migrations for the [${defaultKibanaIndex}] index: Migrations failed. Reason: 1 corrupt saved object documents were found: corrupt:2baf4de0-a6d4-11ed-ba5a-39196fc76e60
To allow migrations to proceed, please delete or fix these documents.
Expand Down Expand Up @@ -135,13 +134,12 @@ const setupBaseline = async () => {
},
];

const { migrator: baselineMigrator, client } = await getKibanaMigratorTestKit({
const { runMigrations, client } = await getKibanaMigratorTestKit({
types: typesCurrent,
logFilePath,
});

baselineMigrator.prepareMigrations();
await baselineMigrator.runMigrations();
await runMigrations();

// inject corrupt saved objects directly using esClient
await Promise.all(
Expand Down Expand Up @@ -175,7 +173,7 @@ const setupNextMinor = async () => {
},
];

const { migrator, client } = await getKibanaMigratorTestKit({
return await getKibanaMigratorTestKit({
types: typesNextMinor,
kibanaVersion: nextMinor,
logFilePath,
Expand Down Expand Up @@ -203,6 +201,4 @@ const setupNextMinor = async () => {
},
},
});

return { migrator, client };
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import {
} from '../kibana_migrator_test_kit';
import { delay } from '../test_utils';

// FLAKY: https://github.com/elastic/kibana/issues/152448
describe.skip('when migrating to a new version', () => {
describe('when migrating to a new version', () => {
let esServer: TestElasticsearchUtils['es'];
let esClient: ElasticsearchClient;
let migrator: IKibanaMigrator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ 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 @@ -150,7 +149,9 @@ export const getKibanaMigratorTestKit = async ({

const runMigrations = async (rerun?: boolean) => {
migrator.prepareMigrations();
return await migrator.runMigrations({ rerun });
const migrationResults = await migrator.runMigrations({ rerun });
await loggingSystem.stop();
return migrationResults;
};

const savedObjectsRepository = SavedObjectsRepository.createRepository(
Expand Down Expand Up @@ -283,13 +284,12 @@ const registerTypes = (
};

export const createBaseline = async () => {
const { client, migrator, savedObjectsRepository } = await getKibanaMigratorTestKit({
const { client, runMigrations, savedObjectsRepository } = await getKibanaMigratorTestKit({
kibanaIndex: defaultKibanaIndex,
types: baselineTypes,
});

migrator.prepareMigrations();
await migrator.runMigrations();
await runMigrations();

await savedObjectsRepository.bulkCreate(baselineDocuments, {
refresh: 'wait_for',
Expand Down Expand Up @@ -385,7 +385,6 @@ export const getIncompatibleMappingsMigrator = async ({
};

export const readLog = async (logFilePath: string = defaultLogFilePath): Promise<string> => {
await delay(0.1); // give the logger enough time to write to the file
return await fs.readFile(logFilePath, 'utf-8');
};

Expand Down

0 comments on commit 17a4ede

Please sign in to comment.