forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cloud Security] use only available agent versions for Cloudformation…
… and Cloud Shell parameters (elastic#166198) ## Summary fixes - elastic/security-team#7557 instead of using Kibana version for Cloudformation and Cloud Shell params in CNVM and CSPM integrations, check if the version of an agent that matches the current Kibana version actually available as an artifact. Relevant for serverless, where Kibana version points to not-yet released versions of Elastic Agent. ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
- Loading branch information
Showing
2 changed files
with
146 additions
and
6 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
x-pack/plugins/fleet/public/hooks/use_agent_version.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,125 @@ | ||
/* | ||
* 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 { renderHook } from '@testing-library/react-hooks'; | ||
|
||
import { useAgentVersion } from './use_agent_version'; | ||
import { useKibanaVersion } from './use_kibana_version'; | ||
import { sendGetAgentsAvailableVersions } from './use_request'; | ||
|
||
jest.mock('./use_kibana_version'); | ||
jest.mock('./use_request'); | ||
|
||
describe('useAgentVersion', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return agent version that matches Kibana version if released', async () => { | ||
const mockKibanaVersion = '8.8.1'; | ||
const mockAvailableVersions = ['8.9.0', '8.8.1', '8.8.0', '8.7.0']; | ||
|
||
(useKibanaVersion as jest.Mock).mockReturnValue(mockKibanaVersion); | ||
(sendGetAgentsAvailableVersions as jest.Mock).mockResolvedValue({ | ||
data: { items: mockAvailableVersions }, | ||
}); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => useAgentVersion()); | ||
|
||
expect(sendGetAgentsAvailableVersions).toHaveBeenCalled(); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual(mockKibanaVersion); | ||
}); | ||
|
||
it('should return the latest availeble agent version if a version that matches Kibana version is not released', async () => { | ||
const mockKibanaVersion = '8.11.0'; | ||
const mockAvailableVersions = ['8.8.0', '8.7.0', '8.9.2', '7.16.0']; | ||
|
||
(useKibanaVersion as jest.Mock).mockReturnValue(mockKibanaVersion); | ||
(sendGetAgentsAvailableVersions as jest.Mock).mockResolvedValue({ | ||
data: { items: mockAvailableVersions }, | ||
}); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => useAgentVersion()); | ||
|
||
expect(sendGetAgentsAvailableVersions).toHaveBeenCalled(); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual('8.9.2'); | ||
}); | ||
|
||
it('should return the agent version that is <= Kibana version if an agent version that matches Kibana version is not released', async () => { | ||
const mockKibanaVersion = '8.8.3'; | ||
const mockAvailableVersions = ['8.8.0', '8.8.1', '8.8.2', '8.7.0', '8.9.2', '7.16.0']; | ||
|
||
(useKibanaVersion as jest.Mock).mockReturnValue(mockKibanaVersion); | ||
(sendGetAgentsAvailableVersions as jest.Mock).mockResolvedValue({ | ||
data: { items: mockAvailableVersions }, | ||
}); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => useAgentVersion()); | ||
|
||
expect(sendGetAgentsAvailableVersions).toHaveBeenCalled(); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual('8.8.2'); | ||
}); | ||
|
||
it('should return the latest availeble agent version if a snapshot version', async () => { | ||
const mockKibanaVersion = '8.10.0-SNAPSHOT'; | ||
const mockAvailableVersions = ['8.8.0', '8.7.0', '8.9.2', '7.16.0']; | ||
|
||
(useKibanaVersion as jest.Mock).mockReturnValue(mockKibanaVersion); | ||
(sendGetAgentsAvailableVersions as jest.Mock).mockResolvedValue({ | ||
data: { items: mockAvailableVersions }, | ||
}); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => useAgentVersion()); | ||
|
||
expect(sendGetAgentsAvailableVersions).toHaveBeenCalled(); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual('8.9.2'); | ||
}); | ||
|
||
it('should return kibana version if no agent versions available', async () => { | ||
const mockKibanaVersion = '8.11.0'; | ||
const mockAvailableVersions: string[] = []; | ||
|
||
(useKibanaVersion as jest.Mock).mockReturnValue(mockKibanaVersion); | ||
(sendGetAgentsAvailableVersions as jest.Mock).mockResolvedValue({ | ||
data: { items: mockAvailableVersions }, | ||
}); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => useAgentVersion()); | ||
|
||
expect(sendGetAgentsAvailableVersions).toHaveBeenCalled(); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual('8.11.0'); | ||
}); | ||
|
||
it('should return kibana version if the list of available agent versions is not available', async () => { | ||
const mockKibanaVersion = '8.11.0'; | ||
|
||
(useKibanaVersion as jest.Mock).mockReturnValue(mockKibanaVersion); | ||
(sendGetAgentsAvailableVersions as jest.Mock).mockRejectedValue(new Error('Fetching error')); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => useAgentVersion()); | ||
|
||
expect(sendGetAgentsAvailableVersions).toHaveBeenCalled(); | ||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual(mockKibanaVersion); | ||
}); | ||
}); |
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