Skip to content

Commit

Permalink
Rewrite Grok Debugger Tests to use Standard Functional Test Runner (#…
Browse files Browse the repository at this point in the history
…108301)

* Converted Grok debugger tests to use standard functional test runner practices.

* Fixed last test.

* Removed old test now that the new test is passing.

* Fixed condition for retry.

* Removed .only to restore test run.

* fixed errors

* Fixed nits.
  • Loading branch information
John Dorlus authored Aug 17, 2021
1 parent 3ab111e commit 9aa1705
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 100 deletions.
89 changes: 0 additions & 89 deletions x-pack/test/functional/apps/grok_debugger/grok_debugger.js

This file was deleted.

110 changes: 110 additions & 0 deletions x-pack/test/functional/apps/grok_debugger/home_page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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 expect from '@kbn/expect';
import { FtrProviderContext } from '../../ftr_provider_context';

export default ({ getPageObjects, getService }: FtrProviderContext) => {
const PageObjects = getPageObjects(['common', 'grokDebugger']);
const browser = getService('browser');
const security = getService('security');
const testSubjects = getService('testSubjects');
const retry = getService('retry');

describe('Grok Debugger', function () {
before(async () => {
// Increase window height to ensure "Simulate" button is shown above the
// fold. Otherwise it can't be clicked by the browser driver.
await browser.setWindowSize(1600, 1000);
await security.testUser.setRoles(['global_devtools_read', 'ingest_pipelines_user']);
await PageObjects.common.navigateToApp('grokDebugger');
await retry.waitFor('Grok Debugger Header to be visible', async () => {
return testSubjects.isDisplayed('grokDebuggerContainer');
});
});

it('Loads the app', async () => {
await retry.waitForWithTimeout('Grok Debugger to be visible', 15000, async () => {
return await (await PageObjects.grokDebugger.simulateButton()).isDisplayed();
});
});

it('Accept and parse input with built-in grok pattern', async () => {
const eventInput = 'SegerCommaBob';
const patternInput = '%{USERNAME:u}';
const response = await PageObjects.grokDebugger.executeGrokSimulation(
eventInput,
patternInput,
null
);
expect(response).to.eql({ u: 'SegerCommaBob' });
});

it('Accept and parse input with custom grok pattern', async () => {
await PageObjects.common.navigateToApp('grokDebugger');
const eventInput =
'Jan 1 06:25:43 mailserver14 postfix/cleanup[21403]: BEF25A72965: message-id=<[email protected]>';
const patternInput = '%{SYSLOGBASE} %{POSTFIX_QUEUEID:queue_id}: %{MSG:syslog_message}';
const customPatternInput = 'POSTFIX_QUEUEID [0-9A-F]{10,11}\nMSG message-id=<%{GREEDYDATA}>';
const testData = {
pid: '21403',
program: 'postfix/cleanup',
logsource: 'mailserver14',
syslog_message: 'message-id=<[email protected]>',
queue_id: 'BEF25A72965',
timestamp: 'Jan 1 06:25:43',
};

const response = await PageObjects.grokDebugger.executeGrokSimulation(
eventInput,
patternInput,
customPatternInput
);
expect(response).to.eql(testData);
});

// This test will need to be fixed.
it.skip('applies the correct CSS classes', async () => {
const grokPattern = '\\[(?:-|%{NUMBER:bytes:int})\\]';

await PageObjects.grokDebugger.setPatternInput(grokPattern);

const GROK_START = 'grokStart';
const GROK_PATTERN_NAME = 'grokPatternName';
const GROK_SEPARATOR = 'grokSeparator';
const GROK_FIELD_NAME = 'grokFieldName';
const GROK_FIELD_TYPE = 'grokFieldType';
const GROK_END = 'grokEnd';
const GROK_ESCAPE = 'grokEscape';
const GROK_ESCAPED = 'grokEscaped';
const GROK_REGEX = 'grokRegex';

await PageObjects.grokDebugger.assertPatternInputSyntaxHighlighting([
{ token: GROK_ESCAPE, content: '\\' },
{ token: GROK_ESCAPED, content: '[' },
{ token: GROK_REGEX, content: '(' },
{ token: GROK_REGEX, content: '?' },
{ token: GROK_REGEX, content: ':' },
{ token: GROK_REGEX, content: '|' },
{ token: GROK_START, content: '%{' },
{ token: GROK_PATTERN_NAME, content: 'NUMBER' },
{ token: GROK_SEPARATOR, content: ':' },
{ token: GROK_FIELD_NAME, content: 'bytes' },
{ token: GROK_SEPARATOR, content: ':' },
{ token: GROK_FIELD_TYPE, content: 'int' },
{ token: GROK_END, content: '}' },
{ token: GROK_REGEX, content: ')' },
{ token: GROK_ESCAPE, content: '\\' },
{ token: GROK_ESCAPED, content: ']' },
]);
});

after(async () => {
await security.testUser.restoreDefaults();
});
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
* 2.0.
*/

export default function ({ loadTestFile }) {
describe('logstash', function () {
this.tags('ciGroup13');
import { FtrProviderContext } from '../../ftr_provider_context';

loadTestFile(require.resolve('./grok_debugger'));
export default ({ loadTestFile }: FtrProviderContext) => {
describe('Grok Debugger App', function () {
this.tags('ciGroup13');
loadTestFile(require.resolve('./home_page'));
});
}
};
84 changes: 78 additions & 6 deletions x-pack/test/functional/page_objects/grok_debugger_page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,87 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import expect from '@kbn/expect';
import { FtrService } from '../ftr_provider_context';

export class GrokDebuggerPageObject extends FtrService {
private readonly common = this.ctx.getPageObject('common');
private readonly grokDebugger = this.ctx.getService('grokDebugger');
private readonly testSubjects = this.ctx.getService('testSubjects');
private readonly aceEditor = this.ctx.getService('aceEditor');
private readonly retry = this.ctx.getService('retry');

async simulateButton() {
return await this.testSubjects.find('btnSimulate');
}

async getEventOutput() {
return await this.aceEditor.getValue(
'grokDebuggerContainer > aceEventOutput > codeEditorContainer'
);
}

async setEventInput(value: string) {
await this.aceEditor.setValue(
'grokDebuggerContainer > aceEventInput > codeEditorContainer',
value
);
}

async setPatternInput(pattern: string) {
await this.aceEditor.setValue(
'grokDebuggerContainer > acePatternInput > codeEditorContainer',
pattern
);
}

async setCustomPatternInput(customPattern: string) {
await this.aceEditor.setValue(
'grokDebuggerContainer > aceCustomPatternsInput > codeEditorContainer',
customPattern
);
}

async toggleSetCustomPattern() {
await this.testSubjects.click('grokDebuggerContainer > btnToggleCustomPatternsInput');
}

async executeGrokSimulation(input: string, pattern: string, customPattern: string | null) {
let value;
await this.setEventInput(input);
await this.setPatternInput(pattern);
if (customPattern) {
await this.toggleSetCustomPattern();
await this.setCustomPatternInput(customPattern);
}
await (await this.simulateButton()).click();
await this.retry.try(async () => {
value = JSON.parse(await this.getEventOutput());
expect(Object.keys(value).length).to.be.greaterThan(0);
});
return value;
}

// This needs to be fixed to work with the new test functionality. This method is skipped currently.
async assertPatternInputSyntaxHighlighting(expectedHighlights: any[]) {
const patternInputElement = await this.testSubjects.find(
'grokDebuggerContainer > acePatternInput > codeEditorContainer'
);
const highlightedElements = await patternInputElement.findAllByXpath(
'.//div[@class="ace_line"]/*'
);

expect(highlightedElements.length).to.be(expectedHighlights.length);
await Promise.all(
highlightedElements.map(async (element: any, index) => {
const highlightClass = await element.getAttribute('class');
const highlightedContent = await element.getVisibleText();

const expectedHighlight = expectedHighlights[index];
const expectedHighlightClass = `ace_${expectedHighlight.token}`;
const expectedHighlightedContent = expectedHighlight.content;

async gotoGrokDebugger() {
await this.common.navigateToApp('grokDebugger');
await this.grokDebugger.assertExists();
expect(highlightClass).to.be(expectedHighlightClass);
expect(highlightedContent).to.be(expectedHighlightedContent);
})
);
}
}

0 comments on commit 9aa1705

Please sign in to comment.