|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +const mockGetBaseMappings = jest.fn(); |
| 10 | + |
| 11 | +// No other way to simulate a change in root mappings unfortunately |
| 12 | +jest.mock( |
| 13 | + '@kbn/core-saved-objects-migration-server-internal/src/core/build_active_mappings', |
| 14 | + () => { |
| 15 | + const actual = jest.requireActual( |
| 16 | + '@kbn/core-saved-objects-migration-server-internal/src/core/build_active_mappings' |
| 17 | + ); |
| 18 | + return { |
| 19 | + ...actual, |
| 20 | + getBaseMappings: () => mockGetBaseMappings(), |
| 21 | + }; |
| 22 | + } |
| 23 | +); |
| 24 | + |
| 25 | +import Path from 'path'; |
| 26 | +import fs from 'fs/promises'; |
| 27 | +import { type TestElasticsearchUtils } from '@kbn/core-test-helpers-kbn-server'; |
| 28 | +import '../jest_matchers'; |
| 29 | +import { getKibanaMigratorTestKit, startElasticsearch } from '../kibana_migrator_test_kit'; |
| 30 | +import { delay, parseLogFile } from '../test_utils'; |
| 31 | +import { getBaseMigratorParams, getFooType } from '../fixtures/zdt_base.fixtures'; |
| 32 | + |
| 33 | +export const logFilePath = Path.join(__dirname, 'root_field_addition.test.log'); |
| 34 | + |
| 35 | +describe('ZDT upgrades - introducing new root fields', () => { |
| 36 | + let esServer: TestElasticsearchUtils['es']; |
| 37 | + |
| 38 | + beforeAll(async () => { |
| 39 | + await fs.unlink(logFilePath).catch(() => {}); |
| 40 | + esServer = await startElasticsearch(); |
| 41 | + }); |
| 42 | + |
| 43 | + afterAll(async () => { |
| 44 | + await esServer?.stop(); |
| 45 | + await delay(10); |
| 46 | + }); |
| 47 | + |
| 48 | + const baseMappings = { |
| 49 | + dynamic: 'strict', |
| 50 | + properties: { |
| 51 | + type: { |
| 52 | + type: 'keyword', |
| 53 | + }, |
| 54 | + namespace: { |
| 55 | + type: 'keyword', |
| 56 | + }, |
| 57 | + coreMigrationVersion: { |
| 58 | + type: 'keyword', |
| 59 | + }, |
| 60 | + typeMigrationVersion: { |
| 61 | + type: 'version', |
| 62 | + }, |
| 63 | + }, |
| 64 | + }; |
| 65 | + |
| 66 | + const createBaseline = async () => { |
| 67 | + mockGetBaseMappings.mockReturnValue(baseMappings); |
| 68 | + |
| 69 | + const fooType = getFooType(); |
| 70 | + const { runMigrations } = await getKibanaMigratorTestKit({ |
| 71 | + ...getBaseMigratorParams(), |
| 72 | + types: [fooType], |
| 73 | + }); |
| 74 | + await runMigrations(); |
| 75 | + }; |
| 76 | + |
| 77 | + it('should support adding the new root fields', async () => { |
| 78 | + await createBaseline(); |
| 79 | + |
| 80 | + const updatedMappings = { |
| 81 | + ...baseMappings, |
| 82 | + properties: { |
| 83 | + ...baseMappings.properties, |
| 84 | + someNewRootField: { |
| 85 | + type: 'keyword', |
| 86 | + }, |
| 87 | + anotherNewRootField: { |
| 88 | + type: 'text', |
| 89 | + }, |
| 90 | + }, |
| 91 | + }; |
| 92 | + mockGetBaseMappings.mockReturnValue(updatedMappings); |
| 93 | + |
| 94 | + const fooType = getFooType(); |
| 95 | + |
| 96 | + const { runMigrations, client } = await getKibanaMigratorTestKit({ |
| 97 | + ...getBaseMigratorParams(), |
| 98 | + logFilePath, |
| 99 | + types: [fooType], |
| 100 | + }); |
| 101 | + |
| 102 | + await runMigrations(); |
| 103 | + |
| 104 | + const records = await parseLogFile(logFilePath); |
| 105 | + |
| 106 | + expect(records).toContainLogEntries( |
| 107 | + [ |
| 108 | + 'mapping version check result: greater', |
| 109 | + 'INIT -> UPDATE_INDEX_MAPPINGS', |
| 110 | + 'INDEX_STATE_UPDATE_DONE -> DOCUMENTS_UPDATE_INIT', |
| 111 | + '-> DONE', |
| 112 | + 'Migration completed', |
| 113 | + ], |
| 114 | + { ordered: true } |
| 115 | + ); |
| 116 | + |
| 117 | + const mappings = await client.indices.getMapping({ index: '.kibana_1' }); |
| 118 | + |
| 119 | + expect(mappings['.kibana_1'].mappings.properties).toEqual({ |
| 120 | + ...updatedMappings.properties, |
| 121 | + foo: fooType.mappings, |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments