Skip to content

Commit

Permalink
Fix windows-latest cypress tests (#360)
Browse files Browse the repository at this point in the history
* [FEATURE] Detector must have at least one alert set #288

Signed-off-by: Jovan Cvetkovic <[email protected]>

* windows latest

Signed-off-by: Jovan Cvetkovic <[email protected]>

* [TASK] Investigate cypress windows latest tests #351

Signed-off-by: Jovan Cvetkovic <[email protected]>

* [TASK] Investigate cypress windows latest tests #351

Signed-off-by: Jovan Cvetkovic <[email protected]>

* [TASK] Investigate cypress windows latest tests #351

Signed-off-by: Jovan Cvetkovic <[email protected]>

* [TASK] Investigate cypress windows latest tests #351

Signed-off-by: Jovan Cvetkovic <[email protected]>

* [TASK] Investigate cypress windows latest tests #351

Signed-off-by: Jovan Cvetkovic <[email protected]>

* [TASK] Investigate cypress windows latest tests #351

Signed-off-by: Jovan Cvetkovic <[email protected]>

* [TASK] Investigate cypress windows latest tests #351

Signed-off-by: Jovan Cvetkovic <[email protected]>

* [TASK] Investigate cypress windows latest tests #351

Signed-off-by: Jovan Cvetkovic <[email protected]>

* [TASK] Investigate cypress windows latest tests #351

Signed-off-by: Jovan Cvetkovic <[email protected]>

* [TASK] Investigate cypress windows latest tests #351

Signed-off-by: Jovan Cvetkovic <[email protected]>

Signed-off-by: Jovan Cvetkovic <[email protected]>
Signed-off-by: AWSHurneyt <[email protected]>
  • Loading branch information
jovancvetkovic3006 authored and AWSHurneyt committed Jan 19, 2023
1 parent 1897bdf commit 9fccdd8
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 72 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/cypress-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ jobs:
name: Run Cypress E2E tests
strategy:
matrix:
os: [ubuntu-latest]
os: [ubuntu-latest, windows-latest]
include:
- os: windows-latest
cypress_cache_folder: ~/AppData/Local/Cypress/Cache
- os: ubuntu-latest
cypress_cache_folder: ~/.cache/Cypress
runs-on: ${{ matrix.os }}
Expand All @@ -32,6 +34,10 @@ jobs:
# TODO: Parse this from security analytics plugin (https://github.com/opensearch-project/security-analytics/issues/170)
java-version: 11

- name: Enable longer filenames
if: ${{ matrix.os == 'windows-latest' }}
run: git config --system core.longpaths true

- name: Checkout security analytics
uses: actions/checkout@v2
with:
Expand Down Expand Up @@ -87,7 +93,14 @@ jobs:
yarn start --no-base-path --no-watch &
shell: bash

- name: Sleep until OSD server starts
# Window is slow so wait longer
- name: Sleep until OSD server starts - windows
if: ${{ matrix.os == 'windows-latest' }}
run: Start-Sleep -s 400
shell: powershell

- name: Sleep until OSD server starts - non-windows
if: ${{ matrix.os != 'windows-latest' }}
run: sleep 300
shell: bash

Expand Down
1 change: 0 additions & 1 deletion cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"viewportHeight": 900,
"viewportWidth": 1440,
"defaultCommandTimeout": 20000,
"retries": 1,
"env": {
"opensearch_url": "localhost:9200",
"opensearch_dashboards": "http://localhost:5601",
Expand Down
21 changes: 11 additions & 10 deletions cypress/integration/1_detectors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ describe('Detectors', () => {
.realType(indexName);

cy.intercept({
method: 'POST',
pathname: '/_plugins/_security_analytics/rules/_search',
query: {
prePackaged: 'true',
},
}).as('getSigmaRules');

// Select threat detector type (Windows logs)
Expand All @@ -86,7 +84,7 @@ describe('Detectors', () => {
cy.get('[data-test-subj="detection-rules-btn"]').click({ force: true, timeout: 5000 });

// find search, type USB
cy.get(`input[placeholder="Search..."]`).ospSearch('USB Device Plugged');
cy.triggerSearchField('Search...', 'USB Device Plugged');

// Disable all rules
cy.contains('tr', 'USB Device Plugged', { timeout: 1000 });
Expand Down Expand Up @@ -209,8 +207,8 @@ describe('Detectors', () => {

// Change detector name
cy.get(`input[placeholder="Enter a name for the detector."]`)
.realClick()
.ospClear()
.clearInput()
.focus()
.realType('test detector edited');

// Change detector description
Expand All @@ -221,13 +219,16 @@ describe('Detectors', () => {
// Change input source
cy.get(`[data-test-subj="define-detector-select-data-source"]`)
.find('input')
.ospClear()
.clearInput()
.focus()
.realType('.opensearch-notifications-config')
.realPress('Enter');

// Change detector scheduling
cy.get(`[data-test-subj="detector-schedule-number-select"]`).ospClear().focus().realType('10');
cy.get(`[data-test-subj="detector-schedule-number-select"]`)
.clearInput()
.focus()
.realType('10');
cy.get(`[data-test-subj="detector-schedule-unit-select"]`).select('Hours');

// Save changes to detector details
Expand Down Expand Up @@ -269,7 +270,7 @@ describe('Detectors', () => {
});

// Search for specific rule
cy.get(`input[placeholder="Search..."]`).ospSearch('USB Device');
cy.triggerSearchField('Search...', 'USB Device');

// Toggle single search result to unchecked
cy.contains('table tr', 'USB Device Plugged').within(() => {
Expand All @@ -293,7 +294,7 @@ describe('Detectors', () => {
});

// Search for specific rule
cy.get(`input[placeholder="Search..."]`).ospSearch('USB');
cy.triggerSearchField('Search...', 'USB');

// Toggle single search result to checked
cy.contains('table tr', 'USB Device Plugged').within(() => {
Expand Down
201 changes: 192 additions & 9 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

const { OPENSEARCH_DASHBOARDS_URL } = require('./constants');
require('./detectors');
require('./rules');
require('./indexes');
require('./typings');

const { NODE_API, OPENSEARCH_DASHBOARDS, OPENSEARCH_DASHBOARDS_URL } = require('./constants');
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
Expand Down Expand Up @@ -99,14 +94,202 @@ Cypress.Commands.add('getTableFirstRow', (selector) => {
return cy.get('tbody > tr:first').find(selector);
});

Cypress.Commands.add('waitForPageLoad', (pathname, { timeout = 60000, contains = null }) => {
const fullUrl = `${OPENSEARCH_DASHBOARDS_URL}/${pathname}`;
Cypress.Commands.add(
'clearInput',
{
prevSubject: true,
},
(subject) => {
return cy
.get(subject)
.wait(10)
.type('{selectall}{enter}')
.clear({ force: true })
.invoke('val', '');
}
);

Cypress.Commands.add('triggerSearchField', (placeholder, text) => {
cy.get(`input[placeholder="${placeholder}"]`)
.clearInput()
.focus()
.realType(text)
.realPress('Enter');
});

Cypress.Commands.add('waitForPageLoad', (url, { timeout = 10000, contains = null }) => {
const fullUrl = `${OPENSEARCH_DASHBOARDS_URL}/${url}`;
Cypress.log({
message: `Wait for url: ${fullUrl} to be loaded.`,
});
cy.url({ timeout: timeout })
.should('include', fullUrl)
.then(() => {
contains && cy.contains(contains).should('be.visible');
contains && cy.contains(contains);
});
});

Cypress.Commands.add('deleteAllIndices', () => {
cy.request({
method: 'DELETE',
url: `${Cypress.env('opensearch')}/index*,sample*,opensearch_dashboards*,test*,cypress*`,
failOnStatusCode: false,
});
});

Cypress.Commands.add('deleteAllDetectors', () => {
cy.request({
method: 'DELETE',
url: `${Cypress.env('opensearch')}/.opensearch-sap-detectors-config`,
failOnStatusCode: false,
});
});

Cypress.Commands.add('createDetector', (detectorJSON) => {
cy.request('POST', `${Cypress.env('opensearch')}${NODE_API.DETECTORS_BASE}`, detectorJSON);
});

Cypress.Commands.add('updateDetector', (detectorId, detectorJSON) => {
cy.request(
'PUT',
`${Cypress.env('opensearch')}/${NODE_API.DETECTORS_BASE}/${detectorId}`,
detectorJSON
);
});

Cypress.Commands.add('deleteDetector', (detectorName) => {
const body = {
from: 0,
size: 5000,
query: {
nested: {
path: 'detector',
query: {
bool: {
must: [{ match: { 'detector.name': detectorName } }],
},
},
},
},
};
cy.request({
method: 'POST',
url: `${Cypress.env('opensearch')}${NODE_API.DETECTORS_BASE}/_search`,
failOnStatusCode: false,
body,
}).then((response) => {
if (response.status === 200) {
for (let hit of response.body.hits.hits) {
cy.request('DELETE', `${Cypress.env('opensearch')}${NODE_API.DETECTORS_BASE}/${hit._id}`);
}
}
});
});

Cypress.Commands.add('deleteIndex', (indexName, options = {}) => {
cy.request({
method: 'DELETE',
url: `${Cypress.env('opensearch')}/${indexName}`,
failOnStatusCode: false,
...options,
});
});

Cypress.Commands.add(
'createAliasMappings',
(indexName, ruleTopic, aliasMappingsBody, partial = true) => {
const body = {
index_name: indexName,
rule_topic: ruleTopic,
partial: partial,
alias_mappings: aliasMappingsBody,
};
cy.request({
method: 'POST',
url: `${Cypress.env('opensearch')}${NODE_API.MAPPINGS_BASE}`,
body: body,
});
}
);

Cypress.Commands.add('createRule', (ruleJSON) => {
return cy.request({
method: 'POST',
url: `${OPENSEARCH_DASHBOARDS}${NODE_API.RULES_BASE}?category=${ruleJSON.category}`,
body: JSON.stringify(ruleJSON),
});
});

Cypress.Commands.add('updateRule', (ruleId, ruleJSON) => {
cy.request('PUT', `${Cypress.env('opensearch')}/${NODE_API.RULES_BASE}/${ruleId}`, ruleJSON);
});

Cypress.Commands.add('createIndex', (index, settings = {}) => {
cy.request('PUT', `${Cypress.env('opensearch')}/${index}`, settings);
});

Cypress.Commands.add('ingestDocument', (indexId, documentJSON) => {
cy.request('POST', `${Cypress.env('opensearch')}/${indexId}/_doc`, documentJSON);
});

Cypress.Commands.add('deleteRule', (ruleName) => {
const body = {
from: 0,
size: 5000,
query: {
nested: {
path: 'rule',
query: {
bool: {
must: [{ match: { 'rule.title': 'Cypress test rule' } }],
},
},
},
},
};
cy.request({
method: 'POST',
url: `${Cypress.env('opensearch')}${NODE_API.RULES_BASE}/_search?pre_packaged=false`,
failOnStatusCode: false,
body,
}).then((response) => {
if (response.status === 200) {
for (let hit of response.body.hits.hits) {
if (hit._source.title === ruleName)
cy.request(
'DELETE',
`${Cypress.env('opensearch')}${NODE_API.RULES_BASE}/${hit._id}?forced=true`
);
}
}
});
});

Cypress.Commands.add('deleteAllCustomRules', () => {
cy.request({
method: 'DELETE',
url: `${Cypress.env('opensearch')}/.opensearch-sap-custom-rules-config`,
failOnStatusCode: false,
body: { query: { match_all: {} } },
});
});

Cypress.Commands.add('createIndex', (index, settings = {}) => {
cy.request('PUT', `${Cypress.env('opensearch')}/${index}`, settings);
});

Cypress.Commands.add('createIndexTemplate', (name, template) => {
cy.request(
'PUT',
`${Cypress.env('opensearch')}${NODE_API.INDEX_TEMPLATE_BASE}/${name}`,
template
);
});

Cypress.Commands.add('insertDocumentToIndex', (indexName, documentId, documentBody) => {
cy.request({
method: 'POST',
url: `${Cypress.env('opensearch')}/${indexName}/_doc/${documentId}`,
body: documentBody,
});
});
Loading

0 comments on commit 9fccdd8

Please sign in to comment.