-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Data Usage] setup integration tests #197112
Merged
neptunian
merged 20 commits into
elastic:main
from
neptunian:data-usage-serverless-api-integration-tests
Nov 6, 2024
+392
−13
Merged
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
92c80ce
setup integration tests
neptunian d5fb8b0
fix error
neptunian ef8ee48
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine f57a79f
add data_streams test
neptunian 406db08
skip data_streams in mki
neptunian dff67dc
remove proxy in favor of mock server
neptunian 1e1b2ad
use certs from dev_utils
neptunian 265df11
add missing config
neptunian 49ebd73
merge error
neptunian 78484af
use existing route constant
neptunian 0c45a7a
add tests
neptunian 6c1828e
Merge branch 'main' into data-usage-serverless-api-integration-tests
neptunian ab2c072
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine be9b1bb
Merge branch 'main' into data-usage-serverless-api-integration-tests
neptunian bdd5f7b
remove codeowners entry
neptunian 7fcac6e
cleanup mock data
neptunian 693b6c1
functional test configs and smoke test
neptunian 0ef4725
skip in MKI
neptunian 6240095
change to main
neptunian dc0f07b
fix search config
neptunian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import type { AxiosError, AxiosRequestConfig } from 'axios'; | |
import axios from 'axios'; | ||
import { LogMeta } from '@kbn/core/server'; | ||
import { | ||
UsageMetricsAutoOpsResponseSchema, | ||
UsageMetricsAutoOpsResponseSchemaBody, | ||
UsageMetricsRequestBody, | ||
} from '../../common/rest_types'; | ||
|
@@ -134,8 +135,10 @@ export class AutoOpsAPIService { | |
} | ||
); | ||
|
||
const validatedResponse = UsageMetricsAutoOpsResponseSchema.body().validate(response.data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should validate the autoops response at runtime. The error will be caught further up. |
||
|
||
logger.debug(`[AutoOps API] Successfully created an autoops agent ${response}`); | ||
return response; | ||
return validatedResponse; | ||
} | ||
|
||
private createTlsConfig(autoopsConfig: AutoOpsConfig | undefined) { | ||
|
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
17 changes: 17 additions & 0 deletions
17
x-pack/test_serverless/api_integration/test_suites/common/data_usage/index.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,17 @@ | ||
/* | ||
* 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 { FtrProviderContext } from '../../../ftr_provider_context'; | ||
|
||
export default function ({ loadTestFile }: FtrProviderContext) { | ||
describe('Serverless Data Usage APIs', function () { | ||
this.tags(['esGate']); | ||
|
||
loadTestFile(require.resolve('./tests/data_streams')); | ||
loadTestFile(require.resolve('./tests/metrics')); | ||
}); | ||
} |
58 changes: 58 additions & 0 deletions
58
x-pack/test_serverless/api_integration/test_suites/common/data_usage/mock_api.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,58 @@ | ||
/* | ||
* 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 { createServer } from '@mswjs/http-middleware'; | ||
import { UsageMetricsAutoOpsResponseSchemaBody } from '@kbn/data-usage-plugin/common/rest_types'; | ||
|
||
import { http, HttpResponse, StrictResponse } from 'msw'; | ||
|
||
export const setupMockServer = () => { | ||
const server = createServer(autoOpsHandler); | ||
return server; | ||
}; | ||
|
||
const autoOpsHandler = http.post( | ||
'/', | ||
async ({ request }): Promise<StrictResponse<UsageMetricsAutoOpsResponseSchemaBody>> => { | ||
return HttpResponse.json({ | ||
metrics: { | ||
ingest_rate: [ | ||
{ | ||
name: 'metrics-system.cpu-default', | ||
data: [ | ||
[1726858530000, 13756849], | ||
[1726862130000, 14657904], | ||
], | ||
}, | ||
{ | ||
name: 'logs-nginx.access-default', | ||
data: [ | ||
[1726858530000, 12894623], | ||
[1726862130000, 14436905], | ||
], | ||
}, | ||
], | ||
storage_retained: [ | ||
{ | ||
name: 'metrics-system.cpu-default', | ||
data: [ | ||
[1726858530000, 12576413], | ||
[1726862130000, 13956423], | ||
], | ||
}, | ||
{ | ||
name: 'logs-nginx.access-default', | ||
data: [ | ||
[1726858530000, 12894623], | ||
[1726862130000, 14436905], | ||
], | ||
}, | ||
], | ||
}, | ||
}); | ||
} | ||
); |
56 changes: 56 additions & 0 deletions
56
x-pack/test_serverless/api_integration/test_suites/common/data_usage/tests/data_streams.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,56 @@ | ||
/* | ||
* 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 expect from '@kbn/expect'; | ||
import { SupertestWithRoleScope } from '@kbn/test-suites-xpack/api_integration/deployment_agnostic/services/role_scoped_supertest'; | ||
import { DataStreamsResponseBodySchemaBody } from '@kbn/data-usage-plugin/common/rest_types'; | ||
import { DATA_USAGE_DATA_STREAMS_API_ROUTE } from '@kbn/data-usage-plugin/common'; | ||
import { FtrProviderContext } from '../../../../ftr_provider_context'; | ||
|
||
export default function ({ getService }: FtrProviderContext) { | ||
const svlDatastreamsHelpers = getService('svlDatastreamsHelpers'); | ||
const roleScopedSupertest = getService('roleScopedSupertest'); | ||
let supertestAdminWithCookieCredentials: SupertestWithRoleScope; | ||
const testDataStreamName = 'test-data-stream'; | ||
describe(`GET ${DATA_USAGE_DATA_STREAMS_API_ROUTE}`, function () { | ||
// due to the plugin depending on yml config (xpack.dataUsage.enabled), we cannot test in MKI until it is on by default | ||
this.tags(['skipMKI']); | ||
before(async () => { | ||
await svlDatastreamsHelpers.createDataStream(testDataStreamName); | ||
supertestAdminWithCookieCredentials = await roleScopedSupertest.getSupertestWithRoleScope( | ||
'admin', | ||
{ | ||
useCookieHeader: true, | ||
withInternalHeaders: true, | ||
} | ||
); | ||
}); | ||
after(async () => { | ||
await svlDatastreamsHelpers.deleteDataStream(testDataStreamName); | ||
}); | ||
|
||
it('returns created data streams', async () => { | ||
const res = await supertestAdminWithCookieCredentials | ||
.get(DATA_USAGE_DATA_STREAMS_API_ROUTE) | ||
.set('elastic-api-version', '1'); | ||
const dataStreams: DataStreamsResponseBodySchemaBody = res.body; | ||
const foundStream = dataStreams.find((stream) => stream.name === testDataStreamName); | ||
expect(foundStream?.name).to.be(testDataStreamName); | ||
expect(foundStream?.storageSizeBytes).to.be(0); | ||
expect(res.statusCode).to.be(200); | ||
}); | ||
it('returns system indices', async () => { | ||
const res = await supertestAdminWithCookieCredentials | ||
.get(DATA_USAGE_DATA_STREAMS_API_ROUTE) | ||
.set('elastic-api-version', '1'); | ||
const dataStreams: DataStreamsResponseBodySchemaBody = res.body; | ||
const systemDataStreams = dataStreams.filter((stream) => stream.name.startsWith('.')); | ||
expect(systemDataStreams.length).to.be.greaterThan(0); | ||
expect(res.statusCode).to.be(200); | ||
}); | ||
}); | ||
} |
168 changes: 168 additions & 0 deletions
168
x-pack/test_serverless/api_integration/test_suites/common/data_usage/tests/metrics.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,168 @@ | ||
/* | ||
* 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 expect from '@kbn/expect'; | ||
import http from 'http'; | ||
|
||
import { SupertestWithRoleScope } from '@kbn/test-suites-xpack/api_integration/deployment_agnostic/services/role_scoped_supertest'; | ||
import { UsageMetricsRequestBody } from '@kbn/data-usage-plugin/common/rest_types'; | ||
import { DATA_USAGE_METRICS_API_ROUTE } from '@kbn/data-usage-plugin/common'; | ||
import { FtrProviderContext } from '../../../../ftr_provider_context'; | ||
import { setupMockServer } from '../mock_api'; | ||
|
||
export default function ({ getService }: FtrProviderContext) { | ||
const svlDatastreamsHelpers = getService('svlDatastreamsHelpers'); | ||
const roleScopedSupertest = getService('roleScopedSupertest'); | ||
let supertestAdminWithCookieCredentials: SupertestWithRoleScope; | ||
const mockAutoopsApiService = setupMockServer(); | ||
describe('Metrics', function () { | ||
let mockApiServer: http.Server; | ||
// due to the plugin depending on yml config (xpack.dataUsage.enabled), we cannot test in MKI until it is on by default | ||
this.tags(['skipMKI']); | ||
|
||
before(async () => { | ||
mockApiServer = mockAutoopsApiService.listen(9000); | ||
supertestAdminWithCookieCredentials = await roleScopedSupertest.getSupertestWithRoleScope( | ||
'admin', | ||
{ | ||
useCookieHeader: true, | ||
withInternalHeaders: true, | ||
} | ||
); | ||
}); | ||
|
||
after(() => { | ||
mockApiServer.close(); | ||
}); | ||
describe(`POST ${DATA_USAGE_METRICS_API_ROUTE}`, () => { | ||
const testDataStreamName = 'test-data-stream'; | ||
before(async () => await svlDatastreamsHelpers.createDataStream(testDataStreamName)); | ||
after(async () => await svlDatastreamsHelpers.deleteDataStream(testDataStreamName)); | ||
it('returns 400 with non-existent data streams', async () => { | ||
const requestBody: UsageMetricsRequestBody = { | ||
from: 'now-24h/h', | ||
to: 'now', | ||
metricTypes: ['ingest_rate', 'storage_retained'], | ||
dataStreams: ['invalid-data-stream'], | ||
}; | ||
const res = await supertestAdminWithCookieCredentials | ||
.post(DATA_USAGE_METRICS_API_ROUTE) | ||
.set('elastic-api-version', '1') | ||
.send(requestBody); | ||
expect(res.statusCode).to.be(400); | ||
expect(res.body.message).to.be('Failed to retrieve data streams'); | ||
}); | ||
|
||
it('returns 400 when requesting no data streams', async () => { | ||
const requestBody = { | ||
from: 'now-24h/h', | ||
to: 'now', | ||
metricTypes: ['ingest_rate'], | ||
dataStreams: [], | ||
}; | ||
const res = await supertestAdminWithCookieCredentials | ||
.post(DATA_USAGE_METRICS_API_ROUTE) | ||
.set('elastic-api-version', '1') | ||
.send(requestBody); | ||
expect(res.statusCode).to.be(400); | ||
expect(res.body.message).to.be('[request body.dataStreams]: no data streams selected'); | ||
}); | ||
|
||
it('returns 400 when requesting an invalid metric type', async () => { | ||
const requestBody = { | ||
from: 'now-24h/h', | ||
to: 'now', | ||
metricTypes: [testDataStreamName], | ||
dataStreams: ['datastream'], | ||
}; | ||
const res = await supertestAdminWithCookieCredentials | ||
.post(DATA_USAGE_METRICS_API_ROUTE) | ||
.set('elastic-api-version', '1') | ||
.send(requestBody); | ||
expect(res.statusCode).to.be(400); | ||
expect(res.body.message).to.be( | ||
'[request body.metricTypes]: must be one of ingest_rate, storage_retained, search_vcu, ingest_vcu, ml_vcu, index_latency, index_rate, search_latency, search_rate' | ||
); | ||
}); | ||
|
||
it('returns 200 with valid request', async () => { | ||
const requestBody: UsageMetricsRequestBody = { | ||
from: 'now-24h/h', | ||
to: 'now', | ||
metricTypes: ['ingest_rate', 'storage_retained'], | ||
dataStreams: [testDataStreamName], | ||
}; | ||
const res = await supertestAdminWithCookieCredentials | ||
.post(DATA_USAGE_METRICS_API_ROUTE) | ||
.set('elastic-api-version', '1') | ||
.send(requestBody); | ||
expect(res.statusCode).to.be(200); | ||
// TODO: decide on how to generate data | ||
neptunian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(res.body).to.eql({ | ||
metrics: { | ||
ingest_rate: [ | ||
{ | ||
data: [ | ||
{ | ||
x: 1726858530000, | ||
y: 13756849, | ||
}, | ||
{ | ||
x: 1726862130000, | ||
y: 14657904, | ||
}, | ||
], | ||
name: 'metrics-system.cpu-default', | ||
}, | ||
{ | ||
data: [ | ||
{ | ||
x: 1726858530000, | ||
y: 12894623, | ||
}, | ||
{ | ||
x: 1726862130000, | ||
y: 14436905, | ||
}, | ||
], | ||
name: 'logs-nginx.access-default', | ||
}, | ||
], | ||
storage_retained: [ | ||
{ | ||
data: [ | ||
{ | ||
x: 1726858530000, | ||
y: 12576413, | ||
}, | ||
{ | ||
x: 1726862130000, | ||
y: 13956423, | ||
}, | ||
], | ||
name: 'metrics-system.cpu-default', | ||
}, | ||
{ | ||
data: [ | ||
{ | ||
x: 1726858530000, | ||
y: 12894623, | ||
}, | ||
{ | ||
x: 1726862130000, | ||
y: 14436905, | ||
}, | ||
], | ||
name: 'logs-nginx.access-default', | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this type because I was getting a type error when only sending a subset of metrics (using the type in mock server response), so this allows the subset instead of all. @ashokaditya Hope thats ok.