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

[Security Solutions] Make Cypress tests work independent of on-disk rules vs cloud rules #105857

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import { loginAndWaitForPageWithoutDateRange } from '../../tasks/login';

import { ALERTS_URL } from '../../urls/navigation';

import { totalNumberOfPrebuiltRules } from '../../objects/rule';
FrankHassanabad marked this conversation as resolved.
Show resolved Hide resolved
import { cleanKibana } from '../../tasks/common';
import { totalNumberOfPrebuiltRules } from '../../objects/rule';

describe('Alerts rules, prebuilt rules', () => {
beforeEach(() => {
Expand All @@ -48,7 +48,6 @@ describe('Alerts rules, prebuilt rules', () => {
const rowsPerPage = 100;
const expectedNumberOfRules = totalNumberOfPrebuiltRules;
const expectedNumberOfPages = Math.ceil(totalNumberOfPrebuiltRules / rowsPerPage);
const expectedElasticRulesBtnText = `Elastic rules (${expectedNumberOfRules})`;

loginAndWaitForPageWithoutDateRange(ALERTS_URL);
waitForAlertsIndexToBeCreated();
Expand All @@ -57,29 +56,52 @@ describe('Alerts rules, prebuilt rules', () => {
loadPrebuiltDetectionRules();
waitForPrebuiltDetectionRulesToBeLoaded();

cy.get(ELASTIC_RULES_BTN).should('have.text', expectedElasticRulesBtnText);
// The result should be at least equal to the number of rules we have on disk
// but could be greater than this number if there are additional cloud rules
cy.get(ELASTIC_RULES_BTN)
.first()
.then((result) => {
expect(+result.text().replace(/[^0-9]/g, '')).to.be.least(expectedNumberOfRules);
expect(result.text()).to.match(/Elastic rules\s\([0-9]+\)/);
});

changeRowsPerPageTo(rowsPerPage);

cy.get(SHOWING_RULES_TEXT).should('have.text', `Showing ${expectedNumberOfRules} rules`);
// The result should be at least equal to the number of rules we have on disk
// but could be greater than this number if there are additional cloud rules
cy.get(SHOWING_RULES_TEXT)
.first()
.then((result) => {
expect(+result.text().replace(/[^0-9]/g, '')).to.be.least(expectedNumberOfRules);
expect(result.text()).to.match(/Showing\s[0-9]+\srules/);
});

cy.get(pageSelector(expectedNumberOfPages)).should('exist');
});
});

describe('Actions with prebuilt rules', () => {
beforeEach(() => {
const expectedNumberOfRules = totalNumberOfPrebuiltRules;
const expectedElasticRulesBtnText = `Elastic rules (${expectedNumberOfRules})`;
const expectedNumberOfRules = totalNumberOfPrebuiltRules;

beforeEach(() => {
cleanKibana();
loginAndWaitForPageWithoutDateRange(ALERTS_URL);
waitForAlertsIndexToBeCreated();
goToManageAlertsDetectionRules();
waitForRulesTableToBeLoaded();
loadPrebuiltDetectionRules();
waitForPrebuiltDetectionRulesToBeLoaded();
});

cy.get(ELASTIC_RULES_BTN).should('have.text', expectedElasticRulesBtnText);
it('Has the correct number of rules', () => {
// The result should be at least equal to the number of rules we have on disk
// but could be greater than this number if there are additional cloud rules
cy.get(ELASTIC_RULES_BTN)
.first()
.then((result) => {
expect(+result.text().replace(/[^0-9]/g, '')).to.be.least(expectedNumberOfRules);
expect(result.text()).to.match(/Elastic rules\s\([0-9]+\)/);
});
});

it('Allows to activate/deactivate all rules at once', () => {
Expand Down Expand Up @@ -122,10 +144,17 @@ describe('Actions with prebuilt rules', () => {
cy.reload();
changeRowsPerPageTo100();

cy.get(ELASTIC_RULES_BTN).should(
'have.text',
`Elastic rules (${expectedNumberOfRulesAfterDeletion})`
);
// The result should be at least equal to the number of rules we have on disk
// but could be greater than this number if there are additional cloud rules.
cy.get(ELASTIC_RULES_BTN)
.first()
.then((result) => {
expect(+result.text().replace(/[^0-9]/g, '')).to.be.least(
expectedNumberOfRulesAfterDeletion
);
expect(result.text()).to.match(/Elastic rules\s\([0-9]+\)/);
});

cy.get(RELOAD_PREBUILT_RULES_BTN).should('exist');
cy.get(RELOAD_PREBUILT_RULES_BTN).should('have.text', 'Install 1 Elastic prebuilt rule ');

Expand All @@ -136,10 +165,16 @@ describe('Actions with prebuilt rules', () => {
cy.reload();
changeRowsPerPageTo100();

cy.get(ELASTIC_RULES_BTN).should(
'have.text',
`Elastic rules (${expectedNumberOfRulesAfterRecovering})`
);
// The result should be at least equal to the number of rules we have on disk
// but could be greater than this number if there are additional cloud rules
cy.get(ELASTIC_RULES_BTN)
.first()
.then((result) => {
expect(+result.text().replace(/[^0-9]/g, '')).to.be.least(
expectedNumberOfRulesAfterRecovering
);
expect(result.text()).to.match(/Elastic rules\s\([0-9]+\)/);
});
});

it('Deletes and recovers more than one rule', () => {
Expand All @@ -159,10 +194,17 @@ describe('Actions with prebuilt rules', () => {
'have.text',
`Install ${numberOfRulesToBeSelected} Elastic prebuilt rules `
);
cy.get(ELASTIC_RULES_BTN).should(
'have.text',
`Elastic rules (${expectedNumberOfRulesAfterDeletion})`
);

// The result should be at least equal to the number of rules we have on disk
// but could be greater than this number if there are additional cloud rules
cy.get(ELASTIC_RULES_BTN)
.first()
.then((result) => {
expect(+result.text().replace(/[^0-9]/g, '')).to.be.least(
expectedNumberOfRulesAfterDeletion
);
expect(result.text()).to.match(/Elastic rules\s\([0-9]+\)/);
});

reloadDeletedRules();

Expand All @@ -171,9 +213,15 @@ describe('Actions with prebuilt rules', () => {
cy.reload();
changeRowsPerPageTo100();

cy.get(ELASTIC_RULES_BTN).should(
'have.text',
`Elastic rules (${expectedNumberOfRulesAfterRecovering})`
);
// The result should be at least equal to the number of rules we have on disk
// but could be greater than this number if there are additional cloud rules
cy.get(ELASTIC_RULES_BTN)
.first()
.then((result) => {
expect(+result.text().replace(/[^0-9]/g, '')).to.be.least(
expectedNumberOfRulesAfterRecovering
);
expect(result.text()).to.match(/Elastic rules\s\([0-9]+\)/);
});
});
});
4 changes: 0 additions & 4 deletions x-pack/plugins/security_solution/cypress/objects/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import { getTimeline, CompleteTimeline, getIndicatorMatchTimelineTemplate } from

export const totalNumberOfPrebuiltRules = rawRules.length;

export const totalNumberOfPrebuiltRulesInEsArchive = 127;
FrankHassanabad marked this conversation as resolved.
Show resolved Hide resolved

export const totalNumberOfPrebuiltRulesInEsArchiveCustomRule = 145;

const ccsRemoteName: string = Cypress.env('CCS_REMOTE_NAME');

interface MitreAttackTechnique {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,10 @@ export const waitForRulesTableToBeAutoRefreshed = () => {
};

export const waitForPrebuiltDetectionRulesToBeLoaded = () => {
cy.get(LOAD_PREBUILT_RULES_BTN).should('not.exist');
cy.get(RULES_TABLE).should('exist');
cy.get(RULES_TABLE_REFRESH_INDICATOR).should('not.exist');
// Wait up to 5 minutes for the rules to load as in CI containers this can be very slow
FrankHassanabad marked this conversation as resolved.
Show resolved Hide resolved
cy.get(LOAD_PREBUILT_RULES_BTN, { timeout: 300000 }).should('not.exist');
cy.get(RULES_TABLE, { timeout: 300000 }).should('exist');
cy.get(RULES_TABLE_REFRESH_INDICATOR, { timeout: 300000 }).should('not.exist');
};

export const waitForRuleToChangeStatus = () => {
Expand Down