Skip to content

Commit

Permalink
add version-specific logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Feb 18, 2021
1 parent 2e2e92f commit b23698b
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 144 deletions.
15 changes: 15 additions & 0 deletions x-pack/plugins/upgrade_assistant/common/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import SemVer from 'semver/classes/semver';

/*
* These constants are used only in tests to add conditional logic based on Kibana version
* On master, the version should represent the next major version (e.g., master --> 8.0.0)
* The release branch should match the release version (e.g., 7.x --> 7.0.0)
*/
export const mockKibanaVersion = '8.0.0';
export const mockKibanaSemverVersion = new SemVer(mockKibanaVersion);
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/

import React from 'react';
import SemVer from 'semver/classes/semver';
import { mountWithIntl } from '@kbn/test/jest';
import { httpServiceMock } from 'src/core/public/mocks';
import { mockKibanaSemverVersion } from '../../../common/constants';
import { UpgradeAssistantTabs } from './tabs';
import { LoadingState } from './types';

Expand All @@ -18,7 +18,6 @@ import { OverviewTab } from './tabs/overview';
const promisesToResolve = () => new Promise((resolve) => setTimeout(resolve, 0));

const mockHttp = httpServiceMock.createSetupContract();
const mockKibanaVersion = new SemVer('8.0.0');

jest.mock('../app_context', () => {
return {
Expand All @@ -29,9 +28,9 @@ jest.mock('../app_context', () => {
ELASTIC_WEBSITE_URL: 'https://www.elastic.co/',
},
kibanaVersionInfo: {
currentMajor: mockKibanaVersion.major,
prevMajor: mockKibanaVersion.major - 1,
nextMajor: mockKibanaVersion.major + 1,
currentMajor: mockKibanaSemverVersion.major,
prevMajor: mockKibanaSemverVersion.major - 1,
nextMajor: mockKibanaSemverVersion.major + 1,
},
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { shallow } from 'enzyme';
import React from 'react';
import SemVer from 'semver/classes/semver';
import { mockKibanaSemverVersion } from '../../../../../common/constants';

import { LoadingState } from '../../types';
import AssistanceData from '../__fixtures__/checkup_api_response.json';
Expand All @@ -22,8 +22,6 @@ const defaultProps = {
setSelectedTabIndex: jest.fn(),
};

const mockKibanaVersion = new SemVer('8.0.0');

jest.mock('../../../app_context', () => {
return {
useAppContext: () => {
Expand All @@ -33,9 +31,9 @@ jest.mock('../../../app_context', () => {
ELASTIC_WEBSITE_URL: 'https://www.elastic.co/',
},
kibanaVersionInfo: {
currentMajor: mockKibanaVersion.major,
prevMajor: mockKibanaVersion.major - 1,
nextMajor: mockKibanaVersion.major + 1,
currentMajor: mockKibanaSemverVersion.major,
prevMajor: mockKibanaSemverVersion.major - 1,
nextMajor: mockKibanaSemverVersion.major + 1,
},
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ export class IndexDeprecationTable extends React.Component<
// NOTE: this naive implementation assumes all indices in the table are
// should show the reindex button. This should work for known use cases.
const { indices } = this.props;
const showActionsColumn = Boolean(indices.find((i) => i.reindex === true));
const hasActionsColumn = Boolean(indices.find((i) => i.reindex === true));

if (showActionsColumn === false) {
if (hasActionsColumn === false) {
return null;
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { I18nProvider } from '@kbn/i18n/react';
import { mount, shallow } from 'enzyme';
import React from 'react';
import { mockKibanaSemverVersion } from '../../../../../../../../common/constants';

import { ReindexWarning } from '../../../../../../../../common/types';
import { idForWarning, WarningsFlyoutStep } from './warnings_step';
Expand All @@ -20,6 +21,11 @@ jest.mock('../../../../../../app_context', () => {
DOC_LINK_VERSION: 'current',
ELASTIC_WEBSITE_URL: 'https://www.elastic.co/',
},
kibanaVersionInfo: {
currentMajor: mockKibanaSemverVersion.major,
prevMajor: mockKibanaSemverVersion.major - 1,
nextMajor: mockKibanaSemverVersion.major + 1,
},
};
},
};
Expand All @@ -37,19 +43,21 @@ describe('WarningsFlyoutStep', () => {
expect(shallow(<WarningsFlyoutStep {...defaultProps} />)).toMatchSnapshot();
});

it('does not allow proceeding until all are checked', () => {
const wrapper = mount(
<I18nProvider>
<WarningsFlyoutStep {...defaultProps} />
</I18nProvider>
);
const button = wrapper.find('EuiButton');

button.simulate('click');
expect(defaultProps.advanceNextStep).not.toHaveBeenCalled();

wrapper.find(`input#${idForWarning(ReindexWarning.customTypeName)}`).simulate('change');
button.simulate('click');
expect(defaultProps.advanceNextStep).toHaveBeenCalled();
});
if (mockKibanaSemverVersion.major === 7) {
it('does not allow proceeding until all are checked', () => {
const wrapper = mount(
<I18nProvider>
<WarningsFlyoutStep {...defaultProps} />
</I18nProvider>
);
const button = wrapper.find('EuiButton');

button.simulate('click');
expect(defaultProps.advanceNextStep).not.toHaveBeenCalled();

wrapper.find(`input#${idForWarning(ReindexWarning.customTypeName)}`).simulate('change');
button.simulate('click');
expect(defaultProps.advanceNextStep).toHaveBeenCalled();
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const WarningsFlyoutStep: React.FunctionComponent<WarningsConfirmationFly
}));
};

const { docLinks } = useAppContext();
const { docLinks, kibanaVersionInfo } = useAppContext();
const { ELASTIC_WEBSITE_URL, DOC_LINK_VERSION } = docLinks;
const esDocBasePath = `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/reference`;

Expand Down Expand Up @@ -130,7 +130,7 @@ export const WarningsFlyoutStep: React.FunctionComponent<WarningsConfirmationFly

<EuiSpacer />

{warnings.includes(ReindexWarning.customTypeName) && (
{kibanaVersionInfo.currentMajor === 7 && warnings.includes(ReindexWarning.customTypeName) && (
<WarningCheckbox
checkedIds={checkedIds}
onChange={onChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
* 2.0.
*/

import { SemVer } from 'semver';
import { mockKibanaSemverVersion } from '../../../common/constants';

export const MOCK_VERSION_STRING = '8.0.0';

export const getMockVersionInfo = (versionString = MOCK_VERSION_STRING) => {
const currentVersion = new SemVer(versionString);
const currentMajor = currentVersion.major;
export const getMockVersionInfo = () => {
const currentMajor = mockKibanaSemverVersion.major;

return {
currentVersion,
currentVersion: mockKibanaSemverVersion,
currentMajor,
prevMajor: currentMajor - 1,
nextMajor: currentMajor + 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SemVer } from 'semver';
import { IScopedClusterClient, kibanaResponseFactory } from 'src/core/server';
import { coreMock } from 'src/core/server/mocks';
import { licensingMock } from '../../../../plugins/licensing/server/mocks';
import { MOCK_VERSION_STRING, getMockVersionInfo } from './__fixtures__/version';
import { getMockVersionInfo } from './__fixtures__/version';

import {
esVersionCheck,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
* 2.0.
*/

import { mockKibanaSemverVersion, mockKibanaVersion } from '../../../common/constants';
import { ReindexWarning } from '../../../common/types';
import { versionService } from '../version';
import { MOCK_VERSION_STRING, getMockVersionInfo } from '../__fixtures__/version';
import { getMockVersionInfo } from '../__fixtures__/version';

import {
generateNewIndexName,
Expand Down Expand Up @@ -63,7 +64,7 @@ describe('transformFlatSettings', () => {

describe('sourceNameForIndex', () => {
beforeEach(() => {
versionService.setup(MOCK_VERSION_STRING);
versionService.setup(mockKibanaVersion);
});

it('parses internal indices', () => {
Expand All @@ -89,7 +90,7 @@ describe('sourceNameForIndex', () => {

describe('generateNewIndexName', () => {
beforeEach(() => {
versionService.setup(MOCK_VERSION_STRING);
versionService.setup(mockKibanaVersion);
});

it('parses internal indices', () => {
Expand Down Expand Up @@ -133,21 +134,23 @@ describe('getReindexWarnings', () => {
).toEqual([]);
});

it('returns customTypeName for non-_doc mapping types', () => {
expect(
getReindexWarnings({
settings: {},
mappings: { doc: {} },
})
).toEqual([ReindexWarning.customTypeName]);
});
if (mockKibanaSemverVersion.major === 7) {
it('returns customTypeName for non-_doc mapping types', () => {
expect(
getReindexWarnings({
settings: {},
mappings: { doc: {} },
})
).toEqual([ReindexWarning.customTypeName]);
});

it('does not return customTypeName for _doc mapping types', () => {
expect(
getReindexWarnings({
settings: {},
mappings: { _doc: {} },
})
).toEqual([]);
});
it('does not return customTypeName for _doc mapping types', () => {
expect(
getReindexWarnings({
settings: {},
mappings: { _doc: {} },
})
).toEqual([]);
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import { flow, omit } from 'lodash';
import { ReindexWarning } from '../../../common/types';
import { versionService } from '../version';
import { FlatSettings, FlatSettingsWithTypeName } from './types';

export const DEFAULT_TYPE_NAME = '_doc';

export interface ParsedIndexName {
cleanIndexName: string;
baseName: string;
Expand Down Expand Up @@ -75,21 +72,25 @@ export const generateNewIndexName = (indexName: string): string => {
* Returns an array of warnings that should be displayed to user before reindexing begins.
* @param flatSettings
*/
export const getReindexWarnings = (flatSettings: FlatSettingsWithTypeName): ReindexWarning[] => {
const typeNameWarning = usesCustomTypeName(flatSettings);
export const getReindexWarnings = (
flatSettings: FlatSettingsWithTypeName | FlatSettings
): ReindexWarning[] => {
const warnings = [
// No warnings yet for 8.0 -> 9.0
] as Array<[ReindexWarning, boolean]>;

const warnings = [[ReindexWarning.customTypeName, typeNameWarning]] as Array<
[ReindexWarning, boolean]
>;
if (versionService.getMajorVersion() === 7) {
const DEFAULT_TYPE_NAME = '_doc';
// In 7+ it's not possible to have more than one type anyways, so always grab the first
// (and only) key.
const typeName = Object.getOwnPropertyNames(flatSettings.mappings)[0];

return warnings.filter(([_, applies]) => applies).map(([warning, _]) => warning);
};
const typeNameWarning = Boolean(typeName && typeName !== DEFAULT_TYPE_NAME);

warnings.push([ReindexWarning.customTypeName, typeNameWarning]);
}

const usesCustomTypeName = (flatSettings: FlatSettingsWithTypeName) => {
// In 7+ it's not possible to have more than one type anyways, so always grab the first
// (and only) key.
const typeName = Object.getOwnPropertyNames(flatSettings.mappings)[0];
return typeName && typeName !== DEFAULT_TYPE_NAME;
return warnings.filter(([_, applies]) => applies).map(([warning, _]) => warning);
};

const removeUnsettableSettings = (settings: FlatSettings['settings']) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '../../../common/types';
import { versionService } from '../version';
import { LOCK_WINDOW, ReindexActions, reindexActionsFactory } from './reindex_actions';
import { MOCK_VERSION_STRING, getMockVersionInfo } from '../__fixtures__/version';
import { getMockVersionInfo } from '../__fixtures__/version';

const { currentMajor, prevMajor } = getMockVersionInfo();

Expand Down
Loading

0 comments on commit b23698b

Please sign in to comment.