Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ymao1 committed Nov 13, 2024
1 parent 40139e7 commit 74f0097
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ const getIndexTemplatePutBody = (opts?: GetIndexTemplatePutBodyOpts) => {
body: {
index_patterns: indexPatterns,
composed_of: [
...(useEcs ? ['.alerts-ecs-mappings'] : []),
...(useEcs ? ['ecs@mappings'] : []),
`.alerts-${context ? `${context}.alerts` : 'test.alerts'}-mappings`,
...(useLegacyAlerts ? ['.alerts-legacy-alert-mappings'] : []),
'.alerts-framework-mappings',
Expand Down Expand Up @@ -289,14 +289,12 @@ describe('Alerts Service', () => {
if (!useDataStreamForAlerts) {
expect(clusterClient.ilm.putLifecycle).toHaveBeenCalledWith(IlmPutBody);
}
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(2);

const componentTemplate1 = clusterClient.cluster.putComponentTemplate.mock.calls[0][0];
expect(componentTemplate1.name).toEqual('.alerts-framework-mappings');
const componentTemplate2 = clusterClient.cluster.putComponentTemplate.mock.calls[1][0];
expect(componentTemplate2.name).toEqual('.alerts-legacy-alert-mappings');
const componentTemplate3 = clusterClient.cluster.putComponentTemplate.mock.calls[2][0];
expect(componentTemplate3.name).toEqual('.alerts-ecs-mappings');
});

test('should not initialize common resources if ES is not ready', async () => {
Expand Down Expand Up @@ -329,14 +327,12 @@ describe('Alerts Service', () => {
if (!useDataStreamForAlerts) {
expect(clusterClient.ilm.putLifecycle).toHaveBeenCalledWith(IlmPutBody);
}
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(2);

const componentTemplate1 = clusterClient.cluster.putComponentTemplate.mock.calls[0][0];
expect(componentTemplate1.name).toEqual('.alerts-framework-mappings');
const componentTemplate2 = clusterClient.cluster.putComponentTemplate.mock.calls[1][0];
expect(componentTemplate2.name).toEqual('.alerts-legacy-alert-mappings');
const componentTemplate3 = clusterClient.cluster.putComponentTemplate.mock.calls[2][0];
expect(componentTemplate3.name).toEqual('.alerts-ecs-mappings');
});

test('should log error and set initialized to false if adding ILM policy throws error', async () => {
Expand Down Expand Up @@ -481,7 +477,7 @@ describe('Alerts Service', () => {
);
// 3x for framework, legacy-alert and ecs mappings, then 1 extra time to update component template
// after updating index template field limit
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
});
});

Expand Down Expand Up @@ -516,14 +512,12 @@ describe('Alerts Service', () => {
expect(clusterClient.ilm.putLifecycle).not.toHaveBeenCalled();
}

expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
const componentTemplate1 = clusterClient.cluster.putComponentTemplate.mock.calls[0][0];
expect(componentTemplate1.name).toEqual('.alerts-framework-mappings');
const componentTemplate2 = clusterClient.cluster.putComponentTemplate.mock.calls[1][0];
expect(componentTemplate2.name).toEqual('.alerts-legacy-alert-mappings');
const componentTemplate3 = clusterClient.cluster.putComponentTemplate.mock.calls[2][0];
expect(componentTemplate3.name).toEqual('.alerts-ecs-mappings');
const componentTemplate4 = clusterClient.cluster.putComponentTemplate.mock.calls[3][0];
const componentTemplate4 = clusterClient.cluster.putComponentTemplate.mock.calls[2][0];
expect(componentTemplate4.name).toEqual('.alerts-test.alerts-mappings');

expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalledWith(
Expand Down Expand Up @@ -576,15 +570,13 @@ describe('Alerts Service', () => {
expect(clusterClient.ilm.putLifecycle).not.toHaveBeenCalled();
}

expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
const componentTemplate1 = clusterClient.cluster.putComponentTemplate.mock.calls[0][0];
expect(componentTemplate1.name).toEqual('.alerts-framework-mappings');
const componentTemplate2 = clusterClient.cluster.putComponentTemplate.mock.calls[1][0];
expect(componentTemplate2.name).toEqual('.alerts-legacy-alert-mappings');
const componentTemplate3 = clusterClient.cluster.putComponentTemplate.mock.calls[2][0];
expect(componentTemplate3.name).toEqual('.alerts-ecs-mappings');
const componentTemplate4 = clusterClient.cluster.putComponentTemplate.mock.calls[3][0];
expect(componentTemplate4.name).toEqual('.alerts-test.alerts-mappings');
expect(componentTemplate3.name).toEqual('.alerts-test.alerts-mappings');

expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalledWith(
getIndexTemplatePutBody({
Expand Down Expand Up @@ -639,15 +631,13 @@ describe('Alerts Service', () => {
expect(clusterClient.ilm.putLifecycle).not.toHaveBeenCalled();
}

expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
const componentTemplate1 = clusterClient.cluster.putComponentTemplate.mock.calls[0][0];
expect(componentTemplate1.name).toEqual('.alerts-framework-mappings');
const componentTemplate2 = clusterClient.cluster.putComponentTemplate.mock.calls[1][0];
expect(componentTemplate2.name).toEqual('.alerts-legacy-alert-mappings');
const componentTemplate3 = clusterClient.cluster.putComponentTemplate.mock.calls[2][0];
expect(componentTemplate3.name).toEqual('.alerts-ecs-mappings');
const componentTemplate4 = clusterClient.cluster.putComponentTemplate.mock.calls[3][0];
expect(componentTemplate4.name).toEqual('.alerts-test.alerts-mappings');
expect(componentTemplate3.name).toEqual('.alerts-test.alerts-mappings');

expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalledWith(
getIndexTemplatePutBody({ useEcs: true, useDataStream: useDataStreamForAlerts })
Expand Down Expand Up @@ -698,7 +688,7 @@ describe('Alerts Service', () => {
expect(clusterClient.ilm.putLifecycle).not.toHaveBeenCalled();
}

expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.indices.putIndexTemplate).toHaveBeenNthCalledWith(
1,
getIndexTemplatePutBody({ useDataStream: useDataStreamForAlerts })
Expand Down Expand Up @@ -802,15 +792,13 @@ describe('Alerts Service', () => {

expect(clusterClient.ilm.putLifecycle).toHaveBeenCalledWith(IlmPutBody);

expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
const componentTemplate1 = clusterClient.cluster.putComponentTemplate.mock.calls[0][0];
expect(componentTemplate1.name).toEqual('.alerts-framework-mappings');
const componentTemplate2 = clusterClient.cluster.putComponentTemplate.mock.calls[1][0];
expect(componentTemplate2.name).toEqual('.alerts-legacy-alert-mappings');
const componentTemplate3 = clusterClient.cluster.putComponentTemplate.mock.calls[2][0];
expect(componentTemplate3.name).toEqual('.alerts-ecs-mappings');
const componentTemplate4 = clusterClient.cluster.putComponentTemplate.mock.calls[3][0];
expect(componentTemplate4.name).toEqual('.alerts-test.alerts-mappings');
expect(componentTemplate3.name).toEqual('.alerts-test.alerts-mappings');

expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalledWith(
getIndexTemplatePutBody({
Expand Down Expand Up @@ -853,13 +841,11 @@ describe('Alerts Service', () => {
expect(clusterClient.ilm.putLifecycle).not.toHaveBeenCalled();
}

expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(2);
const componentTemplate1 = clusterClient.cluster.putComponentTemplate.mock.calls[0][0];
expect(componentTemplate1.name).toEqual('.alerts-framework-mappings');
const componentTemplate2 = clusterClient.cluster.putComponentTemplate.mock.calls[1][0];
expect(componentTemplate2.name).toEqual('.alerts-legacy-alert-mappings');
const componentTemplate3 = clusterClient.cluster.putComponentTemplate.mock.calls[2][0];
expect(componentTemplate3.name).toEqual('.alerts-ecs-mappings');

const template = {
name: `.alerts-empty.alerts-default-index-template`,
Expand Down Expand Up @@ -1002,7 +988,7 @@ describe('Alerts Service', () => {
expect(clusterClient.ilm.putLifecycle).toHaveBeenCalledTimes(
useDataStreamForAlerts ? 0 : 1
);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.indices.simulateTemplate).toHaveBeenCalled();
// putIndexTemplate is skipped but other operations are called as expected
expect(clusterClient.indices.putIndexTemplate).not.toHaveBeenCalled();
Expand Down Expand Up @@ -1050,7 +1036,7 @@ describe('Alerts Service', () => {
expect(clusterClient.ilm.putLifecycle).toHaveBeenCalledTimes(
useDataStreamForAlerts ? 0 : 1
);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.indices.simulateTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putIndexTemplate).not.toHaveBeenCalled();
expect(clusterClient.indices.getAlias).not.toHaveBeenCalled();
Expand Down Expand Up @@ -1081,7 +1067,7 @@ describe('Alerts Service', () => {
expect(clusterClient.ilm.putLifecycle).toHaveBeenCalledTimes(
useDataStreamForAlerts ? 0 : 1
);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.indices.simulateTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalled();
expect(clusterClient.indices.getAlias).not.toHaveBeenCalled();
Expand Down Expand Up @@ -1113,7 +1099,7 @@ describe('Alerts Service', () => {
expect(clusterClient.ilm.putLifecycle).toHaveBeenCalledTimes(
useDataStreamForAlerts ? 0 : 1
);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.indices.simulateTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putSettings).not.toHaveBeenCalled();
Expand All @@ -1137,7 +1123,7 @@ describe('Alerts Service', () => {
expect(clusterClient.ilm.putLifecycle).toHaveBeenCalledTimes(
useDataStreamForAlerts ? 0 : 1
);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.indices.simulateTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putSettings).not.toHaveBeenCalled();
Expand Down Expand Up @@ -1173,7 +1159,7 @@ describe('Alerts Service', () => {
expect(clusterClient.ilm.putLifecycle).toHaveBeenCalledTimes(
useDataStreamForAlerts ? 0 : 1
);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.indices.simulateTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putSettings).toHaveBeenCalled();
Expand Down Expand Up @@ -1207,7 +1193,7 @@ describe('Alerts Service', () => {
expect(clusterClient.ilm.putLifecycle).toHaveBeenCalledTimes(
useDataStreamForAlerts ? 0 : 1
);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.indices.simulateTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putSettings).toHaveBeenCalled();
Expand Down Expand Up @@ -1248,7 +1234,7 @@ describe('Alerts Service', () => {
expect(clusterClient.ilm.putLifecycle).toHaveBeenCalledTimes(
useDataStreamForAlerts ? 0 : 1
);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.indices.simulateTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putSettings).toHaveBeenCalled();
Expand Down Expand Up @@ -1279,7 +1265,7 @@ describe('Alerts Service', () => {
expect(clusterClient.ilm.putLifecycle).toHaveBeenCalledTimes(
useDataStreamForAlerts ? 0 : 1
);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.indices.simulateTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putSettings).not.toHaveBeenCalled();
Expand Down Expand Up @@ -1328,7 +1314,7 @@ describe('Alerts Service', () => {
);

expect(clusterClient.ilm.putLifecycle).toHaveBeenCalled();
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.indices.simulateTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalled();
expect(clusterClient.indices.getAlias).toHaveBeenCalled();
Expand Down Expand Up @@ -1364,7 +1350,7 @@ describe('Alerts Service', () => {
);

expect(clusterClient.ilm.putLifecycle).toHaveBeenCalled();
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.indices.simulateTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putSettings).toHaveBeenCalled();
Expand Down Expand Up @@ -1400,7 +1386,7 @@ describe('Alerts Service', () => {
expect(logger.error).toHaveBeenCalledWith(`Error creating concrete write index - fail`);

expect(clusterClient.ilm.putLifecycle).toHaveBeenCalled();
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.indices.simulateTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putSettings).toHaveBeenCalled();
Expand Down Expand Up @@ -1444,7 +1430,7 @@ describe('Alerts Service', () => {
expect(logger.error).toHaveBeenCalledWith(`Error creating concrete write index - fail`);

expect(clusterClient.ilm.putLifecycle).toHaveBeenCalled();
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.indices.simulateTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalled();
expect(clusterClient.indices.getAlias).toHaveBeenCalled();
Expand Down Expand Up @@ -1490,7 +1476,7 @@ describe('Alerts Service', () => {
expect(logger.error).toHaveBeenCalledWith(`Error creating concrete write index - fail`);

expect(clusterClient.ilm.putLifecycle).toHaveBeenCalled();
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(3);
expect(clusterClient.indices.simulateTemplate).toHaveBeenCalled();
expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalled();
expect(clusterClient.indices.getAlias).toHaveBeenCalled();
Expand Down Expand Up @@ -2368,7 +2354,7 @@ describe('Alerts Service', () => {
'alert service initialized',
async () => alertsService.isInitialized() === true
);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(5);
expect(clusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
});

test('should retry updating index template for transient ES errors', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import { setAlertsToUntracked, SetAlertsToUntrackedParams } from './lib/set_aler

export const TOTAL_FIELDS_LIMIT = 2500;
const LEGACY_ALERT_CONTEXT = 'legacy-alert';
const ECS_COMPONENT_TEMPLATE_NAME = 'ecs@mappings';

interface AlertsServiceParams {
logger: Logger;
pluginStop$: Observable<void>;
Expand Down Expand Up @@ -403,7 +405,7 @@ export class AlertsService implements IAlertsService {

// If useEcs is set to true, add the ECS component template to the references
if (useEcs) {
componentTemplateRefs.push('ecs@mappings');
componentTemplateRefs.push(ECS_COMPONENT_TEMPLATE_NAME);
}

// If fieldMap is not empty, create a context specific component template and add to the references
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,9 @@ describe('resourceInstaller', () => {
expect(mockClusterClient.ilm.putLifecycle).toHaveBeenCalledTimes(
useDataStreamForAlerts ? 0 : 1
);
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(2);
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(1);
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenNthCalledWith(
1,
expect.objectContaining({ name: '' })
);
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ name: TECHNICAL_COMPONENT_TEMPLATE_NAME })
);
});
Expand Down

0 comments on commit 74f0097

Please sign in to comment.