-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow upgrade agent from actions column (#6476)
* Add upgrade action * Add upgrade errors alert * Update CHANGELOG * Add unit tests * Fix filter * Fix table filter suggestions * Fix search bar table suggestions * Improve upgrade progress * Improve API requests and modal refresh * Improve imposter response for task status * Improve imposter response for task status --------- Co-authored-by: Federico Rodriguez <federico.rodriguez@wazuh.com>
- Loading branch information
1 parent
d79db98
commit 4a9fe77
Showing
39 changed files
with
1,437 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"data": { | ||
"affected_items": [], | ||
"total_affected_items": 0, | ||
"total_failed_items": 0, | ||
"failed_items": [] | ||
}, | ||
"message": "All specified task's status were returned", | ||
"error": 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
var storeWazuh = stores.open('storeWazuh'); | ||
var attemptRestart = storeWazuh.load('attempt'); | ||
|
||
var taskStatus = context.request.queryParams.status; | ||
|
||
if (!taskStatus) { | ||
respond().withStatusCode(200).withFile('tasks/status_in_progress_2.json'); | ||
} | ||
|
||
if (attemptRestart < 5) { | ||
storeWazuh.save('attempt', attemptRestart + 1); | ||
|
||
if (taskStatus === 'In progress') { | ||
respond().withStatusCode(200).withFile('tasks/status_in_progress_2.json'); | ||
} | ||
|
||
if (taskStatus === 'Done' || taskStatus === 'Failed') { | ||
respond().withStatusCode(200).withFile('tasks/empty.json'); | ||
} | ||
} else if (attemptRestart < 10) { | ||
storeWazuh.save('attempt', attemptRestart + 1); | ||
|
||
if (taskStatus === 'In progress') { | ||
respond().withStatusCode(200).withFile('tasks/status_in_progress_1.json'); | ||
} | ||
|
||
if (taskStatus === 'Done') { | ||
respond().withStatusCode(200).withFile('tasks/status_done.json'); | ||
} | ||
|
||
if (taskStatus === 'Failed') { | ||
respond().withStatusCode(200).withFile('tasks/empty.json'); | ||
} | ||
} else { | ||
if (taskStatus === 'In progress') { | ||
respond().withStatusCode(200).withFile('tasks/empty.json'); | ||
} | ||
|
||
if (taskStatus === 'Done') { | ||
respond().withStatusCode(200).withFile('tasks/status_done.json'); | ||
} | ||
|
||
if (taskStatus === 'Failed') { | ||
storeWazuh.save('attempt', 0); | ||
respond().withStatusCode(200).withFile('tasks/status_failed.json'); | ||
} | ||
} |
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,22 @@ | ||
{ | ||
"data": { | ||
"affected_items": [ | ||
{ | ||
"message": "Success", | ||
"agent": "001", | ||
"task_id": 1, | ||
"node": "worker2", | ||
"module": "upgrade_module", | ||
"command": "upgrade", | ||
"status": "Done", | ||
"create_time": "2024-03-11T11:55:33.000Z", | ||
"last_update_time": "2020-03-11T12:05:10.000Z" | ||
} | ||
], | ||
"total_affected_items": 1, | ||
"total_failed_items": 0, | ||
"failed_items": [] | ||
}, | ||
"message": "All specified task's status were returned", | ||
"error": 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"data": { | ||
"affected_items": [ | ||
{ | ||
"message": "Success", | ||
"agent": "002", | ||
"task_id": 2, | ||
"node": "worker2", | ||
"module": "upgrade_module", | ||
"command": "upgrade", | ||
"status": "Failed", | ||
"create_time": "2024-03-11T11:57:44.000Z", | ||
"last_update_time": "2020-03-11T12:11:32.000Z" | ||
} | ||
], | ||
"total_affected_items": 1, | ||
"total_failed_items": 0, | ||
"failed_items": [] | ||
}, | ||
"message": "All specified task's status were returned", | ||
"error": 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"data": { | ||
"affected_items": [ | ||
{ | ||
"message": "Success", | ||
"agent": "002", | ||
"task_id": 2, | ||
"node": "worker2", | ||
"module": "upgrade_module", | ||
"command": "upgrade", | ||
"status": "In progress", | ||
"create_time": "2024-03-11T11:57:44.000Z", | ||
"last_update_time": "2020-03-11T11:57:46.000Z" | ||
} | ||
], | ||
"total_affected_items": 1, | ||
"total_failed_items": 0, | ||
"failed_items": [] | ||
}, | ||
"message": "All specified task's status were returned", | ||
"error": 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"data": { | ||
"affected_items": [ | ||
{ | ||
"message": "Success", | ||
"agent": "001", | ||
"task_id": 1, | ||
"node": "worker2", | ||
"module": "upgrade_module", | ||
"command": "upgrade", | ||
"status": "In progress", | ||
"create_time": "2024-03-11T11:55:33.000Z", | ||
"last_update_time": "2020-03-11T11:55:36.000Z" | ||
}, | ||
{ | ||
"message": "Success", | ||
"agent": "002", | ||
"task_id": 2, | ||
"node": "worker2", | ||
"module": "upgrade_module", | ||
"command": "upgrade", | ||
"status": "In progress", | ||
"create_time": "2024-03-11T11:57:44.000Z", | ||
"last_update_time": "2020-03-11T11:57:46.000Z" | ||
} | ||
], | ||
"total_affected_items": 2, | ||
"total_failed_items": 0, | ||
"failed_items": [] | ||
}, | ||
"message": "All specified task's status were returned", | ||
"error": 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
2 changes: 1 addition & 1 deletion
2
plugins/main/public/components/endpoints-summary/hooks/agents.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
1 change: 1 addition & 0 deletions
1
plugins/main/public/components/endpoints-summary/hooks/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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { useGetTotalAgents } from './agents'; | ||
export { useGetGroups } from './groups'; | ||
export { useGetUpgradeTasks } from './upgrade-tasks'; |
91 changes: 91 additions & 0 deletions
91
plugins/main/public/components/endpoints-summary/hooks/upgrade-tasks.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,91 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { getTasks } from '../services'; | ||
import { useGetUpgradeTasks } from './upgrade-tasks'; | ||
import { API_NAME_TASK_STATUS } from '../../../../common/constants'; | ||
|
||
jest.mock('../services', () => ({ | ||
getTasks: jest.fn(), | ||
})); | ||
|
||
jest.useFakeTimers(); | ||
jest.spyOn(global, 'clearInterval'); | ||
|
||
describe('useGetUpgradeTasks hook', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should fetch initial data without any error', async () => { | ||
const mockGetTasks = jest.requireMock('../services').getTasks; | ||
mockGetTasks.mockImplementation(async ({ status }) => { | ||
if (status === API_NAME_TASK_STATUS.IN_PROGRESS) { | ||
return { total_affected_items: 5 }; | ||
} | ||
if (status === API_NAME_TASK_STATUS.DONE) { | ||
return { total_affected_items: 3 }; | ||
} | ||
return { total_affected_items: 2 }; | ||
}); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => | ||
useGetUpgradeTasks(false), | ||
); | ||
|
||
expect(result.current.getInProgressIsLoading).toBe(true); | ||
expect(result.current.totalInProgressTasks).toBe(0); | ||
expect(result.current.getInProgressError).toBeUndefined(); | ||
|
||
expect(result.current.getSuccessIsLoading).toBe(true); | ||
expect(result.current.totalSuccessTasks).toBe(0); | ||
expect(result.current.getSuccessError).toBeUndefined(); | ||
|
||
expect(result.current.getErrorIsLoading).toBe(true); | ||
expect(result.current.totalErrorUpgradeTasks).toBe(0); | ||
expect(result.current.getErrorTasksError).toBeUndefined(); | ||
|
||
await waitForNextUpdate(); | ||
jest.advanceTimersByTime(500); | ||
|
||
expect(result.current.getInProgressIsLoading).toBe(false); | ||
expect(result.current.totalInProgressTasks).toBe(5); | ||
expect(result.current.getInProgressError).toBeUndefined(); | ||
|
||
jest.advanceTimersByTime(500); | ||
|
||
expect(result.current.getSuccessIsLoading).toBe(false); | ||
expect(result.current.totalSuccessTasks).toBe(3); | ||
expect(result.current.getSuccessError).toBeUndefined(); | ||
|
||
jest.advanceTimersByTime(500); | ||
|
||
expect(result.current.getErrorIsLoading).toBe(false); | ||
expect(result.current.totalErrorUpgradeTasks).toBe(2); | ||
expect(result.current.getErrorTasksError).toBeUndefined(); | ||
}); | ||
|
||
it('should clear interval when totalInProgressTasks is 0', async () => { | ||
const mockGetTasks = jest.requireMock('../services').getTasks; | ||
mockGetTasks.mockResolvedValue({ total_affected_items: 0 }); | ||
|
||
const { waitForNextUpdate } = renderHook(() => useGetUpgradeTasks(false)); | ||
|
||
await waitForNextUpdate(); | ||
jest.advanceTimersByTime(500); | ||
|
||
expect(clearInterval).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should handle error while fetching data', async () => { | ||
const mockErrorMessage = 'Some error occurred'; | ||
(getTasks as jest.Mock).mockRejectedValue(mockErrorMessage); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => | ||
useGetUpgradeTasks(0), | ||
); | ||
|
||
expect(result.current.getInProgressIsLoading).toBeTruthy(); | ||
await waitForNextUpdate(); | ||
expect(result.current.getInProgressError).toBe(mockErrorMessage); | ||
expect(result.current.getInProgressIsLoading).toBeFalsy(); | ||
}); | ||
}); |
Oops, something went wrong.