Skip to content

Commit

Permalink
[Fleet] refactor auto upgrade package policies logic (elastic#125909)
Browse files Browse the repository at this point in the history
* refactor upgrade package policies

* fixed tests

* code cleanup

* review improvements

* added api test

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
2 people authored and lucasfcosta committed Mar 2, 2022
1 parent 01952af commit 158433c
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 246 deletions.
238 changes: 95 additions & 143 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 @@ -20,28 +18,38 @@ jest.mock('./app_context', () => {
...jest.requireActual('./app_context'),
appContextService: {
getLogger: jest.fn(() => {
return { error: jest.fn() };
return { error: jest.fn(), debug: jest.fn() };
}),
},
};
});

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: [],
});

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 +58,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 +110,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 +155,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 +212,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

0 comments on commit 158433c

Please sign in to comment.