Skip to content

Commit

Permalink
[RAM][Maintenance Window] Improve Maintenance Window Backend Integrat…
Browse files Browse the repository at this point in the history
…ion Testing (#157120)

## Summary
Resolves: #155902

Improves backend integration testing for maintenance windows, tests the
following:

# API:
- Add more tests around create/update, ensuring events are generated
correctly
- Add more tests around `/_active`, ensuring the query we use is solid
- Add more tests around cancelling/archiving
- Verify functionality of the 3 MW scenarios
- Verify summarized actions and security rules function correctly

### Checklist
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
JiaweiWu and kibanamachine authored May 16, 2023
1 parent 556ba07 commit 9910615
Show file tree
Hide file tree
Showing 11 changed files with 674 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export default function activeMaintenanceWindowTests({ getService }: FtrProvider
for (const scenario of UserAtSpaceScenarios) {
const { user, space } = scenario;
describe(scenario.id, () => {
afterEach(() => objectRemover.removeAll());
it('should handle update maintenance window request appropriately', async () => {
it('should handle get active maintenance window request appropriately', async () => {
// Create 2 active and 1 inactive maintenance window
const { body: createdMaintenanceWindow1 } = await supertest
.post(`${getUrlPrefix(space.id)}/internal/alerting/rules/maintenance_window`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function updateMaintenanceWindowTests({ getService }: FtrProvider
freq: 2, // weekly
},
};
after(() => objectRemover.removeAll());
afterEach(() => objectRemover.removeAll());

for (const scenario of UserAtSpaceScenarios) {
const { user, space } = scenario;
Expand Down Expand Up @@ -84,5 +84,103 @@ export default function updateMaintenanceWindowTests({ getService }: FtrProvider
});
});
}

it('can archive and unarchive a maintenance window', async () => {
const space1 = UserAtSpaceScenarios[1].space.id;
const { body: createdMaintenanceWindow } = await supertest
.post(`${getUrlPrefix(space1)}/internal/alerting/rules/maintenance_window`)
.set('kbn-xsrf', 'foo')
.send(createParams)
.expect(200);

objectRemover.add(
space1,
createdMaintenanceWindow.id,
'rules/maintenance_window',
'alerting',
true
);

expect(createdMaintenanceWindow.status).eql('running');

const { body: archive } = await supertest
.post(
`${getUrlPrefix(space1)}/internal/alerting/rules/maintenance_window/${
createdMaintenanceWindow.id
}/_archive`
)
.set('kbn-xsrf', 'foo')
.send({ archive: true })
.expect(200);

expect(archive.status).eql('archived');

const { body: unarchived } = await supertest
.post(
`${getUrlPrefix(space1)}/internal/alerting/rules/maintenance_window/${
createdMaintenanceWindow.id
}/_archive`
)
.set('kbn-xsrf', 'foo')
.send({ archive: false })
.expect(200);

expect(unarchived.status).eql('running');
});

it('archiving a finished maintenance window does not change the events', async () => {
const space1 = UserAtSpaceScenarios[1].space.id;
const { body: createdMaintenanceWindow } = await supertest
.post(`${getUrlPrefix(space1)}/internal/alerting/rules/maintenance_window`)
.set('kbn-xsrf', 'foo')
.send({
...createParams,
r_rule: {
...createParams.r_rule,
dtstart: moment.utc().subtract(1, 'day').toISOString(),
freq: 3, // daily
count: 4,
},
})
.expect(200);

objectRemover.add(
space1,
createdMaintenanceWindow.id,
'rules/maintenance_window',
'alerting',
true
);

const { body: finish } = await supertest
.post(
`${getUrlPrefix(space1)}/internal/alerting/rules/maintenance_window/${
createdMaintenanceWindow.id
}/_finish`
)
.set('kbn-xsrf', 'foo')
.send()
.expect(200);

// The finished maintenance window has a different end date for the first event
expect(finish.events[0].lte).eql(createdMaintenanceWindow.events[0].lte);
expect(finish.events[1].lte).not.eql(createdMaintenanceWindow.events[1].lte);
expect(finish.events[2].lte).eql(createdMaintenanceWindow.events[2].lte);
expect(finish.events[3].lte).eql(createdMaintenanceWindow.events[3].lte);

const { body: archive } = await supertest
.post(
`${getUrlPrefix(space1)}/internal/alerting/rules/maintenance_window/${
createdMaintenanceWindow.id
}/_archive`
)
.set('kbn-xsrf', 'foo')
.send({ archive: true })
.expect(200);

// Archiving should not change the events
expect(finish.events[0].lte).eql(archive.events[0].lte);
expect(finish.events[1].lte).eql(archive.events[1].lte);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function createMaintenanceWindowTests({ getService }: FtrProvider
freq: 2, // weekly
},
};
after(() => objectRemover.removeAll());
afterEach(() => objectRemover.removeAll());

for (const scenario of UserAtSpaceScenarios) {
const { user, space } = scenario;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function deleteMaintenanceWindowTests({ getService }: FtrProvider
freq: 2, // weekly
},
};
after(() => objectRemover.removeAll());
afterEach(() => objectRemover.removeAll());

for (const scenario of UserAtSpaceScenarios) {
const { user, space } = scenario;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ export default function findMaintenanceWindowTests({ getService }: FtrProviderCo
freq: 2, // weekly
},
};
after(() => objectRemover.removeAll());
afterEach(() => objectRemover.removeAll());

for (const scenario of UserAtSpaceScenarios) {
const { user, space } = scenario;
describe(scenario.id, () => {
afterEach(() => objectRemover.removeAll());
it('should handle update maintenance window request appropriately', async () => {
it('should handle find maintenance window request appropriately', async () => {
const { body: createdMaintenanceWindow1 } = await supertest
.post(`${getUrlPrefix(space.id)}/internal/alerting/rules/maintenance_window`)
.set('kbn-xsrf', 'foo')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,99 @@ export default function findMaintenanceWindowTests({ getService }: FtrProviderCo
});
});
}

it('should error when trying to finish a finished maintenance window', async () => {
const space1 = UserAtSpaceScenarios[1].space.id;
const { body: createdMaintenanceWindow } = await supertest
.post(`${getUrlPrefix(space1)}/internal/alerting/rules/maintenance_window`)
.set('kbn-xsrf', 'foo')
.send({
...createParams,
r_rule: {
...createParams.r_rule,
count: 1,
},
})
.expect(200);

objectRemover.add(
space1,
createdMaintenanceWindow.id,
'rules/maintenance_window',
'alerting',
true
);

const { body: finish } = await supertest
.post(
`${getUrlPrefix(space1)}/internal/alerting/rules/maintenance_window/${
createdMaintenanceWindow.id
}/_finish`
)
.set('kbn-xsrf', 'foo')
.send()
.expect(200);
expect(finish.status).eql('finished');

// Cant finish a finished maintenance window
await supertest
.post(
`${getUrlPrefix(space1)}/internal/alerting/rules/maintenance_window/${
createdMaintenanceWindow.id
}/_finish`
)
.set('kbn-xsrf', 'foo')
.send()
.expect(400);
});

it('should error when trying to finish a upcoming maintenance window', async () => {
const space1 = UserAtSpaceScenarios[1].space.id;
const { body: createdMaintenanceWindow } = await supertest
.post(`${getUrlPrefix(space1)}/internal/alerting/rules/maintenance_window`)
.set('kbn-xsrf', 'foo')
.send({
...createParams,
r_rule: {
...createParams.r_rule,
count: 2, // 2 occurrences
},
})
.expect(200);

objectRemover.add(
space1,
createdMaintenanceWindow.id,
'rules/maintenance_window',
'alerting',
true
);

const { body: finish } = await supertest
.post(
`${getUrlPrefix(space1)}/internal/alerting/rules/maintenance_window/${
createdMaintenanceWindow.id
}/_finish`
)
.set('kbn-xsrf', 'foo')
.send()
.expect(200);

// Status now upcoming, new start and end dates reflect the upcoming event
expect(finish.status).eql('upcoming');
expect(finish.event_start_time).eql(createdMaintenanceWindow.events[1].gte);
expect(finish.event_end_time).eql(createdMaintenanceWindow.events[1].lte);

// Cannot finish an upcoming maintenance window
await supertest
.post(
`${getUrlPrefix(space1)}/internal/alerting/rules/maintenance_window/${
createdMaintenanceWindow.id
}/_finish`
)
.set('kbn-xsrf', 'foo')
.send()
.expect(400);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function getMaintenanceWindowTests({ getService }: FtrProviderCon
freq: 2, // weekly
},
};
after(() => objectRemover.removeAll());
afterEach(() => objectRemover.removeAll());

for (const scenario of UserAtSpaceScenarios) {
const { user, space } = scenario;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function updateMaintenanceWindowTests({ getService }: FtrProvider
freq: 2, // weekly
},
};
after(() => objectRemover.removeAll());
afterEach(() => objectRemover.removeAll());

for (const scenario of UserAtSpaceScenarios) {
const { user, space } = scenario;
Expand Down Expand Up @@ -90,6 +90,58 @@ export default function updateMaintenanceWindowTests({ getService }: FtrProvider
});
}

it('should update fields correctly', async () => {
const { body: createdMaintenanceWindow } = await supertest
.post(`${getUrlPrefix('space1')}/internal/alerting/rules/maintenance_window`)
.set('kbn-xsrf', 'foo')
.send({
title: 'test-maintenance-window',
duration: 60 * 60 * 1000, // 1 hr
r_rule: {
dtstart: new Date().toISOString(),
tzid: 'UTC',
freq: 2, // weekly
count: 1,
},
})
.expect(200);

objectRemover.add(
'space1',
createdMaintenanceWindow.id,
'rules/maintenance_window',
'alerting',
true
);

const newRRule = {
dtstart: moment.utc().add(1, 'day').toISOString(),
tzid: 'CET',
freq: 3,
count: 5,
};

const { body: updatedMW } = await supertest
.post(
`${getUrlPrefix('space1')}/internal/alerting/rules/maintenance_window/${
createdMaintenanceWindow.id
}`
)
.set('kbn-xsrf', 'foo')
.send({
title: 'test-maintenance-window-new',
duration: 60 * 1000,
r_rule: newRRule,
enabled: false,
})
.expect(200);

expect(updatedMW.title).eql('test-maintenance-window-new');
expect(updatedMW.duration).eql(60 * 1000);
expect(updatedMW.r_rule).eql(newRRule);
expect(updatedMW.title).eql('test-maintenance-window-new');
});

it('should update RRule correctly when removing fields', async () => {
const { body: createdMaintenanceWindow } = await supertest
.post(`${getUrlPrefix('space1')}/internal/alerting/rules/maintenance_window`)
Expand Down
Loading

0 comments on commit 9910615

Please sign in to comment.