Skip to content

Commit

Permalink
Improve tests, reorganize code.
Browse files Browse the repository at this point in the history
  • Loading branch information
justinkambic committed Feb 19, 2024
1 parent 9de233d commit ce574a9
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 59 deletions.
35 changes: 35 additions & 0 deletions x-pack/plugins/synthetics/server/common/inline_to_zip.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 { inlineToProjectZip } from './inline_to_zip';
import { unzipFile } from './unzipt_project_code';

describe('inlineToProjectZip', () => {
it('should return base64 encoded zip data', async () => {
const inlineJourney = `
step('goto', () => page.goto('https://elastic.co'));
step('throw error', () => { throw new Error('error'); });
`;
const monitorId = 'testMonitorId';
const logger = jest.fn();

// @ts-expect-error not checking logger functionality
const result = await inlineToProjectZip(inlineJourney, monitorId, logger);

expect(result.length).toBeGreaterThan(0);
expect(await unzipFile(result)).toEqual(
`import { journey, step, expect } from '@elastic/synthetics';
journey('inline', ({ page, context, browser, params, request }) => {
step('goto', () => page.goto('https://elastic.co'));
step('throw error', () => { throw new Error('error'); });
});`
);
});
});
44 changes: 44 additions & 0 deletions x-pack/plugins/synthetics/server/common/inline_to_zip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 { Logger } from '@kbn/logging';
import archiver from 'archiver';
import { MemWritable } from './mem_writable';

function wrapInlineInProject(inlineJourney: string) {
return `import { journey, step, expect } from '@elastic/synthetics';
journey('inline', ({ page, context, browser, params, request }) => {
${inlineJourney}
});`;
}

export async function inlineToProjectZip(
inlineJourney: string,
monitorId: string,
logger: Logger
): Promise<string> {
const mWriter = new MemWritable();
try {
await new Promise((resolve, reject) => {
const archive = archiver('zip', {
zlib: { level: 9 },
});
archive.on('error', reject);
mWriter.on('close', resolve);
archive.pipe(mWriter);
archive.append(wrapInlineInProject(inlineJourney), {
name: 'inline.journey.ts',
});
archive.finalize();
});
} catch (e) {
logger.error(`Failed to create zip for inline monitor ${monitorId}`);
throw e;
}
return mWriter.buffer.toString('base64');
}
19 changes: 1 addition & 18 deletions x-pack/plugins/synthetics/server/common/mem_writable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { inlineToProjectZip, MemWritable } from './mem_writable';
import { MemWritable } from './mem_writable';

describe('MemWritable', () => {
it('should write chunks to the buffer', async () => {
Expand Down Expand Up @@ -35,20 +35,3 @@ describe('MemWritable', () => {
expect(memWritable.buffer).toEqual(expectedBuffer);
});
});

describe('inlineToProjectZip', () => {
it('should return base64 encoded zip data', async () => {
const inlineJourney = `
step('goto', () => page.goto('https://elastic.co'));
step('throw error', () => { throw new Error('error'); });
`;
const monitorId = 'testMonitorId';
const logger = jest.fn();

// @ts-expect-error not checking logger functionality
const result = await inlineToProjectZip(inlineJourney, monitorId, logger);

// zip is not deterministic, so we can't check the exact value
expect(result.length).toBeGreaterThan(0);
});
});
37 changes: 0 additions & 37 deletions x-pack/plugins/synthetics/server/common/mem_writable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* 2.0.
*/

import { Logger } from '@kbn/logging';
import archiver from 'archiver';
import { Writable, WritableOptions } from 'node:stream';

export class MemWritable extends Writable {
Expand All @@ -29,38 +27,3 @@ export class MemWritable extends Writable {
callback();
}
}

function wrapInlineInProject(inlineJourney: string) {
return `import { journey, step, expect } from '@elastic/synthetics';
journey('inline', ({ page, context, browser, params, request }) => {
${inlineJourney}
});
`;
}

export async function inlineToProjectZip(
inlineJourney: string,
monitorId: string,
logger: Logger
): Promise<string> {
const mWriter = new MemWritable();
try {
await new Promise((resolve, reject) => {
const archive = archiver('zip', {
zlib: { level: 9 },
});
archive.on('error', reject);
mWriter.on('close', resolve);
archive.pipe(mWriter);
archive.append(wrapInlineInProject(inlineJourney), {
name: 'inline.journey.ts',
});
archive.finalize();
});
} catch (e) {
logger.error(`Failed to create zip for inline monitor ${monitorId}`);
throw e;
}
return mWriter.buffer.toString('base64');
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { sendTelemetryEvents, formatTelemetryEvent } from '../telemetry/monitor_
import { formatSecrets } from '../../synthetics_service/utils/secrets';
import { deleteMonitor } from './delete_monitor';
import { mapSavedObjectToMonitor } from './helper';
import { inlineToProjectZip } from '../../common/mem_writable';
import { inlineToProjectZip } from '../../common/inline_to_zip';

export const addSyntheticsMonitorRoute: SyntheticsRestApiRouteFactory = () => ({
method: 'POST',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
} from '../telemetry/monitor_upgrade_sender';
import { formatSecrets, normalizeSecrets } from '../../synthetics_service/utils/secrets';
import { mapSavedObjectToMonitor } from './helper';
import { inlineToProjectZip } from '../../common/mem_writable';
import { inlineToProjectZip } from '../../common/inline_to_zip';
import { SyntheticsServerSetup } from '../../types';

// Simplify return promise type and type it with runtime_types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
TLSFields,
} from '../../../../common/runtime_types';
import { publicFormatters } from '.';
import { inlineToProjectZip } from '../../../common/mem_writable';
import { inlineToProjectZip } from '../../../common/inline_to_zip';

const UI_KEYS_TO_SKIP = [
ConfigKey.JOURNEY_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
formatMonitorConfigFields,
mixParamsWithGlobalParams,
} from './formatters/public_formatters/format_configs';
import { inlineToProjectZip } from '../common/mem_writable';
import { inlineToProjectZip } from '../common/inline_to_zip';

const SYNTHETICS_SERVICE_SYNC_MONITORS_TASK_TYPE =
'UPTIME:SyntheticsService:Sync-Saved-Monitor-Objects';
Expand Down

0 comments on commit ce574a9

Please sign in to comment.