Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Feb 17, 2022
1 parent cae61df commit f40c881
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 145 deletions.
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);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const upgradeManagedPackagePolicies = async (
if (isPolicyVersionGteInstalledVersion(packagePolicy, installedPackage)) {
continue;
}
upgradePackagePolicy(soClient, esClient, packagePolicy.id, results);
await upgradePackagePolicy(soClient, esClient, packagePolicy.id, results);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/fleet/server/services/preconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
PackagePolicy,
} from '../../common';
import { PRECONFIGURATION_LATEST_KEYWORD } from '../../common';
import { SO_SEARCH_LIMIT, normalizeHostsForAgents } from '../../common';
import { normalizeHostsForAgents } from '../../common';
import { PRECONFIGURATION_DELETION_RECORD_SAVED_OBJECT_TYPE } from '../constants';

import { escapeSearchQueryPhrase } from './saved_object';
Expand All @@ -32,7 +32,7 @@ import { ensurePackagesCompletedInstall } from './epm/packages/install';
import { bulkInstallPackages } from './epm/packages/bulk_install_packages';
import { agentPolicyService, addPackageToAgentPolicy } from './agent_policy';
import type { InputsOverride } from './package_policy';
import { preconfigurePackageInputs, packagePolicyService } from './package_policy';
import { preconfigurePackageInputs } from './package_policy';
import { appContextService } from './app_context';
import type { UpgradeManagedPackagePoliciesResult } from './managed_package_policies';
import { upgradeManagedPackagePolicies } from './managed_package_policies';
Expand Down

0 comments on commit f40c881

Please sign in to comment.