[Uptime] Fix bug causing all monitors to be saved to all locations [solves #132314] (#132325)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Lucas F. da Costa 2022-05-23 09:29:11 +01:00 committed by GitHub
parent e0944d17ec
commit ae8b6c8beb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 4 deletions

View file

@ -53,6 +53,7 @@ describe('formatMonitorConfig', () => {
expect(yamlConfig).toEqual({
'check.request.method': 'GET',
enabled: true,
locations: [],
max_redirects: '0',
name: 'Test',
password: '3z9SBOQWW5F0UrdqLVFqlF6z',
@ -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':

View file

@ -15,7 +15,6 @@ const UI_KEYS_TO_SKIP = [
ConfigKey.DOWNLOAD_SPEED,
ConfigKey.LATENCY,
ConfigKey.IS_THROTTLING_ENABLED,
ConfigKey.LOCATIONS,
ConfigKey.REVISION,
'secrets',
];

View file

@ -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 = {
@ -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',
})
);
});
});
});

View file

@ -48,7 +48,7 @@ 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 };
};
@ -56,7 +56,7 @@ type SyntheticsConfig = SyntheticsMonitorWithId & {
export class SyntheticsService {
private logger: Logger;
private readonly server: UptimeServerSetup;
private apiClient: ServiceAPIClient;
public apiClient: ServiceAPIClient;
private readonly config: ServiceConfig;
private readonly esHosts: string[];