Skip to content

Commit

Permalink
[Upgrade Assistant] Delete deprecation log cache (elastic#114113)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabarasaba committed Oct 26, 2021
1 parent 2460388 commit 0748c08
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => {
]);
};

const setDeleteLogsCacheResponse = (response?: string, error?: ResponseError) => {
const status = error ? error.statusCode || 400 : 200;
const body = error ? error : response;
server.respondWith('DELETE', `${API_BASE_PATH}/deprecation_logging/cache`, [
status,
{ 'Content-Type': 'application/json' },
JSON.stringify(body),
]);
};

const setUpdateDeprecationLoggingResponse = (
response?: DeprecationLoggingStatus,
error?: ResponseError
Expand Down Expand Up @@ -169,6 +179,7 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => {
setDeleteMlSnapshotResponse,
setUpgradeMlSnapshotStatusResponse,
setLoadDeprecationLogsCountResponse,
setDeleteLogsCacheResponse,
setStartReindexingResponse,
setReindexStatusResponse,
setLoadMlUpgradeModeResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ describe('Overview - Fix deprecation logs step', () => {
describe('Step 3 - Resolve log issues', () => {
beforeEach(async () => {
httpRequestsMockHelpers.setLoadDeprecationLoggingResponse(getLoggingResponse(true));
httpRequestsMockHelpers.setDeleteLogsCacheResponse('ok');
});

test('With deprecation warnings', async () => {
Expand Down Expand Up @@ -337,6 +338,48 @@ describe('Overview - Fix deprecation logs step', () => {
expect(exists('noWarningsCallout')).toBe(true);
});

test('Shows a toast if deleting cache fails', async () => {
const error = {
statusCode: 500,
error: 'Internal server error',
message: 'Internal server error',
};

httpRequestsMockHelpers.setDeleteLogsCacheResponse(undefined, error);
// Initially we want to have the callout to have a warning state
httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 10 });

const addDanger = jest.fn();
await act(async () => {
testBed = await setupOverviewPage({
services: {
core: {
notifications: {
toasts: {
addDanger,
},
},
},
},
});
});

const { exists, actions, component } = testBed;

component.update();

httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 0 });

await actions.clickResetButton();

// The toast should always be shown if the delete logs cache fails.
expect(addDanger).toHaveBeenCalled();
// Even though we changed the response of the getLogsCountResponse, when the
// deleteLogsCache fails the getLogsCount api should not be called and the
// status of the callout should remain the same it initially was.
expect(exists('hasWarningsCallout')).toBe(true);
});

describe('Poll for logs count', () => {
beforeEach(async () => {
jest.useFakeTimers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { FunctionComponent, useEffect } from 'react';
import React, { FunctionComponent, useEffect, useState } from 'react';
import moment from 'moment-timezone';
import { FormattedDate, FormattedTime, FormattedMessage } from '@kbn/i18n/react';

Expand Down Expand Up @@ -44,6 +44,9 @@ const i18nTexts = {
defaultMessage: 'Reset counter',
}
),
errorToastTitle: i18n.translate('xpack.upgradeAssistant.overview.verifyChanges.errorToastTitle', {
defaultMessage: 'Could not delete deprecation logs cache',
}),
};

interface Props {
Expand All @@ -57,8 +60,12 @@ export const DeprecationsCountCheckpoint: FunctionComponent<Props> = ({
setCheckpoint,
setHasNoDeprecationLogs,
}) => {
const [isDeletingCache, setIsDeletingCache] = useState(false);
const {
services: { api },
services: {
api,
core: { notifications },
},
} = useAppContext();
const { data, error, isLoading, resendRequest, isInitialRequest } = api.getDeprecationLogsCount(
checkpoint
Expand All @@ -70,7 +77,19 @@ export const DeprecationsCountCheckpoint: FunctionComponent<Props> = ({
const calloutIcon = hasLogs ? 'alert' : 'check';
const calloutTestId = hasLogs ? 'hasWarningsCallout' : 'noWarningsCallout';

const onResetClick = () => {
const onResetClick = async () => {
setIsDeletingCache(true);
const { error: deleteLogsCacheError } = await api.deleteDeprecationLogsCache();
setIsDeletingCache(false);

if (deleteLogsCacheError) {
notifications.toasts.addDanger({
title: i18nTexts.errorToastTitle,
text: deleteLogsCacheError.message.toString(),
});
return;
}

const now = moment().toISOString();
setCheckpoint(now);
};
Expand Down Expand Up @@ -115,7 +134,12 @@ export const DeprecationsCountCheckpoint: FunctionComponent<Props> = ({
data-test-subj={calloutTestId}
>
<p>{i18nTexts.calloutBody}</p>
<EuiButton color={calloutTint} onClick={onResetClick} data-test-subj="resetLastStoredDate">
<EuiButton
color={calloutTint}
onClick={onResetClick}
isLoading={isDeletingCache || isLoading}
data-test-subj="resetLastStoredDate"
>
{i18nTexts.resetCounterButton}
</EuiButton>
</EuiCallOut>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ export class ApiService {
});
}

public deleteDeprecationLogsCache() {
return this.sendRequest({
path: `${API_BASE_PATH}/deprecation_logging/cache`,
method: 'delete',
});
}

public async updateIndexSettings(indexName: string, settings: string[]) {
const result = await this.sendRequest({
path: `${API_BASE_PATH}/${indexName}/index_settings`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,42 @@ describe('deprecation logging API', () => {
).rejects.toThrow('scary error!');
});
});

describe('DELETE /api/upgrade_assistant/deprecation_logging/cache', () => {
it('returns ok if if the cache was deleted', async () => {
(
routeHandlerContextMock.core.elasticsearch.client.asCurrentUser.transport
.request as jest.Mock
).mockResolvedValue({
body: 'ok',
});

const resp = await routeDependencies.router.getHandler({
method: 'delete',
pathPattern: '/api/upgrade_assistant/deprecation_logging/cache',
})(routeHandlerContextMock, createRequestMock(), kibanaResponseFactory);

expect(resp.status).toEqual(200);
expect(
routeHandlerContextMock.core.elasticsearch.client.asCurrentUser.transport.request
).toHaveBeenCalledWith({
method: 'DELETE',
path: '/_logging/deprecation_cache',
});
expect(resp.payload).toEqual('ok');
});

it('returns an error if it throws', async () => {
(
routeHandlerContextMock.core.elasticsearch.client.asCurrentUser.transport
.request as jest.Mock
).mockRejectedValue(new Error('scary error!'));
await expect(
routeDependencies.router.getHandler({
method: 'delete',
pathPattern: '/api/upgrade_assistant/deprecation_logging/cache',
})(routeHandlerContextMock, createRequestMock(), kibanaResponseFactory)
).rejects.toThrow('scary error!');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,33 @@ export function registerDeprecationLoggingRoutes({
}
)
);

router.delete(
{
path: `${API_BASE_PATH}/deprecation_logging/cache`,
validate: false,
},
versionCheckHandlerWrapper(
async (
{
core: {
elasticsearch: { client },
},
},
request,
response
) => {
try {
await client.asCurrentUser.transport.request({
method: 'DELETE',
path: '/_logging/deprecation_cache',
});

return response.ok({ body: 'ok' });
} catch (error) {
return handleEsError({ error, response });
}
}
)
);
}

0 comments on commit 0748c08

Please sign in to comment.