Skip to content

Commit

Permalink
[SR] Prevents negative values for Snapshot retention policies (#51295) (
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Nov 26, 2019
1 parent fa6c98e commit aecea19
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jest.mock('ui/i18n', () => {
return { I18nContext };
});

jest.mock('ui/new_platform');

const POLICY_NAME = 'my_policy';
const SNAPSHOT_NAME = 'my_snapshot';
const MIN_COUNT = '5';
Expand Down Expand Up @@ -141,6 +143,25 @@ describe.skip('<PolicyAdd />', () => {
'Minimum count cannot be greater than maximum count.',
]);
});

test('should not allow negative values for the delete after, minimum and maximum counts', () => {
const { find, form } = testBed;

form.setInputValue('expireAfterValueInput', '-1');
find('expireAfterValueInput').simulate('blur');

form.setInputValue('minCountInput', '-1');
find('minCountInput').simulate('blur');

form.setInputValue('maxCountInput', '-1');
find('maxCountInput').simulate('blur');

expect(form.getErrorsMessages()).toEqual([
'Delete after cannot be negative.',
'Minimum count cannot be negative.',
'Maximum count cannot be negative.',
]);
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const PolicyStepRetention: React.FunctionComponent<StepProps> = ({
}
describedByIds={['expirationDescription']}
isInvalid={touched.expireAfterValue && Boolean(errors.expireAfterValue)}
error={errors.expireAfter}
error={errors.expireAfterValue}
fullWidth
>
<EuiFlexGroup>
Expand All @@ -100,6 +100,7 @@ export const PolicyStepRetention: React.FunctionComponent<StepProps> = ({
});
}}
data-test-subj="expireAfterValueInput"
min={0}
/>
</EuiFlexItem>
<EuiFlexItem>
Expand Down Expand Up @@ -167,6 +168,7 @@ export const PolicyStepRetention: React.FunctionComponent<StepProps> = ({
});
}}
data-test-subj="minCountInput"
min={0}
/>
</EuiFormRow>
</EuiFlexItem>
Expand All @@ -179,6 +181,7 @@ export const PolicyStepRetention: React.FunctionComponent<StepProps> = ({
/>
}
describedByIds={['countDescription']}
isInvalid={touched.maxCount && Boolean(errors.maxCount)}
error={errors.maxCount}
fullWidth
>
Expand All @@ -193,6 +196,7 @@ export const PolicyStepRetention: React.FunctionComponent<StepProps> = ({
});
}}
data-test-subj="maxCountInput"
min={0}
/>
</EuiFormRow>
</EuiFlexItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export const validatePolicy = (policy: SlmPolicyPayload): PolicyValidation => {
schedule: [],
repository: [],
indices: [],
expireAfterValue: [],
minCount: [],
maxCount: [],
},
};

Expand Down Expand Up @@ -92,6 +94,34 @@ export const validatePolicy = (policy: SlmPolicyPayload): PolicyValidation => {
})
);
}

if (retention && retention.expireAfterValue && retention.expireAfterValue < 0) {
validation.errors.expireAfterValue.push(
i18n.translate(
'xpack.snapshotRestore.policyValidation.invalidNegativeDeleteAfterErrorMessage',
{
defaultMessage: 'Delete after cannot be negative.',
}
)
);
}

if (retention && retention.minCount && retention.minCount < 0) {
validation.errors.minCount.push(
i18n.translate('xpack.snapshotRestore.policyValidation.invalidNegativeMinCountErrorMessage', {
defaultMessage: 'Minimum count cannot be negative.',
})
);
}

if (retention && retention.maxCount && retention.maxCount < 0) {
validation.errors.maxCount.push(
i18n.translate('xpack.snapshotRestore.policyValidation.invalidNegativeMaxCountErrorMessage', {
defaultMessage: 'Maximum count cannot be negative.',
})
);
}

// Remove fields with no errors
validation.errors = Object.entries(validation.errors)
.filter(([key, value]) => value.length > 0)
Expand Down

0 comments on commit aecea19

Please sign in to comment.