From 7e73abd5848cc391b12541e6948c06f1f538a16f Mon Sep 17 00:00:00 2001 From: Maxim Kholod Date: Thu, 21 Sep 2023 14:41:45 +0200 Subject: [PATCH] use agent version less then kibana version if the matching version is not released --- .../public/hooks/use_agent_version.test.ts | 36 +++++++++++++++++++ .../fleet/public/hooks/use_agent_version.ts | 13 +++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/fleet/public/hooks/use_agent_version.test.ts b/x-pack/plugins/fleet/public/hooks/use_agent_version.test.ts index 75a419f77fba9..6cb1c8ee42248 100644 --- a/x-pack/plugins/fleet/public/hooks/use_agent_version.test.ts +++ b/x-pack/plugins/fleet/public/hooks/use_agent_version.test.ts @@ -55,6 +55,24 @@ describe('useAgentVersion', () => { 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']; @@ -73,6 +91,24 @@ describe('useAgentVersion', () => { 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'; diff --git a/x-pack/plugins/fleet/public/hooks/use_agent_version.ts b/x-pack/plugins/fleet/public/hooks/use_agent_version.ts index 9874d7e506b2c..7e9c732f8a8df 100644 --- a/x-pack/plugins/fleet/public/hooks/use_agent_version.ts +++ b/x-pack/plugins/fleet/public/hooks/use_agent_version.ts @@ -6,12 +6,13 @@ */ import { useEffect, useState } from 'react'; import semverRcompare from 'semver/functions/rcompare'; +import semverLt from 'semver/functions/lt'; import { useKibanaVersion } from './use_kibana_version'; import { sendGetAgentsAvailableVersions } from './use_request'; /** - * @returns The most recent agent version available to install or upgrade to. + * @returns The most compatible agent version available to install or upgrade to. */ export const useAgentVersion = (): string | undefined => { const kibanaVersion = useKibanaVersion(); @@ -22,6 +23,7 @@ export const useAgentVersion = (): string | undefined => { try { const res = await sendGetAgentsAvailableVersions(); const availableVersions = res?.data?.items; + let agentVersionToUse = kibanaVersion; if ( availableVersions && @@ -29,10 +31,15 @@ export const useAgentVersion = (): string | undefined => { availableVersions.indexOf(kibanaVersion) === -1 ) { availableVersions.sort(semverRcompare); - setAgentVersion(availableVersions[0]); + agentVersionToUse = + availableVersions.find((version) => { + return semverLt(version, kibanaVersion); + }) || availableVersions[0]; } else { - setAgentVersion(kibanaVersion); + agentVersionToUse = kibanaVersion; } + + setAgentVersion(agentVersionToUse); } catch (err) { setAgentVersion(kibanaVersion); }