Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fleet] refactor auto upgrade package policies logic #125909

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
244 changes: 102 additions & 142 deletions x-pack/plugins/fleet/server/services/managed_package_policies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

import { elasticsearchServiceMock, savedObjectsClientMock } from 'src/core/server/mocks';

import type { Installation } from '../../common';

import { shouldUpgradePolicies, upgradeManagedPackagePolicies } from './managed_package_policies';
import { upgradeManagedPackagePolicies } from './managed_package_policies';
import { packagePolicyService } from './package_policy';
import { getInstallation } from './epm/packages';
import { getInstallations } from './epm/packages';

jest.mock('./package_policy');
jest.mock('./epm/packages');
Expand All @@ -28,20 +26,38 @@ jest.mock('./app_context', () => {

describe('upgradeManagedPackagePolicies', () => {
afterEach(() => {
(packagePolicyService.get as jest.Mock).mockReset();
(packagePolicyService.getUpgradeDryRunDiff as jest.Mock).mockReset();
(getInstallation as jest.Mock).mockReset();
(packagePolicyService.upgrade as jest.Mock).mockReset();
jest.clearAllMocks();
});

it('should not upgrade policies for non-managed package', async () => {
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
const soClient = savedObjectsClientMock.create();

(packagePolicyService.get as jest.Mock).mockImplementationOnce(
(savedObjectsClient: any, id: string) => {
return {
id,
(getInstallations as jest.Mock).mockResolvedValueOnce({
saved_objects: [
{
attributes: {
id: 'test-installation',
version: '0.0.1',
keep_policies_up_to_date: false,
},
},
],
});

await upgradeManagedPackagePolicies(soClient, esClient);

expect(packagePolicyService.upgrade).not.toBeCalled();
});

it('should upgrade policies for managed package', async () => {
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
const soClient = savedObjectsClientMock.create();

(packagePolicyService.list as jest.Mock).mockResolvedValueOnce({
items: [
{
id: 'managed-package-id',
inputs: {},
version: '',
revision: 1,
Expand All @@ -50,43 +66,48 @@ describe('upgradeManagedPackagePolicies', () => {
created_at: '',
created_by: '',
package: {
name: 'non-managed-package',
title: 'Non-Managed Package',
version: '1.0.0',
name: 'managed-package',
title: 'Managed Package',
version: '0.0.1',
},
};
}
);
},
],
});

(packagePolicyService.getUpgradeDryRunDiff as jest.Mock).mockImplementationOnce(
(savedObjectsClient: any, id: string) => {
return {
name: 'non-managed-package-policy',
diff: [{ id: 'foo' }, { id: 'bar' }],
hasErrors: false,
};
}
);
(packagePolicyService.getUpgradeDryRunDiff as jest.Mock).mockResolvedValueOnce({
name: 'non-managed-package-policy',
diff: [{ id: 'foo' }, { id: 'bar' }],
hasErrors: false,
});

(getInstallation as jest.Mock).mockResolvedValueOnce({
id: 'test-installation',
version: '0.0.1',
keep_policies_up_to_date: false,
(getInstallations as jest.Mock).mockResolvedValueOnce({
saved_objects: [
{
attributes: {
id: 'test-installation',
version: '1.0.0',
keep_policies_up_to_date: true,
},
},
],
});

await upgradeManagedPackagePolicies(soClient, esClient, ['non-managed-package-id']);
const results = await upgradeManagedPackagePolicies(soClient, esClient);
expect(results).toEqual([
{ packagePolicyId: 'managed-package-id', diff: [{ id: 'foo' }, { id: 'bar' }], errors: [] },
]);

expect(packagePolicyService.upgrade).not.toBeCalled();
expect(packagePolicyService.upgrade).toBeCalledWith(soClient, esClient, ['managed-package-id']);
});

it('should upgrade policies for managed package', async () => {
it('should not upgrade policy if newer than installed package version', async () => {
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
const soClient = savedObjectsClientMock.create();

(packagePolicyService.get as jest.Mock).mockImplementationOnce(
(savedObjectsClient: any, id: string) => {
return {
id,
(packagePolicyService.list as jest.Mock).mockResolvedValueOnce({
items: [
{
id: 'managed-package-id',
inputs: {},
version: '',
revision: 1,
Expand All @@ -97,42 +118,39 @@ describe('upgradeManagedPackagePolicies', () => {
package: {
name: 'managed-package',
title: 'Managed Package',
version: '0.0.1',
version: '1.0.1',
},
};
}
);

(packagePolicyService.getUpgradeDryRunDiff as jest.Mock).mockImplementationOnce(
(savedObjectsClient: any, id: string) => {
return {
name: 'non-managed-package-policy',
diff: [{ id: 'foo' }, { id: 'bar' }],
hasErrors: false,
};
}
);
},
],
});

(getInstallation as jest.Mock).mockResolvedValueOnce({
id: 'test-installation',
version: '1.0.0',
keep_policies_up_to_date: true,
(getInstallations as jest.Mock).mockResolvedValueOnce({
saved_objects: [
{
attributes: {
id: 'test-installation',
version: '1.0.0',
keep_policies_up_to_date: true,
},
},
],
});

await upgradeManagedPackagePolicies(soClient, esClient, ['managed-package-id']);
await upgradeManagedPackagePolicies(soClient, esClient);

expect(packagePolicyService.upgrade).toBeCalledWith(soClient, esClient, ['managed-package-id']);
expect(packagePolicyService.getUpgradeDryRunDiff).not.toHaveBeenCalled();
expect(packagePolicyService.upgrade).not.toHaveBeenCalled();
});

describe('when dry run reports conflicts', () => {
it('should return errors + diff without performing upgrade', async () => {
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
const soClient = savedObjectsClientMock.create();

(packagePolicyService.get as jest.Mock).mockImplementationOnce(
(savedObjectsClient: any, id: string) => {
return {
id,
(packagePolicyService.list as jest.Mock).mockResolvedValueOnce({
items: [
{
id: 'conflicting-package-policy',
inputs: {},
version: '',
revision: 1,
Expand All @@ -145,32 +163,32 @@ describe('upgradeManagedPackagePolicies', () => {
title: 'Conflicting Package',
version: '0.0.1',
},
};
}
);
},
],
});

(packagePolicyService.getUpgradeDryRunDiff as jest.Mock).mockImplementationOnce(
(savedObjectsClient: any, id: string) => {
return {
name: 'conflicting-package-policy',
diff: [
{ id: 'foo' },
{ id: 'bar', errors: [{ key: 'some.test.value', message: 'Conflict detected' }] },
],
hasErrors: true,
};
}
);
(packagePolicyService.getUpgradeDryRunDiff as jest.Mock).mockResolvedValueOnce({
name: 'conflicting-package-policy',
diff: [
{ id: 'foo' },
{ id: 'bar', errors: [{ key: 'some.test.value', message: 'Conflict detected' }] },
],
hasErrors: true,
});

(getInstallation as jest.Mock).mockResolvedValueOnce({
id: 'test-installation',
version: '1.0.0',
keep_policies_up_to_date: true,
(getInstallations as jest.Mock).mockResolvedValueOnce({
saved_objects: [
{
attributes: {
id: 'test-installation',
version: '1.0.0',
keep_policies_up_to_date: true,
},
},
],
});

const result = await upgradeManagedPackagePolicies(soClient, esClient, [
'conflicting-package-policy',
]);
const result = await upgradeManagedPackagePolicies(soClient, esClient);

expect(result).toEqual([
{
Expand Down Expand Up @@ -202,61 +220,3 @@ describe('upgradeManagedPackagePolicies', () => {
});
});
});

describe('shouldUpgradePolicies', () => {
describe('package policy is up-to-date', () => {
describe('keep_policies_up_to_date is true', () => {
it('returns false', () => {
const installedPackage = {
version: '1.0.0',
keep_policies_up_to_date: true,
};

const result = shouldUpgradePolicies('1.0.0', installedPackage as Installation);

expect(result).toBe(false);
});
});

describe('keep_policies_up_to_date is false', () => {
it('returns false', () => {
const installedPackage = {
version: '1.0.0',
keep_policies_up_to_date: false,
};

const result = shouldUpgradePolicies('1.0.0', installedPackage as Installation);

expect(result).toBe(false);
});
});
});

describe('package policy is out-of-date', () => {
describe('keep_policies_up_to_date is true', () => {
it('returns true', () => {
const installedPackage = {
version: '1.1.0',
keep_policies_up_to_date: true,
};

const result = shouldUpgradePolicies('1.0.0', installedPackage as Installation);

expect(result).toBe(true);
});
});

describe('keep_policies_up_to_date is false', () => {
it('returns false', () => {
const installedPackage = {
version: '1.1.0',
keep_policies_up_to_date: false,
};

const result = shouldUpgradePolicies('1.0.0', installedPackage as Installation);

expect(result).toBe(false);
});
});
});
});
Loading