Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bundle package deps as local packages #673

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions __tests__/push/__snapshots__/bundler.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Bundler build journey 1`] = `
"
__tests__/push/test-bundler/bundle.journey.ts
import { journey, step, monitor } from \\"@elastic/synthetics\\";
import isPositive from \\"./bundles/is-positive/index\\";
import utils from \\"./utils\\";
journey(\\"journey 1\\", () => {
utils();
monitor.use({ id: \\"duplicate id\\" });
launchStep(-1);
});
const launchStep = (no) => {
step(\\"step1\\", () => {
isPositive(no);
});
};

__tests__/push/test-bundler/bundles/is-positive/index.js
\\"use strict\\";
module.exports = function(n) {
return toString.call(n) === \\"[object Number]\\" && n > 0;
};

__tests__/push/test-bundler/utils.ts
import isNegative from \\"./bundles/is-negative/index\\";
export default utils = () => {
isNegative(1);
};

__tests__/push/test-bundler/bundles/is-negative/index.js
\\"use strict\\";
module.exports = function(n) {
return toString.call(n) === \\"[object Number]\\" && n < 0;
};
"
`;
115 changes: 39 additions & 76 deletions __tests__/push/__snapshots__/plugin.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,128 +1,91 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Plugin bundle jouneys 1`] = `
"/**
* MIT License
*
* Copyright (c) 2020-present, Elastic NV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the \\"Software\\"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED \\"AS IS\\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

import { beforeAll, journey, step } from '@elastic/synthetics';
import axios from 'axios';

"import { beforeAll, journey, step } from \\"@elastic/synthetics\\";
import axios from \\"axios\\";
beforeAll(async () => {
await waitForElasticSearch();
await waitForSyntheticsData();
await waitForKibana();
});

journey('E2e test synthetics', async ({ page }) => {
journey(\\"E2e test synthetics\\", async ({ page }) => {
async function refreshUptimeApp() {
while (!(await page.$('div.euiBasicTable'))) {
await page.click('[data-test-subj=superDatePickerApplyTimeButton]', {
timeout: 60 * 1000,
while (!await page.$(\\"div.euiBasicTable\\")) {
await page.click(\\"[data-test-subj=superDatePickerApplyTimeButton]\\", {
timeout: 60 * 1e3
});
}
}

step('Go to kibana uptime app', async () => {
await page.goto('http://localhost:5620/app/uptime', {
waitUntil: 'networkidle',
step(\\"Go to kibana uptime app\\", async () => {
await page.goto(\\"http://localhost:5620/app/uptime\\", {
waitUntil: \\"networkidle\\"
});
});

step('Check if there is table data', async () => {
await page.click('[data-test-subj=uptimeOverviewPage]', {
timeout: 60 * 1000,
step(\\"Check if there is table data\\", async () => {
await page.click(\\"[data-test-subj=uptimeOverviewPage]\\", {
timeout: 60 * 1e3
});
await refreshUptimeApp();
await page.click('div.euiBasicTable', { timeout: 60 * 1000 });
await page.click(\\"div.euiBasicTable\\", { timeout: 60 * 1e3 });
});

step('Click on my monitor', async () => {
await page.click('[data-test-subj=monitor-page-link-my-monitor]');
step(\\"Click on my monitor\\", async () => {
await page.click(\\"[data-test-subj=monitor-page-link-my-monitor]\\");
});

step('It navigates to details page', async () => {
await page.click('[data-test-subj=uptimeMonitorPage]');
step(\\"It navigates to details page\\", async () => {
await page.click(\\"[data-test-subj=uptimeMonitorPage]\\");
});
});

async function waitForSyntheticsData() {
console.log('Waiting for Synthetics to send data to ES for test monitor');
console.log(\\"Waiting for Synthetics to send data to ES for test monitor\\");
let status = false;

while (!status) {
try {
const { data } = await axios.post(
'http://localhost:9220/heartbeat-*/_search',
\\"http://localhost:9220/heartbeat-*/_search\\",
{
query: {
bool: {
filter: [
{
term: {
'monitor.id': 'my-monitor',
},
\\"monitor.id\\": \\"my-monitor\\"
}
},
{
exists: {
field: 'summary',
},
},
],
},
},
field: \\"summary\\"
}
}
]
}
}
}
);

// we want some data in uptime app
status = data?.hits.total.value >= 2;
} catch (e) {}
} catch (e) {
}
}
}

async function waitForElasticSearch() {
console.log('Waiting for Elastic Search to start');
console.log(\\"Waiting for Elastic Search to start\\");
let esStatus = false;

while (!esStatus) {
try {
const { data } = await axios.get('http://localhost:9220/_cluster/health');
esStatus = data?.status !== 'red';
} catch (e) {}
const { data } = await axios.get(\\"http://localhost:9220/_cluster/health\\");
esStatus = data?.status !== \\"red\\";
} catch (e) {
}
}
}

async function waitForKibana() {
console.log('Waiting for kibana server to start');

console.log(\\"Waiting for kibana server to start\\");
let esStatus = false;

while (!esStatus) {
try {
const { data } = await axios.get('http://localhost:5620/api/status');
esStatus = data?.status.overall.level === 'available';
} catch (e) {}
const { data } = await axios.get(\\"http://localhost:5620/api/status\\");
esStatus = data?.status.overall.level === \\"available\\";
} catch (e) {
}
}
}
"
Expand Down
101 changes: 68 additions & 33 deletions __tests__/push/bundler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,66 @@ import { createReadStream } from 'fs';
import { writeFile, unlink, mkdir, rm } from 'fs/promises';
import unzipper from 'unzipper';
import { join } from 'path';
import { spawn } from 'child_process';
import { generateTempPath } from '../../src/helpers';
import { Bundler } from '../../src/push/bundler';

const PROJECT_DIR = join(__dirname, 'test-bundler');
const journeyFile = join(PROJECT_DIR, 'bundle.journey.ts');

async function validateZip(content) {
const partialPath = join(
'__tests__',
'push',
'test-bundler',
'bundle.journey.ts'
const setup = async () => {
process.env.PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD = '1';
await writeFile(
join(PROJECT_DIR, `setup.sh`),
`
#!/bin/bash
set -e
npm init -y
npm install @elastic/synthetics is-positive is-negative
`
);

await writeFile(
journeyFile,
`import {journey, step, monitor} from '@elastic/synthetics';
import isPositive from 'is-positive';
import isNegative from 'is-negative';
import utils from "./utils"

journey('journey 1', () => {
// avoid dead code elimination
utils();
monitor.use({ id: 'duplicate id' });
launchStep(-1);
});

const launchStep = (no: number) => {
step("step1", () => {
isPositive(no);
})
};`
);

await writeFile(
join(PROJECT_DIR, 'utils.ts'),
`import isNegative from 'is-negative';
export default utils = () => {
isNegative(1);
};`
);

return new Promise(res => {
const proc = spawn('sh', ['setup.sh'], {
cwd: PROJECT_DIR,
stdio: 'pipe',
});
proc.on('exit', code => {
res(code);
});
});
};

async function validateZip(content) {
const decoded = Buffer.from(content, 'base64');
const pathToZip = generateTempPath();
await writeFile(pathToZip, decoded);
Expand All @@ -53,20 +100,21 @@ async function validateZip(content) {
.on('entry', function (entry) {
const fileName = entry.path;
files.push(fileName);
if (fileName === partialPath) {
entry.on('data', d => (targetFileContent += d));
} else {
entry.autodrain();
}
targetFileContent += '\n' + fileName + '\n';
entry.on('data', d => (targetFileContent += d));
})
.on('close', r);
});

expect(files).toEqual([partialPath]);

expect(targetFileContent).toContain('__toESM');
expect(targetFileContent).toContain('node_modules/is-positive/index.js');
const expectedFiles = [
'bundle.journey.ts',
'bundles/is-positive/index.js',
'utils.ts',
'bundles/is-negative/index.js',
].map(f => join('__tests__/push/test-bundler', f));

expect(files).toEqual(expectedFiles);
expect(targetFileContent).toMatchSnapshot();
await unlink(pathToZip);
}

Expand All @@ -75,22 +123,7 @@ describe('Bundler', () => {

beforeAll(async () => {
await mkdir(PROJECT_DIR, { recursive: true });
await writeFile(
journeyFile,
`import {journey, step, monitor} from '@elastic/synthetics';
import isPositive from 'is-positive';

journey('journey 1', () => {
monitor.use({ id: 'duplicate id' });
launchStep(-1);
});

const launchStep = (no: number) => {
step("step1", () => {
isPositive(no);
})
};`
);
await setup();
});

afterAll(async () => {
Expand All @@ -100,7 +133,7 @@ const launchStep = (no: number) => {
it('build journey', async () => {
const content = await bundler.build(journeyFile, generateTempPath());
await validateZip(content);
});
}, 15000);

it('bundle should be idempotent', async () => {
const content1 = await bundler.build(journeyFile, generateTempPath());
Expand All @@ -112,7 +145,9 @@ const launchStep = (no: number) => {
try {
await bundler.build(join(PROJECT_DIR, 'blah.ts'), generateTempPath());
} catch (e) {
expect(e.message).toContain('ENOENT');
expect(e.message).toContain(
'[plugin: SyntheticsBundlePlugin] Cannot find module'
);
}
});
});
13 changes: 4 additions & 9 deletions __tests__/push/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@

import * as esbuild from 'esbuild';
import { join } from 'path';
import NodeResolve from '@esbuild-plugins/node-resolve';
import {
MultiAssetPlugin,
SyntheticsBundlePlugin,
commonOptions,
PluginData,
} from '../../src/push/plugin';
Expand All @@ -43,14 +42,10 @@ describe('Plugin', () => {

await esbuild.build({
...commonOptions(),
bundle: false,
external: [],
entryPoints: [join(E2E_DIR, 'uptime.journey.ts')],
plugins: [
MultiAssetPlugin(callback),
NodeResolve({
extensions: ['.ts', '.js'],
resolveOptions: { basedir: E2E_DIR },
}),
],
plugins: [SyntheticsBundlePlugin(callback)],
});
});
});
Loading