Skip to content

Commit

Permalink
[Fleet] Fix maintenance options for rolling upgrades (#133369) (#133387)
Browse files Browse the repository at this point in the history
(cherry picked from commit eaca3c7)

Co-authored-by: Nicolas Chaulet <[email protected]>
  • Loading branch information
kibanamachine and nchaulet authored Jun 2, 2022
1 parent 25476b5 commit 1a61c3d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ export const FALLBACK_VERSIONS = [
'7.17.0',
];

export const MAINTAINANCE_VALUES = [1, 2, 4, 8, 12, 24, 48];
export const MAINTAINANCE_VALUES = [0, 1, 2, 4, 8, 12, 24, 48];
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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 React from 'react';

import { createFleetTestRendererMock } from '../../../../../../mock';

import { AgentUpgradeAgentModal } from '.';
import type { AgentUpgradeAgentModalProps } from '.';

jest.mock('../../../../../../hooks/use_fleet_status', () => ({
FleetStatusProvider: (props: any) => {
return props.children;
},
useFleetStatus: jest.fn().mockReturnValue({}),
}));

jest.mock('@elastic/eui', () => {
return {
...jest.requireActual('@elastic/eui'),
EuiConfirmModal: ({ children }: any) => <>{children}</>,
};
});

function renderAgentUpgradeAgentModal(props: Partial<AgentUpgradeAgentModalProps>) {
const renderer = createFleetTestRendererMock();

const utils = renderer.render(
<AgentUpgradeAgentModal agents="" agentCount={12} onClose={() => {}} {...props} />
);

return { utils };
}
describe('AgentUpgradeAgentModal', () => {
it('should set the default to Immediately if there is less than 10 agents using kuery', async () => {
const { utils } = renderAgentUpgradeAgentModal({
agents: '*',
agentCount: 3,
});

const el = utils.container.querySelector(
'[data-test-subj="agentUpgradeModal.MaintainanceCombobox"]'
);
expect(el).not.toBeNull();
expect(el?.textContent).toBe('Immediately');
});

it('should set the default to Immediately if there is less than 10 agents using selected agents', async () => {
const { utils } = renderAgentUpgradeAgentModal({
agents: [{ id: 'agent1' }, { id: 'agent2' }] as any,
agentCount: 3,
});

const el = utils.container.querySelector(
'[data-test-subj="agentUpgradeModal.MaintainanceCombobox"]'
);
expect(el).not.toBeNull();
expect(el).not.toBeNull();
expect(el?.textContent).toBe('Immediately');
});

it('should set the default to 1 hour if there is more than 10 agents', async () => {
const { utils } = renderAgentUpgradeAgentModal({
agents: '*',
agentCount: 13,
});

const el = utils.container.querySelector(
'[data-test-subj="agentUpgradeModal.MaintainanceCombobox"]'
);

expect(el).not.toBeNull();
expect(el?.textContent).toBe('1 hour');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {

import { FALLBACK_VERSIONS, MAINTAINANCE_VALUES } from './constants';

interface Props {
export interface AgentUpgradeAgentModalProps {
onClose: () => void;
agents: Agent[] | string;
agentCount: number;
Expand All @@ -47,7 +47,7 @@ interface Props {

const getVersion = (version: Array<EuiComboBoxOptionOption<string>>) => version[0]?.value as string;

export const AgentUpgradeAgentModal: React.FunctionComponent<Props> = ({
export const AgentUpgradeAgentModal: React.FunctionComponent<AgentUpgradeAgentModalProps> = ({
onClose,
agents,
agentCount,
Expand All @@ -59,17 +59,23 @@ export const AgentUpgradeAgentModal: React.FunctionComponent<Props> = ({
const [errors, setErrors] = useState<string | undefined>();

const isSingleAgent = Array.isArray(agents) && agents.length === 1;
const isSmallBatch = Array.isArray(agents) && agents.length > 1 && agents.length <= 10;
const isSmallBatch = agentCount <= 10;
const isAllAgents = agents === '';

const fallbackVersions = [kibanaVersion].concat(FALLBACK_VERSIONS);
const fallbackVersions = useMemo(
() => [kibanaVersion].concat(FALLBACK_VERSIONS),
[kibanaVersion]
);

const minVersion = useMemo(() => {
if (!Array.isArray(agents)) {
return getMinVersion(fallbackVersions);
}
const versions = (agents as Agent[]).map(
(agent) => agent.local_metadata?.elastic?.agent?.version
);
return getMinVersion(versions);
}, [agents]);
}, [agents, fallbackVersions]);

const versionOptions: Array<EuiComboBoxOptionOption<string>> = useMemo(() => {
const displayVersions = minVersion
Expand All @@ -81,9 +87,7 @@ export const AgentUpgradeAgentModal: React.FunctionComponent<Props> = ({
}));
}, [fallbackVersions, minVersion]);

const maintainanceWindows =
isSmallBatch && !isScheduled ? [0].concat(MAINTAINANCE_VALUES) : MAINTAINANCE_VALUES;
const maintainanceOptions: Array<EuiComboBoxOptionOption<number>> = maintainanceWindows.map(
const maintainanceOptions: Array<EuiComboBoxOptionOption<number>> = MAINTAINANCE_VALUES.map(
(option) => ({
label:
option === 0
Expand All @@ -99,7 +103,7 @@ export const AgentUpgradeAgentModal: React.FunctionComponent<Props> = ({
);
const [selectedVersion, setSelectedVersion] = useState([versionOptions[0]]);
const [selectedMantainanceWindow, setSelectedMantainanceWindow] = useState([
maintainanceOptions[0],
isSmallBatch ? maintainanceOptions[0] : maintainanceOptions[1],
]);

const initialDatetime = useMemo(() => moment(), []);
Expand Down

0 comments on commit 1a61c3d

Please sign in to comment.