Skip to content

Commit

Permalink
[Fleet] Improve output validation performance (elastic#192107)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Sep 4, 2024
1 parent 0f30af9 commit a392bb2
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 23 deletions.
102 changes: 102 additions & 0 deletions x-pack/plugins/fleet/server/services/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,18 @@ describe('Output Service', () => {
],
} as unknown as ReturnType<typeof mockedAgentPolicyService.list>;

const mockedPackagePolicyWithFleetServerResolvedValue = {
items: [
{
name: 'fleet-server-123',
policy_ids: ['fleet_server_policy'],
package: {
name: 'fleet_server',
},
},
],
} as unknown as ReturnType<typeof mockedPackagePolicyService.list>;

const mockedAgentPolicyWithSyntheticsResolvedValue = {
items: [
{
Expand Down Expand Up @@ -291,9 +303,22 @@ describe('Output Service', () => {
],
} as unknown as ReturnType<typeof mockedAgentPolicyService.list>;

const mockedPackagePolicyWithSyntheticsResolvedValue = {
items: [
{
name: 'synthetics-123',
policy_ids: ['synthetics_policy'],
package: {
name: 'synthetics',
},
},
],
} as unknown as ReturnType<typeof mockedPackagePolicyService.list>;

beforeEach(() => {
mockedAgentPolicyService.getByIDs.mockResolvedValue([]);
mockedAgentPolicyService.list.mockClear();
mockedPackagePolicyService.list.mockReset();
mockedAgentPolicyService.hasAPMIntegration.mockClear();
mockedAgentPolicyService.hasFleetServerIntegration.mockClear();
mockedAgentPolicyService.hasSyntheticsIntegration.mockClear();
Expand All @@ -303,6 +328,9 @@ describe('Output Service', () => {
mockedAppContextService.getEncryptedSavedObjectsSetup.mockReset();
mockedAuditLoggingService.writeCustomSoAuditLog.mockReset();
mockedAgentPolicyService.update.mockReset();
mockedPackagePolicyService.list.mockResolvedValue({
items: [],
} as any);
});

afterEach(() => {
Expand Down Expand Up @@ -657,6 +685,12 @@ describe('Output Service', () => {
mockedAgentPolicyWithFleetServerResolvedValue
);
mockedAgentPolicyService.hasFleetServerIntegration.mockReturnValue(true);
mockedPackagePolicyService.list.mockResolvedValue(
mockedPackagePolicyWithFleetServerResolvedValue
);
mockedAgentPolicyService.getByIDs.mockResolvedValue(
(await mockedAgentPolicyWithFleetServerResolvedValue).items
);

await outputService.create(
soClient,
Expand Down Expand Up @@ -690,6 +724,12 @@ describe('Output Service', () => {
mockedAgentPolicyWithSyntheticsResolvedValue
);
mockedAgentPolicyService.hasSyntheticsIntegration.mockReturnValue(true);
mockedPackagePolicyService.list.mockResolvedValue(
mockedPackagePolicyWithSyntheticsResolvedValue
);
mockedAgentPolicyService.getByIDs.mockResolvedValue(
(await mockedAgentPolicyWithSyntheticsResolvedValue).items
);

await outputService.create(
soClient,
Expand Down Expand Up @@ -802,6 +842,12 @@ describe('Output Service', () => {
mockedAgentPolicyWithFleetServerResolvedValue
);
mockedAgentPolicyService.hasFleetServerIntegration.mockReturnValue(true);
mockedPackagePolicyService.list.mockResolvedValue(
mockedPackagePolicyWithFleetServerResolvedValue
);
mockedAgentPolicyService.getByIDs.mockResolvedValue(
(await mockedAgentPolicyWithFleetServerResolvedValue).items
);

await outputService.create(
soClient,
Expand Down Expand Up @@ -835,6 +881,12 @@ describe('Output Service', () => {
mockedAgentPolicyWithSyntheticsResolvedValue
);
mockedAgentPolicyService.hasSyntheticsIntegration.mockReturnValue(true);
mockedPackagePolicyService.list.mockResolvedValue(
mockedPackagePolicyWithSyntheticsResolvedValue
);
mockedAgentPolicyService.getByIDs.mockResolvedValue(
(await mockedAgentPolicyWithSyntheticsResolvedValue).items
);

await outputService.create(
soClient,
Expand Down Expand Up @@ -911,6 +963,7 @@ describe('Output Service', () => {
const soClient = getMockedSoClient({
defaultOutputId: 'existing-default-output',
});
mockedPackagePolicyService.list.mockResolvedValue({ items: [] } as any);

await outputService.update(soClient, esClientMock, 'output-test', {
is_default: true,
Expand All @@ -931,6 +984,7 @@ describe('Output Service', () => {
const soClient = getMockedSoClient({
defaultOutputId: 'existing-default-output',
});
mockedPackagePolicyService.list.mockResolvedValue({ items: [] } as any);

await outputService.update(soClient, esClientMock, 'existing-default-output', {
is_default: true,
Expand Down Expand Up @@ -1036,6 +1090,7 @@ describe('Output Service', () => {

it('Allow to update preconfigured output allowed to edit field from preconfiguration', async () => {
const soClient = getMockedSoClient();
mockedPackagePolicyService.list.mockResolvedValue({ items: [] } as any);
await outputService.update(
soClient,
esClientMock,
Expand All @@ -1055,6 +1110,7 @@ describe('Output Service', () => {
const soClient = getMockedSoClient({
defaultOutputId: 'existing-preconfigured-default-output',
});
mockedPackagePolicyService.list.mockResolvedValue({ items: [] } as any);

await expect(
outputService.update(soClient, esClientMock, 'output-test', {
Expand All @@ -1072,6 +1128,7 @@ describe('Output Service', () => {
const soClient = getMockedSoClient({
defaultOutputId: 'existing-default-output',
});
mockedPackagePolicyService.list.mockResolvedValue({ items: [] } as any);

await outputService.update(
soClient,
Expand Down Expand Up @@ -1101,6 +1158,7 @@ describe('Output Service', () => {
items: [{}],
} as unknown as ReturnType<typeof mockedAgentPolicyService.list>);
mockedAgentPolicyService.hasAPMIntegration.mockReturnValue(false);
mockedPackagePolicyService.list.mockResolvedValue({ items: [] } as any);

await outputService.update(soClient, esClientMock, 'existing-logstash-output', {
type: 'elasticsearch',
Expand All @@ -1121,6 +1179,7 @@ describe('Output Service', () => {
items: [{}],
} as unknown as ReturnType<typeof mockedAgentPolicyService.list>);
mockedAgentPolicyService.hasAPMIntegration.mockReturnValue(false);
mockedPackagePolicyService.list.mockResolvedValue({ items: [] } as any);

await outputService.update(soClient, esClientMock, 'existing-kafka-output', {
type: 'elasticsearch',
Expand Down Expand Up @@ -1162,6 +1221,7 @@ describe('Output Service', () => {
items: [{}],
} as unknown as ReturnType<typeof mockedAgentPolicyService.list>);
mockedAgentPolicyService.hasAPMIntegration.mockReturnValue(false);
mockedPackagePolicyService.list.mockResolvedValue({ items: [] } as any);

await outputService.update(soClient, esClientMock, 'existing-logstash-output', {
is_default: true,
Expand All @@ -1176,6 +1236,7 @@ describe('Output Service', () => {
items: [{}],
} as unknown as ReturnType<typeof mockedAgentPolicyService.list>);
mockedAgentPolicyService.hasAPMIntegration.mockReturnValue(false);
mockedPackagePolicyService.list.mockResolvedValue({ items: [] } as any);

await outputService.update(soClient, esClientMock, 'existing-logstash-output', {
is_default: true,
Expand All @@ -1201,6 +1262,7 @@ describe('Output Service', () => {
items: [{}],
} as unknown as ReturnType<typeof mockedAgentPolicyService.list>);
mockedAgentPolicyService.hasAPMIntegration.mockReturnValue(true);
mockedPackagePolicyService.list.mockResolvedValue({ items: [] } as any);

await expect(
outputService.update(soClient, esClientMock, 'existing-logstash-output', {
Expand All @@ -1214,6 +1276,7 @@ describe('Output Service', () => {
mockedAgentPolicyService.list.mockResolvedValue({
items: [{}],
} as unknown as ReturnType<typeof mockedAgentPolicyService.list>);
mockedPackagePolicyService.list.mockResolvedValue({ items: [] } as any);
mockedAgentPolicyService.hasAPMIntegration.mockReturnValue(false);
mockedAgentPolicyService.hasFleetServerIntegration.mockReturnValue(false);
mockedAgentPolicyService.hasSyntheticsIntegration.mockReturnValue(false);
Expand All @@ -1238,6 +1301,7 @@ describe('Output Service', () => {
} as unknown as ReturnType<typeof mockedAgentPolicyService.list>);
mockedAgentPolicyService.hasAPMIntegration.mockReturnValue(false);
mockedAgentPolicyService.hasFleetServerIntegration.mockReturnValue(false);
mockedPackagePolicyService.list.mockResolvedValue({ items: [] } as any);

await outputService.update(soClient, esClientMock, 'existing-kafka-output', {
type: 'logstash',
Expand Down Expand Up @@ -1280,6 +1344,9 @@ describe('Output Service', () => {
mockedAgentPolicyService.list.mockResolvedValue(
mockedAgentPolicyWithFleetServerResolvedValue
);
mockedPackagePolicyService.list.mockResolvedValue(
mockedPackagePolicyWithFleetServerResolvedValue
);
mockedAgentPolicyService.hasFleetServerIntegration.mockReturnValue(true);

await outputService.update(soClient, esClientMock, 'output-test', {
Expand Down Expand Up @@ -1312,6 +1379,9 @@ describe('Output Service', () => {
mockedAgentPolicyWithFleetServerResolvedValue
);
mockedAgentPolicyService.hasFleetServerIntegration.mockReturnValue(true);
mockedPackagePolicyService.list.mockResolvedValue(
mockedPackagePolicyWithFleetServerResolvedValue
);

await outputService.update(
soClient,
Expand Down Expand Up @@ -1349,6 +1419,9 @@ describe('Output Service', () => {
});
mockedAgentPolicyService.list.mockResolvedValue(mockedAgentPolicyWithSyntheticsResolvedValue);
mockedAgentPolicyService.hasSyntheticsIntegration.mockReturnValue(true);
mockedPackagePolicyService.list.mockResolvedValue(
mockedPackagePolicyWithSyntheticsResolvedValue
);

await outputService.update(soClient, esClientMock, 'output-test', {
type: 'logstash',
Expand Down Expand Up @@ -1378,6 +1451,9 @@ describe('Output Service', () => {
});
mockedAgentPolicyService.list.mockResolvedValue(mockedAgentPolicyWithSyntheticsResolvedValue);
mockedAgentPolicyService.hasSyntheticsIntegration.mockReturnValue(true);
mockedPackagePolicyService.list.mockResolvedValue(
mockedPackagePolicyWithSyntheticsResolvedValue
);

await outputService.update(
soClient,
Expand Down Expand Up @@ -1415,6 +1491,9 @@ describe('Output Service', () => {
mockedAgentPolicyWithFleetServerResolvedValue
);
mockedAgentPolicyService.hasFleetServerIntegration.mockReturnValue(true);
mockedPackagePolicyService.list.mockResolvedValue(
mockedPackagePolicyWithFleetServerResolvedValue
);

await expect(
outputService.update(soClient, esClientMock, 'existing-es-output', {
Expand All @@ -1431,6 +1510,9 @@ describe('Output Service', () => {
mockedAgentPolicyService.list.mockResolvedValue(mockedAgentPolicyWithSyntheticsResolvedValue);
mockedAgentPolicyService.hasFleetServerIntegration.mockReturnValue(false);
mockedAgentPolicyService.hasSyntheticsIntegration.mockReturnValue(true);
mockedPackagePolicyService.list.mockResolvedValue(
mockedPackagePolicyWithSyntheticsResolvedValue
);

await expect(
outputService.update(soClient, esClientMock, 'existing-es-output', {
Expand Down Expand Up @@ -1465,6 +1547,9 @@ describe('Output Service', () => {
mockedAgentPolicyService.hasAPMIntegration.mockReturnValue(false);
mockedAgentPolicyService.hasFleetServerIntegration.mockReturnValue(false);
mockedAgentPolicyService.hasSyntheticsIntegration.mockReturnValue(false);
mockedAgentPolicyService.list.mockResolvedValue({
items: [],
} as any);

await outputService.update(soClient, esClientMock, 'existing-es-output', {
type: 'kafka',
Expand Down Expand Up @@ -1498,6 +1583,9 @@ describe('Output Service', () => {
} as unknown as ReturnType<typeof mockedAgentPolicyService.list>);
mockedAgentPolicyService.hasAPMIntegration.mockReturnValue(false);
mockedAgentPolicyService.hasFleetServerIntegration.mockReturnValue(false);
mockedAgentPolicyService.list.mockResolvedValue({
items: [],
} as any);

await outputService.update(soClient, esClientMock, 'existing-logstash-output', {
type: 'kafka',
Expand Down Expand Up @@ -1532,6 +1620,9 @@ describe('Output Service', () => {
mockedAgentPolicyWithFleetServerResolvedValue
);
mockedAgentPolicyService.hasFleetServerIntegration.mockReturnValue(true);
mockedPackagePolicyService.list.mockResolvedValue(
mockedPackagePolicyWithFleetServerResolvedValue
);

await outputService.update(soClient, esClientMock, 'output-test', {
type: 'kafka',
Expand Down Expand Up @@ -1575,6 +1666,9 @@ describe('Output Service', () => {
mockedAgentPolicyWithFleetServerResolvedValue
);
mockedAgentPolicyService.hasFleetServerIntegration.mockReturnValue(true);
mockedPackagePolicyService.list.mockResolvedValue(
mockedPackagePolicyWithFleetServerResolvedValue
);

await outputService.update(
soClient,
Expand Down Expand Up @@ -1624,6 +1718,9 @@ describe('Output Service', () => {
});
mockedAgentPolicyService.list.mockResolvedValue(mockedAgentPolicyWithSyntheticsResolvedValue);
mockedAgentPolicyService.hasSyntheticsIntegration.mockReturnValue(true);
mockedPackagePolicyService.list.mockResolvedValue(
mockedPackagePolicyWithSyntheticsResolvedValue
);

await outputService.update(soClient, esClientMock, 'output-test', {
type: 'kafka',
Expand Down Expand Up @@ -1665,6 +1762,9 @@ describe('Output Service', () => {
});
mockedAgentPolicyService.list.mockResolvedValue(mockedAgentPolicyWithSyntheticsResolvedValue);
mockedAgentPolicyService.hasSyntheticsIntegration.mockReturnValue(true);
mockedPackagePolicyService.list.mockResolvedValue(
mockedPackagePolicyWithSyntheticsResolvedValue
);

await outputService.update(
soClient,
Expand Down Expand Up @@ -1975,6 +2075,7 @@ describe('Output Service', () => {

describe('backfillAllOutputPresets', () => {
it('should update non-preconfigured output', async () => {
mockedPackagePolicyService.list.mockResolvedValue({ items: [] } as any);
const soClient = getMockedSoClient({});

soClient.find.mockResolvedValue({
Expand Down Expand Up @@ -2005,6 +2106,7 @@ describe('Output Service', () => {
});

it('should update preconfigured output', async () => {
mockedPackagePolicyService.list.mockResolvedValue({ items: [] } as any);
const soClient = getMockedSoClient({});

soClient.find.mockResolvedValue({
Expand Down
Loading

0 comments on commit a392bb2

Please sign in to comment.