Skip to content

Commit

Permalink
cleanup server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Jan 29, 2021
1 parent 06ee97b commit 45574dd
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { SemVer } from 'semver';

export const MOCK_VERSION_STRING = '8.0.0';

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

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

import {
esVersionCheck,
getAllNodeVersions,
verifyAllMatchKibanaVersion,
} from './es_version_precheck';
import { versionService } from './version';

const MOCK_CURRENT_VERSION = new SemVer('8.0.0');
const MOCK_CURRENT_MAJOR_VERSION = MOCK_CURRENT_VERSION.major;
const { currentMajor, currentVersion } = getMockVersionInfo();

describe('getAllNodeVersions', () => {
it('returns a list of unique node versions', async () => {
Expand Down Expand Up @@ -44,48 +45,45 @@ describe('getAllNodeVersions', () => {

describe('verifyAllMatchKibanaVersion', () => {
it('detects higher version nodes', () => {
const result = verifyAllMatchKibanaVersion(
[new SemVer('99999.0.0')],
MOCK_CURRENT_MAJOR_VERSION
);
const result = verifyAllMatchKibanaVersion([new SemVer('99999.0.0')], currentMajor);
expect(result.allNodesMatch).toBe(false);
expect(result.allNodesUpgraded).toBe(true);
});

it('detects lower version nodes', () => {
const result = verifyAllMatchKibanaVersion([new SemVer('0.0.0')], MOCK_CURRENT_MAJOR_VERSION);
const result = verifyAllMatchKibanaVersion([new SemVer('0.0.0')], currentMajor);
expect(result.allNodesMatch).toBe(false);
expect(result.allNodesUpgraded).toBe(true);
});

it('detects if all are on same major correctly', () => {
const versions = [
MOCK_CURRENT_VERSION,
MOCK_CURRENT_VERSION.inc('minor'),
MOCK_CURRENT_VERSION.inc('minor').inc('minor'),
currentVersion,
currentVersion.inc('minor'),
currentVersion.inc('minor').inc('minor'),
];

const result = verifyAllMatchKibanaVersion(versions, MOCK_CURRENT_MAJOR_VERSION);
const result = verifyAllMatchKibanaVersion(versions, currentMajor);
expect(result.allNodesMatch).toBe(true);
expect(result.allNodesUpgraded).toBe(false);
});

it('detects partial matches', () => {
const versions = [
new SemVer('0.0.0'),
MOCK_CURRENT_VERSION.inc('minor'),
MOCK_CURRENT_VERSION.inc('minor').inc('minor'),
currentVersion.inc('minor'),
currentVersion.inc('minor').inc('minor'),
];

const result = verifyAllMatchKibanaVersion(versions, MOCK_CURRENT_MAJOR_VERSION);
const result = verifyAllMatchKibanaVersion(versions, currentMajor);
expect(result.allNodesMatch).toBe(false);
expect(result.allNodesUpgraded).toBe(false);
});
});

describe('EsVersionPrecheck', () => {
beforeEach(() => {
versionService.setup('8.0.0');
versionService.setup(MOCK_VERSION_STRING);
});

it('returns a 403 when callCluster fails with a 403', async () => {
Expand Down Expand Up @@ -117,8 +115,8 @@ describe('EsVersionPrecheck', () => {
info: jest.fn().mockResolvedValue({
body: {
nodes: {
node1: { version: MOCK_CURRENT_VERSION.raw },
node2: { version: new SemVer(MOCK_CURRENT_VERSION.raw).inc('major').raw },
node1: { version: currentVersion.raw },
node2: { version: new SemVer(currentVersion.raw).inc('major').raw },
},
},
}),
Expand All @@ -142,8 +140,8 @@ describe('EsVersionPrecheck', () => {
info: jest.fn().mockResolvedValue({
body: {
nodes: {
node1: { version: new SemVer(MOCK_CURRENT_VERSION.raw).inc('major').raw },
node2: { version: new SemVer(MOCK_CURRENT_VERSION.raw).inc('major').raw },
node1: { version: new SemVer(currentVersion.raw).inc('major').raw },
node2: { version: new SemVer(currentVersion.raw).inc('major').raw },
},
},
}),
Expand All @@ -167,8 +165,8 @@ describe('EsVersionPrecheck', () => {
info: jest.fn().mockResolvedValue({
body: {
nodes: {
node1: { version: MOCK_CURRENT_VERSION.raw },
node2: { version: MOCK_CURRENT_VERSION.raw },
node1: { version: currentVersion.raw },
node2: { version: currentVersion.raw },
},
},
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SemVer } from 'semver';
import { versionService } from '../version';
import { MOCK_VERSION_STRING, getMockVersionInfo } from '../__fixtures__/version';

import {
generateNewIndexName,
getReindexWarnings,
sourceNameForIndex,
transformFlatSettings,
} from './index_settings';

const MOCK_VERSION = '8.0.0';
const MOCK_CURRENT_VERSION = new SemVer(MOCK_VERSION);
const MOCK_CURRENT_MAJOR_VERSION = MOCK_CURRENT_VERSION.major;
const MOCK_PREV_MAJOR_VERSION = MOCK_CURRENT_MAJOR_VERSION - 1;
const { currentMajor, prevMajor } = getMockVersionInfo();

describe('transformFlatSettings', () => {
it('does not blow up for empty mappings', () => {
Expand Down Expand Up @@ -63,7 +61,7 @@ describe('transformFlatSettings', () => {

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

it('parses internal indices', () => {
Expand All @@ -79,48 +77,46 @@ describe('sourceNameForIndex', () => {
expect(sourceNameForIndex('.myInternalIndex-reindexed-v5')).toEqual('.myInternalIndex');
});

it(`replaces reindexed-v${MOCK_PREV_MAJOR_VERSION} with reindexed-v${MOCK_CURRENT_MAJOR_VERSION} in newIndexName`, () => {
expect(sourceNameForIndex(`reindexed-v${MOCK_PREV_MAJOR_VERSION}-myIndex`)).toEqual('myIndex');
expect(sourceNameForIndex(`.reindexed-v${MOCK_PREV_MAJOR_VERSION}-myInternalIndex`)).toEqual(
it(`replaces reindexed-v${prevMajor} with reindexed-v${currentMajor} in newIndexName`, () => {
expect(sourceNameForIndex(`reindexed-v${prevMajor}-myIndex`)).toEqual('myIndex');
expect(sourceNameForIndex(`.reindexed-v${prevMajor}-myInternalIndex`)).toEqual(
'.myInternalIndex'
);
});
});

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

it('parses internal indices', () => {
expect(generateNewIndexName('.myInternalIndex')).toEqual(
`.reindexed-v${MOCK_CURRENT_MAJOR_VERSION}-myInternalIndex`
`.reindexed-v${currentMajor}-myInternalIndex`
);
});

it('parses non-internal indices', () => {
expect(generateNewIndexName('myIndex')).toEqual(
`reindexed-v${MOCK_CURRENT_MAJOR_VERSION}-myIndex`
);
expect(generateNewIndexName('myIndex')).toEqual(`reindexed-v${currentMajor}-myIndex`);
});

it('excludes appended v5 reindexing string from generateNewIndexName', () => {
expect(generateNewIndexName('myIndex-reindexed-v5')).toEqual(
`reindexed-v${MOCK_CURRENT_MAJOR_VERSION}-myIndex`
`reindexed-v${currentMajor}-myIndex`
);

expect(generateNewIndexName('.myInternalIndex-reindexed-v5')).toEqual(
`.reindexed-v${MOCK_CURRENT_MAJOR_VERSION}-myInternalIndex`
`.reindexed-v${currentMajor}-myInternalIndex`
);
});

it(`replaces reindexed-v${MOCK_PREV_MAJOR_VERSION} with reindexed-v${MOCK_CURRENT_MAJOR_VERSION} in generateNewIndexName`, () => {
expect(generateNewIndexName(`reindexed-v${MOCK_PREV_MAJOR_VERSION}-myIndex`)).toEqual(
`reindexed-v${MOCK_CURRENT_MAJOR_VERSION}-myIndex`
it(`replaces reindexed-v${prevMajor} with reindexed-v${currentMajor} in generateNewIndexName`, () => {
expect(generateNewIndexName(`reindexed-v${prevMajor}-myIndex`)).toEqual(
`reindexed-v${currentMajor}-myIndex`
);

expect(generateNewIndexName(`.reindexed-v${MOCK_PREV_MAJOR_VERSION}-myInternalIndex`)).toEqual(
`.reindexed-v${MOCK_CURRENT_MAJOR_VERSION}-myInternalIndex`
expect(generateNewIndexName(`.reindexed-v${prevMajor}-myInternalIndex`)).toEqual(
`.reindexed-v${currentMajor}-myInternalIndex`
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { SemVer } from 'semver';
import { RequestEvent } from '@elastic/elasticsearch/lib/Transport';
import { SavedObjectsErrorHelpers } from 'src/core/server';
import { elasticsearchServiceMock } from 'src/core/server/mocks';
Expand All @@ -20,10 +19,9 @@ import {
} from '../../../common/types';
import { versionService } from '../version';
import { LOCK_WINDOW, ReindexActions, reindexActionsFactory } from './reindex_actions';
import { MOCK_VERSION_STRING, getMockVersionInfo } from '../__fixtures__/version';

const MOCK_VERSION = '8.0.0';
const MOCK_CURRENT_MAJOR_VERSION = new SemVer(MOCK_VERSION).major;
const MOCK_PREV_MAJOR_VERSION = MOCK_CURRENT_MAJOR_VERSION - 1;
const { currentMajor, prevMajor } = getMockVersionInfo();

describe('ReindexActions', () => {
let client: jest.Mocked<any>;
Expand Down Expand Up @@ -53,15 +51,15 @@ describe('ReindexActions', () => {

describe('createReindexOp', () => {
beforeEach(() => {
versionService.setup(MOCK_VERSION);
versionService.setup(MOCK_VERSION_STRING);
client.create.mockResolvedValue();
});

it(`prepends reindexed-v${MOCK_CURRENT_MAJOR_VERSION} to new name`, async () => {
it(`prepends reindexed-v${currentMajor} to new name`, async () => {
await actions.createReindexOp('myIndex');
expect(client.create).toHaveBeenCalledWith(REINDEX_OP_TYPE, {
indexName: 'myIndex',
newIndexName: `reindexed-v${MOCK_CURRENT_MAJOR_VERSION}-myIndex`,
newIndexName: `reindexed-v${currentMajor}-myIndex`,
reindexOptions: undefined,
status: ReindexStatus.inProgress,
lastCompletedStep: ReindexStep.created,
Expand All @@ -73,11 +71,11 @@ describe('ReindexActions', () => {
});
});

it(`prepends reindexed-v${MOCK_CURRENT_MAJOR_VERSION} to new name, preserving leading period`, async () => {
it(`prepends reindexed-v${currentMajor} to new name, preserving leading period`, async () => {
await actions.createReindexOp('.internalIndex');
expect(client.create).toHaveBeenCalledWith(REINDEX_OP_TYPE, {
indexName: '.internalIndex',
newIndexName: `.reindexed-v${MOCK_CURRENT_MAJOR_VERSION}-internalIndex`,
newIndexName: `.reindexed-v${currentMajor}-internalIndex`,
reindexOptions: undefined,
status: ReindexStatus.inProgress,
lastCompletedStep: ReindexStep.created,
Expand All @@ -90,12 +88,12 @@ describe('ReindexActions', () => {
});

// in v5.6, the upgrade assistant appended to the index name instead of prepending
it(`prepends reindexed-v${MOCK_CURRENT_MAJOR_VERSION}- and removes reindex appended in v5`, async () => {
it(`prepends reindexed-v${currentMajor}- and removes reindex appended in v5`, async () => {
const indexName = 'myIndex-reindexed-v5';
await actions.createReindexOp(indexName);
expect(client.create).toHaveBeenCalledWith(REINDEX_OP_TYPE, {
indexName,
newIndexName: `reindexed-v${MOCK_CURRENT_MAJOR_VERSION}-myIndex`,
newIndexName: `reindexed-v${currentMajor}-myIndex`,
reindexOptions: undefined,
status: ReindexStatus.inProgress,
lastCompletedStep: ReindexStep.created,
Expand All @@ -107,11 +105,11 @@ describe('ReindexActions', () => {
});
});

it(`replaces reindexed-v${MOCK_PREV_MAJOR_VERSION} with reindexed-v${MOCK_CURRENT_MAJOR_VERSION}`, async () => {
await actions.createReindexOp(`reindexed-v${MOCK_PREV_MAJOR_VERSION}-myIndex`);
it(`replaces reindexed-v${prevMajor} with reindexed-v${currentMajor}`, async () => {
await actions.createReindexOp(`reindexed-v${prevMajor}-myIndex`);
expect(client.create).toHaveBeenCalledWith(REINDEX_OP_TYPE, {
indexName: `reindexed-v${MOCK_PREV_MAJOR_VERSION}-myIndex`,
newIndexName: `reindexed-v${MOCK_CURRENT_MAJOR_VERSION}-myIndex`,
indexName: `reindexed-v${prevMajor}-myIndex`,
newIndexName: `reindexed-v${currentMajor}-myIndex`,
reindexOptions: undefined,
status: ReindexStatus.inProgress,
lastCompletedStep: ReindexStep.created,
Expand Down
Loading

0 comments on commit 45574dd

Please sign in to comment.