kibana/x-pack/test/profiling_api_integration/common/bettertest.ts
Cauê Marcondes 98d2766de8
[Profiling-APM] Removing Profiling dependency from APM (#166253)
This PR removes the Profiling dependency from APM, introduced on `8.10`.

- Exposes a new service in profiling-data-access plugin
- Create a new APM API that calls the new service and checks if
Profiling is initialized
- Move Locators from the Profiling plugin to the Observability-shared
plugin
- Move logic to check Profiling status (has_setup/has_data...) from
Profiling server to profiling-data-access plugin
- Create API tests, testing the status services based on different
scenarios:
  - When profiling hasn't been initialized and there's no data
  - When profiling is initialized but has no data
  - When collector integration is not installed
  - When symbolized integration is not installed
  - When APM server integration is not found

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
2023-09-22 09:16:48 +01:00

70 lines
1.8 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { format } from 'url';
import supertest from 'supertest';
import request from 'superagent';
type HttpMethod = 'get' | 'post' | 'put' | 'delete';
export type BetterTest = <T extends any>(options: {
pathname: string;
query?: Record<string, any>;
method?: HttpMethod;
body?: any;
}) => Promise<{ status: number; body: T }>;
/*
* This is a wrapper around supertest that throws an error if the response status is not 200.
* This is useful for tests that expect a 200 response
* It also makes it easier to debug tests that fail because of a 500 response.
*/
export function getBettertest(st: supertest.SuperTest<supertest.Test>): BetterTest {
return async ({ pathname, method = 'get', query, body }) => {
const url = format({ pathname, query });
let res: request.Response;
if (body) {
res = await st[method](url).send(body).set('kbn-xsrf', 'true');
} else {
res = await st[method](url).set('kbn-xsrf', 'true');
}
// supertest doesn't throw on http errors
if (res?.status !== 200 && res?.status !== 202) {
throw new BetterTestError(res);
}
return res;
};
}
type ErrorResponse = Omit<request.Response, 'body'> & {
body: {
statusCode: number;
error: string;
message: string;
attributes: object;
};
};
export class BetterTestError extends Error {
res: ErrorResponse;
constructor(res: request.Response) {
// @ts-expect-error
const req = res.req as any;
super(
`Unhandled BetterTestError:
Status: "${res.status}"
Path: "${req.method} ${req.path}"
Body: ${JSON.stringify(res.body)}`
);
this.res = res;
}
}