From c5fabffd983a6683de2cb08059c4598007f89f4b Mon Sep 17 00:00:00 2001 From: Alison Goryachev Date: Wed, 27 May 2020 07:02:32 -0400 Subject: [PATCH] [7.x] [SR] Fix bug when editing retention of slm policy (#67137) (#67436) --- .../client_integration/helpers/constant.ts | 2 +- .../client_integration/policy_edit.test.ts | 48 +++++++++++++++---- .../components/policy_form/policy_form.tsx | 5 +- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/constant.ts b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/constant.ts index 1fb6aa8686b7e..8524f2776a6c0 100644 --- a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/constant.ts +++ b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/constant.ts @@ -12,4 +12,4 @@ export const REPOSITORY_EDIT = getRepository({ name: REPOSITORY_NAME }); export const POLICY_NAME = 'my-test-policy'; -export const POLICY_EDIT = getPolicy({ name: POLICY_NAME }); +export const POLICY_EDIT = getPolicy({ name: POLICY_NAME, retention: { minCount: 1 } }); diff --git a/x-pack/plugins/snapshot_restore/__jest__/client_integration/policy_edit.test.ts b/x-pack/plugins/snapshot_restore/__jest__/client_integration/policy_edit.test.ts index 57363b3c873c9..edaf1891f6e81 100644 --- a/x-pack/plugins/snapshot_restore/__jest__/client_integration/policy_edit.test.ts +++ b/x-pack/plugins/snapshot_restore/__jest__/client_integration/policy_edit.test.ts @@ -10,12 +10,13 @@ import { setupEnvironment, pageHelpers, nextTick } from './helpers'; import { PolicyForm } from '../../public/application/components/policy_form'; import { PolicyFormTestBed } from './helpers/policy_form.helpers'; import { POLICY_EDIT } from './helpers/constant'; +import { TIME_UNITS } from '../../common/constants'; const { setup } = pageHelpers.policyEdit; const { setup: setupPolicyAdd } = pageHelpers.policyAdd; const EXPIRE_AFTER_VALUE = '5'; -const EXPIRE_AFTER_UNIT = 'm'; +const EXPIRE_AFTER_UNIT = TIME_UNITS.MINUTE; jest.mock('ui/i18n', () => { const I18nContext = ({ children }: any) => children; @@ -83,28 +84,23 @@ describe('', () => { }); describe('form payload', () => { - beforeEach(async () => { + it('should send the correct payload with changed values', async () => { const { form, actions } = testBed; const { snapshotName } = POLICY_EDIT; - // Complete step 1 + // Complete step 1, change snapshot name form.setInputValue('snapshotNameInput', `${snapshotName}-edited`); actions.clickNextButton(); - // Complete step 2 - // console.log(testBed.component.debug()); + // Complete step 2, enable ignore unavailable indices switch form.toggleEuiSwitch('ignoreUnavailableIndicesToggle'); actions.clickNextButton(); - // Complete step 3 + // Complete step 3, modify retention form.setInputValue('expireAfterValueInput', EXPIRE_AFTER_VALUE); form.setInputValue('expireAfterUnitSelect', EXPIRE_AFTER_UNIT); actions.clickNextButton(); - }); - - it('should send the correct payload with changed values', async () => { - const { actions } = testBed; await act(async () => { actions.clickSubmitButton(); @@ -120,6 +116,7 @@ describe('', () => { ignoreUnavailable: true, }, retention: { + ...POLICY_EDIT.retention, expireAfterValue: Number(EXPIRE_AFTER_VALUE), expireAfterUnit: EXPIRE_AFTER_UNIT, }, @@ -128,6 +125,37 @@ describe('', () => { }; expect(JSON.parse(JSON.parse(latestRequest.requestBody).body)).toEqual(expected); }); + + it('should provide a default time unit value for retention', async () => { + const { form, actions } = testBed; + + // Bypass step 1 + actions.clickNextButton(); + + // Bypass step 2 + actions.clickNextButton(); + + // Step 3: Add expire after value, but do not change unit + form.setInputValue('expireAfterValueInput', EXPIRE_AFTER_VALUE); + actions.clickNextButton(); + + await act(async () => { + actions.clickSubmitButton(); + await nextTick(); + }); + + const latestRequest = server.requests[server.requests.length - 1]; + + const expected = { + ...POLICY_EDIT, + retention: { + ...POLICY_EDIT.retention, + expireAfterValue: Number(EXPIRE_AFTER_VALUE), + expireAfterUnit: TIME_UNITS.DAY, // default value + }, + }; + expect(JSON.parse(JSON.parse(latestRequest.requestBody).body)).toEqual(expected); + }); }); }); }); diff --git a/x-pack/plugins/snapshot_restore/public/application/components/policy_form/policy_form.tsx b/x-pack/plugins/snapshot_restore/public/application/components/policy_form/policy_form.tsx index 524c8f8ed39a7..f9cad7cc4e070 100644 --- a/x-pack/plugins/snapshot_restore/public/application/components/policy_form/policy_form.tsx +++ b/x-pack/plugins/snapshot_restore/public/application/components/policy_form/policy_form.tsx @@ -66,9 +66,8 @@ export const PolicyForm: React.FunctionComponent = ({ ...(originalPolicy.config || {}), }, retention: { - ...(originalPolicy.retention || { - expireAfterUnit: TIME_UNITS.DAY, - }), + ...originalPolicy.retention, + expireAfterUnit: originalPolicy.retention?.expireAfterUnit ?? TIME_UNITS.DAY, }, });