Skip to content

Commit

Permalink
[Alerting] Fixing Failing test: X-Pack Alerting API Integration Tests…
Browse files Browse the repository at this point in the history
….x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/rbac_legacy·ts - alerting api integration security and spaces enabled Alerts legacy alerts alerts superuser at space1 should schedule actions on legacy alerts (#92549)

* Unskipping test

* Increasing runAt time

* Increasing runAt time

* Logging

* Increasing wait time even more

* Removing logs

* Resetting task status

* Re-enabling all tests

* Re-enabling all tests

* Adding comment
  • Loading branch information
ymao1 authored and kibanamachine committed Feb 24, 2021
1 parent 84b5b68 commit c2f7684
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import {
import { schema } from '@kbn/config-schema';
import { InvalidatePendingApiKey } from '../../../../../../../plugins/alerts/server/types';
import { RawAlert } from '../../../../../../../plugins/alerts/server/types';
import { TaskInstance } from '../../../../../../../plugins/task_manager/server';
import {
ConcreteTaskInstance,
TaskInstance,
} from '../../../../../../../plugins/task_manager/server';
import { FixtureStartDeps } from './plugin';

export function defineRoutes(core: CoreSetup<FixtureStartDeps>) {
Expand Down Expand Up @@ -188,6 +191,40 @@ export function defineRoutes(core: CoreSetup<FixtureStartDeps>) {
}
);

router.put(
{
path: '/api/alerts_fixture/{id}/reset_task_status',
validate: {
params: schema.object({
id: schema.string(),
}),
body: schema.object({
status: schema.string(),
}),
},
},
async (
context: RequestHandlerContext,
req: KibanaRequest<any, any, any, any>,
res: KibanaResponseFactory
): Promise<IKibanaResponse<any>> => {
const { id } = req.params;
const { status } = req.body;

const [{ savedObjects }] = await core.getStartServices();
const savedObjectsWithTasksAndAlerts = await savedObjects.getScopedClient(req, {
includedHiddenTypes: ['task', 'alert'],
});
const alert = await savedObjectsWithTasksAndAlerts.get<RawAlert>('alert', id);
const result = await savedObjectsWithTasksAndAlerts.update<ConcreteTaskInstance>(
'task',
alert.attributes.scheduledTaskId!,
{ status }
);
return res.ok({ body: result });
}
);

router.get(
{
path: '/api/alerts_fixture/api_keys_pending_invalidation',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { setupSpacesAndUsers, tearDown } from '..';
// eslint-disable-next-line import/no-default-export
export default function alertingTests({ loadTestFile, getService }: FtrProviderContext) {
describe('Alerts', () => {
// FLAKY: https://github.com/elastic/kibana/issues/86952
describe.skip('legacy alerts', () => {
describe('legacy alerts', () => {
before(async () => {
await setupSpacesAndUsers(getService);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export default function alertTests({ getService }: FtrProviderContext) {
case 'space_1_all at space1':
case 'superuser at space1':
case 'space_1_all_with_restricted_fixture at space1':
await resetTaskStatus(migratedAlertId);
await ensureLegacyAlertHasBeenMigrated(migratedAlertId);

await updateMigratedAlertToUseApiKeyOfCurrentUser(migratedAlertId);
Expand All @@ -92,6 +93,7 @@ export default function alertTests({ getService }: FtrProviderContext) {
await ensureAlertIsRunning();
break;
case 'global_read at space1':
await resetTaskStatus(migratedAlertId);
await ensureLegacyAlertHasBeenMigrated(migratedAlertId);

await updateMigratedAlertToUseApiKeyOfCurrentUser(migratedAlertId);
Expand All @@ -115,6 +117,7 @@ export default function alertTests({ getService }: FtrProviderContext) {
});
break;
case 'space_1_all_alerts_none_actions at space1':
await resetTaskStatus(migratedAlertId);
await ensureLegacyAlertHasBeenMigrated(migratedAlertId);

await updateMigratedAlertToUseApiKeyOfCurrentUser(migratedAlertId);
Expand All @@ -140,6 +143,21 @@ export default function alertTests({ getService }: FtrProviderContext) {
throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`);
}

async function resetTaskStatus(alertId: string) {
// occasionally when the task manager starts running while the alert saved objects
// are mid-migration, the task will fail and set its status to "failed". this prevents
// the alert from running ever again and downstream tasks that depend on successful alert
// execution will fail. this ensures the task status is set to "idle" so the
// task manager will continue claiming and executing it.
await supertest
.put(`${getUrlPrefix(space.id)}/api/alerts_fixture/${alertId}/reset_task_status`)
.set('kbn-xsrf', 'foo')
.send({
status: 'idle',
})
.expect(200);
}

async function ensureLegacyAlertHasBeenMigrated(alertId: string) {
const getResponse = await supertestWithoutAuth
.get(`${getUrlPrefix(space.id)}/api/alerts/alert/${alertId}`)
Expand Down

0 comments on commit c2f7684

Please sign in to comment.