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

* Removed legacySupertestAsApmWriteUser + legacySupertestAsApmReadUserWithoutMlAccess

* [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix'

* [CI] Auto-commit changed files from 'node scripts/build_plugin_list_docs'

* Addressing PR changes

* [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix'

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Yngrid Coello 2022-10-06 12:10:20 +02:00 committed by GitHub
parent 46d87da2c1
commit 41b28155d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 64 deletions

View file

@ -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,
@ -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})`,

View file

@ -10,6 +10,10 @@ import { castArray, groupBy } from 'lodash';
import callsites from 'callsites';
import { maybe } from '@kbn/apm-plugin/common/utils/maybe';
import { joinByKey } from '@kbn/apm-plugin/common/utils/join_by_key';
import {
ApmUsername,
APM_TEST_PASSWORD,
} from '@kbn/apm-plugin/server/test_helpers/create_apm_users/authentication';
import { APMFtrConfigName } from '../configs';
import { FtrProviderContext } from './ftr_provider_context';
@ -104,8 +108,7 @@ export function RegistryProvider({ getService }: FtrProviderContext) {
const esArchiver = getService('esArchiver');
const logger = getService('log');
const supertest = getService('legacySupertestAsApmWriteUser');
const supertest = getService('supertest');
const logWithTimer = () => {
const start = process.hrtime();
@ -148,7 +151,10 @@ 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 supertest
.get('/api/ml/saved_objects/sync')
.set('kbn-xsrf', 'foo')
.auth(ApmUsername.editorUser, APM_TEST_PASSWORD);
}
if (condition.archives.length) {
log('Loaded all archives');

View file

@ -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');
@ -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
@ -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,
@ -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 () => {

View file

@ -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({
@ -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 Promise.allSettled(jobIds.map((jobId) => ml.deleteAnomalyDetectionJobES(jobId)));
}
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);
});