Skip to content

Commit

Permalink
Fixed alerting_api_integration/security_and_spaces tests failing if a…
Browse files Browse the repository at this point in the history
…ctions proxy set on for parallel process running using commands 'scripts/functional_tests_server' and 'scripts/functional_test_runner' (#75232)

* Fixed alerting_api_integration/security_and_spaces tests failing if actions proxy set on for parallel process running using commands 'scripts/functional_tests_server' and 'scripts/functional_test_runner'

* -

* Fixed get port from range for Slack and webhook simulators, removed some test warnings

* Added check for listening proxy server

* changed logger to debug removed not useful error

* -

* changed proxy to dynamic target in a single place

* test retry

* -

* -

* -

* -

* test with no cleanup

* -

* -

* -

* -

* Added environment variable ALERTING_PROXY_PORT

* fixed type checks

* fixed clean up proxy server port
  • Loading branch information
YulNaumenko committed Aug 25, 2020
1 parent 20cad14 commit 8cdce2b
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 122 deletions.
2 changes: 2 additions & 0 deletions vars/kibanaPipeline.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def withFunctionalTestEnv(List additionalEnvs = [], Closure closure) {
def esPort = "61${parallelId}2"
def esTransportPort = "61${parallelId}3"
def ingestManagementPackageRegistryPort = "61${parallelId}4"
def alertingProxyPort = "61${parallelId}5"

withEnv([
"CI_GROUP=${parallelId}",
Expand All @@ -98,6 +99,7 @@ def withFunctionalTestEnv(List additionalEnvs = [], Closure closure) {
"TEST_ES_TRANSPORT_PORT=${esTransportPort}",
"KBN_NP_PLUGINS_BUILT=true",
"INGEST_MANAGEMENT_PACKAGE_REGISTRY_PORT=${ingestManagementPackageRegistryPort}",
"ALERTING_PROXY_PORT=${alertingProxyPort}"
] + additionalEnvs) {
closure()
}
Expand Down
2 changes: 1 addition & 1 deletion x-pack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
"font-awesome": "4.7.0",
"formsy-react": "^1.1.5",
"fp-ts": "^2.3.1",
"get-port": "^4.2.0",
"get-port": "^5.0.0",
"getos": "^3.1.0",
"git-url-parse": "11.1.2",
"github-markdown-css": "^2.10.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ export const createExternalService = (

const createIncident = async ({ incident }: ExternalServiceParams) => {
try {
logger.warn(`incident error : ${JSON.stringify(proxySettings)}`);
logger.warn(`incident error : ${url}`);
const res = await request({
axios: axiosInstance,
url: `${incidentUrl}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ describe('execute()', () => {
rejectUnauthorizedCertificates: false,
},
});
expect(mockedLogger.info).toHaveBeenCalledWith(
expect(mockedLogger.debug).toHaveBeenCalledWith(
'IncomingWebhook was called with proxyUrl https://someproxyhost'
);
});
Expand Down
4 changes: 1 addition & 3 deletions x-pack/plugins/actions/server/builtin_action_types/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async function slackExecutor(
let proxyAgent: HttpsProxyAgent | HttpProxyAgent | undefined;
if (execOptions.proxySettings) {
proxyAgent = getProxyAgent(execOptions.proxySettings, logger);
logger.info(`IncomingWebhook was called with proxyUrl ${execOptions.proxySettings.proxyUrl}`);
logger.debug(`IncomingWebhook was called with proxyUrl ${execOptions.proxySettings.proxyUrl}`);
}

try {
Expand All @@ -130,8 +130,6 @@ async function slackExecutor(
});
result = await webhook.send(message);
} catch (err) {
logger.error(`error on ${actionId} slack event: ${err.message}`);

if (err.original == null || err.original.response == null) {
return serviceErrorResult(actionId, err.message);
}
Expand Down
8 changes: 6 additions & 2 deletions x-pack/test/alerting_api_integration/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ export function createTestConfig(name: string, options: CreateTestConfigOptions)
fs.statSync(path.resolve(__dirname, 'fixtures', 'plugins', file)).isDirectory()
);

const proxyPort =
process.env.ALERTING_PROXY_PORT ?? (await getPort({ port: getPort.makeRange(6200, 6300) }));
const actionsProxyUrl = options.enableActionsProxy
? [`--xpack.actions.proxyUrl=http://localhost:${await getPort()}`]
? [
`--xpack.actions.proxyUrl=http://localhost:${proxyPort}`,
'--xpack.actions.rejectUnauthorizedCertificates=false',
]
: [];

return {
Expand Down Expand Up @@ -92,7 +97,6 @@ export function createTestConfig(name: string, options: CreateTestConfigOptions)
'--xpack.encryptedSavedObjects.encryptionKey="wuGNaIhoMpk5sO4UBxgr3NyW1sFcLgIf"',
`--xpack.actions.enabledActionTypes=${JSON.stringify(enabledActionTypes)}`,
...actionsProxyUrl,
'--xpack.actions.rejectUnauthorizedCertificates=false',

'--xpack.eventLog.logEntries=true',
`--xpack.actions.preconfigured=${JSON.stringify({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export async function initPlugin() {
response.statusCode = 400;
response.end('unknown request to slack simulator');
});
} else {
response.writeHead(400, { 'Content-Type': 'text/plain' });
response.end('Not supported http method to request slack simulator');
}
});
}
41 changes: 28 additions & 13 deletions x-pack/test/alerting_api_integration/common/lib/get_proxy_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,42 @@
* you may not use this file except in compliance with the Elastic License.
*/

import http from 'http';
import httpProxy from 'http-proxy';
import { ToolingLog } from '@kbn/dev-utils';

export const getHttpProxyServer = (
targetUrl: string,
onProxyResHandler: (proxyRes?: unknown, req?: unknown, res?: unknown) => void
): httpProxy => {
const proxyServer = httpProxy.createProxyServer({
target: targetUrl,
secure: false,
selfHandleResponse: false,
});
proxyServer.on('proxyRes', (proxyRes: unknown, req: unknown, res: unknown) => {
onProxyResHandler(proxyRes, req, res);
export const getHttpProxyServer = async (
defaultKibanaTargetUrl: string,
kbnTestServerConfig: any,
log: ToolingLog
): Promise<http.Server> => {
const proxy = httpProxy.createProxyServer({ secure: false, selfHandleResponse: false });

const proxyPort = getProxyPort(kbnTestServerConfig);
const proxyServer = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
const targetUrl = new URL(req.url ?? defaultKibanaTargetUrl);

if (targetUrl.hostname !== 'some.non.existent.com') {
proxy.web(req, res, {
target: `${targetUrl.protocol}//${targetUrl.hostname}:${targetUrl.port}`,
});
} else {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.write('error on call some.non.existent.com');
res.end();
}
});

proxyServer.listen(proxyPort);

return proxyServer;
};

export const getProxyUrl = (kbnTestServerConfig: any) => {
export const getProxyPort = (kbnTestServerConfig: any): number => {
const proxyUrl = kbnTestServerConfig
.find((val: string) => val.startsWith('--xpack.actions.proxyUrl='))
.replace('--xpack.actions.proxyUrl=', '');

return new URL(proxyUrl);
const urlObject = new URL(proxyUrl);
return Number(urlObject.port);
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import expect from '@kbn/expect';

import { getHttpProxyServer, getProxyUrl } from '../../../../common/lib/get_proxy_server';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';

import {
Expand Down Expand Up @@ -36,7 +35,6 @@ const mapping = [
export default function jiraTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const kibanaServer = getService('kibanaServer');
const config = getService('config');

const mockJira = {
config: {
Expand Down Expand Up @@ -75,20 +73,12 @@ export default function jiraTest({ getService }: FtrProviderContext) {
};

let jiraSimulatorURL: string = '<could not determine kibana url>';
let proxyServer: any;
let proxyHaveBeenCalled = false;

// FLAKY: https://github.com/elastic/kibana/issues/75722
describe.skip('Jira', () => {
describe('Jira', () => {
before(() => {
jiraSimulatorURL = kibanaServer.resolveUrl(
getExternalServiceSimulatorPath(ExternalServiceSimulator.JIRA)
);
proxyServer = getHttpProxyServer(kibanaServer.resolveUrl('/'), () => {
proxyHaveBeenCalled = true;
});
const proxyUrl = getProxyUrl(config.get('kbnTestServer.serverArgs'));
proxyServer.listen(Number(proxyUrl.port));
});

describe('Jira - Action Creation', () => {
Expand Down Expand Up @@ -539,8 +529,6 @@ export default function jiraTest({ getService }: FtrProviderContext) {
})
.expect(200);

expect(proxyHaveBeenCalled).to.equal(true);

expect(body).to.eql({
status: 'ok',
actionId: simulatedActionId,
Expand All @@ -554,9 +542,5 @@ export default function jiraTest({ getService }: FtrProviderContext) {
});
});
});

after(() => {
proxyServer.close();
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import expect from '@kbn/expect';

import { getHttpProxyServer, getProxyUrl } from '../../../../common/lib/get_proxy_server';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';

import {
Expand All @@ -18,26 +17,16 @@ import {
export default function pagerdutyTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const kibanaServer = getService('kibanaServer');
const config = getService('config');

// FLAKY: https://github.com/elastic/kibana/issues/75386
describe.skip('pagerduty action', () => {
describe('pagerduty action', () => {
let simulatedActionId = '';
let pagerdutySimulatorURL: string = '<could not determine kibana url>';
let proxyServer: any;
let proxyHaveBeenCalled = false;

// need to wait for kibanaServer to settle ...
before(() => {
pagerdutySimulatorURL = kibanaServer.resolveUrl(
getExternalServiceSimulatorPath(ExternalServiceSimulator.PAGERDUTY)
);

proxyServer = getHttpProxyServer(kibanaServer.resolveUrl('/'), () => {
proxyHaveBeenCalled = true;
});
const proxyUrl = getProxyUrl(config.get('kbnTestServer.serverArgs'));
proxyServer.listen(Number(proxyUrl.port));
});

it('should return successfully when passed valid create parameters', async () => {
Expand Down Expand Up @@ -155,7 +144,6 @@ export default function pagerdutyTest({ getService }: FtrProviderContext) {
},
})
.expect(200);
expect(proxyHaveBeenCalled).to.equal(true);

expect(result).to.eql({
status: 'ok',
Expand Down Expand Up @@ -215,9 +203,5 @@ export default function pagerdutyTest({ getService }: FtrProviderContext) {
expect(result.message).to.match(/error posting pagerduty event: http status 502/);
expect(result.retry).to.equal(true);
});

after(() => {
proxyServer.close();
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import expect from '@kbn/expect';

import { getHttpProxyServer, getProxyUrl } from '../../../../common/lib/get_proxy_server';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';

import {
Expand Down Expand Up @@ -36,7 +35,6 @@ const mapping = [
export default function resilientTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const kibanaServer = getService('kibanaServer');
const config = getService('config');

const mockResilient = {
config: {
Expand Down Expand Up @@ -75,19 +73,12 @@ export default function resilientTest({ getService }: FtrProviderContext) {
};

let resilientSimulatorURL: string = '<could not determine kibana url>';
let proxyServer: any;
let proxyHaveBeenCalled = false;

describe('IBM Resilient', () => {
before(() => {
resilientSimulatorURL = kibanaServer.resolveUrl(
getExternalServiceSimulatorPath(ExternalServiceSimulator.RESILIENT)
);
proxyServer = getHttpProxyServer(kibanaServer.resolveUrl('/'), () => {
proxyHaveBeenCalled = true;
});
const proxyUrl = getProxyUrl(config.get('kbnTestServer.serverArgs'));
proxyServer.listen(Number(proxyUrl.port));
});

describe('IBM Resilient - Action Creation', () => {
Expand Down Expand Up @@ -538,8 +529,6 @@ export default function resilientTest({ getService }: FtrProviderContext) {
})
.expect(200);

expect(proxyHaveBeenCalled).to.equal(true);

expect(body).to.eql({
status: 'ok',
actionId: simulatedActionId,
Expand All @@ -553,9 +542,5 @@ export default function resilientTest({ getService }: FtrProviderContext) {
});
});
});

after(() => {
proxyServer.close();
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import expect from '@kbn/expect';

import { getHttpProxyServer, getProxyUrl } from '../../../../common/lib/get_proxy_server';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';

import {
Expand Down Expand Up @@ -36,7 +35,6 @@ const mapping = [
export default function servicenowTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const kibanaServer = getService('kibanaServer');
const config = getService('config');

const mockServiceNow = {
config: {
Expand Down Expand Up @@ -74,21 +72,12 @@ export default function servicenowTest({ getService }: FtrProviderContext) {
};

let servicenowSimulatorURL: string = '<could not determine kibana url>';
let proxyServer: any;
let proxyHaveBeenCalled = false;

// FLAKY: https://github.com/elastic/kibana/issues/75522
describe.skip('ServiceNow', () => {
describe('ServiceNow', () => {
before(() => {
servicenowSimulatorURL = kibanaServer.resolveUrl(
getExternalServiceSimulatorPath(ExternalServiceSimulator.SERVICENOW)
);

proxyServer = getHttpProxyServer(kibanaServer.resolveUrl('/'), () => {
proxyHaveBeenCalled = true;
});
const proxyUrl = getProxyUrl(config.get('kbnTestServer.serverArgs'));
proxyServer.listen(Number(proxyUrl.port));
});

describe('ServiceNow - Action Creation', () => {
Expand Down Expand Up @@ -459,7 +448,6 @@ export default function servicenowTest({ getService }: FtrProviderContext) {
},
})
.expect(200);
expect(proxyHaveBeenCalled).to.equal(true);

expect(result).to.eql({
status: 'ok',
Expand All @@ -474,9 +462,5 @@ export default function servicenowTest({ getService }: FtrProviderContext) {
});
});
});

after(() => {
proxyServer.close();
});
});
}
Loading

0 comments on commit 8cdce2b

Please sign in to comment.