Skip to content

Commit

Permalink
Added lightweight params
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Feb 2, 2023
1 parent 6dc77ce commit ab7b42d
Show file tree
Hide file tree
Showing 30 changed files with 1,645 additions and 365 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
/x-pack/test/functional/es_archives/uptime @elastic/uptime
/x-pack/test/functional/services/uptime @elastic/uptime
/x-pack/test/api_integration/apis/uptime @elastic/uptime
/x-pack/test/api_integration/apis/synthetics @elastic/uptime
/x-pack/plugins/observability/public/components/shared/exploratory_view @elastic/uptime


Expand Down
51 changes: 21 additions & 30 deletions x-pack/plugins/synthetics/common/formatters/browser/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
*/
import { BrowserFields, ConfigKey } from '../../runtime_types/monitor_management';

import { Formatter, commonFormatters } from '../common/formatters';
import {
Formatter,
commonFormatters,
objectToJsonFormatter,
arrayToJsonFormatter,
objectToJsonFormatter,
paramReplaceFormatter,
stringToJsonFormatter,
} from '../common/formatters';
import {
tlsValueToYamlFormatter,
tlsValueToStringFormatter,
tlsArrayToYamlFormatter,
} from '../tls/formatters';
tlsValueToStringFormatter,
tlsValueToYamlFormatter,
} from '../formatting_utils';

import { tlsFormatters } from '../tls/formatters';

export type BrowserFormatMap = Record<keyof BrowserFields, Formatter>;
Expand All @@ -38,43 +37,35 @@ const throttlingFormatter: Formatter = (fields) => {
};

export const browserFormatters: BrowserFormatMap = {
[ConfigKey.METADATA]: (fields) => objectToJsonFormatter(fields[ConfigKey.METADATA]),
[ConfigKey.URLS]: null,
[ConfigKey.PORT]: null,
[ConfigKey.SOURCE_ZIP_URL]: null,
[ConfigKey.SOURCE_ZIP_USERNAME]: null,
[ConfigKey.SOURCE_ZIP_PASSWORD]: null,
[ConfigKey.SOURCE_ZIP_FOLDER]: null,
[ConfigKey.SOURCE_ZIP_PROXY_URL]: null,
[ConfigKey.SOURCE_PROJECT_CONTENT]: null,
[ConfigKey.SOURCE_INLINE]: (fields) => stringToJsonFormatter(fields[ConfigKey.SOURCE_INLINE]),
[ConfigKey.PARAMS]: null,
[ConfigKey.SCREENSHOTS]: null,
[ConfigKey.IS_THROTTLING_ENABLED]: null,
[ConfigKey.DOWNLOAD_SPEED]: null,
[ConfigKey.UPLOAD_SPEED]: null,
[ConfigKey.LATENCY]: null,
[ConfigKey.SYNTHETICS_ARGS]: (fields) => arrayToJsonFormatter(fields[ConfigKey.SYNTHETICS_ARGS]),
[ConfigKey.ZIP_URL_TLS_CERTIFICATE_AUTHORITIES]: (fields) =>
tlsValueToYamlFormatter(fields[ConfigKey.ZIP_URL_TLS_CERTIFICATE_AUTHORITIES]),
[ConfigKey.ZIP_URL_TLS_CERTIFICATE]: (fields) =>
tlsValueToYamlFormatter(fields[ConfigKey.ZIP_URL_TLS_CERTIFICATE]),
[ConfigKey.ZIP_URL_TLS_KEY]: (fields) =>
tlsValueToYamlFormatter(fields[ConfigKey.ZIP_URL_TLS_KEY]),
[ConfigKey.ZIP_URL_TLS_KEY_PASSPHRASE]: (fields) =>
tlsValueToStringFormatter(fields[ConfigKey.ZIP_URL_TLS_KEY_PASSPHRASE]),
[ConfigKey.ZIP_URL_TLS_VERIFICATION_MODE]: (fields) =>
tlsValueToStringFormatter(fields[ConfigKey.ZIP_URL_TLS_VERIFICATION_MODE]),
[ConfigKey.ZIP_URL_TLS_VERSION]: (fields) =>
tlsArrayToYamlFormatter(fields[ConfigKey.ZIP_URL_TLS_VERSION]),
[ConfigKey.JOURNEY_FILTERS_MATCH]: (fields) =>
stringToJsonFormatter(fields[ConfigKey.JOURNEY_FILTERS_MATCH]),
[ConfigKey.JOURNEY_FILTERS_TAGS]: (fields) =>
arrayToJsonFormatter(fields[ConfigKey.JOURNEY_FILTERS_TAGS]),
[ConfigKey.THROTTLING_CONFIG]: throttlingFormatter,
[ConfigKey.IGNORE_HTTPS_ERRORS]: null,
[ConfigKey.PLAYWRIGHT_OPTIONS]: null,
[ConfigKey.TEXT_ASSERTION]: null,
[ConfigKey.PORT]: paramReplaceFormatter,
[ConfigKey.URLS]: paramReplaceFormatter,
[ConfigKey.METADATA]: objectToJsonFormatter,
[ConfigKey.SOURCE_INLINE]: stringToJsonFormatter,
[ConfigKey.SYNTHETICS_ARGS]: arrayToJsonFormatter,
[ConfigKey.ZIP_URL_TLS_CERTIFICATE_AUTHORITIES]: tlsValueToYamlFormatter,
[ConfigKey.ZIP_URL_TLS_CERTIFICATE]: tlsValueToYamlFormatter,
[ConfigKey.ZIP_URL_TLS_KEY]: tlsValueToYamlFormatter,
[ConfigKey.ZIP_URL_TLS_KEY_PASSPHRASE]: tlsValueToStringFormatter,
[ConfigKey.ZIP_URL_TLS_VERIFICATION_MODE]: tlsValueToStringFormatter,
[ConfigKey.ZIP_URL_TLS_VERSION]: tlsArrayToYamlFormatter,
[ConfigKey.JOURNEY_FILTERS_MATCH]: stringToJsonFormatter,
[ConfigKey.JOURNEY_FILTERS_TAGS]: arrayToJsonFormatter,
[ConfigKey.THROTTLING_CONFIG]: throttlingFormatter,
...commonFormatters,
...tlsFormatters,
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,62 @@
* 2.0.
*/

import { ConfigKey } from '../../runtime_types';
import {
secondsToCronFormatter,
arrayToJsonFormatter,
objectToJsonFormatter,
stringToJsonFormatter,
secondsToCronFormatter,
} from './formatters';
} from '../formatting_utils';

describe('formatters', () => {
describe('cronToSecondsNormalizer', () => {
it('takes a number of seconds and converts it to cron format', () => {
expect(secondsToCronFormatter('3')).toEqual('3s');
expect(secondsToCronFormatter({ [ConfigKey.WAIT]: '3' }, ConfigKey.WAIT)).toEqual('3s');
});
});

describe('arrayToJsonFormatter', () => {
it('takes an array and converts it to json', () => {
expect(arrayToJsonFormatter(['tag1', 'tag2'])).toEqual('["tag1","tag2"]');
expect(arrayToJsonFormatter({ [ConfigKey.TAGS]: ['tag1', 'tag2'] }, ConfigKey.TAGS)).toEqual(
'["tag1","tag2"]'
);
});

it('returns null if the array has length of 0', () => {
expect(arrayToJsonFormatter([])).toEqual(null);
expect(arrayToJsonFormatter({ [ConfigKey.TAGS]: [] }, ConfigKey.TAGS)).toEqual(null);
});
});

describe('objectToJsonFormatter', () => {
it('takes a json object string and returns an object', () => {
expect(objectToJsonFormatter({ key: 'value' })).toEqual('{"key":"value"}');
expect(
objectToJsonFormatter(
{ [ConfigKey.RESPONSE_HEADERS_CHECK]: { key: 'value' } },
ConfigKey.RESPONSE_HEADERS_CHECK
)
).toEqual('{"key":"value"}');
});

it('returns null if the object has no keys', () => {
expect(objectToJsonFormatter({})).toEqual(null);
expect(objectToJsonFormatter({ [ConfigKey.METADATA]: {} }, ConfigKey.METADATA)).toEqual(null);
});
});

describe('stringToJsonFormatter', () => {
it('takes a string and returns an json string', () => {
expect(stringToJsonFormatter('step("test step", () => {})')).toEqual(
'"step(\\"test step\\", () => {})"'
);
expect(
stringToJsonFormatter(
{ [ConfigKey.SOURCE_INLINE]: 'step("test step", () => {})' },
ConfigKey.SOURCE_INLINE
)
).toEqual('"step(\\"test step\\", () => {})"');
});

it('returns null if the string is falsy', () => {
expect(stringToJsonFormatter('')).toEqual(null);
expect(
stringToJsonFormatter({ [ConfigKey.SOURCE_INLINE]: '' }, ConfigKey.SOURCE_INLINE)
).toEqual(null);
});
});
});
29 changes: 12 additions & 17 deletions x-pack/plugins/synthetics/common/formatters/common/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,21 @@
* 2.0.
*/

import { CommonFields, ConfigKey, MonitorFields } from '../../runtime_types/monitor_management';
import { CommonFields, ConfigKey, SourceType } from '../../runtime_types/monitor_management';
import { arrayToJsonFormatter, FormatterFn } from '../formatting_utils';

export type Formatter = null | ((fields: Partial<MonitorFields>) => string | null);
export type Formatter = null | FormatterFn;

export type CommonFormatMap = Record<keyof CommonFields | ConfigKey.NAME, Formatter>;

export const commonFormatters: CommonFormatMap = {
[ConfigKey.APM_SERVICE_NAME]: null,
[ConfigKey.NAME]: null,
[ConfigKey.LOCATIONS]: null,
[ConfigKey.MONITOR_TYPE]: null,
[ConfigKey.ENABLED]: null,
[ConfigKey.ALERT_CONFIG]: null,
[ConfigKey.CONFIG_ID]: null,
[ConfigKey.SCHEDULE]: (fields) =>
JSON.stringify(
`@every ${fields[ConfigKey.SCHEDULE]?.number}${fields[ConfigKey.SCHEDULE]?.unit}`
),
[ConfigKey.APM_SERVICE_NAME]: null,
[ConfigKey.TAGS]: (fields) => arrayToJsonFormatter(fields[ConfigKey.TAGS]),
[ConfigKey.TIMEOUT]: (fields) => secondsToCronFormatter(fields[ConfigKey.TIMEOUT] || undefined),
[ConfigKey.NAMESPACE]: null,
[ConfigKey.REVISION]: null,
[ConfigKey.MONITOR_SOURCE_TYPE]: null,
Expand All @@ -35,14 +30,14 @@ export const commonFormatters: CommonFormatMap = {
[ConfigKey.ORIGINAL_SPACE]: null,
[ConfigKey.CONFIG_HASH]: null,
[ConfigKey.MONITOR_QUERY_ID]: null,
[ConfigKey.SCHEDULE]: (fields) =>
JSON.stringify(
`@every ${fields[ConfigKey.SCHEDULE]?.number}${fields[ConfigKey.SCHEDULE]?.unit}`
),
[ConfigKey.TAGS]: arrayToJsonFormatter,
[ConfigKey.TIMEOUT]: (fields) => secondsToCronFormatter(fields[ConfigKey.TIMEOUT] || undefined),
[ConfigKey.MONITOR_SOURCE_TYPE]: (fields) =>
fields[ConfigKey.MONITOR_SOURCE_TYPE] || SourceType.UI,
};

export const arrayToJsonFormatter = (value: string[] = []) =>
value.length ? JSON.stringify(value) : null;

export const secondsToCronFormatter = (value: string = '') => (value ? `${value}s` : null);

export const objectToJsonFormatter = (value: Record<string, any> = {}) =>
Object.keys(value).length ? JSON.stringify(value) : null;

export const stringToJsonFormatter = (value: string = '') => (value ? JSON.stringify(value) : null);
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ describe('formatSyntheticsPolicy', () => {
},
params: {
type: 'yaml',
value: '',
value: '{"proxyUrl":"https://proxy.com"}',
},
playwright_options: {
type: 'yaml',
Expand Down Expand Up @@ -591,7 +591,7 @@ describe('formatSyntheticsPolicy', () => {
},
proxy_url: {
type: 'text',
value: '',
value: 'https://proxy.com',
},
'response.include_body': {
type: 'text',
Expand Down Expand Up @@ -848,36 +848,29 @@ describe('formatSyntheticsPolicy', () => {
vars: {
__ui: {
type: 'yaml',
value:
'{"script_source":{"is_generated_script":false,"file_name":""},"is_zip_url_tls_enabled":false,"is_tls_enabled":false}',
},
config_id: {
type: 'text',
value: '00bb3ceb-a242-4c7a-8405-8da963661374',
},
enabled: {
type: 'bool',
value: true,
},
'filter_journeys.match': {
type: 'text',
value: null,
},
'filter_journeys.tags': {
type: 'yaml',
value: null,
},
id: {
type: 'text',
value: '00bb3ceb-a242-4c7a-8405-8da963661374',
},
ignore_https_errors: {
type: 'bool',
value: false,
},
location_name: {
type: 'text',
value: 'Test private location 0',
value: 'Fleet managed',
},
'monitor.project.id': {
type: 'text',
Expand All @@ -887,19 +880,15 @@ describe('formatSyntheticsPolicy', () => {
},
name: {
type: 'text',
value: 'Test HTTP Monitor 03',
},
origin: {
type: 'text',
value: 'ui',
},
params: {
type: 'yaml',
value: '',
},
playwright_options: {
type: 'yaml',
value: '',
},
run_once: {
type: 'bool',
Expand All @@ -911,32 +900,24 @@ describe('formatSyntheticsPolicy', () => {
},
screenshots: {
type: 'text',
value: 'on',
},
'service.name': {
type: 'text',
value: '',
},
'source.inline.script': {
type: 'yaml',
value:
'"step(\\"Visit /users api route\\", async () => {\\\\n const response = await page.goto(\'https://nextjs-test-synthetics.vercel.app/api/users\');\\\\n expect(response.status()).toEqual(200);\\\\n});"',
},
'source.project.content': {
type: 'text',
value: '',
},
'source.zip_url.folder': {
type: 'text',
value: '',
},
'source.zip_url.password': {
type: 'password',
value: '',
},
'source.zip_url.proxy_url': {
type: 'text',
value: '',
},
'source.zip_url.ssl.certificate': {
type: 'yaml',
Expand All @@ -958,27 +939,21 @@ describe('formatSyntheticsPolicy', () => {
},
'source.zip_url.url': {
type: 'text',
value: '',
},
'source.zip_url.username': {
type: 'text',
value: '',
},
synthetics_args: {
type: 'text',
value: null,
},
tags: {
type: 'yaml',
value: '["cookie-test","browser"]',
},
'throttling.config': {
type: 'text',
value: '5d/3u/20l',
},
timeout: {
type: 'text',
value: '16s',
},
type: {
type: 'text',
Expand Down Expand Up @@ -1232,7 +1207,7 @@ const browserConfig: any = {
is_zip_url_tls_enabled: false,
is_tls_enabled: false,
},
params: '',
params: '{"proxyUrl":"https://proxy.com"}',
'url.port': null,
'source.inline.script':
'step("Visit /users api route", async () => {\\n const response = await page.goto(\'https://nextjs-test-synthetics.vercel.app/api/users\');\\n expect(response.status()).toEqual(200);\\n});',
Expand Down Expand Up @@ -1295,7 +1270,7 @@ const httpPolicy: any = {
max_redirects: '0',
'url.port': null,
password: 'changeme',
proxy_url: '',
proxy_url: '${proxyUrl}',
'check.response.body.negative': [],
'check.response.body.positive': [],
'response.include_body': 'on_error',
Expand All @@ -1314,6 +1289,6 @@ const httpPolicy: any = {
'ssl.supported_protocols': ['TLSv1.1', 'TLSv1.2', 'TLSv1.3'],
fields: { config_id: '51ccd9d9-fc3f-4718-ba9d-b6ef80e73fc5' },
fields_under_root: true,
params: '',
params: '{"proxyUrl":"https://proxy.com"}',
location_name: 'Test private location 0',
};
Loading

0 comments on commit ab7b42d

Please sign in to comment.