Skip to content

Commit

Permalink
[Fleet] Fix duplicates in agent version list (elastic#190411)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored and bryce-b committed Aug 13, 2024
1 parent b95e8ee commit 36860ab
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
36 changes: 36 additions & 0 deletions x-pack/plugins/fleet/server/services/agents/versions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]`);
Expand Down
10 changes: 6 additions & 4 deletions x-pack/plugins/fleet/server/services/agents/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 36860ab

Please sign in to comment.