Skip to content

Commit

Permalink
use agent version less then kibana version if the matching version is…
Browse files Browse the repository at this point in the history
… not released
  • Loading branch information
maxcold committed Sep 21, 2023
1 parent 39ce984 commit 7e73abd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
36 changes: 36 additions & 0 deletions x-pack/plugins/fleet/public/hooks/use_agent_version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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';

Expand Down
13 changes: 10 additions & 3 deletions x-pack/plugins/fleet/public/hooks/use_agent_version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -22,17 +23,23 @@ export const useAgentVersion = (): string | undefined => {
try {
const res = await sendGetAgentsAvailableVersions();
const availableVersions = res?.data?.items;
let agentVersionToUse = kibanaVersion;

if (
availableVersions &&
availableVersions.length > 0 &&
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);
}
Expand Down

0 comments on commit 7e73abd

Please sign in to comment.