Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[APM] Migrate api tests to use apm api client #142724

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/developer/plugin-list.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ as uiSettings within the code.

|{kib-repo}blob/{branch}/src/plugins/data_views/README.mdx[dataViews]
|The data views API provides a consistent method of structuring and formatting documents
and field lists across the various Kibana apps. Its typically used in conjunction with
and field lists across the various Kibana apps. It's typically used in conjunction with
<DocLink id="kibDevTutorialDataSearchAndSessions" section="high-level-search" text="SearchSource" /> for composing queries.


Expand Down
20 changes: 0 additions & 20 deletions x-pack/test/apm_api_integration/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,6 @@ export interface ApmFtrConfig {
kibanaConfig?: Record<string, string | string[]>;
}

function getLegacySupertestClient(kibanaServer: UrlObject, username: ApmUsername) {
return async (context: InheritedFtrProviderContext) => {
const url = format({
...kibanaServer,
auth: `${username}:${APM_TEST_PASSWORD}`,
});

return supertest(url);
};
}

async function getApmApiClient({
kibanaServer,
username,
Expand Down Expand Up @@ -125,15 +114,6 @@ export function createTestConfig(config: ApmFtrConfig) {
};
},
ml: MachineLearningAPIProvider,
// legacy clients
legacySupertestAsApmWriteUser: getLegacySupertestClient(
kibanaServer,
ApmUsername.editorUser
),
legacySupertestAsApmReadUserWithoutMlAccess: getLegacySupertestClient(
kibanaServer,
ApmUsername.apmReadUserWithoutMlAccess
),
},
junit: {
reportName: `APM API Integration tests (${name})`,
Expand Down
5 changes: 2 additions & 3 deletions x-pack/test/apm_api_integration/common/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ export function RegistryProvider({ getService }: FtrProviderContext) {

const esArchiver = getService('esArchiver');
const logger = getService('log');

const supertest = getService('legacySupertestAsApmWriteUser');
const ml = getService('ml');

const logWithTimer = () => {
const start = process.hrtime();
Expand Down Expand Up @@ -148,7 +147,7 @@ export function RegistryProvider({ getService }: FtrProviderContext) {
);

// sync jobs from .ml-config to .kibana SOs
await supertest.get('/api/ml/saved_objects/sync').set('kbn-xsrf', 'foo');
await ml.syncMlJobs();
yngrdyn marked this conversation as resolved.
Show resolved Hide resolved
}
if (condition.archives.length) {
log('Loaded all archives');
Expand Down
63 changes: 28 additions & 35 deletions x-pack/test/apm_api_integration/tests/feature_controls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
*/

import expect from '@kbn/expect';
import { APIClientRequestParamsOf } from '@kbn/apm-plugin/public/services/rest/create_call_apm_api';
import { FtrProviderContext } from '../common/ftr_provider_context';

export default function featureControlsTests({ getService }: FtrProviderContext) {
const registry = getService('registry');
const supertest = getService('legacySupertestAsApmWriteUser');
const apmApiClient = getService('apmApiClient');
const supertestWithoutAuth = getService('supertestWithoutAuth');
const security = getService('security');
const spaces = getService('spaces');
Expand Down Expand Up @@ -40,6 +41,29 @@ export default function featureControlsTests({ getService }: FtrProviderContext)
expectResponse: (result: any) => void;
onExpectationFail?: () => Promise<any>;
}

function createAgent(
body: APIClientRequestParamsOf<'PUT /api/apm/settings/agent-configuration'>['params']['body']
) {
return apmApiClient.writeUser({
endpoint: 'PUT /api/apm/settings/agent-configuration',
params: {
body,
},
});
}

function deleteAgent(
body: APIClientRequestParamsOf<'DELETE /api/apm/settings/agent-configuration'>['params']['body']
) {
return apmApiClient.writeUser({
endpoint: 'DELETE /api/apm/settings/agent-configuration',
params: {
body,
},
});
}

const endpoints: Endpoint[] = [
{
// this doubles as a smoke test for the _inspect query parameter
Expand Down Expand Up @@ -200,28 +224,6 @@ export default function featureControlsTests({ getService }: FtrProviderContext)
.catch((error: any) => ({ error, response: undefined }));
}

async function executeAsAdmin({ method = 'get', url, body }: Endpoint['req'], spaceId?: string) {
const basePath = spaceId ? `/s/${spaceId}` : '';
const fullPath = `${basePath}${url}`;
let request = supertest[method](fullPath);

// json body
if (body) {
request = request.send(body);
}

const response = await request.set('kbn-xsrf', 'foo');

const { status } = response;
if (status !== 200) {
throw new Error(`Endpoint: ${method} ${fullPath}
Status code: ${status}
Response: ${response.body.message}`);
}

return response;
}

async function executeRequests({
username,
password,
Expand Down Expand Up @@ -268,23 +270,14 @@ export default function featureControlsTests({ getService }: FtrProviderContext)
};
before(async () => {
log.info(`Creating agent configuration`);
await executeAsAdmin({
method: 'put',
url: '/api/apm/settings/agent-configuration',
body: config,
});
await createAgent(config);
log.info(`Agent configuration created`);
});

after(async () => {
log.info('deleting agent configuration');
await executeAsAdmin({
method: 'delete',
url: `/api/apm/settings/agent-configuration`,
body: {
service: config.service,
},
});
await deleteAgent({ service: config.service });
log.info('Agent configuration deleted');
});

it(`APIs can't be accessed by logstash_read user`, async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { FtrProviderContext } from '../../../common/ftr_provider_context';
export default function apiTest({ getService }: FtrProviderContext) {
const registry = getService('registry');
const apmApiClient = getService('apmApiClient');
const legacyWriteUserClient = getService('legacySupertestAsApmWriteUser');
const ml = getService('ml');

function getJobs() {
return apmApiClient.writeUser({
Expand All @@ -30,17 +30,14 @@ export default function apiTest({ getService }: FtrProviderContext) {
}

function deleteJobs(jobIds: string[]) {
return legacyWriteUserClient
.post(`/api/ml/jobs/delete_jobs`)
.send({ jobIds })
.set('kbn-xsrf', 'foo');
return ml.deleteMlJobs(jobIds);
yngrdyn marked this conversation as resolved.
Show resolved Hide resolved
}

registry.when('ML jobs', { config: 'trial', archives: [] }, () => {
describe('when user has write access to ML', () => {
after(async () => {
const res = await getJobs();
const jobIds = res.body.jobs.map((job: any) => job.job_id);
const jobIds = res.body.jobs.map((job: any) => job.jobId);
await deleteJobs(jobIds);
});

Expand Down
19 changes: 19 additions & 0 deletions x-pack/test/functional/services/ml/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1463,5 +1463,24 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) {
log.debug('Module set up');
return module;
},

async deleteMlJobs(jobIds: string[]) {
log.debug(`Deleting ml jobs ids [${jobIds.join(',')}]`);
const { body, status } = await kbnSupertest
.post('/api/ml/jobs/delete_jobs')
.set(COMMON_REQUEST_HEADERS)
.send({ jobIds });
this.assertResponseStatusCode(200, status, body);

log.debug('> ml jobs deleted');
},

async syncMlJobs() {
log.debug('Syncing ml jobs');
const { body, status } = await kbnSupertest.get('/api/ml/saved_objects/sync');
this.assertResponseStatusCode(200, status, body);

log.debug('> ml jobs synced');
},
yngrdyn marked this conversation as resolved.
Show resolved Hide resolved
};
}