Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moving test to x-pack/test/rule_registry
Browse files Browse the repository at this point in the history
simianhacker committed Nov 10, 2021

Verified

This commit was signed with the committer’s verified signature.
gitbook-bot GitBook Bot
1 parent 7d8e609 commit 18a2261
Showing 22 changed files with 163 additions and 183 deletions.
1 change: 1 addition & 0 deletions x-pack/scripts/functional_tests.js
Original file line number Diff line number Diff line change
@@ -45,6 +45,7 @@ const onlyNotInCoverageTests = [
require.resolve('../test/plugin_api_integration/config.ts'),
require.resolve('../test/rule_registry/security_and_spaces/config_basic.ts'),
require.resolve('../test/rule_registry/security_and_spaces/config_trial.ts'),
require.resolve('../test/rule_registry/spaces_only/config_basic.ts'),
require.resolve('../test/rule_registry/spaces_only/config_trial.ts'),
require.resolve('../test/security_api_integration/saml.config.ts'),
require.resolve('../test/security_api_integration/session_idle.config.ts'),
Original file line number Diff line number Diff line change
@@ -12,6 +12,5 @@ export default function observabilityApiIntegrationTests({ loadTestFile }: FtrPr
describe('Observability specs (basic)', function () {
this.tags('ciGroup1');
loadTestFile(require.resolve('./annotations'));
loadTestFile(require.resolve('./rule_registry'));
});
}
62 changes: 0 additions & 62 deletions x-pack/test/observability_api_integration/common/users.ts

This file was deleted.

19 changes: 0 additions & 19 deletions x-pack/test/observability_api_integration/lib/create_alert.ts

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -12,6 +12,5 @@ export default function apmApiIntegrationTests({ loadTestFile }: FtrProviderCont
describe('Observability specs (trial)', function () {
this.tags('ciGroup1');
loadTestFile(require.resolve('./annotations'));
loadTestFile(require.resolve('./rule_registry'));
});
}
Original file line number Diff line number Diff line change
@@ -6,13 +6,13 @@
*/

import expect from '@kbn/expect';
import { GetService } from '../common/types';
import { User } from '../common/users';
import { GetService } from '../../types';
import { User } from '../authentication/types';
import { getAlertsTargetIndices } from './get_alerts_target_indices';

export const cleanupTargetIndices = async (getService: GetService, user: User) => {
export const cleanupTargetIndices = async (getService: GetService, user: User, spaceId: string) => {
const es = getService('es');
const { body: targetIndices } = await getAlertsTargetIndices(getService, user);
const { body: targetIndices } = await getAlertsTargetIndices(getService, user, spaceId);
try {
const aliasMap = await es.indices.getAlias({
name: targetIndices,
25 changes: 25 additions & 0 deletions x-pack/test/rule_registry/common/lib/helpers/create_alert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { User } from '../authentication/types';
import { GetService, AlertDef } from '../../types';
import { getSpaceUrlPrefix } from '../authentication/spaces';

export const createAlert = async (
getService: GetService,
user: User,
spaceId: string,
alertDef: AlertDef
) => {
const supertest = getService('supertestWithoutAuth');
const { body: response, status } = await supertest
.post(`${getSpaceUrlPrefix(spaceId)}/api/alerts/alert`)
.auth(user.username, user.password)
.send(alertDef)
.set('kbn-xsrf', 'foo');
return { alert: response, status };
};
Original file line number Diff line number Diff line change
@@ -4,12 +4,12 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { APM_METRIC_INDEX_NAME } from '../common/constants';
import { GetService } from '../common/types';
import { APM_METRIC_INDEX_NAME } from '../../constants';
import { GetService } from '../../types';

export const createApmMetricIndex = async (getService: GetService) => {
const es = getService('es');
return es.indices.create({
await es.indices.create({
index: APM_METRIC_INDEX_NAME,
body: {
mappings: {
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
*/

import { merge } from 'lodash';
import { INDEXING_DELAY } from '../common/constants';
import { INDEXING_DELAY } from '../../constants';

export const createTransactionMetric = (override: Record<string, any>) => {
const now = Date.now();
Original file line number Diff line number Diff line change
@@ -5,19 +5,25 @@
* 2.0.
*/

import { APM_METRIC_INDEX_NAME } from '../common/constants';
import { GetService } from '../common/types';
import { User, TEST_PASSWORD } from '../common/users';
import { APM_METRIC_INDEX_NAME } from '../../constants';
import { GetService } from '../../types';
import { getSpaceUrlPrefix } from '../authentication/spaces';
import { User } from '../authentication/types';
import { getAlertsTargetIndices } from './get_alerts_target_indices';

export const deleteAlert = async (getService: GetService, user: User, id: string | undefined) => {
export const deleteAlert = async (
getService: GetService,
user: User,
spaceId: string,
id: string | undefined
) => {
const es = getService('es');
const supertest = getService('supertest');
const { body: targetIndices } = await getAlertsTargetIndices(getService, user);
const supertest = getService('supertestWithoutAuth');
const { body: targetIndices } = await getAlertsTargetIndices(getService, user, spaceId);
if (id) {
const { body, status } = await supertest
.delete(`/api/alerts/alert/${id}`)
.auth(user, TEST_PASSWORD)
.delete(`${getSpaceUrlPrefix(spaceId)}/api/alerts/alert/${id}`)
.auth(user.username, user.password)
.set('kbn-xsrf', 'foo');

if (status >= 300) {
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { ALERTS_TARGET_INDICES_URL } from '../../constants';
import { GetService } from '../../types';
import { User } from '../authentication/types';
import { getSpaceUrlPrefix } from '../authentication/spaces';

export const getAlertsTargetIndices = async (
getService: GetService,
user: User,
spaceId: string
) => {
const supertest = getService('supertestWithoutAuth');
return supertest
.get(`${getSpaceUrlPrefix(spaceId)}${ALERTS_TARGET_INDICES_URL}`)
.auth(user.username, user.password)
.send()
.set('kbn-xsrf', 'foo');
};
Original file line number Diff line number Diff line change
@@ -4,37 +4,40 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { User, TEST_PASSWORD } from '../common/users';
import { GetService } from '../common/types';
import { GetService } from '../../types';
import { getAlertsTargetIndices } from './get_alerts_target_indices';
import { BULK_INDEX_DELAY, MAX_POLLS } from '../common/constants';
import { Alert } from '../../../plugins/alerting/common';
import { BULK_INDEX_DELAY, MAX_POLLS } from '../../constants';
import { Alert } from '../../../../../plugins/alerting/common';
import { getSpaceUrlPrefix } from '../authentication/spaces';
import { User } from '../authentication/types';

export async function waitUntilNextExecution(
getService: GetService,
user: User,
alert: Alert,
spaceId: string,
intervalInSeconds: number = 1,
count: number = 0
): Promise<Alert> {
const supertest = getService('supertest');
const supertest = getService('supertestWithoutAuth');
const es = getService('es');

await new Promise((resolve) => {
setTimeout(resolve, intervalInSeconds * 1000);
});

const { body, status } = await supertest
.get(`/api/alerts/alert/${alert.id}`)
.auth(user, TEST_PASSWORD)
.get(`${getSpaceUrlPrefix(spaceId)}/api/alerts/alert/${alert.id}`)
.auth(user.username, user.password)
.set('kbn-xsrf', 'foo');

const { body: targetIndices, status: targetIndicesStatus } = await getAlertsTargetIndices(
getService,
user
user,
spaceId
);
if (targetIndices.length === 0) {
const error = new Error('Error getting alert');
const error = new Error('Error getting target indices');
Object.assign(error, { response: { body: targetIndices, status: targetIndicesStatus } });
throw error;
}
@@ -74,5 +77,5 @@ export async function waitUntilNextExecution(
throw new Error('Maximum number of polls exceeded');
}

return waitUntilNextExecution(getService, user, alert, intervalInSeconds, count + 1);
return waitUntilNextExecution(getService, user, alert, spaceId, intervalInSeconds, count + 1);
}
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
*/
import { GenericFtrProviderContext } from '@kbn/test';
import { Alert, AlertTypeParams } from '../../../plugins/alerting/common';
import { services } from '../../api_integration/services';
import { services } from './services';

export type GetService = GenericFtrProviderContext<typeof services, {}>['getService'];

16 changes: 16 additions & 0 deletions x-pack/test/rule_registry/spaces_only/config_basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { createTestConfig } from '../common/config';

// eslint-disable-next-line import/no-default-export
export default createTestConfig('spaces_only', {
license: 'basic',
disabledPlugins: ['security'],
ssl: false,
testFiles: [require.resolve('./tests/basic')],
});
Original file line number Diff line number Diff line change
@@ -6,23 +6,22 @@
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../../../common/ftr_provider_context';
import { createUser, User } from '../../common/users';
import { getAlertsTargetIndices } from '../../lib';
import type { FtrProviderContext } from '../../../common/ftr_provider_context';
import { obsOnlyRead } from '../../../common/lib/authentication/users';
import { getAlertsTargetIndices } from '../../../common/lib/helpers';

// eslint-disable-next-line import/no-default-export
export default function registryRulesApiTest({ getService }: FtrProviderContext) {
const es = getService('es');
const security = getService('security');

describe('Rule Registry API', () => {
before(async () => {
await createUser(security, User.apmReadUser);
});

describe('with read permissions', () => {
it('does not bootstrap the apm rule indices', async () => {
const { body: targetIndices } = await getAlertsTargetIndices(getService, User.apmReadUser);
const { body: targetIndices } = await getAlertsTargetIndices(
getService,
obsOnlyRead,
'space1'
);
const errorOrUndefined = await es.indices
.get({
index: targetIndices[0],
28 changes: 28 additions & 0 deletions x-pack/test/rule_registry/spaces_only/tests/basic/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { FtrProviderContext } from '../../../common/ftr_provider_context';
import { createSpaces, deleteSpaces } from '../../../common/lib/authentication';

// eslint-disable-next-line import/no-default-export
export default ({ loadTestFile, getService }: FtrProviderContext): void => {
describe('rule registry spaces only: trial', function () {
// Fastest ciGroup for the moment.
this.tags('ciGroup5');

before(async () => {
await createSpaces(getService);
});

after(async () => {
await deleteSpaces(getService);
});

// Basic
loadTestFile(require.resolve('./bootstrap'));
});
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -17,8 +17,7 @@ import {
VERSION,
} from '@kbn/rule-data-utils';
import { omit } from 'lodash';
import { FtrProviderContext } from '../../common/ftr_provider_context';
import { createUser, User } from '../../common/users';
import type { FtrProviderContext } from '../../../common/ftr_provider_context';
import {
getAlertsTargetIndices,
createApmMetricIndex,
@@ -27,24 +26,22 @@ import {
createTransactionMetric,
cleanupTargetIndices,
deleteAlert,
} from '../../lib';
import { AlertDef, AlertParams } from '../../common/types';
import { Alert } from '../../../../plugins/alerting/common';
import { APM_METRIC_INDEX_NAME } from '../../common/constants';
} from '../../../common/lib/helpers';
import { AlertDef, AlertParams } from '../../../common/types';
import { Alert } from '../../../../../plugins/alerting/common';
import { APM_METRIC_INDEX_NAME } from '../../../common/constants';
import { obsOnly } from '../../../common/lib/authentication/users';

const SPACE_ID = 'space1';

// eslint-disable-next-line import/no-default-export
export default function registryRulesApiTest({ getService }: FtrProviderContext) {
const es = getService('es');
const security = getService('security');

describe('Rule Registry API', () => {
before(async () => {
await createUser(security, User.apmWriteUser);
});

describe('with write permissions', () => {
it('does not bootstrap indices on plugin startup', async () => {
const { body: targetIndices } = await getAlertsTargetIndices(getService, User.apmWriteUser);
const { body: targetIndices } = await getAlertsTargetIndices(getService, obsOnly, SPACE_ID);
try {
const res = await es.indices.get({
index: targetIndices[0],
@@ -81,11 +78,11 @@ export default function registryRulesApiTest({ getService }: FtrProviderContext)
notifyWhen: 'onActionGroupChange',
name: 'Failed transaction rate threshold | opbeans-go',
};
createResponse = await createAlert(getService, User.apmWriteUser, alertDef);
createResponse = await createAlert(getService, obsOnly, SPACE_ID, alertDef);
});
after(async () => {
await deleteAlert(getService, User.apmWriteUser, createResponse.alert.id);
await cleanupTargetIndices(getService, User.apmWriteUser);
await deleteAlert(getService, obsOnly, SPACE_ID, createResponse.alert.id);
await cleanupTargetIndices(getService, obsOnly, SPACE_ID);
});

it('writes alerts data to the alert indices', async () => {
@@ -94,13 +91,15 @@ export default function registryRulesApiTest({ getService }: FtrProviderContext)
expect(createResponse.alert).not.to.be(undefined);
let alert = await waitUntilNextExecution(
getService,
User.apmWriteUser,
createResponse.alert
obsOnly,
createResponse.alert,
SPACE_ID
);

const { body: targetIndices } = await getAlertsTargetIndices(
getService,
User.apmWriteUser
obsOnly,
SPACE_ID
);

try {
@@ -133,7 +132,7 @@ export default function registryRulesApiTest({ getService }: FtrProviderContext)
refresh: true,
});

alert = await waitUntilNextExecution(getService, User.apmWriteUser, alert);
alert = await waitUntilNextExecution(getService, obsOnly, alert, SPACE_ID);

try {
const res = await es.search({
@@ -165,7 +164,7 @@ export default function registryRulesApiTest({ getService }: FtrProviderContext)
refresh: true,
});

alert = await waitUntilNextExecution(getService, User.apmWriteUser, alert);
alert = await waitUntilNextExecution(getService, obsOnly, alert, SPACE_ID);

const afterViolatingDataResponse = await es.search({
index: targetIndices[0],
@@ -213,7 +212,7 @@ export default function registryRulesApiTest({ getService }: FtrProviderContext)
refresh: true,
});

alert = await waitUntilNextExecution(getService, User.apmWriteUser, alert);
alert = await waitUntilNextExecution(getService, obsOnly, alert, SPACE_ID);

const afterRecoveryResponse = await es.search({
index: targetIndices[0],
@@ -249,25 +248,5 @@ export default function registryRulesApiTest({ getService }: FtrProviderContext)
});
});
});

describe('with read permissions', () => {
it('does not bootstrap the apm rule indices', async () => {
const { body: targetIndices } = await getAlertsTargetIndices(getService, User.apmReadUser);
const errorOrUndefined = await es.indices
.get({
index: targetIndices[0],
expand_wildcards: 'open',
allow_no_indices: false,
})
.then(() => {})
.catch((error) => {
return error.toString();
});

expect(errorOrUndefined).not.to.be(undefined);

expect(errorOrUndefined).to.contain('index_not_found_exception');
});
});
});
}
3 changes: 2 additions & 1 deletion x-pack/test/rule_registry/spaces_only/tests/trial/index.ts
Original file line number Diff line number Diff line change
@@ -22,8 +22,9 @@ export default ({ loadTestFile, getService }: FtrProviderContext): void => {
await deleteSpaces(getService);
});

// Basic
// Trial
loadTestFile(require.resolve('./get_alert_by_id'));
loadTestFile(require.resolve('./update_alert'));
loadTestFile(require.resolve('./create_rule'));
});
};

0 comments on commit 18a2261

Please sign in to comment.