-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[APM] Api test for feature flags in serverless (#162128)
Closes #159020 --------- Co-authored-by: kibanamachine <[email protected]> Co-authored-by: Achyut Jhunjhunwala <[email protected]>
- Loading branch information
1 parent
45f7a0b
commit 0706dd3
Showing
10 changed files
with
400 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
...api_integration/test_suites/observability/apm_api_integration/common/apm_api_supertest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* 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 { | ||
ApmUsername, | ||
APM_TEST_PASSWORD, | ||
// eslint-disable-next-line @kbn/imports/no_boundary_crossing | ||
} from '@kbn/apm-plugin/server/test_helpers/create_apm_users/authentication'; | ||
import { format, UrlObject } from 'url'; | ||
import supertest from 'supertest'; | ||
import request from 'superagent'; | ||
import type { | ||
APIReturnType, | ||
APIClientRequestParamsOf, | ||
} from '@kbn/apm-plugin/public/services/rest/create_call_apm_api'; | ||
import type { APIEndpoint } from '@kbn/apm-plugin/server'; | ||
import { formatRequest } from '@kbn/server-route-repository'; | ||
import { InheritedFtrProviderContext } from '../../../../services'; | ||
|
||
export function createApmApiClient(st: supertest.SuperTest<supertest.Test>) { | ||
return async <TEndpoint extends APIEndpoint>( | ||
options: { | ||
type?: 'form-data'; | ||
endpoint: TEndpoint; | ||
} & APIClientRequestParamsOf<TEndpoint> & { params?: { query?: { _inspect?: boolean } } } | ||
): Promise<SupertestReturnType<TEndpoint>> => { | ||
const { endpoint, type } = options; | ||
|
||
const params = 'params' in options ? (options.params as Record<string, any>) : {}; | ||
|
||
const { method, pathname, version } = formatRequest(endpoint, params.path); | ||
const url = format({ pathname, query: params?.query }); | ||
|
||
const headers: Record<string, string> = { 'kbn-xsrf': 'foo' }; | ||
|
||
if (version) { | ||
headers['Elastic-Api-Version'] = version; | ||
} | ||
|
||
let res: request.Response; | ||
if (type === 'form-data') { | ||
const fields: Array<[string, any]> = Object.entries(params.body); | ||
const formDataRequest = st[method](url) | ||
.set(headers) | ||
.set('Content-type', 'multipart/form-data'); | ||
|
||
for (const field of fields) { | ||
formDataRequest.field(field[0], field[1]); | ||
} | ||
|
||
res = await formDataRequest; | ||
} else if (params.body) { | ||
res = await st[method](url).send(params.body).set(headers); | ||
} else { | ||
res = await st[method](url).set(headers); | ||
} | ||
|
||
// supertest doesn't throw on http errors | ||
if (res?.status !== 200) { | ||
throw new ApmApiError(res, endpoint); | ||
} | ||
|
||
return res; | ||
}; | ||
} | ||
|
||
type ApiErrorResponse = Omit<request.Response, 'body'> & { | ||
body: { | ||
statusCode: number; | ||
error: string; | ||
message: string; | ||
attributes: object; | ||
}; | ||
}; | ||
|
||
export type ApmApiSupertest = ReturnType<typeof createApmApiClient>; | ||
|
||
export class ApmApiError extends Error { | ||
res: ApiErrorResponse; | ||
|
||
constructor(res: request.Response, endpoint: string) { | ||
super( | ||
`Unhandled ApmApiError. | ||
Status: "${res.status}" | ||
Endpoint: "${endpoint}" | ||
Body: ${JSON.stringify(res.body)}` | ||
); | ||
|
||
this.res = res; | ||
} | ||
} | ||
|
||
async function getApmApiClient({ | ||
kibanaServer, | ||
username, | ||
}: { | ||
kibanaServer: UrlObject; | ||
username: ApmUsername | 'elastic'; | ||
}) { | ||
const url = format({ | ||
...kibanaServer, | ||
auth: `${username}:${APM_TEST_PASSWORD}`, | ||
}); | ||
|
||
return createApmApiClient(supertest(url)); | ||
} | ||
|
||
export interface SupertestReturnType<TEndpoint extends APIEndpoint> { | ||
status: number; | ||
body: APIReturnType<TEndpoint>; | ||
} | ||
|
||
type ApmApiClientKey = 'slsUser'; | ||
export type ApmApiClient = Record<ApmApiClientKey, Awaited<ReturnType<typeof getApmApiClient>>>; | ||
|
||
export async function getApmApiClientService({ | ||
getService, | ||
}: InheritedFtrProviderContext): Promise<ApmApiClient> { | ||
const svlSharedConfig = getService('config'); | ||
const kibanaServer = svlSharedConfig.get('servers.kibana'); | ||
|
||
return { | ||
slsUser: await getApmApiClient({ | ||
kibanaServer, | ||
username: 'elastic', | ||
}), | ||
}; | ||
} |
25 changes: 25 additions & 0 deletions
25
...rverless/api_integration/test_suites/observability/apm_api_integration/common/services.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* 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 { GenericFtrProviderContext } from '@kbn/test'; | ||
import { ApmApiClient, getApmApiClientService } from './apm_api_supertest'; | ||
import { | ||
InheritedServices, | ||
InheritedFtrProviderContext, | ||
services as inheritedServices, | ||
} from '../../../../services'; | ||
|
||
export type APMServices = InheritedServices & { | ||
apmApiClient: (context: InheritedFtrProviderContext) => Promise<ApmApiClient>; | ||
}; | ||
|
||
export const services: APMServices = { | ||
...inheritedServices, | ||
apmApiClient: getApmApiClientService, | ||
}; | ||
|
||
export type APMFtrContextProvider = GenericFtrProviderContext<typeof services, {}>; |
Oops, something went wrong.