Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
74 changes: 73 additions & 1 deletion __tests__/locations/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ 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';

describe('Locations', () => {
const apiKey = 'foo';
Expand Down Expand Up @@ -97,7 +98,7 @@ describe('Locations', () => {
});

const runLocations = async (args: Array<string> = []) => {
const cli = new CLIMock()
const cli = new CLIMock(true)
.args(['locations', ...args])
.run({ cwd: PROJECT_DIR, env: process.env });
expect(await cli.exitCode).toBe(0);
Expand Down Expand Up @@ -125,5 +126,76 @@ describe('Locations', () => {
expect(output).toContain(`custom location 1`);
expect(output).toContain(`custom location 2`);
});

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

beforeAll(async () => {
proxyServer = new Straightforward();
proxyServer.onConnect.use(async ({ req }, next) => {
requests.push(req);
return next();
});
await proxyServer.listen(8019);
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 }));
});
});

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

beforeEach(() => {
requests = [];
delete process.env.HTTP_PROXY,
process.env.HTTP_PROXYS,
process.env.NO_PROXY,
process.env.NODE_TLS_REJECT_UNAUTHORIZED;
});

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

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

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

it('enables proxy based on --proxy-uri', async () => {
await fakeProjectSetup({ url: server.PREFIX });
const output = await runLocations([
'--auth',
apiKey,
'--proxy-uri',
'http://localhost:8019',
]);
expect(output).toContain(`custom location 1`);
expect(requests).toHaveLength(1);
});
});
});
});
138 changes: 138 additions & 0 deletions __tests__/push/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ 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';

describe('Push', () => {
let server: Server;
Expand Down Expand Up @@ -353,4 +354,141 @@ heartbeat.monitors:
expect(output).toMatchSnapshot();
});
});
describe('Proxy options', () => {
let requests: Array<any> = [];
let proxyServer;
let tlsServer;

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

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

beforeEach(() => {
requests = [];
delete process.env.HTTP_PROXY,
process.env.HTTP_PROXYS,
process.env.NO_PROXY,
process.env.NODE_TLS_REJECT_UNAUTHORIZED;
});

it('enables proxy based on HTTP_PROXY', async () => {
process.env.HTTP_PROXY = 'http://localhost:8019';
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',
});
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 () => {
process.env.HTTP_PROXY = 'http://localhost:8019';
process.env.NO_PROXY = '*';
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',
});
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 () => {
process.env.HTTPS_PROXY = 'http://localhost:8019';
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
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',
});
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 } },
{ 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:8019',
],
{
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);
});
});
});
56 changes: 48 additions & 8 deletions __tests__/utils/kibana-test-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,40 @@ import {
PutResponse,
} from '../../src/push/kibana_api';

export const createKibanaTestServer = async (kibanaVersion: string) => {
const server = await Server.create({ port: 0 });
server.route('/s/dummy/api/status', (req, res) =>
res.end(JSON.stringify({ version: { number: kibanaVersion } }))
);
server.route('/s/dummy/api/stats', (req, res) =>
res.end(JSON.stringify({ kibana: { version: kibanaVersion } }))
);
export const createKibanaTestServer = async (
kibanaVersion: string,
tls?: boolean,
reqCallback?: any
) => {
const server = await Server.create({ port: 0, tls });
server.route('/s/dummy/api/status', (req, res) => {
(
reqCallback ??
(() => {
return;
})
)(req);
res.end(JSON.stringify({ version: { number: kibanaVersion } }));
});
server.route('/s/dummy/api/stats', (req, res) => {
(
reqCallback ??
(() => {
return;
})
)(req);
res.end(JSON.stringify({ kibana: { version: kibanaVersion } }));
});
// Legacy
server.route(
'/s/dummy/api/synthetics/service/project/monitors',
async (req, res) => {
(
reqCallback ??
(() => {
return;
})
)(req);
await new Promise(r => setTimeout(r, 20));
req.on('data', chunks => {
const schema = JSON.parse(chunks.toString()) as LegacyAPISchema;
Expand All @@ -66,6 +88,12 @@ export const createKibanaTestServer = async (kibanaVersion: string) => {
// Post 8.6
const basePath = '/s/dummy/api/synthetics/project/test-project/monitors';
server.route(basePath, (req, res) => {
(
reqCallback ??
(() => {
return;
})
)(req);
const getResp = {
total: 2,
monitors: [
Expand All @@ -76,6 +104,12 @@ export const createKibanaTestServer = async (kibanaVersion: string) => {
res.end(JSON.stringify(getResp));
});
server.route(basePath + '/_bulk_update', (req, res) => {
(
reqCallback ??
(() => {
return;
})
)(req);
const updateResponse = {
createdMonitors: ['j1', 'j2'],
updatedMonitors: [],
Expand All @@ -84,6 +118,12 @@ export const createKibanaTestServer = async (kibanaVersion: string) => {
res.end(JSON.stringify(updateResponse));
});
server.route(basePath + '/_bulk_delete', (req, res) => {
(
reqCallback ??
(() => {
return;
})
)(req);
res.end(JSON.stringify({ deleted_monitors: ['j3', 'j4'] }));
});
return server;
Expand Down
Loading
Loading