Skip to content

Commit

Permalink
[Fleet] Improve tests around upgrade (#137190)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Jul 26, 2022
1 parent 9748143 commit 0523cd2
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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.
*/

export { UpgradeStatusCallout } from './upgrade';
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* 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.
*/

/*
* 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, { useState } from 'react';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import {
EuiCallOut,
EuiLink,
EuiFlyout,
EuiCodeBlock,
EuiPortal,
EuiFlyoutBody,
EuiFlyoutHeader,
EuiTitle,
} from '@elastic/eui';
import styled from 'styled-components';

import type { UpgradePackagePolicyDryRunResponse } from '../../../../../../../common/types/rest_spec';

const FlyoutBody = styled(EuiFlyoutBody)`
.euiFlyoutBody__overflowContent {
padding: 0;
}
`;

export const UpgradeStatusCallout: React.FunctionComponent<{
dryRunData: UpgradePackagePolicyDryRunResponse;
}> = ({ dryRunData }) => {
const [isPreviousVersionFlyoutOpen, setIsPreviousVersionFlyoutOpen] = useState<boolean>(false);

if (!dryRunData) {
return null;
}

const isReadyForUpgrade = !dryRunData[0].hasErrors;

const [currentPackagePolicy, proposedUpgradePackagePolicy] = dryRunData[0].diff || [];

return (
<>
{isPreviousVersionFlyoutOpen && currentPackagePolicy && (
<EuiPortal>
<EuiFlyout onClose={() => setIsPreviousVersionFlyoutOpen(false)} size="l" maxWidth={640}>
<EuiFlyoutHeader hasBorder>
<EuiTitle size="m">
<h2 id="FleetPackagePolicyPreviousVersionFlyoutTitle">
<FormattedMessage
id="xpack.fleet.upgradePackagePolicy.previousVersionFlyout.title"
defaultMessage="'{name}' integration policy"
values={{ name: currentPackagePolicy?.name }}
/>
</h2>
</EuiTitle>
</EuiFlyoutHeader>
<FlyoutBody>
<EuiCodeBlock isCopyable fontSize="m" whiteSpace="pre">
{JSON.stringify(dryRunData[0].agent_diff?.[0] || [], null, 2)}
</EuiCodeBlock>
</FlyoutBody>
</EuiFlyout>
</EuiPortal>
)}

{isReadyForUpgrade && currentPackagePolicy ? (
<EuiCallOut
title={i18n.translate('xpack.fleet.upgradePackagePolicy.statusCallOut.successTitle', {
defaultMessage: 'Ready to upgrade',
})}
color="success"
iconType="checkInCircleFilled"
>
<FormattedMessage
id="xpack.fleet.upgradePackagePolicy.statusCallout.successContent"
defaultMessage="This integration is ready to be upgraded from version {currentVersion} to {upgradeVersion}. Review the changes below and save to upgrade."
values={{
currentVersion: currentPackagePolicy?.package?.version,
upgradeVersion: proposedUpgradePackagePolicy?.package?.version,
}}
/>
</EuiCallOut>
) : (
<EuiCallOut
title={i18n.translate('xpack.fleet.upgradePackagePolicy.statusCallOut.errorTitle', {
defaultMessage: 'Review field conflicts',
})}
color="warning"
iconType="alert"
>
<FormattedMessage
id="xpack.fleet.upgradePackagePolicy.statusCallout.errorContent"
defaultMessage="This integration has conflicting fields from version {currentVersion} to {upgradeVersion} Review the configuration and save to perform the upgrade. You may reference your {previousConfigurationLink} for comparison."
values={{
currentVersion: currentPackagePolicy?.package?.version,
upgradeVersion: proposedUpgradePackagePolicy?.package?.version,
previousConfigurationLink: (
<EuiLink onClick={() => setIsPreviousVersionFlyoutOpen(true)}>
<FormattedMessage
id="xpack.fleet.upgradePackagePolicy.statusCallout.previousConfigurationLink"
defaultMessage="previous configuration"
/>
</EuiLink>
),
}}
/>
</EuiCallOut>
)}
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,11 @@ import deepEqual from 'fast-deep-equal';
import {
EuiButtonEmpty,
EuiBottomBar,
EuiCallOut,
EuiFlexGroup,
EuiFlexItem,
EuiSpacer,
EuiLink,
EuiFlyout,
EuiCodeBlock,
EuiPortal,
EuiFlyoutBody,
EuiFlyoutHeader,
EuiTitle,
EuiErrorBoundary,
} from '@elastic/eui';
import styled from 'styled-components';

import type { AgentPolicy, PackageInfo, UpdatePackagePolicy, PackagePolicy } from '../../../types';
import {
Expand Down Expand Up @@ -71,6 +62,7 @@ import { pkgKeyFromPackageInfo } from '../../../services';

import { fixApmDurationVars, hasUpgradeAvailable } from './utils';
import { useHistoryBlock } from './hooks';
import { UpgradeStatusCallout } from './components';

export const EditPackagePolicyPage = memo(() => {
const {
Expand Down Expand Up @@ -760,94 +752,3 @@ const UpgradeBreadcrumb: React.FunctionComponent<{
useBreadcrumbs('upgrade_package_policy', { policyName, policyId });
return null;
};

const FlyoutBody = styled(EuiFlyoutBody)`
.euiFlyoutBody__overflowContent {
padding: 0;
}
`;

const UpgradeStatusCallout: React.FunctionComponent<{
dryRunData: UpgradePackagePolicyDryRunResponse;
}> = ({ dryRunData }) => {
const [isPreviousVersionFlyoutOpen, setIsPreviousVersionFlyoutOpen] = useState<boolean>(false);

if (!dryRunData) {
return null;
}

const isReadyForUpgrade = !dryRunData[0].hasErrors;

const [currentPackagePolicy, proposedUpgradePackagePolicy] = dryRunData[0].diff || [];

return (
<>
{isPreviousVersionFlyoutOpen && currentPackagePolicy && (
<EuiPortal>
<EuiFlyout onClose={() => setIsPreviousVersionFlyoutOpen(false)} size="l" maxWidth={640}>
<EuiFlyoutHeader hasBorder>
<EuiTitle size="m">
<h2 id="FleetPackagePolicyPreviousVersionFlyoutTitle">
<FormattedMessage
id="xpack.fleet.upgradePackagePolicy.previousVersionFlyout.title"
defaultMessage="'{name}' integration policy"
values={{ name: currentPackagePolicy?.name }}
/>
</h2>
</EuiTitle>
</EuiFlyoutHeader>
<FlyoutBody>
<EuiCodeBlock isCopyable fontSize="m" whiteSpace="pre">
{JSON.stringify(dryRunData[0].agent_diff?.[0] || [], null, 2)}
</EuiCodeBlock>
</FlyoutBody>
</EuiFlyout>
</EuiPortal>
)}

{isReadyForUpgrade && currentPackagePolicy ? (
<EuiCallOut
title={i18n.translate('xpack.fleet.upgradePackagePolicy.statusCallOut.successTitle', {
defaultMessage: 'Ready to upgrade',
})}
color="success"
iconType="checkInCircleFilled"
>
<FormattedMessage
id="xpack.fleet.upgradePackagePolicy.statusCallout.successContent"
defaultMessage="This integration is ready to be upgraded from version {currentVersion} to {upgradeVersion}. Review the changes below and save to upgrade."
values={{
currentVersion: currentPackagePolicy?.package?.version,
upgradeVersion: proposedUpgradePackagePolicy?.package?.version,
}}
/>
</EuiCallOut>
) : (
<EuiCallOut
title={i18n.translate('xpack.fleet.upgradePackagePolicy.statusCallOut.errorTitle', {
defaultMessage: 'Review field conflicts',
})}
color="warning"
iconType="alert"
>
<FormattedMessage
id="xpack.fleet.upgradePackagePolicy.statusCallout.errorContent"
defaultMessage="This integration has conflicting fields from version {currentVersion} to {upgradeVersion} Review the configuration and save to perform the upgrade. You may reference your {previousConfigurationLink} for comparison."
values={{
currentVersion: currentPackagePolicy?.package?.version,
upgradeVersion: proposedUpgradePackagePolicy?.package?.version,
previousConfigurationLink: (
<EuiLink onClick={() => setIsPreviousVersionFlyoutOpen(true)}>
<FormattedMessage
id="xpack.fleet.upgradePackagePolicy.statusCallout.previousConfigurationLink"
defaultMessage="previous configuration"
/>
</EuiLink>
),
}}
/>
</EuiCallOut>
)}
</>
);
};
100 changes: 100 additions & 0 deletions x-pack/plugins/fleet/server/services/package_policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,36 @@ async function mockedGetPackageInfo(params: any) {
if (params.pkgName === 'apache') pkg = { version: '1.3.2' };
if (params.pkgName === 'aws') pkg = { version: '0.3.3' };
if (params.pkgName === 'endpoint') pkg = {};
if (params.pkgName === 'test') {
pkg = {
version: '1.0.2',
};
}
if (params.pkgName === 'test-conflict') {
pkg = {
version: '1.0.2',
policy_templates: [
{
name: 'test-conflict',
inputs: [
{
title: 'test',
type: 'logs',
description: 'test',
vars: [
{
name: 'test-var-required',
required: true,
type: 'integer',
},
],
},
],
},
],
};
}

return Promise.resolve(pkg);
}

Expand Down Expand Up @@ -1111,6 +1141,10 @@ describe('Package policy service', () => {
});
});

describe('delete', () => {
it('should allow to delete a package policy', async () => {});
});

describe('runDeleteExternalCallbacks', () => {
let callbackOne: jest.MockedFunction<PostPackagePolicyDeleteCallback>;
let callbackTwo: jest.MockedFunction<PostPackagePolicyDeleteCallback>;
Expand Down Expand Up @@ -3384,6 +3418,72 @@ describe('Package policy service', () => {
});
});

describe('getUpgradeDryRunDiff', () => {
let savedObjectsClient: jest.Mocked<SavedObjectsClientContract>;
beforeEach(() => {
savedObjectsClient = savedObjectsClientMock.create();
appContextService.start(createAppContextStartContractMock());
});
afterEach(() => {
appContextService.stop();
});
it('should return no errors if there is no conflict to upgrade', async () => {
const res = await packagePolicyService.getUpgradeDryRunDiff(
savedObjectsClient,
'package-policy-id',
{
id: '123',
name: 'test-123',
package: {
title: 'test',
name: 'test',
version: '1.0.1',
},
namespace: 'default',
inputs: [
{
id: 'toto',
enabled: true,
streams: [],
type: 'logs',
},
],
} as any,
'1.0.2'
);

expect(res.hasErrors).toBeFalsy();
});

it('should no errors if there is a conflict to upgrade', async () => {
const res = await packagePolicyService.getUpgradeDryRunDiff(
savedObjectsClient,
'package-policy-id',
{
id: '123',
name: 'test-123',
package: {
title: 'test',
name: 'test-conflict',
version: '1.0.1',
},
namespace: 'default',
inputs: [
{
id: 'toto',
enabled: true,
streams: [],
type: 'logs',
},
],
} as any,
'1.0.2'
);

expect(res.hasErrors).toBeTruthy();
});
});

describe('_applyIndexPrivileges()', () => {
function createPackageStream(indexPrivileges?: string[]): RegistryDataStream {
const stream: RegistryDataStream = {
Expand Down

0 comments on commit 0523cd2

Please sign in to comment.