Skip to content

Commit

Permalink
move hot reload to e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMegavolt committed Aug 16, 2017
1 parent cd6ffcd commit 3992caa
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 126 deletions.
34 changes: 2 additions & 32 deletions test/common/gateway.helper.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const net = require('net');
const yaml = require('js-yaml');
const fs = require('fs');
const { fork } = require('child_process');
const path = require('path');
const request = require('superagent');
const { generateBackendServer } =
const { generateBackendServer, findOpenPortNumbers } =
require('../common/server-helper');
let gatewayPort = null;
let adminPort = null;
Expand All @@ -21,37 +20,8 @@ module.exports.getYmlConfig = function ({ymlConfigPath}) {
return yaml.load(content);
};

module.exports.findOpenPortNumbers = function (count = 1) {
let completeCount = 0;
const ports = [];
return new Promise((resolve, reject) => {
for (let i = 0; i < count; i++) {
const server = net.createServer();

server.listen(0);

server.on('listening', () => {
ports.push(server.address().port);

server.once('close', () => {
completeCount++;

if (completeCount === count) {
resolve(ports);
}
});
server.close();
});

server.on('error', (err) => {
reject(err);
});
}
});
};

module.exports.startGatewayInstance = function ({dirInfo, gatewayConfig}) {
return this.findOpenPortNumbers(4)
return findOpenPortNumbers(4)
.then(ports => {
gatewayPort = ports[0];
backendPort = ports[1];
Expand Down
37 changes: 19 additions & 18 deletions test/common/server-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,33 @@ const generateBackendServer = port => {
});
};

const findOpenPortNumbers = (count, cb) => {
const findOpenPortNumbers = function (count = 1) {
let completeCount = 0;
const ports = [];
return new Promise((resolve, reject) => {
for (let i = 0; i < count; i++) {
const server = net.createServer();

for (let i = 0; i < count; i++) {
const server = net.createServer();
server.listen(0);

server.listen(0);
server.on('listening', () => {
ports.push(server.address().port);

server.on('listening', () => {
ports.push(server.address().port);
server.once('close', () => {
completeCount++;

server.once('close', () => {
completeCount++;

if (completeCount === count) {
cb(null, ports);
}
if (completeCount === count) {
resolve(ports);
}
});
server.close();
});
server.close();
});

server.on('error', (err) => {
cb(err);
});
}
server.on('error', (err) => {
reject(err);
});
}
});
};

module.exports = {
Expand Down
10 changes: 5 additions & 5 deletions test/hot-reload.test.js → test/e2e/hot-reload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const rimraf = require('rimraf');
const tmp = require('tmp');
const yaml = require('js-yaml');

const { findOpenPortNumbers } = require('./common/server-helper');
const { findOpenPortNumbers } = require('../common/server-helper');

/*
1) Copy config to a temp directory.
Expand All @@ -22,7 +22,7 @@ const { findOpenPortNumbers } = require('./common/server-helper');
7) Clean up the temp directory.
*/

const baseConfigDirectory = path.join(__dirname, 'fixtures', 'hot-reload');
const baseConfigDirectory = path.join(__dirname, '..', 'fixtures', 'hot-reload');

describe('hot-reload', () => {
describe('gateway config', () => {
Expand All @@ -45,7 +45,7 @@ describe('hot-reload', () => {

testGatewayConfigPath = path.join(tempPath, 'gateway.config.yml');

findOpenPortNumbers(3, (err, ports) => {
findOpenPortNumbers(3).then(ports => {
if (err) {
throw err;
}
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('hot-reload', () => {
// Need to remove this paramter in the child process.
delete childEnv.EG_DISABLE_CONFIG_WATCH;

const modulePath = path.join(__dirname, '..', 'lib', 'index.js');
const modulePath = path.join(__dirname, '../..', 'lib', 'index.js');
childProcess = fork(modulePath, [], {
cwd: tempPath,
env: childEnv
Expand All @@ -99,7 +99,7 @@ describe('hot-reload', () => {
}, 5000);
});
});
});
}).catch(err => done(err));
});
});
});
Expand Down
56 changes: 20 additions & 36 deletions test/e2e/oauth2-authorization-code.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { exec, fork } = require('child_process');
const { fork } = require('child_process');
const {runCLICommand} = require('../common/cli.helper');
const fs = require('fs');
const path = require('path');
const url = require('url');
Expand All @@ -13,6 +14,7 @@ const rimraf = require('rimraf');
const tmp = require('tmp');
const webdriver = require('selenium-webdriver');
const yaml = require('js-yaml');
let tempPath = null;

require('util.promisify/shim')();

Expand Down Expand Up @@ -171,16 +173,14 @@ describe('oauth2 authorization code grant type', () => {
});

function startGatewayInstance (done) {
let tempPath = null;

return util.promisify(tmp.dir)()
.then(temp => {
tempPath = temp;
return util.promisify(cpr)(baseConfigDirectory, tempPath);
})
.then(files => {
testGatewayConfigPath = path.join(tempPath, 'gateway.config.yml');
return util.promisify(findOpenPortNumbers)(4);
return findOpenPortNumbers(4);
})
.then(ports => {
gatewayPort = ports[0];
Expand Down Expand Up @@ -235,49 +235,33 @@ describe('oauth2 authorization code grant type', () => {
assert(res.statusCode, 404);
resolve();
});
}, 2000);
}, 4000);
});
});
}

function runCLICommand (args) {
return new Promise((resolve, reject) => {
const childEnv = Object.assign({}, process.env);
childEnv.EG_CONFIG_DIR = path.join(testGatewayConfigPath, '..');

const modulePath = path.join(__dirname, '..', '..', 'bin', 'index.js');

const command = [process.argv[0], modulePath].concat(args).join(' ');
exec(command, { env: childEnv }, (err, stdout) => {
if (err) {
reject(err);
return;
}

try {
const obj = JSON.parse(stdout);
resolve(obj);
} catch (err) {
if (err instanceof SyntaxError) {
resolve(stdout);
} else {
reject(err);
}
}
});
});
}

function createUser (args, done) {
return runCLICommand(['users', 'create'].concat(args));
return runCLICommand({
cliArgs: ['users', 'create'].concat(args),
adminPort,
configDirectoryPath: tempPath
});
}

function createCredential (args, done) {
return runCLICommand(['credentials', 'create'].concat(args));
return runCLICommand({
cliArgs: ['credentials', 'create'].concat(args),
adminPort,
configDirectoryPath: tempPath
});
}

function createApp (args, done) {
return runCLICommand(['apps', 'create'].concat(args));
return runCLICommand({
cliArgs: ['apps', 'create'].concat(args),
adminPort,
configDirectoryPath: tempPath
});
}

function generateRedirectServer (port) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let testHelper = require('../common/routing.helper');
let config = require('../../lib/config');
let originalGatewayConfig = config.gatewayConfig;

describe('End to End tests with oauth2, proxy, log, expression, rate-limit policies', () => {
describe('E2E: oauth2, proxy, log, expression, rate-limit policies', () => {
let helper = testHelper();
let spy = sinon.spy();
let originalAppConfig, originalCredentialConfig, originalUserConfig;
Expand Down
8 changes: 2 additions & 6 deletions test/e2e/round-robin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ describe('round-robin load balancing', () => {

testGatewayConfigPath = path.join(tempPath, 'gateway.config.yml');

findOpenPortNumbers(4, (err, ports) => {
if (err) {
throw err;
}

findOpenPortNumbers(4).then(ports => {
fs.readFile(testGatewayConfigPath, (err, configData) => {
if (err) {
throw err;
Expand Down Expand Up @@ -100,7 +96,7 @@ describe('round-robin load balancing', () => {
});
});
});
});
}).catch(err => done(err));
});
});
});
Expand Down
40 changes: 12 additions & 28 deletions test/policies/basic-auth-policy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,52 +132,36 @@ describe('Functional Tests basic auth Policy', () => {
done();
});

it('should not authenticate token for requests without token header', function (done) {
request(app)
it('should not authenticate token for requests without token header', function () {
return request(app)
.get('/authorizedPath')
.expect(401)
.end(function (err) {
should.not.exist(err);
done();
});
.expect(401);
});

it('should not authenticate token for requests if requester doesn\'t have authorized scopes', function (done) {
it('should not authenticate token for requests if requester doesn\'t have authorized scopes', function () {
let credentials = Buffer.from(user.id.concat(':user-secret')).toString('base64');

request(app)
return request(app)
.get('/unauthorizedPath')
.set('Authorization', 'basic ' + credentials)
.expect(401)
.end(function (err) {
should.not.exist(err);
done();
});
.expect(401);
});

it('should authenticate token for requests with scopes if requester is authorized', function (done) {
it('should authenticate token for requests with scopes if requester is authorized', function () {
let credentials = Buffer.from(user.username.concat(':user-secret')).toString('base64');

request(app)
return request(app)
.get('/authorizedPath')
.set('Authorization', 'basic ' + credentials)
.expect(200)
.end(function (err) {
should.not.exist(err);
done();
});
.expect(200);
});

it('should not authenticate invalid token', function (done) {
it('should not authenticate invalid token', function () {
let credentials = Buffer.from(user.id.concat(':wrongPassword')).toString('base64');

request(app)
return request(app)
.get('/authorizedPath')
.set('Authorization', 'basic ' + credentials)
.expect(401)
.end(function (err) {
should.not.exist(err);
done();
});
.expect(401);
});
});

0 comments on commit 3992caa

Please sign in to comment.