From 36860ab9272ce8d18d42a4b2f31a4b932c6b7e4d Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Tue, 13 Aug 2024 11:13:23 -0400 Subject: [PATCH] [Fleet] Fix duplicates in agent version list (#190411) --- .../server/services/agents/versions.test.ts | 36 +++++++++++++++++++ .../fleet/server/services/agents/versions.ts | 10 +++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/agents/versions.test.ts b/x-pack/plugins/fleet/server/services/agents/versions.test.ts index 1277099f65f9a..58e6dadd7f9e0 100644 --- a/x-pack/plugins/fleet/server/services/agents/versions.test.ts +++ b/x-pack/plugins/fleet/server/services/agents/versions.test.ts @@ -221,6 +221,42 @@ describe('getAvailableVersions', () => { expect(res).toEqual(['8.10.0', '8.9.2', '8.1.0', '8.0.0', '7.17.0']); }); + it('should not include duplicate', async () => { + mockKibanaVersion = '300.0.0'; + mockedReadFile.mockResolvedValue(`["8.1.0", "8.0.0", "7.17.0", "7.16.0"]`); + mockedFetch.mockResolvedValueOnce({ + status: 200, + text: jest.fn().mockResolvedValue( + JSON.stringify([ + [ + { + title: 'Elastic Agent 8.1.0', + version_number: '8.1.0', + }, + { + title: 'Elastic Agent 8.10.0', + version_number: '8.10.0', + }, + { + title: 'Elastic Agent 8.10.0', + version_number: '8.10.0+build202407291657', + }, + { + title: 'Elastic Agent 8.9.2', + version_number: '8.9.2', + }, + , + ], + ]) + ), + } as any); + + const res = await getAvailableVersions({ ignoreCache: true }); + + // Should sort, uniquify and filter out versions < 7.17 + expect(res).toEqual(['8.10.0', '8.9.2', '8.1.0', '8.0.0', '7.17.0']); + }); + it('should cache results', async () => { mockKibanaVersion = '9.0.0'; mockedReadFile.mockResolvedValue(`["8.1.0", "8.0.0", "7.17.0", "7.16.0"]`); diff --git a/x-pack/plugins/fleet/server/services/agents/versions.ts b/x-pack/plugins/fleet/server/services/agents/versions.ts index fad0028c80691..f374275bf941b 100644 --- a/x-pack/plugins/fleet/server/services/agents/versions.ts +++ b/x-pack/plugins/fleet/server/services/agents/versions.ts @@ -111,10 +111,12 @@ export const getAvailableVersions = async ({ // Coerce each version to a semver object and compare to our `MINIMUM_SUPPORTED_VERSION` - we // only want support versions in the final result. We'll also sort by newest version first. - availableVersions = uniq([...availableVersions, ...apiVersions]) - .map((item: any) => semverCoerce(item)?.version || '') - .filter((v: any) => semverGte(v, MINIMUM_SUPPORTED_VERSION)) - .sort((a: any, b: any) => (semverGt(a, b) ? -1 : 1)); + availableVersions = uniq( + [...availableVersions, ...apiVersions] + .map((item: any) => semverCoerce(item)?.version || '') + .filter((v: any) => semverGte(v, MINIMUM_SUPPORTED_VERSION)) + .sort((a: any, b: any) => (semverGt(a, b) ? -1 : 1)) + ); // if api versions are empty (air gapped or API not available), we add current kibana version, as the build file might not contain the latest released version if (