Skip to content

Commit

Permalink
do not alter the original policy in the serilalization process
Browse files Browse the repository at this point in the history
  • Loading branch information
jloleysens committed Sep 15, 2020
1 parent c0b8c1e commit 83ac6b9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import cloneDeep from 'lodash/cloneDeep';
import { serializePolicy } from './policy_serialization';
import {
defaultNewColdPhase,
Expand All @@ -12,6 +12,7 @@ import {
defaultNewHotPhase,
defaultNewWarmPhase,
} from '../../constants';
import { DataTierAllocationType } from '../../../../common/types';

describe('Policy serialization', () => {
test('serialize a policy using "default" data allocation', () => {
Expand Down Expand Up @@ -407,4 +408,57 @@ describe('Policy serialization', () => {
},
});
});

test('serialization does not alter the original policy', () => {
const originalPolicy = {
name: 'test',
phases: {
hot: { actions: {} },
warm: {
actions: { allocate: { include: {}, exclude: {}, require: { something: 'here' } } },
},
cold: {
actions: { allocate: { include: {}, exclude: {}, require: { something: 'here' } } },
},
frozen: {
actions: { allocate: { include: {}, exclude: {}, require: { something: 'here' } } },
},
},
};

const originalClone = cloneDeep(originalPolicy);

const deserializedPolicy = {
name: 'test',
phases: {
hot: { ...defaultNewHotPhase },
warm: {
...defaultNewWarmPhase,
dataTierAllocationType: 'none' as DataTierAllocationType,
selectedNodeAttrs: 'ignore:this',
phaseEnabled: true,
},
cold: {
...defaultNewColdPhase,
dataTierAllocationType: 'none' as DataTierAllocationType,
selectedNodeAttrs: 'ignore:this',
phaseEnabled: true,
},
frozen: {
...defaultNewFrozenPhase,
dataTierAllocationType: 'none' as DataTierAllocationType,
selectedNodeAttrs: 'ignore:this',
phaseEnabled: true,
},
delete: { ...defaultNewDeletePhase },
},
};

serializePolicy(deserializedPolicy, originalPolicy);
deserializedPolicy.phases.warm.dataTierAllocationType = 'custom';
serializePolicy(deserializedPolicy, originalPolicy);
deserializedPolicy.phases.warm.dataTierAllocationType = 'default';
serializePolicy(deserializedPolicy, originalPolicy);
expect(originalPolicy).toEqual(originalClone);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import cloneDeep from 'lodash/cloneDeep';

import {
AllocateAction,
PhaseWithAllocationAction,
Expand All @@ -14,7 +16,7 @@ export const serializePhaseWithAllocation = (
phase: PhaseWithAllocationAction,
originalPhaseActions: SerializedPhase['actions'] = {}
): SerializedPhase['actions'] => {
const esPhaseActions: SerializedPhase['actions'] = { ...originalPhaseActions };
const esPhaseActions: SerializedPhase['actions'] = cloneDeep(originalPhaseActions);

if (phase.dataTierAllocationType === 'custom' && phase.selectedNodeAttrs) {
const [name, value] = phase.selectedNodeAttrs.split(':');
Expand Down

0 comments on commit 83ac6b9

Please sign in to comment.