Skip to content

Commit

Permalink
add some FT for security_and_spaces per PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuellr committed Jul 15, 2020
1 parent d1ab30e commit 8df8d44
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ import {
ES_TEST_INDEX_NAME,
getUrlPrefix,
ObjectRemover,
getEventLog,
} from '../../../common/lib';
import { FtrProviderContext } from '../../../common/ftr_provider_context';
import { IValidatedEvent } from '../../../../../plugins/event_log/server';

const NANOS_IN_MILLIS = 1000 * 1000;

// eslint-disable-next-line import/no-default-export
export default function ({ getService }: FtrProviderContext) {
Expand Down Expand Up @@ -107,6 +111,13 @@ export default function ({ getService }: FtrProviderContext) {
reference,
source: 'action:test.index-record',
});

await validateEventLog({
spaceId: space.id,
actionId: createdAction.id,
outcome: 'success',
message: `action executed: test.index-record:${createdAction.id}: My action`,
});
break;
default:
throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`);
Expand Down Expand Up @@ -480,4 +491,66 @@ export default function ({ getService }: FtrProviderContext) {
});
}
});

interface ValidateEventLogParams {
spaceId: string;
actionId: string;
outcome: string;
message: string;
errorMessage?: string;
}

async function validateEventLog(params: ValidateEventLogParams): Promise<void> {
const { spaceId, actionId, outcome, message, errorMessage } = params;

const events: IValidatedEvent[] = await retry.try(async () => {
return await getEventLog({
getService,
spaceId,
type: 'action',
id: actionId,
provider: 'actions',
actions: ['execute'],
});
});

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

const event = events[0];

const duration = event?.event?.duration;
const eventStart = Date.parse(event?.event?.start || 'undefined');
const eventEnd = Date.parse(event?.event?.end || 'undefined');
const dateNow = Date.now();

expect(typeof duration).to.be('number');
expect(eventStart).to.be.ok();
expect(eventEnd).to.be.ok();

const durationDiff = Math.abs(
Math.round(duration! / NANOS_IN_MILLIS) - (eventEnd - eventStart)
);

// account for rounding errors
expect(durationDiff < 1).to.equal(true);
expect(eventStart <= eventEnd).to.equal(true);
expect(eventEnd <= dateNow).to.equal(true);

expect(event?.event?.outcome).to.equal(outcome);

expect(event?.kibana?.saved_objects).to.eql([
{
rel: 'primary',
type: 'action',
id: actionId,
namespace: spaceId,
},
]);

expect(event?.message).to.eql(message);

if (errorMessage) {
expect(event?.error?.message).to.eql(errorMessage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import {
ObjectRemover,
AlertUtils,
TaskManagerUtils,
getEventLog,
} from '../../../common/lib';
import { IValidatedEvent } from '../../../../../plugins/event_log/server';

const NANOS_IN_MILLIS = 1000 * 1000;

// eslint-disable-next-line import/no-default-export
export default function alertTests({ getService }: FtrProviderContext) {
Expand Down Expand Up @@ -160,6 +164,13 @@ instanceStateValue: true
});

await taskManagerUtils.waitForActionTaskParamsToBeCleanedUp(testStart);

await validateEventLog({
spaceId: space.id,
alertId,
outcome: 'success',
message: `alert executed: test.always-firing:${alertId}: 'abc'`,
});
break;
default:
throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`);
Expand Down Expand Up @@ -928,4 +939,66 @@ instanceStateValue: true
});
}
});

interface ValidateEventLogParams {
spaceId: string;
alertId: string;
outcome: string;
message: string;
errorMessage?: string;
}

async function validateEventLog(params: ValidateEventLogParams): Promise<void> {
const { spaceId, alertId, outcome, message, errorMessage } = params;

const events: IValidatedEvent[] = await retry.try(async () => {
return await getEventLog({
getService,
spaceId,
type: 'alert',
id: alertId,
provider: 'alerting',
actions: ['execute'],
});
});

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

const event = events[0];

const duration = event?.event?.duration;
const eventStart = Date.parse(event?.event?.start || 'undefined');
const eventEnd = Date.parse(event?.event?.end || 'undefined');
const dateNow = Date.now();

expect(typeof duration).to.be('number');
expect(eventStart).to.be.ok();
expect(eventEnd).to.be.ok();

const durationDiff = Math.abs(
Math.round(duration! / NANOS_IN_MILLIS) - (eventEnd - eventStart)
);

// account for rounding errors
expect(durationDiff < 1).to.equal(true);
expect(eventStart <= eventEnd).to.equal(true);
expect(eventEnd <= dateNow).to.equal(true);

expect(event?.event?.outcome).to.equal(outcome);

expect(event?.kibana?.saved_objects).to.eql([
{
rel: 'primary',
type: 'alert',
id: alertId,
namespace: spaceId,
},
]);

expect(event?.message).to.eql(message);

if (errorMessage) {
expect(event?.error?.message).to.eql(errorMessage);
}
}
}

0 comments on commit 8df8d44

Please sign in to comment.