Skip to content

Commit

Permalink
Improve test coverage for upgrade policies check
Browse files Browse the repository at this point in the history
  • Loading branch information
kpollich committed Oct 15, 2021
1 parent 54a23e6 commit 09e9313
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 11 deletions.
157 changes: 153 additions & 4 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,9 +7,12 @@

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

import { upgradeManagedPackagePolicies } from './managed_package_policies';
import type { Installation, PackageInfo } from '../../common';
import { AUTO_UPDATE_PACKAGES } from '../../common';

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

jest.mock('./package_policy');
jest.mock('./epm/packages');
Expand All @@ -24,11 +27,12 @@ jest.mock('./app_context', () => {
};
});

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

Expand All @@ -50,7 +54,7 @@ describe('managed package policies', () => {
package: {
name: 'non-managed-package',
title: 'Non-Managed Package',
version: '0.0.1',
version: '1.0.0',
},
};
}
Expand All @@ -74,6 +78,11 @@ describe('managed package policies', () => {
})
);

(getInstallation as jest.Mock).mockResolvedValueOnce({
id: 'test-installation',
version: '0.0.1',
});

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

expect(packagePolicyService.upgrade).not.toBeCalled();
Expand Down Expand Up @@ -121,6 +130,11 @@ describe('managed package policies', () => {
})
);

(getInstallation as jest.Mock).mockResolvedValueOnce({
id: 'test-installation',
version: '1.0.0',
});

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

expect(packagePolicyService.upgrade).toBeCalledWith(soClient, esClient, ['managed-package-id']);
Expand Down Expand Up @@ -172,6 +186,11 @@ describe('managed package policies', () => {
})
);

(getInstallation as jest.Mock).mockResolvedValueOnce({
id: 'test-installation',
version: '1.0.0',
});

const result = await upgradeManagedPackagePolicies(soClient, esClient, [
'conflicting-package-policy',
]);
Expand Down Expand Up @@ -206,3 +225,133 @@ describe('managed package policies', () => {
});
});
});

describe('shouldUpgradePolicies', () => {
describe('package is marked as AUTO_UPDATE', () => {
describe('keep_policies_up_to_date is true', () => {
it('returns false', () => {
const packageInfo = {
version: '1.0.0',
keepPoliciesUpToDate: true,
name: AUTO_UPDATE_PACKAGES[0].name,
};

const installedPackage = {
version: '1.0.0',
};

const result = shouldUpgradePolicies(
packageInfo as PackageInfo,
installedPackage as Installation
);

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

describe('keep_policies_up_to_date is false', () => {
it('returns false', () => {
const packageInfo = {
version: '1.0.0',
keepPoliciesUpToDate: false,
name: AUTO_UPDATE_PACKAGES[0].name,
};

const installedPackage = {
version: '1.0.0',
};

const result = shouldUpgradePolicies(
packageInfo as PackageInfo,
installedPackage as Installation
);

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

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

const installedPackage = {
version: '1.0.0',
};

const result = shouldUpgradePolicies(
packageInfo as PackageInfo,
installedPackage as Installation
);

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

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

const installedPackage = {
version: '1.0.0',
};

const result = shouldUpgradePolicies(
packageInfo as PackageInfo,
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 packageInfo = {
version: '1.0.0',
keepPoliciesUpToDate: true,
};

const installedPackage = {
version: '1.1.0',
};

const result = shouldUpgradePolicies(
packageInfo as PackageInfo,
installedPackage as Installation
);

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

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

const installedPackage = {
version: '1.1.0',
};

const result = shouldUpgradePolicies(
packageInfo as PackageInfo,
installedPackage as Installation
);

expect(result).toBe(false);
});
});
});
});
34 changes: 27 additions & 7 deletions x-pack/plugins/fleet/server/services/managed_package_policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@
*/

import type { ElasticsearchClient, SavedObjectsClientContract } from 'src/core/server';
import semverGte from 'semver/functions/gte';

import type { UpgradePackagePolicyDryRunResponseItem } from '../../common';
import type {
Installation,
PackageInfo,
UpgradePackagePolicyDryRunResponseItem,
} from '../../common';

import { appContextService } from './app_context';
import { getInstallation, getPackageInfo } from './epm/packages';
import { packagePolicyService } from './package_policy';

export interface UpgradeManagedPackagePoliciesResult {
packagePolicyId: string;
diff: UpgradePackagePolicyDryRunResponseItem['diff'];
diff?: UpgradePackagePolicyDryRunResponseItem['diff'];
errors: any;
}

Expand Down Expand Up @@ -48,13 +53,16 @@ export const upgradeManagedPackagePolicies = async (
pkgName: packagePolicy.package.name,
});

const isPolicyVersionAlignedWithInstalledVersion =
packageInfo.version === installedPackage?.version;
if (!installedPackage) {
results.push({
packagePolicyId,
errors: [`${packagePolicy.package.name} is not installed`],
});

const shouldUpgradePolicies =
!isPolicyVersionAlignedWithInstalledVersion && packageInfo.keepPoliciesUpToDate;
continue;
}

if (shouldUpgradePolicies) {
if (shouldUpgradePolicies(packageInfo, installedPackage)) {
// Since upgrades don't report diffs/errors, we need to perform a dry run first in order
// to notify the user of any granular policy upgrade errors that occur during Fleet's
// preconfiguration check
Expand Down Expand Up @@ -88,3 +96,15 @@ export const upgradeManagedPackagePolicies = async (

return results;
};

export function shouldUpgradePolicies(
packageInfo: PackageInfo,
installedPackage: Installation
): boolean {
const isPolicyVersionGteInstalledVersion = semverGte(
packageInfo.version,
installedPackage.version
);

return !isPolicyVersionGteInstalledVersion && !!packageInfo.keepPoliciesUpToDate;
}

0 comments on commit 09e9313

Please sign in to comment.