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

Bug 1832752: Monitoring silence form time tests #5327

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions frontend/integration-tests/tests/monitoring.scenario.ts
Expand Up @@ -20,6 +20,48 @@ const testDetailsPage = (subTitle, alertName, expectLabel = true) => {
}
};

const testSilenceTimeInputs = async () => {
// Default start and end times
expect(monitoringView.silenceStartNowCheckbox.getAttribute('checked')).toBeTruthy();
expect(monitoringView.silenceStartsAtInput.getAttribute('value')).toEqual('Now');
expect(monitoringView.silenceDurationMenuButton.getText()).toEqual('2h');
expect(monitoringView.silenceEndsAtInput.getAttribute('value')).toEqual('2h from now');

// Change duration
await monitoringView.silenceDurationMenuButton.click();
await monitoringView.wait(until.presenceOf(monitoringView.silenceDurationOption('1h')));
await monitoringView.silenceDurationOption('1h').click();
expect(monitoringView.silenceEndsAtInput.getAttribute('value')).toEqual('1h from now');

// Change to not start now
await monitoringView.silenceStartNowCheckbox.click();
expect(monitoringView.silenceStartNowCheckbox.getAttribute('checked')).toBeFalsy();
// Allow for some difference in times
expect(monitoringView.silenceStartsAtInput.getAttribute('value')).not.toEqual('Now');
expect(monitoringView.silenceStartsAtInput.getAttribute('value')).toBeTruthy();
monitoringView.silenceStartsAtInput.getAttribute('value').then((start: string) => {
expect(Date.parse(start) - Date.now()).toBeLessThan(10000);
monitoringView.silenceEndsAtInput.getAttribute('value').then((end: string) => {
expect(Date.parse(end) - Date.parse(start)).toEqual(60 * 60 * 1000);
});
});

// Invalid start time
await monitoringView.silenceStartsAtInput.sendKeys('abc');
expect(monitoringView.silenceEndsAtInput.getAttribute('value')).toEqual('-');

// Change to back to start now
await monitoringView.silenceStartNowCheckbox.click();
expect(monitoringView.silenceStartNowCheckbox.getAttribute('checked')).toBeTruthy();
expect(monitoringView.silenceEndsAtInput.getAttribute('value')).toEqual('1h from now');

// Change duration back again
await monitoringView.silenceDurationMenuButton.click();
await monitoringView.wait(until.presenceOf(monitoringView.silenceDurationOption('2h')));
await monitoringView.silenceDurationOption('2h').click();
expect(monitoringView.silenceEndsAtInput.getAttribute('value')).toEqual('2h from now');
};

describe('Monitoring: Alerts', () => {
afterEach(() => {
checkLogs();
Expand Down Expand Up @@ -68,6 +110,7 @@ describe('Monitoring: Alerts', () => {
it('creates a new Silence from an existing alert', async () => {
await monitoringView.actionButton.click();
await monitoringView.wait(until.presenceOf(monitoringView.saveButton));
await testSilenceTimeInputs();
await monitoringView.commentTextarea.sendKeys('Test Comment');
await monitoringView.saveButton.click();
expect(crudView.errorMessage.isPresent()).toBe(false);
Expand Down Expand Up @@ -127,6 +170,7 @@ describe('Monitoring: Silences', () => {
it('creates a new Silence', async () => {
await monitoringView.createButton.click();
await monitoringView.wait(until.presenceOf(monitoringView.matcherNameInput));
await testSilenceTimeInputs();
await monitoringView.matcherNameInput.sendKeys('alertname');
await monitoringView.matcherValueInput.sendKeys(testAlertName);
await monitoringView.commentTextarea.sendKeys('Test Comment');
Expand Down
6 changes: 6 additions & 0 deletions frontend/integration-tests/views/monitoring.view.ts
Expand Up @@ -26,6 +26,12 @@ export const firstAlertsListLink = $$('.co-resource-list__item a.co-resource-ite
// Silence form
export const matcherNameInput = $('input[placeholder=Name]');
export const matcherValueInput = $('input[placeholder=Value]');
export const silenceStartNowCheckbox = $('input[type=checkbox]');
export const silenceStartsAtInput = $$('[data-test-id="datetime"]').get(0);
export const silenceEndsAtInput = $$('[data-test-id="datetime"]').get(1);
export const silenceDurationMenuButton = $('[data-test-id="dropdown-button"]');
export const silenceDurationOption = (duration: string) =>
$(`[data-test-dropdown-menu="${duration}"]`);
export const commentTextarea = $('textarea');
export const saveButton = $('button[type=submit]');

Expand Down
14 changes: 9 additions & 5 deletions frontend/public/components/monitoring.tsx
Expand Up @@ -1281,6 +1281,7 @@ const DatetimeTextInput = (props) => {
<TextInput
{...props}
aria-label="Datetime"
data-test-id="datetime"
isValid={isValid || !!props.isDisabled}
pattern={pattern}
placeholder="YYYY/MM/DD hh:mm:ss"
Expand Down Expand Up @@ -1318,7 +1319,7 @@ const SilenceForm_: React.FC<SilenceFormProps> = ({ defaults, Info, title }) =>
const [createdBy, setCreatedBy] = React.useState(defaults.createdBy ?? '');
const [duration, setDuration] = React.useState(defaultDuration);
const [endsAt, setEndsAt] = React.useState(
defaults.endsAt ?? formatDate(new Date(now.setHours(now.getHours() + 2))),
defaults.endsAt ?? formatDate(new Date(new Date(now).setHours(now.getHours() + 2))),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working with dates in JS is still silly. 🙄

);
const [error, setError] = React.useState<string>();
const [inProgress, setInProgress] = React.useState(false);
Expand All @@ -1328,9 +1329,12 @@ const SilenceForm_: React.FC<SilenceFormProps> = ({ defaults, Info, title }) =>
);
const [startsAt, setStartsAt] = React.useState(defaults.startsAt ?? formatDate(now));

const durationEnd = formatDate(
new Date((isStartNow ? Date.now() : Date.parse(startsAt)) + parsePrometheusDuration(duration)),
);
const getEndsAtValue = (): string => {
const startsAtDate = Date.parse(startsAt);
return startsAtDate
? formatDate(new Date(startsAtDate + parsePrometheusDuration(duration)))
: '-';
};

const setMatcherField = (i: number, field: string, v: any): void => {
const newMatchers = _.clone(matchers);
Expand Down Expand Up @@ -1440,7 +1444,7 @@ const SilenceForm_: React.FC<SilenceFormProps> = ({ defaults, Info, title }) =>
) : (
<DatetimeTextInput
isDisabled
value={isStartNow ? `${duration} from now` : durationEnd}
value={isStartNow ? `${duration} from now` : getEndsAtValue()}
/>
)}
</div>
Expand Down