Browse files

#4 Add proxy support via http_proxy environment variable.

  • Loading branch information...
1 parent 3894ff9 commit f9a5f998079584a2df94b430880c2ddb3cd62b2f @cliffano committed Jul 29, 2012
Showing with 88 additions and 23 deletions.
  1. +1 −1 CHANGELOG.md
  2. +2 −0 README.md
  3. +10 −8 lib/cli.js
  4. +20 −10 lib/jenkins.js
  5. +19 −3 test/cli.js
  6. +36 −1 test/jenkins.js
View
2 CHANGELOG.md
@@ -1,5 +1,5 @@
### 0.0.9 (SNAPSHOT)
-*
+* Add proxy support
### 0.0.8
* Fix unexpected status code 405 on parameterised build
View
2 README.md
@@ -57,3 +57,5 @@ Set Jenkins URL in JENKINS_URL environment variable (defaults to http://localhos
(Windows)
set JENKINS_URL=http://user:pass@host:port/path
+
+If http_proxy environment variable is set, then Nestor will automatically use it.
View
18 lib/cli.js
@@ -4,14 +4,16 @@ var _ = require('underscore'),
/**
* Execute nestor using JENKINS_URL environment variable.
+ * Nestor uses http_proxy environment variable when it's available.
*/
function exec() {
- var url = process.env.JENKINS_URL;
+ var url = process.env.JENKINS_URL,
+ proxy = process.env.http_proxy;
function _build() {
return function (jobName, params) {
- new jenkins(url).build(
+ new jenkins(url, proxy).build(
jobName,
(_.isString(params)) ? params : undefined,
bag.cli.exitCb(null, function (result) {
@@ -23,7 +25,7 @@ function exec() {
function _dashboard() {
return function () {
- new jenkins(url).dashboard(bag.cli.exitCb(null, function (result) {
+ new jenkins(url, proxy).dashboard(bag.cli.exitCb(null, function (result) {
if (result.length === 0) {
console.log('Jobless Jenkins');
} else {
@@ -37,15 +39,15 @@ function exec() {
function _discover() {
return function (host) {
- new jenkins(url).discover(host, bag.cli.exitCb(null, function (result) {
+ new jenkins(url, proxy).discover(host, bag.cli.exitCb(null, function (result) {
console.log('Jenkins ver. %s is running on %s', result.version, result.url);
}));
};
}
function _executor() {
return function () {
- new jenkins(url).executor(bag.cli.exitCb(null, function (result) {
+ new jenkins(url, proxy).executor(bag.cli.exitCb(null, function (result) {
if (!_.isEmpty(_.keys(result))) {
_.keys(result).forEach(function (computer) {
console.log('+ ' + computer);
@@ -66,7 +68,7 @@ function exec() {
function _job() {
return function (name) {
- new jenkins(url).job(name, bag.cli.exitCb(null, function (result) {
+ new jenkins(url, proxy).job(name, bag.cli.exitCb(null, function (result) {
console.log('%s | %s', name, result.status);
result.reports.forEach(function (report) {
console.log(' - %s', report);
@@ -77,7 +79,7 @@ function exec() {
function _queue() {
return function () {
- new jenkins(url).queue(bag.cli.exitCb(null, function (result) {
+ new jenkins(url, proxy).queue(bag.cli.exitCb(null, function (result) {
if (result.length === 0) {
console.log('Queue is empty');
} else {
@@ -91,7 +93,7 @@ function exec() {
function _version() {
return function () {
- new jenkins(url).version(bag.cli.exitCb(null, function (result) {
+ new jenkins(url, proxy).version(bag.cli.exitCb(null, function (result) {
console.log('Jenkins ver. %s', result);
}));
};
View
30 lib/jenkins.js
@@ -11,16 +11,24 @@ var _ = require('underscore'),
*
* @param {String} method: http method
* @param {String} url: Jenkins URL without query string
+ * @param {String} proxy: proxy server URL in format http://user:pass@host:port
* @param {Object} qs: object containing URL query strings with format { name: value }
* @param {Object} handlers: response handlers with format { statuscode: handlerfunction }
* @param {Function} cb: standard cb(err, result) callback
*/
-function _http(method, url, queryStrings, handlers, cb) {
- request({
+function _http(method, url, proxy, queryStrings, handlers, cb) {
+
+ var params = {
method: method,
uri: url,
qs: queryStrings
- }, function (err, result) {
+ };
+
+ if (proxy) {
+ params.proxy = proxy;
+ }
+
+ request(params, function (err, result) {
if (err) {
cb(err);
} else if (result.statusCode === 401) {
@@ -97,9 +105,11 @@ function _status(color) {
* class Jenkins
*
* @param {String} url: Jenkins URL
+ * @param {String} proxy: proxy server URL in format http://user:pass@host:port
*/
-function Jenkins (url) {
+function Jenkins (url, proxy) {
this.url = url || 'http://localhost:8080';
+ this.proxy = proxy;
}
/**
@@ -134,7 +144,7 @@ Jenkins.prototype.build = function (jobName, params, cb) {
method = 'post';
}
- _http(method, this.url + '/job/' + jobName + '/build', { token: 'nestor', json: JSON.stringify(json) }, { 200: _success, 404: _notFound, 405: _notAllowed }, cb);
+ _http(method, this.url + '/job/' + jobName + '/build', this.proxy, { token: 'nestor', json: JSON.stringify(json) }, { 200: _success, 404: _notFound, 405: _notAllowed }, cb);
};
/**
@@ -157,7 +167,7 @@ Jenkins.prototype.dashboard = function (cb) {
cb(null, data);
}
- _http('get', this.url + '/api/json', {}, { 200: _success }, cb);
+ _http('get', this.url + '/api/json', this.proxy, {}, { 200: _success }, cb);
};
/**
@@ -197,7 +207,7 @@ Jenkins.prototype.executor = function (cb) {
cb(null, data);
}
- _http('get', this.url + '/computer/api/json', { depth: 1 }, { 200: _success }, cb);
+ _http('get', this.url + '/computer/api/json', this.proxy, { depth: 1 }, { 200: _success }, cb);
};
/**
@@ -225,7 +235,7 @@ Jenkins.prototype.job = function (name, cb) {
cb(new Error('Job ' + name + ' does not exist'));
}
- _http('get', this.url + '/job/' + name + '/api/json', {}, { 200: _success, 404: _notFound }, cb);
+ _http('get', this.url + '/job/' + name + '/api/json', this.proxy, {}, { 200: _success, 404: _notFound }, cb);
};
/**
@@ -248,7 +258,7 @@ Jenkins.prototype.queue = function (cb) {
cb(null, data);
}
- _http('get', this.url + '/queue/api/json', {}, { 200: _success }, cb);
+ _http('get', this.url + '/queue/api/json', this.proxy, {}, { 200: _success }, cb);
};
/**
@@ -267,7 +277,7 @@ Jenkins.prototype.version = function (cb) {
}
}
- _http('head', this.url, {}, { 200: _success }, cb);
+ _http('head', this.url, this.proxy, {}, { 200: _success }, cb);
};
module.exports = Jenkins;
View
22 test/cli.js
@@ -32,8 +32,9 @@ describe('cli', function () {
}
}
},
- './jenkins': function (url) {
+ './jenkins': function (url, proxy) {
checks.jenkins_url = url;
+ checks.jenkins_proxy = proxy;
function _cb(cb) {
cb(mocks.jenkins_action_err, mocks.jenkins_action_result);
}
@@ -73,15 +74,19 @@ describe('cli', function () {
mocks = {};
});
- describe('exec', function () {
+ describe('exec with environment variables set', function () {
beforeEach(function () {
- mocks.process_env = { JENKINS_URL: 'http://ci.jenkins-ci.org' };
+ mocks.process_env = {
+ JENKINS_URL: 'http://ci.jenkins-ci.org',
+ http_proxy: 'http://someproxy:8080'
+ };
});
afterEach(function () {
checks.bag_parse_dir.should.equal('/somedir/nestor/lib');
checks.jenkins_url.should.equal('http://ci.jenkins-ci.org');
+ checks.jenkins_proxy.should.equal('http://someproxy:8080');
});
it('should log job started successfully when exec build is called and job exists', function () {
@@ -244,4 +249,15 @@ describe('cli', function () {
checks.console_error_messages[0].should.equal('someerror');
});
});
+
+ describe('exec with environment variables unset', function () {
+
+ it('should not set Jenkins URL and http proxy when environment variables are not set', function () {
+ mocks.process_env = {};
+ cli = create(checks, mocks);
+ cli.exec();
+ should.not.exist(checks.jenkins_url);
+ should.not.exist(checks.jenkins_proxy);
+ });
+ });
});
View
37 test/jenkins.js
@@ -26,9 +26,16 @@ describe('jenkins', function () {
jenkins.url.should.equal('http://localhost:8080');
});
- it('should set URL as specified in constructor', function () {
+ it('should set URL as specified in constructor but not proxy when it is not specified', function () {
jenkins = new (create(checks, mocks))('http://ci.jenkins-ci.org');
jenkins.url.should.equal('http://ci.jenkins-ci.org');
+ should.not.exist(jenkins.proxy);
+ });
+
+ it('should set URL and proxy as specified in constructor', function () {
+ jenkins = new (create(checks, mocks))('http://ci.jenkins-ci.org', 'http://someproxy:8080');
+ jenkins.url.should.equal('http://ci.jenkins-ci.org');
+ jenkins.proxy.should.equal('http://someproxy:8080');
});
it('should pass error to callback when an error occurs while sending request', function (done) {
@@ -87,6 +94,34 @@ describe('jenkins', function () {
err.message.should.equal('Unexpected status code 503 from Jenkins\nResponse body:\nunexpectedbody');
should.not.exist(checks.jenkins_dashboard_cb_args[1]);
});
+
+ it('should pass proxy to http request when specified', function (done) {
+ mocks.request_result = { statusCode: 503, body: 'unexpectedbody' };
+ mocks.requires = {
+ request: bag.mock.request(checks, mocks)
+ };
+ jenkins = new (create(checks, mocks))('http://localhost:8080');
+ jenkins.dashboard(function cb(err, result) {
+ checks.jenkins_dashboard_cb_args = cb['arguments'];
+ done();
+ });
+ checks.request_opts.uri.should.equal('http://localhost:8080/api/json');
+ should.not.exist(checks.request_opts.proxy);
+ });
+
+ it('should pass proxy to http request when specified', function (done) {
+ mocks.request_result = { statusCode: 503, body: 'unexpectedbody' };
+ mocks.requires = {
+ request: bag.mock.request(checks, mocks)
+ };
+ jenkins = new (create(checks, mocks))('http://localhost:8080', 'http://someproxy:8080');
+ jenkins.dashboard(function cb(err, result) {
+ checks.jenkins_dashboard_cb_args = cb['arguments'];
+ done();
+ });
+ checks.request_opts.uri.should.equal('http://localhost:8080/api/json');
+ checks.request_opts.proxy.should.equal('http://someproxy:8080');
+ });
});
describe('build', function () {

0 comments on commit f9a5f99

Please sign in to comment.