Skip to content

Commit

Permalink
test: add unit tests for is_managed behaviour (elastic#107759) (elast…
Browse files Browse the repository at this point in the history
…ic#107787)

Co-authored-by: Mark Hopkin <mark.hopkin@elastic.co>
  • Loading branch information
kibanamachine and hop-dev committed Aug 5, 2021
1 parent 1387fae commit 3a67301
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions x-pack/plugins/fleet/server/services/preconfiguration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import type { AgentPolicy, NewPackagePolicy, Output } from '../types';

import { AGENT_POLICY_SAVED_OBJECT_TYPE } from '../constants';

import * as agentPolicy from './agent_policy';

import {
ensurePreconfiguredPackagesAndPolicies,
comparePreconfiguredPolicyToCurrent,
Expand Down Expand Up @@ -128,6 +130,7 @@ jest.mock('./epm/packages/get', () => ({
jest.mock('./package_policy', () => ({
...jest.requireActual('./package_policy'),
packagePolicyService: {
getByIDs: jest.fn().mockReturnValue([]),
create(soClient: any, esClient: any, newPackagePolicy: NewPackagePolicy) {
return {
id: 'mocked',
Expand All @@ -152,10 +155,13 @@ jest.mock('./app_context', () => ({
},
}));

const spyAgentPolicyServiceUpdate = jest.spyOn(agentPolicy.agentPolicyService, 'update');

describe('policy preconfiguration', () => {
beforeEach(() => {
mockInstalledPackages.clear();
mockConfiguredPolicies.clear();
spyAgentPolicyServiceUpdate.mockClear();
});

it('should perform a no-op when passed no policies or packages', async () => {
Expand Down Expand Up @@ -331,6 +337,84 @@ describe('policy preconfiguration', () => {
expect(policiesB[0].updated_at).toEqual(policiesA[0].updated_at);
expect(nonFatalErrorsB.length).toBe(0);
});

it('should update a managed policy if top level fields are changed', async () => {
const soClient = getPutPreconfiguredPackagesMock();
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;

mockConfiguredPolicies.set('test-id', {
name: 'Test policy',
description: 'Test policy description',
unenroll_timeout: 120,
namespace: 'default',
id: 'test-id',
package_policies: [],
is_managed: true,
} as PreconfiguredAgentPolicy);

const {
policies,
nonFatalErrors: nonFatalErrorsB,
} = await ensurePreconfiguredPackagesAndPolicies(
soClient,
esClient,
[
{
name: 'Renamed Test policy',
description: 'Renamed Test policy description',
unenroll_timeout: 999,
namespace: 'default',
id: 'test-id',
is_managed: true,
package_policies: [],
},
] as PreconfiguredAgentPolicy[],
[],
mockDefaultOutput
);
expect(spyAgentPolicyServiceUpdate).toBeCalled();
expect(spyAgentPolicyServiceUpdate).toBeCalledWith(
expect.anything(), // soClient
expect.anything(), // esClient
'test-id',
expect.objectContaining({
name: 'Renamed Test policy',
description: 'Renamed Test policy description',
unenroll_timeout: 999,
})
);
expect(policies.length).toEqual(1);
expect(policies[0].id).toBe('test-id');
expect(nonFatalErrorsB.length).toBe(0);
});

it('should not update a managed policy if a top level field has not changed', async () => {
const soClient = getPutPreconfiguredPackagesMock();
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
const policy: PreconfiguredAgentPolicy = {
name: 'Test policy',
namespace: 'default',
id: 'test-id',
package_policies: [],
is_managed: true,
};
mockConfiguredPolicies.set('test-id', policy);

const {
policies,
nonFatalErrors: nonFatalErrorsB,
} = await ensurePreconfiguredPackagesAndPolicies(
soClient,
esClient,
[policy],
[],
mockDefaultOutput
);
expect(spyAgentPolicyServiceUpdate).not.toBeCalled();
expect(policies.length).toEqual(1);
expect(policies[0].id).toBe('test-id');
expect(nonFatalErrorsB.length).toBe(0);
});
});

describe('comparePreconfiguredPolicyToCurrent', () => {
Expand Down

0 comments on commit 3a67301

Please sign in to comment.