-
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
Changes from 8 commits
92c80ce
d5fb8b0
ef8ee48
f57a79f
406db08
dff67dc
1e1b2ad
265df11
49ebd73
78484af
0c45a7a
6c1828e
ab2c072
be9b1bb
bdd5f7b
7fcac6e
693b6c1
0ef4725
6240095
dc0f07b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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] Created an autoops agent ${response}`); | ||
return response; | ||
return validatedResponse; | ||
} | ||
|
||
private createTlsConfig(autoopsConfig: AutoOpsConfig | undefined) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,6 @@ export class DataUsageService { | |
metricTypes, | ||
dataStreams, | ||
}); | ||
return response.data; | ||
return response; | ||
} | ||
} |
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')); | ||
}); | ||
} |
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], | ||
], | ||
}, | ||
], | ||
}, | ||
}); | ||
} | ||
); |
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 { FtrProviderContext } from '../../../../ftr_provider_context'; | ||
|
||
const API_PATH = '/internal/api/data_usage/data_streams'; | ||
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. You can import and use import { DATA_USAGE_METRICS_API_ROUTE } from '@kbn/data-usage-plugin/common'; |
||
export default function ({ getService }: FtrProviderContext) { | ||
const svlDatastreamsHelpers = getService('svlDatastreamsHelpers'); | ||
const roleScopedSupertest = getService('roleScopedSupertest'); | ||
let supertestAdminWithCookieCredentials: SupertestWithRoleScope; | ||
const testDataStreamName = 'test-data-stream'; | ||
describe(`GET ${API_PATH}`, 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(API_PATH) | ||
.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(API_PATH) | ||
.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); | ||
}); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* 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 { FtrProviderContext } from '../../../../ftr_provider_context'; | ||
import { setupMockServer } from '../mock_api'; | ||
|
||
const API_PATH = '/internal/api/data_usage/metrics'; | ||
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 ${API_PATH}`, () => { | ||
const testDataStreamName = 'test-data-stream'; | ||
before(async () => await svlDatastreamsHelpers.createDataStream(testDataStreamName)); | ||
after(async () => await svlDatastreamsHelpers.deleteDataStream(testDataStreamName)); | ||
it('returns 500 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(API_PATH) | ||
.set('elastic-api-version', '1') | ||
.send(requestBody); | ||
expect(res.statusCode).to.be(500); | ||
}); | ||
|
||
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(API_PATH) | ||
.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', | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,6 +5,7 @@ | |||||
* 2.0. | ||||||
*/ | ||||||
|
||||||
import { CA_CERT_PATH, KBN_CERT_PATH, KBN_KEY_PATH } from '@kbn/dev-utils'; | ||||||
import { createTestConfig } from '../../config.base'; | ||||||
|
||||||
export default createTestConfig({ | ||||||
|
@@ -21,5 +22,12 @@ export default createTestConfig({ | |||||
kbnServerArgs: [ | ||||||
// useful for testing (also enabled in MKI QA) | ||||||
'--coreApp.allowDynamicConfigOverrides=true', | ||||||
'--xpack.dataUsage.enabled=true', | ||||||
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. we need to enable this to run the tests in MKI. once we enable this in serverless only, we can remove this. 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.
Suggested change
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 added |
||||||
// dataUsage.autoops* config is set in kibana controller | ||||||
'--xpack.dataUsage.autoops.enabled=true', | ||||||
'--xpack.dataUsage.autoops.api.url=http://localhost:9000', | ||||||
`--xpack.dataUsage.autoops.api.tls.certificate=${KBN_CERT_PATH}`, | ||||||
`--xpack.dataUsage.autoops.api.tls.key=${KBN_KEY_PATH}`, | ||||||
`--xpack.dataUsage.autoops.api.tls.ca=${CA_CERT_PATH}`, | ||||||
], | ||||||
}); |
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.