Skip to content

Commit

Permalink
[UA] skip system indices migration status check if `featureSet.migrat…
Browse files Browse the repository at this point in the history
…eSystemIndices` is set to `false` (#150710)

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
Bamieh and kibanamachine authored Feb 9, 2023
1 parent 4308929 commit d2f8639
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
54 changes: 43 additions & 11 deletions x-pack/plugins/upgrade_assistant/server/routes/status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import { kibanaResponseFactory } from '@kbn/core/server';

import { handleEsError } from '../shared_imports';
import { createMockRouter, MockRouter, routeHandlerContextMock } from './__mocks__/routes.mock';
import { createMockRouter, routeHandlerContextMock } from './__mocks__/routes.mock';
import { createRequestMock } from './__mocks__/request.mock';
import { registerUpgradeStatusRoute } from './status';
import { getESUpgradeStatus } from '../lib/es_deprecations_status';
import { getKibanaUpgradeStatus } from '../lib/kibana_status';
import { getESSystemIndicesMigrationStatus } from '../lib/es_system_indices_migration';
import type { FeatureSet } from '../../common/types';

jest.mock('../lib/es_version_precheck', () => ({
versionCheckHandlerWrapper: (a: any) => a,
Expand Down Expand Up @@ -86,31 +87,33 @@ const systemIndicesNoMigrationResponse = {
};

describe('Status API', () => {
let mockRouter: MockRouter;
let routeDependencies: any;

beforeEach(() => {
mockRouter = createMockRouter();
routeDependencies = {
const registerRoutes = (featureSetOverrides: Partial<FeatureSet> = {}) => {
const mockRouter = createMockRouter();
const routeDependencies: any = {
config: {
featureSet: {
mlSnapshots: true,
migrateSystemIndices: true,
reindexCorrectiveActions: true,
...featureSetOverrides,
},
},
router: mockRouter,
lib: { handleEsError },
};

registerUpgradeStatusRoute(routeDependencies);
});

afterEach(() => {
jest.resetAllMocks();
});
return { mockRouter, routeDependencies };
};

describe('GET /api/upgrade_assistant/status', () => {
afterEach(() => {
jest.resetAllMocks();
});

it('returns readyForUpgrade === false if Kibana or ES contain critical deprecations and no system indices need migration', async () => {
const { routeDependencies } = registerRoutes();
getESUpgradeStatusMock.mockResolvedValue(esDeprecationsResponse);

getKibanaUpgradeStatusMock.mockResolvedValue({
Expand All @@ -124,6 +127,7 @@ describe('Status API', () => {
pathPattern: '/api/upgrade_assistant/status',
})(routeHandlerContextMock, createRequestMock(), kibanaResponseFactory);

expect(getESSystemIndicesMigrationStatusMock).toBeCalledTimes(1);
expect(resp.status).toEqual(200);
expect(resp.payload).toEqual({
readyForUpgrade: false,
Expand All @@ -133,6 +137,7 @@ describe('Status API', () => {
});

it('returns readyForUpgrade === false if Kibana or ES contain critical deprecations and system indices need migration', async () => {
const { routeDependencies } = registerRoutes();
getESUpgradeStatusMock.mockResolvedValue(esDeprecationsResponse);

getKibanaUpgradeStatusMock.mockResolvedValue({
Expand All @@ -146,6 +151,7 @@ describe('Status API', () => {
pathPattern: '/api/upgrade_assistant/status',
})(routeHandlerContextMock, createRequestMock(), kibanaResponseFactory);

expect(getESSystemIndicesMigrationStatusMock).toBeCalledTimes(1);
expect(resp.status).toEqual(200);
expect(resp.payload).toEqual({
readyForUpgrade: false,
Expand All @@ -155,6 +161,7 @@ describe('Status API', () => {
});

it('returns readyForUpgrade === false if no critical Kibana or ES deprecations but system indices need migration', async () => {
const { routeDependencies } = registerRoutes();
getESUpgradeStatusMock.mockResolvedValue(esNoDeprecationsResponse);

getKibanaUpgradeStatusMock.mockResolvedValue({
Expand All @@ -168,6 +175,7 @@ describe('Status API', () => {
pathPattern: '/api/upgrade_assistant/status',
})(routeHandlerContextMock, createRequestMock(), kibanaResponseFactory);

expect(getESSystemIndicesMigrationStatusMock).toBeCalledTimes(1);
expect(resp.status).toEqual(200);
expect(resp.payload).toEqual({
readyForUpgrade: false,
Expand All @@ -177,6 +185,7 @@ describe('Status API', () => {
});

it('returns readyForUpgrade === true if there are no critical deprecations and no system indices need migration', async () => {
const { routeDependencies } = registerRoutes();
getESUpgradeStatusMock.mockResolvedValue(esNoDeprecationsResponse);

getKibanaUpgradeStatusMock.mockResolvedValue({
Expand All @@ -197,7 +206,30 @@ describe('Status API', () => {
});
});

it('skips ES system indices migration check when featureSet.migrateSystemIndices is set to false', async () => {
const { routeDependencies } = registerRoutes({ migrateSystemIndices: false });
getESUpgradeStatusMock.mockResolvedValue(esNoDeprecationsResponse);

getKibanaUpgradeStatusMock.mockResolvedValue({
totalCriticalDeprecations: 0,
});

getESSystemIndicesMigrationStatusMock.mockResolvedValue(systemIndicesMigrationResponse);
const resp = await routeDependencies.router.getHandler({
method: 'get',
pathPattern: '/api/upgrade_assistant/status',
})(routeHandlerContextMock, createRequestMock(), kibanaResponseFactory);

expect(getESSystemIndicesMigrationStatusMock).toBeCalledTimes(0);
expect(resp.status).toEqual(200);
expect(resp.payload).toEqual({
readyForUpgrade: true,
details: 'All deprecation warnings have been resolved.',
});
});

it('returns an error if it throws', async () => {
const { routeDependencies } = registerRoutes();
getESUpgradeStatusMock.mockRejectedValue(new Error('test error'));

getKibanaUpgradeStatusMock.mockResolvedValue({
Expand Down
22 changes: 20 additions & 2 deletions x-pack/plugins/upgrade_assistant/server/routes/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,27 @@ export function registerUpgradeStatusRoute({
esClient,
featureSet
);
// Fetch system indices migration status

const getSystemIndicesMigrationStatus = async () => {
/**
* Skip system indices migration status check if `featureSet.migrateSystemIndices`
* is set to `false`. This flag is enabled from configs for major version stack ugprades.
* returns `migration_status: 'NO_MIGRATION_NEEDED'` to indicate no migation needed.
*/
if (!featureSet.migrateSystemIndices) {
return {
migration_status: 'NO_MIGRATION_NEEDED',
features: [],
};
}

// Fetch system indices migration status from ES
return await getESSystemIndicesMigrationStatus(esClient.asCurrentUser);
};

const { migration_status: systemIndicesMigrationStatus, features } =
await getESSystemIndicesMigrationStatus(esClient.asCurrentUser);
await getSystemIndicesMigrationStatus();

const notMigratedSystemIndices = features.filter(
(feature) => feature.migration_status !== 'NO_MIGRATION_NEEDED'
).length;
Expand Down

0 comments on commit d2f8639

Please sign in to comment.