-
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.
[8.x] [Response Ops][Task Manager] Adding integration test to ensure …
…no `WorkloadAggregator` errors when there are unrecognized task types. (#193479) (#194016) # Backport This will backport the following commits from `main` to `8.x`: - [[Response Ops][Task Manager] Adding integration test to ensure no `WorkloadAggregator` errors when there are unrecognized task types. (#193479)](#193479) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Ying Mao","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-09-25T14:22:11Z","message":"[Response Ops][Task Manager] Adding integration test to ensure no `WorkloadAggregator` errors when there are unrecognized task types. (#193479)\n\nFixes https://github.com/elastic/kibana-team/issues/1036\r\n\r\n## Summary\r\n\r\nAdding integration test as RCA action for incident where unrecognized\r\ntask types was causing issues generating the workload portion of the\r\ntask manager health report.\r\n\r\n## To verify\r\n\r\nAdd this line to your code to that will throw an error when there are\r\nunrecognized task types when generating the health report\r\n\r\n```\r\n--- a/x-pack/plugins/task_manager/server/task_type_dictionary.ts\r\n+++ b/x-pack/plugins/task_manager/server/task_type_dictionary.ts\r\n@@ -128,6 +128,7 @@ export class TaskTypeDictionary {\r\n }\r\n\r\n public get(type: string): TaskDefinition | undefined {\r\n+ this.ensureHas(type);\r\n return this.definitions.get(type);\r\n }\r\n```\r\n\r\nRun the integration test `node scripts/jest_integration.js\r\nx-pack/plugins/task_manager/server/integration_tests/removed_types.test.ts`\r\nand see that it fails because a `WorkloadAggregator` error is logged.\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>","sha":"01eae1556266c8377f6557f4ccacc53e0b4db7fc","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Feature:Task Manager","Team:ResponseOps","v9.0.0","backport:prev-minor","v8.16.0"],"title":"[Response Ops][Task Manager] Adding integration test to ensure no `WorkloadAggregator` errors when there are unrecognized task types.","number":193479,"url":"https://github.com/elastic/kibana/pull/193479","mergeCommit":{"message":"[Response Ops][Task Manager] Adding integration test to ensure no `WorkloadAggregator` errors when there are unrecognized task types. (#193479)\n\nFixes https://github.com/elastic/kibana-team/issues/1036\r\n\r\n## Summary\r\n\r\nAdding integration test as RCA action for incident where unrecognized\r\ntask types was causing issues generating the workload portion of the\r\ntask manager health report.\r\n\r\n## To verify\r\n\r\nAdd this line to your code to that will throw an error when there are\r\nunrecognized task types when generating the health report\r\n\r\n```\r\n--- a/x-pack/plugins/task_manager/server/task_type_dictionary.ts\r\n+++ b/x-pack/plugins/task_manager/server/task_type_dictionary.ts\r\n@@ -128,6 +128,7 @@ export class TaskTypeDictionary {\r\n }\r\n\r\n public get(type: string): TaskDefinition | undefined {\r\n+ this.ensureHas(type);\r\n return this.definitions.get(type);\r\n }\r\n```\r\n\r\nRun the integration test `node scripts/jest_integration.js\r\nx-pack/plugins/task_manager/server/integration_tests/removed_types.test.ts`\r\nand see that it fails because a `WorkloadAggregator` error is logged.\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>","sha":"01eae1556266c8377f6557f4ccacc53e0b4db7fc"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/193479","number":193479,"mergeCommit":{"message":"[Response Ops][Task Manager] Adding integration test to ensure no `WorkloadAggregator` errors when there are unrecognized task types. (#193479)\n\nFixes https://github.com/elastic/kibana-team/issues/1036\r\n\r\n## Summary\r\n\r\nAdding integration test as RCA action for incident where unrecognized\r\ntask types was causing issues generating the workload portion of the\r\ntask manager health report.\r\n\r\n## To verify\r\n\r\nAdd this line to your code to that will throw an error when there are\r\nunrecognized task types when generating the health report\r\n\r\n```\r\n--- a/x-pack/plugins/task_manager/server/task_type_dictionary.ts\r\n+++ b/x-pack/plugins/task_manager/server/task_type_dictionary.ts\r\n@@ -128,6 +128,7 @@ export class TaskTypeDictionary {\r\n }\r\n\r\n public get(type: string): TaskDefinition | undefined {\r\n+ this.ensureHas(type);\r\n return this.definitions.get(type);\r\n }\r\n```\r\n\r\nRun the integration test `node scripts/jest_integration.js\r\nx-pack/plugins/task_manager/server/integration_tests/removed_types.test.ts`\r\nand see that it fails because a `WorkloadAggregator` error is logged.\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>","sha":"01eae1556266c8377f6557f4ccacc53e0b4db7fc"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Ying Mao <[email protected]>
- Loading branch information
1 parent
9307e92
commit 1108910
Showing
13 changed files
with
164 additions
and
17 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
147 changes: 147 additions & 0 deletions
147
x-pack/plugins/task_manager/server/integration_tests/removed_types.test.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,147 @@ | ||
/* | ||
* 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 { v4 as uuidV4 } from 'uuid'; | ||
import { ElasticsearchClient } from '@kbn/core/server'; | ||
import { TaskManagerPlugin, TaskManagerStartContract } from '../plugin'; | ||
import { injectTask, retry, setupTestServers } from './lib'; | ||
import { TestElasticsearchUtils, TestKibanaUtils } from '@kbn/core-test-helpers-kbn-server'; | ||
import { ConcreteTaskInstance, TaskStatus } from '../task'; | ||
import { CreateWorkloadAggregatorOpts } from '../monitoring/workload_statistics'; | ||
|
||
const taskManagerStartSpy = jest.spyOn(TaskManagerPlugin.prototype, 'start'); | ||
|
||
const { createWorkloadAggregator: createWorkloadAggregatorMock } = jest.requireMock( | ||
'../monitoring/workload_statistics' | ||
); | ||
jest.mock('../monitoring/workload_statistics', () => { | ||
const actual = jest.requireActual('../monitoring/workload_statistics'); | ||
return { | ||
...actual, | ||
createWorkloadAggregator: jest.fn().mockImplementation((opts) => { | ||
return new actual.createWorkloadAggregator(opts); | ||
}), | ||
}; | ||
}); | ||
|
||
describe('unrecognized task types', () => { | ||
let esServer: TestElasticsearchUtils; | ||
let kibanaServer: TestKibanaUtils; | ||
let taskManagerPlugin: TaskManagerStartContract; | ||
let createWorkloadAggregatorOpts: CreateWorkloadAggregatorOpts; | ||
|
||
const taskIdsToRemove: string[] = []; | ||
|
||
beforeAll(async () => { | ||
const setupResult = await setupTestServers({ | ||
xpack: { | ||
task_manager: { | ||
monitored_aggregated_stats_refresh_rate: 5000, | ||
}, | ||
}, | ||
}); | ||
esServer = setupResult.esServer; | ||
kibanaServer = setupResult.kibanaServer; | ||
|
||
expect(taskManagerStartSpy).toHaveBeenCalledTimes(1); | ||
taskManagerPlugin = taskManagerStartSpy.mock.results[0].value; | ||
|
||
expect(createWorkloadAggregatorMock).toHaveBeenCalledTimes(1); | ||
createWorkloadAggregatorOpts = createWorkloadAggregatorMock.mock.calls[0][0]; | ||
}); | ||
|
||
afterAll(async () => { | ||
if (kibanaServer) { | ||
await kibanaServer.stop(); | ||
} | ||
if (esServer) { | ||
await esServer.stop(); | ||
} | ||
}); | ||
|
||
beforeEach(async () => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterEach(async () => { | ||
while (taskIdsToRemove.length > 0) { | ||
const id = taskIdsToRemove.pop(); | ||
await taskManagerPlugin.removeIfExists(id!); | ||
} | ||
}); | ||
|
||
test('should be no workload aggregator errors when there are removed task types', async () => { | ||
const errorLogSpy = jest.spyOn(createWorkloadAggregatorOpts.logger, 'error'); | ||
const removeTypeId = uuidV4(); | ||
await injectTask(kibanaServer.coreStart.elasticsearch.client.asInternalUser, { | ||
id: removeTypeId, | ||
taskType: 'sampleTaskRemovedType', | ||
params: {}, | ||
state: { foo: 'test' }, | ||
stateVersion: 1, | ||
runAt: new Date(), | ||
enabled: true, | ||
scheduledAt: new Date(), | ||
attempts: 0, | ||
status: TaskStatus.Idle, | ||
startedAt: null, | ||
retryAt: null, | ||
ownerId: null, | ||
}); | ||
const notRegisteredTypeId = uuidV4(); | ||
await injectTask(kibanaServer.coreStart.elasticsearch.client.asInternalUser, { | ||
id: notRegisteredTypeId, | ||
taskType: 'sampleTaskNotRegisteredType', | ||
params: {}, | ||
state: { foo: 'test' }, | ||
stateVersion: 1, | ||
runAt: new Date(), | ||
enabled: true, | ||
scheduledAt: new Date(), | ||
attempts: 0, | ||
status: TaskStatus.Idle, | ||
startedAt: null, | ||
retryAt: null, | ||
ownerId: null, | ||
}); | ||
|
||
taskIdsToRemove.push(removeTypeId); | ||
taskIdsToRemove.push(notRegisteredTypeId); | ||
|
||
await retry(async () => { | ||
const task = await getTask(kibanaServer.coreStart.elasticsearch.client.asInternalUser); | ||
expect(task?._source?.task?.status).toBe('unrecognized'); | ||
}); | ||
|
||
// monitored_aggregated_stats_refresh_rate is set to the minimum of 5 seconds | ||
// so we want to wait that long to let it refresh | ||
await new Promise((r) => setTimeout(r, 5100)); | ||
|
||
expect(errorLogSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
async function getTask(esClient: ElasticsearchClient) { | ||
const response = await esClient.search<{ task: ConcreteTaskInstance }>({ | ||
index: '.kibana_task_manager', | ||
body: { | ||
query: { | ||
bool: { | ||
filter: [ | ||
{ | ||
term: { | ||
'task.taskType': 'sampleTaskRemovedType', | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
return response.hits.hits[0]; | ||
} |
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
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
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
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