Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ILM] Policy form should not throw away data #83077

Merged
merged 12 commits into from
Nov 20, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,41 @@ export const POLICY_WITH_NODE_ROLE_ALLOCATION: PolicyFromES = {
},
name: POLICY_NAME,
};

export const POLICY_WITH_KNOWN_AND_UNKNOWN_FIELDS = ({
version: 1,
modified_date: Date.now().toString(),
policy: {
foo: 'bar',
phases: {
hot: {
min_age: '0ms',
actions: {
rollover: {
unknown_setting: 123,
max_size: '50gb',
},
},
},
warm: {
actions: {
my_unfollow_action: {},
set_priority: {
priority: 22,
unknown_setting: true,
},
},
},
delete: {
wait_for_snapshot: {
policy: SNAPSHOT_POLICY_NAME,
},
delete: {
delete_searchable_snapshot: true,
},
},
},
name: POLICY_NAME,
},
name: POLICY_NAME,
} as any) as PolicyFromES;
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
POLICY_WITH_INCLUDE_EXCLUDE,
POLICY_WITH_NODE_ATTR_AND_OFF_ALLOCATION,
POLICY_WITH_NODE_ROLE_ALLOCATION,
POLICY_WITH_KNOWN_AND_UNKNOWN_FIELDS,
getDefaultHotPhasePolicy,
} from './constants';

Expand All @@ -31,6 +32,70 @@ describe('<EditPolicy />', () => {
server.restore();
});

describe('serialization', () => {
/**
* We assume that policies that populate this form are loaded directly from ES and so
* are valid according to ES. There may be settings in the policy created through the ILM
* API that the UI does not cater for, like the unfollow action. We do not want to overwrite
* the configuration for these actions in the UI.
*/
it('preserves policy settings it did not configure', async () => {
httpRequestsMockHelpers.setLoadPolicies([POLICY_WITH_KNOWN_AND_UNKNOWN_FIELDS]);
httpRequestsMockHelpers.setLoadSnapshotPolicies([]);
httpRequestsMockHelpers.setListNodes({
nodesByRoles: {},
nodesByAttributes: { test: ['123'] },
isUsingDeprecatedDataRoleConfig: false,
});

await act(async () => {
testBed = await setup();
});

const { component, actions } = testBed;
component.update();

// Set max docs to test whether we keep the unknown fields in that object after serializing
await actions.hot.setMaxDocs('1000');
// Remove the delete phase to ensure that we also correctly remove data
await actions.delete.enable(false);
await actions.savePolicy();

const latestRequest = server.requests[server.requests.length - 1];
const entirePolicy = JSON.parse(JSON.parse(latestRequest.requestBody).body);

expect(entirePolicy).toEqual({
foo: 'bar', // Made up value
name: 'my_policy',
phases: {
hot: {
actions: {
rollover: {
max_docs: 1000,
max_size: '50gb',
unknown_setting: 123, // Made up setting that should stay preserved
},
set_priority: {
priority: 100,
},
},
min_age: '0ms',
},
warm: {
actions: {
my_unfollow_action: {}, // Made up action
set_priority: {
priority: 22,
unknown_setting: true,
},
},
min_age: '0ms',
},
},
});
});
});

describe('hot phase', () => {
describe('serialization', () => {
beforeEach(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,12 @@ describe('edit policy', () => {
phases: {
hot: {
actions: {
set_priority: {
priority: 100,
},
rollover: {
max_size: '50gb',
max_age: '30d',
max_size: '50gb',
},
set_priority: {
priority: 100,
},
},
min_age: '0ms',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { i18nTexts } from '../i18n_texts';
const { emptyField, numberGreaterThanField } = fieldValidators;

const serializers = {
stringToNumber: (v: string): any => (v ? parseInt(v, 10) : undefined),
stringToNumber: (v: string): any => (v != null ? parseInt(v, 10) : undefined),
};

export const schema: FormSchema<FormInternal> = {
Expand Down
Loading