Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
97 changes: 92 additions & 5 deletions __tests__/locations/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import { LOCATIONS } from '../fixtures/locationinfo';
import { Server } from '../utils/server';
import { CLIMock } from '../utils/test-config';
import { mkdir, rm, writeFile } from 'fs/promises';
import { Straightforward } from 'straightforward';
import { AddressInfo } from 'net';

describe('Locations', () => {
const apiKey = 'foo';
Expand Down Expand Up @@ -82,10 +84,10 @@ describe('Locations', () => {

describe('CLI command', () => {
const PROJECT_DIR = join(__dirname, 'new-project');
async function fakeProjectSetup(settings) {
async function fakeProjectSetup(settings: any) {
await writeFile(
join(PROJECT_DIR, 'synthetics.config.ts'),
`export default { project: ${JSON.stringify(settings)} }`
`export default ${JSON.stringify(settings)}`
);
}

Expand All @@ -96,10 +98,13 @@ describe('Locations', () => {
await rm(PROJECT_DIR, { recursive: true, force: true });
});

const runLocations = async (args: Array<string> = []) => {
const runLocations = async (
args: Array<string> = [],
cliEnv: NodeJS.ProcessEnv = {}
) => {
const cli = new CLIMock()
.args(['locations', ...args])
.run({ cwd: PROJECT_DIR, env: process.env });
.run({ cwd: PROJECT_DIR, env: { ...process.env, ...cliEnv } });
expect(await cli.exitCode).toBe(0);
return cli.stderr();
};
Expand All @@ -120,10 +125,92 @@ describe('Locations', () => {
});

it('use project url settings for private locations', async () => {
await fakeProjectSetup({ url: server.PREFIX });
await fakeProjectSetup({ project: { url: server.PREFIX } });
const output = await runLocations(['--auth', apiKey]);
expect(output).toContain(`custom location 1`);
expect(output).toContain(`custom location 2`);
});

describe('Proxy options', () => {
let requests: Array<any> = [];
let proxyServer: Straightforward;
let tlsServer;
let proxyPort: number;

beforeAll(async () => {
proxyServer = new Straightforward();
proxyServer.onConnect.use(async ({ req }, next) => {
requests.push(req);
return next();
});
await proxyServer.listen();
tlsServer = await Server.create({ tls: true });
tlsServer.route('/internal/uptime/service/locations', (req, res) => {
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify({ locations: LOCATIONS }));
});
({ port: proxyPort } = proxyServer.server.address() as AddressInfo);
});

afterAll(async () => {
proxyServer.close();
tlsServer.close();
});

beforeEach(() => {
requests = [];
});

it('enables proxy based on HTTP_PROXY', async () => {
await fakeProjectSetup({ project: { url: server.PREFIX } });
const output = await runLocations(['--auth', apiKey], {
HTTP_PROXY: `http://localhost:${proxyPort}`,
});
expect(output).toContain(`custom location 1`);
expect(requests).toHaveLength(1);
});

it('honors NO_PROXY with env variables', async () => {
await fakeProjectSetup({ project: { url: server.PREFIX } });
const output = await runLocations(['--auth', apiKey], {
NO_PROXY: '*',
HTTP_PROXY: `http://localhost:${proxyPort}`,
});
expect(output).toContain(`custom location 1`);
expect(requests).toHaveLength(0);
});

it('enables proxy based on HTTPS_PROXY', async () => {
await fakeProjectSetup({ project: { url: tlsServer.PREFIX } });
const output = await runLocations(['--auth', apiKey], {
HTTPS_PROXY: `http://localhost:${proxyPort}`,
NODE_TLS_REJECT_UNAUTHORIZED: '0',
});
expect(output).toContain(`custom location 1`);
expect(requests).toHaveLength(1);
});

it('enables proxy based on --proxy-uri', async () => {
await fakeProjectSetup({ project: { url: server.PREFIX } });
const output = await runLocations([
'--auth',
apiKey,
'--proxy-uri',
`http://localhost:${proxyPort}`,
]);
expect(output).toContain(`custom location 1`);
expect(requests).toHaveLength(1);
});

it('enables proxy based on proxy settings', async () => {
await fakeProjectSetup({
project: { url: server.PREFIX },
proxy: { uri: `http://localhost:${proxyPort}` },
});
const output = await runLocations(['--auth', apiKey]);
expect(output).toContain(`custom location 1`);
expect(requests).toHaveLength(1);
});
});
});
});
32 changes: 31 additions & 1 deletion __tests__/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
*/

import { CliArgs } from '../src/common_types';
import { normalizeOptions, parsePlaywrightOptions } from '../src/options';
import {
collectOpts,
normalizeOptions,
parseFileOption,
parsePlaywrightOptions,
} from '../src/options';
import { join } from 'path';

describe('options', () => {
Expand Down Expand Up @@ -187,4 +192,29 @@ describe('options', () => {
expect(Buffer.isBuffer(t.passphrase)).toBeFalsy();
});
});

describe('parseFileOption', () => {
it('parses file', () => {
expect(
parseFileOption('test')(
join(__dirname, 'fixtures', 'synthetics.config.ts')
)
).toBeInstanceOf(Buffer);
});
it('parses string', () => {
expect(parseFileOption('test')('test')).toEqual('test');
});
});

describe('collectOpts', () => {
it('collects options in the accumulator', () => {
const opts = { a: 'a', b: 'b', c: true };
const result = {};
Object.entries(opts).forEach(([key, value]) => {
collectOpts(key, result)(value);
});

expect(result).toEqual(opts);
});
});
});
167 changes: 167 additions & 0 deletions __tests__/push/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { createKibanaTestServer } from '../utils/kibana-test-server';
import { Server } from '../utils/server';
import { CLIMock } from '../utils/test-config';
import { THROTTLING_WARNING_MSG } from '../../src/helpers';
import Straightforward from 'straightforward';
import { AddressInfo } from 'net';

describe('Push', () => {
let server: Server;
Expand Down Expand Up @@ -353,4 +355,169 @@ heartbeat.monitors:
expect(output).toMatchSnapshot();
});
});

describe('Proxy options', () => {
let requests: Array<any> = [];
let proxyServer: Straightforward;
let tlsServer;
let proxyPort: number;

beforeAll(async () => {
proxyServer = new Straightforward();
proxyServer.onConnect.use(async ({ req }, next) => {
requests.push(req);
return next();
});
await proxyServer.listen();
tlsServer = await createKibanaTestServer('8.6.0', true, (req: any) =>
requests.push(req)
);
({ port: proxyPort } = proxyServer.server.address() as AddressInfo);
});

afterAll(async () => {
proxyServer.close();
tlsServer.close();
});

beforeEach(() => {
requests = [];
});

it('enables proxy based on HTTP_PROXY', async () => {
await fakeProjectSetup(
{ project: { id: 'test-project', space: 'dummy', url: server.PREFIX } },
{ locations: ['test-loc'], schedule: 3 }
);
const testJourney = join(PROJECT_DIR, 'chunk.journey.ts');
await writeFile(
testJourney,
`import {journey, monitor} from '../../../';
journey('a', () => monitor.use({ tags: ['chunk'] }));
journey('b', () => monitor.use({ tags: ['chunk'] }));`
);
const output = await runPush([...DEFAULT_ARGS, '--tags', 'chunk'], {
CHUNK_SIZE: '1',
HTTP_PROXY: `http://localhost:${proxyPort}`,
});
await rm(testJourney, { force: true });
expect(output).toContain('Added(2)');
expect(output).toContain('creating or updating 1 monitors');
expect(output).toContain('✓ Pushed:');
expect(requests.length).toBeGreaterThan(0);
});

it('honors NO_PROXY with env variables', async () => {
await fakeProjectSetup(
{ project: { id: 'test-project', space: 'dummy', url: server.PREFIX } },
{ locations: ['test-loc'], schedule: 3 }
);
const testJourney = join(PROJECT_DIR, 'chunk.journey.ts');
await writeFile(
testJourney,
`import {journey, monitor} from '../../../';
journey('a', () => monitor.use({ tags: ['chunk'] }));
journey('b', () => monitor.use({ tags: ['chunk'] }));`
);
const output = await runPush([...DEFAULT_ARGS, '--tags', 'chunk'], {
CHUNK_SIZE: '1',
HTTP_PROXY: `http://localhost:${proxyPort}`,
NO_PROXY: '*',
});
await rm(testJourney, { force: true });
expect(output).toContain('Added(2)');
expect(output).toContain('creating or updating 1 monitors');
expect(output).toContain('✓ Pushed:');
expect(requests).toHaveLength(0);
});

it('enables proxy based on HTTPS_PROXY', async () => {
await fakeProjectSetup(
{
project: {
id: 'test-project',
space: 'dummy',
url: tlsServer.PREFIX,
},
},
{ locations: ['test-loc'], schedule: 3 }
);
const testJourney = join(PROJECT_DIR, 'chunk.journey.ts');
await writeFile(
testJourney,
`import {journey, monitor} from '../../../';
journey('a', () => monitor.use({ tags: ['chunk'] }));
journey('b', () => monitor.use({ tags: ['chunk'] }));`
);
const output = await runPush([...DEFAULT_ARGS, '--tags', 'chunk'], {
CHUNK_SIZE: '1',
HTTPS_PROXY: `http://localhost:${proxyPort}`,
NODE_TLS_REJECT_UNAUTHORIZED: '0',
});
await rm(testJourney, { force: true });
expect(output).toContain('Added(2)');
expect(output).toContain('creating or updating 1 monitors');
expect(output).toContain('✓ Pushed:');
expect(requests.length).toBeGreaterThan(0);
});

it('enables proxy based on --proxy-uri', async () => {
await fakeProjectSetup(
{
project: { id: 'test-project', space: 'dummy', url: server.PREFIX },
proxy: { uri: `http://localhost:${proxyPort}` },
},
{ locations: ['test-loc'], schedule: 3 }
);
const testJourney = join(PROJECT_DIR, 'chunk.journey.ts');
await writeFile(
testJourney,
`import {journey, monitor} from '../../../';
journey('a', () => monitor.use({ tags: ['chunk'] }));
journey('b', () => monitor.use({ tags: ['chunk'] }));`
);
const output = await runPush(
[
...DEFAULT_ARGS,
'--tags',
'chunk',
'--proxy-uri',
`http://localhost:${proxyPort}`,
],
{
CHUNK_SIZE: '1',
}
);
await rm(testJourney, { force: true });
expect(output).toContain('Added(2)');
expect(output).toContain('creating or updating 1 monitors');
expect(output).toContain('✓ Pushed:');
expect(requests.length).toBeGreaterThan(0);
});

it('enables proxy based on proxy settings', async () => {
await fakeProjectSetup(
{
project: { id: 'test-project', space: 'dummy', url: server.PREFIX },
proxy: { uri: `http://localhost:${proxyPort}` },
},
{ locations: ['test-loc'], schedule: 3 }
);
const testJourney = join(PROJECT_DIR, 'chunk.journey.ts');
await writeFile(
testJourney,
`import {journey, monitor} from '../../../';
journey('a', () => monitor.use({ tags: ['chunk'] }));
journey('b', () => monitor.use({ tags: ['chunk'] }));`
);
const output = await runPush([...DEFAULT_ARGS, '--tags', 'chunk'], {
CHUNK_SIZE: '1',
});
await rm(testJourney, { force: true });
expect(output).toContain('Added(2)');
expect(output).toContain('creating or updating 1 monitors');
expect(output).toContain('✓ Pushed:');
expect(requests.length).toBeGreaterThan(0);
});
});
});
Loading