Skip to content

Commit

Permalink
Added functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YulNaumenko committed Aug 13, 2020
1 parent 9039f11 commit d7a7254
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 1 deletion.
1 change: 1 addition & 0 deletions x-pack/test/alerting_api_integration/basic/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export default createTestConfig('basic', {
disabledPlugins: [],
license: 'basic',
ssl: true,
enableActionsProxy: false,
});
8 changes: 8 additions & 0 deletions x-pack/test/alerting_api_integration/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import path from 'path';
import getPort from 'get-port';
import fs from 'fs';
import { CA_CERT_PATH } from '@kbn/dev-utils';
import { FtrConfigProviderContext } from '@kbn/test/types/ftr';
Expand All @@ -15,6 +16,7 @@ interface CreateTestConfigOptions {
license: string;
disabledPlugins?: string[];
ssl?: boolean;
enableActionsProxy: boolean;
}

// test.not-enabled is specifically not enabled
Expand Down Expand Up @@ -56,6 +58,10 @@ export function createTestConfig(name: string, options: CreateTestConfigOptions)
fs.statSync(path.resolve(__dirname, 'fixtures', 'plugins', file)).isDirectory()
);

const actionsProxyUrl = options.enableActionsProxy
? `http://localhost:${await getPort()}`
: undefined;

return {
testFiles: [require.resolve(`../${name}/tests/`)],
servers,
Expand Down Expand Up @@ -85,6 +91,8 @@ export function createTestConfig(name: string, options: CreateTestConfigOptions)
])}`,
'--xpack.encryptedSavedObjects.encryptionKey="wuGNaIhoMpk5sO4UBxgr3NyW1sFcLgIf"',
`--xpack.actions.enabledActionTypes=${JSON.stringify(enabledActionTypes)}`,
`--xpack.actions.proxyUrl=${actionsProxyUrl}`,
'--xpack.actions.rejectUnauthorizedCertificates=false',
'--xpack.eventLog.logEntries=true',
`--xpack.actions.preconfigured=${JSON.stringify({
'my-slack1': {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import httpProxy from 'http-proxy';

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);
});
return proxyServer;
};

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

return new URL(proxySlackSimulatorURL);
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export default createTestConfig('security_and_spaces', {
disabledPlugins: [],
license: 'trial',
ssl: true,
enableActionsProxy: true,
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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 @@ -35,6 +36,7 @@ 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 @@ -73,12 +75,19 @@ export default function jiraTest({ getService }: FtrProviderContext) {
};

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

describe('Jira', () => {
before(() => {
jiraSimulatorURL = kibanaServer.resolveUrl(
getExternalServiceSimulatorPath(ExternalServiceSimulator.JIRA)
);
proxyServer = getHttpProxyServer(jiraSimulatorURL, () => {
proxyHaveBeenCalled = true;
});
const proxyUrl = getProxyUrl(config.get('kbnTestServer.serverArgs'));
proxyServer.listen(Number(proxyUrl.port));
});

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

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

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

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

import expect from '@kbn/expect';

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

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

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(pagerdutySimulatorURL, () => {
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 @@ -144,6 +154,8 @@ export default function pagerdutyTest({ getService }: FtrProviderContext) {
},
})
.expect(200);
expect(proxyHaveBeenCalled).to.equal(true);

expect(result).to.eql({
status: 'ok',
actionId: simulatedActionId,
Expand Down Expand Up @@ -202,5 +214,9 @@ 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,6 +6,7 @@

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 @@ -35,6 +36,7 @@ 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 @@ -73,12 +75,19 @@ 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(resilientSimulatorURL, () => {
proxyHaveBeenCalled = true;
});
const proxyUrl = getProxyUrl(config.get('kbnTestServer.serverArgs'));
proxyServer.listen(Number(proxyUrl.port));
});

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

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

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

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

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 @@ -35,6 +36,7 @@ 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 @@ -72,12 +74,20 @@ export default function servicenowTest({ getService }: FtrProviderContext) {
};

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

describe('ServiceNow', () => {
before(() => {
servicenowSimulatorURL = kibanaServer.resolveUrl(
getExternalServiceSimulatorPath(ExternalServiceSimulator.SERVICENOW)
);

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

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

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

after(() => {
proxyServer.close();
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,35 @@
import expect from '@kbn/expect';
import http from 'http';
import getPort from 'get-port';
import { getHttpProxyServer, getProxyUrl } from '../../../../common/lib/get_proxy_server';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';

import { getSlackServer } from '../../../../common/fixtures/plugins/actions_simulators/server/plugin';

// eslint-disable-next-line import/no-default-export
export default function slackTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const config = getService('config');

describe('slack action', () => {
let simulatedActionId = '';

let slackSimulatorURL: string = '';
let slackServer: http.Server;
let proxyServer: any;
let proxyHaveBeenCalled = false;
// need to wait for kibanaServer to settle ...
before(async () => {
slackServer = await getSlackServer();
const availablePort = await getPort({ port: 9000 });
slackServer.listen(availablePort);
slackSimulatorURL = `http://localhost:${availablePort}`;

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

it('should return 200 when creating a slack action successfully', async () => {
Expand Down Expand Up @@ -155,6 +165,7 @@ export default function slackTest({ getService }: FtrProviderContext) {
})
.expect(200);
expect(result.status).to.eql('ok');
expect(proxyHaveBeenCalled).to.equal(true);
});

it('should handle an empty message error', async () => {
Expand Down Expand Up @@ -222,6 +233,7 @@ export default function slackTest({ getService }: FtrProviderContext) {

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

0 comments on commit d7a7254

Please sign in to comment.