Skip to content

Commit

Permalink
[Uptime] Fix bug causing all monitors to be saved to all locations [s…
Browse files Browse the repository at this point in the history
…olves elastic#132314] (elastic#132325)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
lucasfcosta and kibanamachine committed May 23, 2022
1 parent e0944d1 commit ae8b6c8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('formatMonitorConfig', () => {
expect(yamlConfig).toEqual({
'check.request.method': 'GET',
enabled: true,
locations: [],
max_redirects: '0',
name: 'Test',
password: '3z9SBOQWW5F0UrdqLVFqlF6z',
Expand Down Expand Up @@ -110,6 +111,7 @@ describe('formatMonitorConfig', () => {
'filter_journeys.tags': ['dev'],
ignore_https_errors: false,
name: 'Test',
locations: [],
schedule: '@every 3m',
screenshots: 'on',
'source.inline.script':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const UI_KEYS_TO_SKIP = [
ConfigKey.DOWNLOAD_SPEED,
ConfigKey.LATENCY,
ConfigKey.IS_THROTTLING_ENABLED,
ConfigKey.LOCATIONS,
ConfigKey.REVISION,
'secrets',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
* 2.0.
*/

import { SyntheticsService } from './synthetics_service';
jest.mock('axios', () => jest.fn());

import { SyntheticsService, SyntheticsConfig } from './synthetics_service';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { loggerMock } from '@kbn/core/server/logging/logger.mock';
import { UptimeServerSetup } from '../legacy_uptime/lib/adapters';
import axios, { AxiosResponse } from 'axios';

describe('SyntheticsService', () => {
const mockEsClient = {
Expand Down Expand Up @@ -67,4 +70,71 @@ describe('SyntheticsService', () => {
},
]);
});

describe('addConfig', () => {
afterEach(() => jest.restoreAllMocks());

it('saves configs only to the selected locations', async () => {
serverMock.config = { service: { devUrl: 'http://localhost' } };
const service = new SyntheticsService(logger, serverMock, {
username: 'dev',
password: '12345',
});

service.apiClient.locations = [
{
id: 'selected',
label: 'Selected Location',
url: 'example.com/1',
geo: {
lat: 0,
lon: 0,
},
isServiceManaged: true,
},
{
id: 'not selected',
label: 'Not Selected Location',
url: 'example.com/2',
geo: {
lat: 0,
lon: 0,
},
isServiceManaged: true,
},
];

jest.spyOn(service, 'getApiKey').mockResolvedValue({ name: 'example', id: 'i', apiKey: 'k' });
jest.spyOn(service, 'getOutput').mockResolvedValue({ hosts: ['es'], api_key: 'i:k' });

const payload = {
type: 'http',
enabled: true,
schedule: {
number: '3',
unit: 'm',
},
name: 'my mon',
locations: [{ id: 'selected', isServiceManaged: true }],
urls: 'http://google.com',
max_redirects: '0',
password: '',
proxy_url: '',
id: '7af7e2f0-d5dc-11ec-87ac-bdfdb894c53d',
fields: { config_id: '7af7e2f0-d5dc-11ec-87ac-bdfdb894c53d' },
fields_under_root: true,
};

(axios as jest.MockedFunction<typeof axios>).mockResolvedValue({} as AxiosResponse);

await service.addConfig(payload as SyntheticsConfig);

expect(axios).toHaveBeenCalledTimes(1);
expect(axios).toHaveBeenCalledWith(
expect.objectContaining({
url: 'example.com/1/monitors',
})
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ const SYNTHETICS_SERVICE_SYNC_MONITORS_TASK_TYPE =
const SYNTHETICS_SERVICE_SYNC_MONITORS_TASK_ID = 'UPTIME:SyntheticsService:sync-task';
const SYNTHETICS_SERVICE_SYNC_INTERVAL_DEFAULT = '5m';

type SyntheticsConfig = SyntheticsMonitorWithId & {
export type SyntheticsConfig = SyntheticsMonitorWithId & {
fields_under_root?: boolean;
fields?: { config_id: string; run_once?: boolean; test_run_id?: string };
};

export class SyntheticsService {
private logger: Logger;
private readonly server: UptimeServerSetup;
private apiClient: ServiceAPIClient;
public apiClient: ServiceAPIClient;

private readonly config: ServiceConfig;
private readonly esHosts: string[];
Expand Down

0 comments on commit ae8b6c8

Please sign in to comment.