Skip to content

Commit

Permalink
[Uptime] Include synthetics-* for existing alerts (#160063)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 authored Jul 6, 2023
1 parent 97dd41f commit a2cc9a6
Show file tree
Hide file tree
Showing 18 changed files with 367 additions and 299 deletions.
6 changes: 3 additions & 3 deletions x-pack/plugins/synthetics/server/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SYNTHETICS_RULE_TYPES } from '../common/constants/synthetics_alerts';
import { privateLocationsSavedObjectName } from '../common/saved_objects/private_locations';
import { PLUGIN } from '../common/constants/plugin';
import { UPTIME_RULE_TYPES } from '../common/constants/uptime_alerts';
import { umDynamicSettings } from './saved_objects/uptime_settings';
import { settingsObjectType } from './saved_objects/uptime_settings';
import { syntheticsApiKeyObjectType } from './saved_objects/service_api_key';

export const uptimeFeature = {
Expand All @@ -32,7 +32,7 @@ export const uptimeFeature = {
api: ['uptime-read', 'uptime-write', 'lists-all', 'rac'],
savedObject: {
all: [
umDynamicSettings.name,
settingsObjectType,
syntheticsMonitorType,
syntheticsApiKeyObjectType,
privateLocationsSavedObjectName,
Expand Down Expand Up @@ -61,7 +61,7 @@ export const uptimeFeature = {
all: [],
read: [
syntheticsParamType,
umDynamicSettings.name,
settingsObjectType,
syntheticsMonitorType,
syntheticsApiKeyObjectType,
privateLocationsSavedObjectName,
Expand Down
140 changes: 140 additions & 0 deletions x-pack/plugins/synthetics/server/lib.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { UptimeEsClient } from './lib';
import { savedObjectsClientMock, uiSettingsServiceMock } from '@kbn/core/server/mocks';
import { elasticsearchClientMock } from '@kbn/core-elasticsearch-client-server-mocks';

describe('UptimeEsClient', () => {
let uptimeEsClient: UptimeEsClient;
const savedObjectsClient = savedObjectsClientMock.create();
const esClient = elasticsearchClientMock.createClusterClient().asInternalUser;

beforeEach(() => {
uptimeEsClient = new UptimeEsClient(savedObjectsClient, esClient);
});

afterEach(() => {
jest.clearAllMocks();
});

describe('search', () => {
it('should call baseESClient.search with correct parameters', async () => {
const mockSearchParams = {
body: {
query: {
match_all: {},
},
},
};

const result = await uptimeEsClient.search({
body: {
query: {
match_all: {},
},
},
});

expect(esClient.search).toHaveBeenCalledWith(
{
index: 'heartbeat-8*,heartbeat-7*',
...mockSearchParams,
},
{ meta: true }
);
expect(result).toEqual({
body: {},
headers: {
'x-elastic-product': 'Elasticsearch',
},
meta: {},
statusCode: 200,
warnings: [],
});
});

it('should throw an error if baseESClient.search throws an error', async () => {
const mockSearchParams = {
body: {
query: {
match_all: {},
},
},
};
const mockError = new Error('Search error');
esClient.search.mockRejectedValueOnce(mockError);

await expect(uptimeEsClient.search(mockSearchParams)).rejects.toThrow(mockError);
expect(esClient.search).toHaveBeenCalledWith(
{
index: 'heartbeat-8*,heartbeat-7*',
...mockSearchParams,
},
{ meta: true }
);
});
});

describe('count', () => {
it('should call baseESClient.count with correct parameters', async () => {
const mockCountParams = {
index: 'example',
};

const result = await uptimeEsClient.count(mockCountParams);

expect(esClient.count).toHaveBeenCalledWith(mockCountParams, { meta: true });
expect(result).toEqual({
indices: 'heartbeat-8*,heartbeat-7*',
result: {
body: {},
headers: {
'x-elastic-product': 'Elasticsearch',
},
meta: {},
statusCode: 200,
warnings: [],
},
});
});

it('should throw an error if baseESClient.count throws an error', async () => {
const mockCountParams = {
index: 'example',
};
const mockError = new Error('Count error');
esClient.count.mockRejectedValueOnce(mockError);

await expect(uptimeEsClient.count(mockCountParams)).rejects.toThrow(mockError);
expect(esClient.count).toHaveBeenCalledWith(mockCountParams, { meta: true });
});
});

describe('getInspectEnabled', () => {
it('should return false if uiSettings is not available', async () => {
const result = await uptimeEsClient.getInspectEnabled();

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

it('should return the value from uiSettings if available', async () => {
const mockUiSettings = uiSettingsServiceMock.createClient();
uptimeEsClient.uiSettings = {
client: mockUiSettings,
} as any;

// @ts-expect-error
mockUiSettings.get.mockReturnValue(true);

await uptimeEsClient.getInspectEnabled();

expect(uptimeEsClient.isInspectorEnabled).toBe(true);
expect(mockUiSettings.get).toHaveBeenCalledWith('observability:enableInspectEsQueries');
});
});
});
165 changes: 0 additions & 165 deletions x-pack/plugins/synthetics/server/saved_objects/migrations.test.ts

This file was deleted.

63 changes: 0 additions & 63 deletions x-pack/plugins/synthetics/server/saved_objects/migrations.ts

This file was deleted.

Loading

0 comments on commit a2cc9a6

Please sign in to comment.