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

[Alerting][Event Log] ensures we wait for the right number of events in test #84189

Merged
merged 7 commits into from
Dec 3, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 43 additions & 10 deletions x-pack/test/alerting_api_integration/common/lib/get_event_log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,33 @@ import { IValidatedEvent } from '../../../../plugins/event_log/server';
import { getUrlPrefix } from '.';
import { FtrProviderContext } from '../ftr_provider_context';

interface GreaterThanEqualCondition {
gte: number;
}
interface EqualCondition {
equal: number;
}

function isEqualConsition(
condition: GreaterThanEqualCondition | EqualCondition
): condition is EqualCondition {
return Number.isInteger((condition as EqualCondition).equal);
}

interface GetEventLogParams {
getService: FtrProviderContext['getService'];
spaceId: string;
type: string;
id: string;
provider: string;
actions: string[];
actions: Map<string, { gte: number } | { equal: number }>;
}

// Return event log entries given the specified parameters; for the `actions`
// parameter, at least one event of each action must be in the returned entries.
export async function getEventLog(params: GetEventLogParams): Promise<IValidatedEvent[]> {
const { getService, spaceId, type, id, provider, actions } = params;
const supertest = getService('supertest');
const actionsSet = new Set(actions);

const spacePrefix = getUrlPrefix(spaceId);
const url = `${spacePrefix}/api/event_log/${type}/${id}/_find?per_page=5000`;
Expand All @@ -36,14 +48,35 @@ export async function getEventLog(params: GetEventLogParams): Promise<IValidated
const events: IValidatedEvent[] = (result.data as IValidatedEvent[])
.filter((event) => event?.event?.provider === provider)
.filter((event) => event?.event?.action)
.filter((event) => actionsSet.has(event?.event?.action!));
const foundActions = new Set(
events.map((event) => event?.event?.action).filter((action) => !!action)
);

for (const action of actions) {
if (!foundActions.has(action)) {
throw new Error(`no event found with action "${action}"`);
.filter((event) => actions.has(event?.event?.action!));

const foundActions = events
.map((event) => event?.event?.action)
.reduce((actionsSum, action) => {
if (action) {
actionsSum.set(action, 1 + (actionsSum.get(action) ?? 0));
}
return actionsSum;
}, new Map<string, number>());

for (const [action, condition] of actions.entries()) {
if (
!(
foundActions.has(action) &&
(isEqualConsition(condition)
? foundActions.get(action)! === condition.equal
: foundActions.get(action)! >= condition.gte)
)
) {
throw new Error(
`insufficient events found with action "${action}" (${
foundActions.get(action) ?? 0
} must be ${
isEqualConsition(condition)
? `equal to ${condition.equal}`
: `greater than or equal to ${condition.gte}`
})`
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,10 @@ export default function ({ getService }: FtrProviderContext) {
type: 'action',
id: actionId,
provider: 'actions',
actions: ['execute'],
actions: new Map([['execute', { equal: 1 }]]),
});
});

expect(events.length).to.equal(1);

const event = events[0];

const duration = event?.event?.duration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1096,12 +1096,10 @@ instanceStateValue: true
type: 'alert',
id: alertId,
provider: 'alerting',
actions: ['execute'],
actions: new Map([['execute', { gte: 1 }]]),
});
});

expect(events.length).to.be.greaterThan(0);

const event = events[0];

const duration = event?.event?.duration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default function eventLogTests({ getService }: FtrProviderContext) {
type: 'alert',
id: alertId,
provider: 'alerting',
actions: ['execute'],
actions: new Map([['execute', { gte: 1 }]]),
});
const errorEvents = someEvents.filter(
(event) => event?.kibana?.alerting?.status === 'error'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,10 @@ export default function ({ getService }: FtrProviderContext) {
type: 'action',
id: actionId,
provider: 'actions',
actions: ['execute'],
actions: new Map([['execute', { equal: 1 }]]),
});
});

expect(events.length).to.equal(1);

const event = events[0];

const duration = event?.event?.duration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export default function eventLogTests({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const retry = getService('retry');

// FLAKY: https://github.com/elastic/kibana/issues/81668
describe.skip('eventLog', () => {
describe('eventLog', () => {
const objectRemover = new ObjectRemover(supertest);

after(() => objectRemover.removeAll());
Expand Down Expand Up @@ -73,27 +72,22 @@ export default function eventLogTests({ getService }: FtrProviderContext) {
type: 'alert',
id: alertId,
provider: 'alerting',
actions: [
'execute',
'execute-action',
'new-instance',
'active-instance',
'recovered-instance',
],
actions: new Map([
// make sure the counts of the # of events per type are as expected
['execute', { gte: 4 }],
['execute-action', { equal: 2 }],
['new-instance', { equal: 1 }],
['active-instance', { gte: 1 }],
['recovered-instance', { equal: 1 }],
]),
});
});

// make sure the counts of the # of events per type are as expected
const executeEvents = getEventsByAction(events, 'execute');
const executeActionEvents = getEventsByAction(events, 'execute-action');
const newInstanceEvents = getEventsByAction(events, 'new-instance');
const recoveredInstanceEvents = getEventsByAction(events, 'recovered-instance');

expect(executeEvents.length >= 4).to.be(true);
expect(executeActionEvents.length).to.be(2);
expect(newInstanceEvents.length).to.be(1);
expect(recoveredInstanceEvents.length).to.be(1);

// make sure the events are in the right temporal order
const executeTimes = getTimestamps(executeEvents);
const executeActionTimes = getTimestamps(executeActionEvents);
Expand Down Expand Up @@ -137,7 +131,7 @@ export default function eventLogTests({ getService }: FtrProviderContext) {
validateInstanceEvent(event, `created new instance: 'instance'`);
break;
case 'recovered-instance':
validateInstanceEvent(event, `recovered instance: 'instance'`);
validateInstanceEvent(event, `instance 'instance' has recovered`);
break;
case 'active-instance':
validateInstanceEvent(event, `active instance: 'instance' in actionGroup: 'default'`);
Expand Down Expand Up @@ -182,7 +176,7 @@ export default function eventLogTests({ getService }: FtrProviderContext) {
type: 'alert',
id: alertId,
provider: 'alerting',
actions: ['execute'],
actions: new Map([['execute', { gte: 1 }]]),
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export default function createGetAlertInstanceSummaryTests({ getService }: FtrPr
type: 'alert',
id,
provider: 'alerting',
actions,
actions: new Map(actions.map((action) => [action, { gte: 1 }])),
});
});
}
Expand Down