Skip to content

Commit

Permalink
[xpack/ftr/common] follow service provider conventions (#52236)
Browse files Browse the repository at this point in the history
* [xpack/ftr/common] follow conventions, expose a config file

In order to make xpack/test/common ftr config easier to work with, this updates it to follow the conventions established by most other configs of exposing all services from the services module as an object that can be easily merges into local services modules.

* common config file and FtrProviderConfig are unused and unnecessary

* turns out FtrProviderContext was used...
  • Loading branch information
Spencer authored Dec 5, 2019
1 parent 05fd394 commit b66415e
Show file tree
Hide file tree
Showing 53 changed files with 157 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SpacesService, SecurityService } from '../../../common/services';
import { FtrProviderContext } from '../../common/ftr_provider_context';
import { isCustomRoleSpecification } from '../../common/types';
import { Spaces, Users } from '../scenarios';
Expand All @@ -14,8 +13,8 @@ export default function alertingApiIntegrationTests({
loadTestFile,
getService,
}: FtrProviderContext) {
const securityService: SecurityService = getService('security');
const spacesService: SpacesService = getService('spaces');
const securityService = getService('security');
const spacesService = getService('spaces');
const esArchiver = getService('esArchiver');

describe('alerting api integration security and spaces enabled', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SpacesService } from '../../../common/services';
import { FtrProviderContext } from '../../common/ftr_provider_context';
import { Spaces } from '../scenarios';

Expand All @@ -13,7 +12,7 @@ export default function alertingApiIntegrationTests({
loadTestFile,
getService,
}: FtrProviderContext) {
const spacesService: SpacesService = getService('spaces');
const spacesService = getService('spaces');
const esArchiver = getService('esArchiver');

describe('alerting api integration spaces only', function() {
Expand Down
5 changes: 2 additions & 3 deletions x-pack/test/api_integration/apis/apm/feature_controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
*/

import expect from '@kbn/expect';
import { SecurityService, SpacesService } from '../../../common/services';
import { FtrProviderContext } from '../../ftr_provider_context';

export default function featureControlsTests({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const supertestWithoutAuth = getService('supertestWithoutAuth');
const security: SecurityService = getService('security');
const spaces: SpacesService = getService('spaces');
const security = getService('security');
const spaces = getService('spaces');
const log = getService('log');

const start = encodeURIComponent(new Date(Date.now() - 10000).toISOString());
Expand Down
5 changes: 2 additions & 3 deletions x-pack/test/api_integration/apis/console/feature_controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SecurityService, SpacesService } from '../../../common/services';
import { FtrProviderContext } from '../../ftr_provider_context';

export default function securityTests({ getService }: FtrProviderContext) {
const supertest = getService('supertestWithoutAuth');
const security: SecurityService = getService('security');
const spaces: SpacesService = getService('spaces');
const security = getService('security');
const spaces = getService('spaces');

describe('/api/console/proxy', () => {
it('cannot be accessed by an anonymous user', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
*/

import expect from '@kbn/expect';
import { SecurityService } from '../../../../common/services';
import { Feature } from '../../../../../plugins/features/server';
import { FtrProviderContext } from '../../../ftr_provider_context';

export default function({ getService }: FtrProviderContext) {
const supertest = getService('supertest');

const supertestWithoutAuth = getService('supertestWithoutAuth');
const security: SecurityService = getService('security');
const security = getService('security');

describe('/api/features', () => {
describe('with the "global all" privilege', () => {
Expand Down
5 changes: 2 additions & 3 deletions x-pack/test/api_integration/apis/infra/feature_controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import expect from '@kbn/expect';
import gql from 'graphql-tag';
import { SecurityService, SpacesService } from '../../../common/services';
import { FtrProviderContext } from '../../ftr_provider_context';

const introspectionQuery = gql`
Expand All @@ -21,8 +20,8 @@ const introspectionQuery = gql`

export default function({ getService }: FtrProviderContext) {
const supertest = getService('supertestWithoutAuth');
const security: SecurityService = getService('security');
const spaces: SpacesService = getService('spaces');
const security = getService('security');
const spaces = getService('spaces');
const clientFactory = getService('infraOpsGraphQLClientFactory');

const expectGraphQL404 = (result: any) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

import expect from '@kbn/expect';
import { SuperTest } from 'supertest';
import { SecurityService, SpacesService } from '../../../../common/services';
import { FtrProviderContext } from '../../../ftr_provider_context';

export default function featureControlsTests({ getService }: FtrProviderContext) {
const supertest: SuperTest<any> = getService('supertestWithoutAuth');
const security: SecurityService = getService('security');
const spaces: SpacesService = getService('spaces');
const security = getService('security');
const spaces = getService('spaces');

const expect403 = (result: any) => {
expect(result.error).to.be(undefined);
Expand Down
3 changes: 1 addition & 2 deletions x-pack/test/api_integration/apis/security/change_password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

import { Cookie, cookie } from 'request';
import { FtrProviderContext } from '../../ftr_provider_context';
import { SecurityService } from '../../../common/services/security';

export default function({ getService }: FtrProviderContext) {
const supertest = getService('supertestWithoutAuth');
const security: SecurityService = getService('security');
const security = getService('security');

const mockUserName = 'test-user';
const mockUserPassword = 'test-password';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
*/

import expect from '@kbn/expect';
import { SecurityService } from '../../../common/services';
import { FtrProviderContext } from '../../ftr_provider_context';

export default function featureControlsTests({ getService }: FtrProviderContext) {
const supertest = getService('supertestWithoutAuth');
const security: SecurityService = getService('security');
const security = getService('security');

describe('feature controls', () => {
const kibanaUsername = 'kibana_user';
Expand Down
5 changes: 2 additions & 3 deletions x-pack/test/api_integration/apis/siem/feature_controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import expect from '@kbn/expect';
import gql from 'graphql-tag';
import { SecurityService, SpacesService } from '../../../common/services';
import { FtrProviderContext } from '../../ftr_provider_context';

const introspectionQuery = gql`
Expand All @@ -22,8 +21,8 @@ const introspectionQuery = gql`
export default function({ getService }: FtrProviderContext) {
const config = getService('config');
const supertest = getService('supertestWithoutAuth');
const security: SecurityService = getService('security');
const spaces: SpacesService = getService('spaces');
const security = getService('security');
const spaces = getService('spaces');
const clientFactory = getService('siemGraphQLClientFactory');

const expectGraphQL404 = (result: any) => {
Expand Down
5 changes: 2 additions & 3 deletions x-pack/test/api_integration/apis/uptime/feature_controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

import expect from '@kbn/expect';
import { docCountQueryString } from '../../../../legacy/plugins/uptime/public/queries';
import { SecurityService, SpacesService } from '../../../common/services';
import { FtrProviderContext } from '../../ftr_provider_context';
import { PINGS_DATE_RANGE_END, PINGS_DATE_RANGE_START } from './constants';

export default function featureControlsTests({ getService }: FtrProviderContext) {
const supertest = getService('supertestWithoutAuth');
const security: SecurityService = getService('security');
const spaces: SpacesService = getService('spaces');
const security = getService('security');
const spaces = getService('spaces');

const expect404 = (result: any) => {
expect(result.error).to.be(undefined);
Expand Down
8 changes: 2 additions & 6 deletions x-pack/test/api_integration/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
*/

import { services as kibanaApiIntegrationServices } from '../../../../test/api_integration/services';
import { services as kibanaCommonServices } from '../../../../test/common/services';

import { SecurityServiceProvider, SpacesServiceProvider } from '../../common/services';
import { services as commonServices } from '../../common/services';

// @ts-ignore not ts yet
import { LegacyEsProvider } from './legacy_es';
Expand All @@ -25,7 +23,7 @@ import { SiemGraphQLClientProvider, SiemGraphQLClientFactoryProvider } from './s
import { InfraOpsSourceConfigurationProvider } from './infraops_source_configuration';

export const services = {
...kibanaCommonServices,
...commonServices,

esSupertest: kibanaApiIntegrationServices.esSupertest,
supertest: kibanaApiIntegrationServices.supertest,
Expand All @@ -35,10 +33,8 @@ export const services = {
infraOpsGraphQLClient: InfraOpsGraphQLClientProvider,
infraOpsGraphQLClientFactory: InfraOpsGraphQLClientFactoryProvider,
infraOpsSourceConfiguration: InfraOpsSourceConfigurationProvider,
security: SecurityServiceProvider,
siemGraphQLClient: SiemGraphQLClientProvider,
siemGraphQLClientFactory: SiemGraphQLClientFactoryProvider,
spaces: SpacesServiceProvider,
supertestWithoutAuth: SupertestWithoutAuthProvider,
usageAPI: UsageAPIProvider,
};
11 changes: 11 additions & 0 deletions x-pack/test/common/ftr_provider_context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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 { GenericFtrProviderContext } from '@kbn/test/types/ftr';

import { services } from './services';

export type FtrProviderContext = GenericFtrProviderContext<typeof services, {}>;
13 changes: 11 additions & 2 deletions x-pack/test/common/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,14 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { SecurityServiceProvider, SecurityService } from './security';
export { SpacesServiceProvider, SpacesService } from './spaces';
import { services as kibanaCommonServices } from '../../../../test/common/services';

import { SecurityServiceProvider } from './security';
import { SpacesServiceProvider } from './spaces';

export const services = {
...kibanaCommonServices,

security: SecurityServiceProvider,
spaces: SpacesServiceProvider,
};
23 changes: 1 addition & 22 deletions x-pack/test/common/services/security/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,5 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { format as formatUrl } from 'url';
import { ToolingLog } from '@kbn/dev-utils';
import { GenericFtrProviderContext } from '@kbn/test/types/ftr';
import { Role } from './role';
import { User } from './user';

export class SecurityService {
public role: Role;
public user: User;

constructor(url: string, log: ToolingLog) {
this.role = new Role(url, log);
this.user = new User(url, log);
}
}

export function SecurityServiceProvider({ getService }: GenericFtrProviderContext<{}, {}>) {
const log = getService('log');
const config = getService('config');
const url = formatUrl(config.get('servers.kibana'));

return new SecurityService(url, log);
}
export { SecurityServiceProvider } from './security';
21 changes: 21 additions & 0 deletions x-pack/test/common/services/security/security.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 { format as formatUrl } from 'url';

import { Role } from './role';
import { User } from './user';
import { FtrProviderContext } from '../../ftr_provider_context';

export function SecurityServiceProvider({ getService }: FtrProviderContext) {
const log = getService('log');
const config = getService('config');
const url = formatUrl(config.get('servers.kibana'));

return new (class SecurityService {
role = new Role(url, log);
user = new User(url, log);
})();
}
48 changes: 48 additions & 0 deletions x-pack/test/common/services/spaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 Axios from 'axios';
import { format as formatUrl } from 'url';
import util from 'util';
import { FtrProviderContext } from '../ftr_provider_context';

export function SpacesServiceProvider({ getService }: FtrProviderContext) {
const log = getService('log');
const config = getService('config');
const url = formatUrl(config.get('servers.kibana'));

const axios = Axios.create({
headers: { 'kbn-xsrf': 'x-pack/ftr/services/spaces/space' },
baseURL: url,
maxRedirects: 0,
validateStatus: () => true, // we do our own validation below and throw better error messages
});

return new (class SpacesService {
public async create(space: any) {
log.debug('creating space');
const { data, status, statusText } = await axios.post('/api/spaces/space', space);

if (status !== 200) {
throw new Error(
`Expected status code of 200, received ${status} ${statusText}: ${util.inspect(data)}`
);
}
log.debug('created space');
}

public async delete(spaceId: string) {
log.debug(`deleting space: ${spaceId}`);
const { data, status, statusText } = await axios.delete(`/api/spaces/space/${spaceId}`);

if (status !== 204) {
throw new Error(
`Expected status code of 204, received ${status} ${statusText}: ${util.inspect(data)}`
);
}
log.debug(`deleted space: ${spaceId}`);
}
})();
}
56 changes: 0 additions & 56 deletions x-pack/test/common/services/spaces/index.ts

This file was deleted.

Loading

0 comments on commit b66415e

Please sign in to comment.