Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/apocas/docker-modem
Browse files Browse the repository at this point in the history
  • Loading branch information
apocas committed Apr 26, 2020
2 parents b4dd187 + c0eee3e commit 12ceaef
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
9 changes: 7 additions & 2 deletions lib/modem.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ var defaultOpts = function () {
var Modem = function (options) {
var opts = Object.assign({}, defaultOpts(), options);

this.socketPath = opts.socketPath;
this.host = opts.host;

if(!this.host) {
this.socketPath = opts.socketPath;
}

this.port = opts.port;
this.username = opts.username;
this.password = opts.password;
Expand All @@ -84,6 +88,7 @@ var Modem = function (options) {
this.connectionTimeout = opts.connectionTimeout;
this.checkServerIdentity = opts.checkServerIdentity;
this.agent = opts.agent;
this.agentForward = opts.agentForward;
this.headers = opts.headers || {};
this.sshAuthAgent = opts.sshAuthAgent;

Expand Down Expand Up @@ -207,7 +212,7 @@ Modem.prototype.buildRequest = function (options, context, data, callback) {
var connectionTimeoutTimer;

var opts = self.protocol === 'ssh' ? Object.assign(options, {
agent: ssh({ 'host': self.host, 'port': self.port, 'username': self.username, 'password': self.password, 'agent': self.sshAuthAgent }),
agent: ssh({ 'host': self.host, 'port': self.port, 'username': self.username, 'password': self.password, 'agent': self.sshAuthAgent, 'agentForward': self.agentForward }),
protocol: 'http:'
}) : options;

Expand Down
40 changes: 25 additions & 15 deletions test/modem_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@ var http = require('http');
var Modem = require('../lib/modem');
var defaultSocketPath = require('os').type() === 'Windows_NT' ? '//./pipe/docker_engine' : '/var/run/docker.sock';

describe('Modem', function() {
beforeEach(function() {
describe('Modem', function () {
beforeEach(function () {
delete process.env.DOCKER_HOST;
});

it('should default to default socket path', function() {
it('should default to default socket path', function () {
var modem = new Modem();
assert.ok(modem.socketPath);
assert.strictEqual(modem.socketPath, defaultSocketPath);
});

it('should use default value if argument not defined in constructor parameter object', function() {
it('shouldn\'t default to default socket path', function () {
var modem = new Modem({
protocol: 'http',
host: '127.0.0.1',
port: 2375
});
assert.ok(modem.host);
assert.strictEqual(modem.socketPath, undefined);
});

it('should use default value if argument not defined in constructor parameter object', function () {
var customHeaders = {
host: 'my-custom-host'
};
Expand All @@ -27,23 +37,23 @@ describe('Modem', function() {
assert.strictEqual(modem.headers, customHeaders);
});

it('should allow DOCKER_HOST=unix:///path/to/docker.sock', function() {
it('should allow DOCKER_HOST=unix:///path/to/docker.sock', function () {
process.env.DOCKER_HOST = 'unix:///tmp/docker.sock';

var modem = new Modem();
assert.ok(modem.socketPath);
assert.strictEqual(modem.socketPath, '/tmp/docker.sock');
});

it('should interpret DOCKER_HOST=unix:// as /var/run/docker.sock', function() {
it('should interpret DOCKER_HOST=unix:// as /var/run/docker.sock', function () {
process.env.DOCKER_HOST = 'unix://';

var modem = new Modem();
assert.ok(modem.socketPath);
assert.strictEqual(modem.socketPath, '/var/run/docker.sock');
});

it('should interpret DOCKER_HOST=tcp://N.N.N.N:2376 as https', function() {
it('should interpret DOCKER_HOST=tcp://N.N.N.N:2376 as https', function () {
process.env.DOCKER_HOST = 'tcp://192.168.59.103:2376';

var modem = new Modem();
Expand All @@ -55,7 +65,7 @@ describe('Modem', function() {
assert.strictEqual(modem.protocol, 'https');
});

it('should interpret DOCKER_HOST=tcp://[::1]:12345', function() {
it('should interpret DOCKER_HOST=tcp://[::1]:12345', function () {
process.env.DOCKER_HOST = 'tcp://[::1]:12345';

var modem = new Modem();
Expand All @@ -67,7 +77,7 @@ describe('Modem', function() {
assert.strictEqual(modem.protocol, 'http');
});

it('should interpret DOCKER_HOST=tcp://N.N.N.N:5555 as http', function() {
it('should interpret DOCKER_HOST=tcp://N.N.N.N:5555 as http', function () {
delete process.env.DOCKER_TLS_VERIFY;
process.env.DOCKER_HOST = 'tcp://192.168.59.105:5555';

Expand All @@ -80,7 +90,7 @@ describe('Modem', function() {
assert.strictEqual(modem.protocol, 'http');
});

it('should interpret DOCKER_HOST=tcp://N.N.N.N:5555 as http', function() {
it('should interpret DOCKER_HOST=tcp://N.N.N.N:5555 as http', function () {
process.env.DOCKER_TLS_VERIFY = '1';
process.env.DOCKER_HOST = 'tcp://192.168.59.105:5555';

Expand All @@ -93,7 +103,7 @@ describe('Modem', function() {
assert.strictEqual(modem.protocol, 'https');
});

it('should accept DOCKER_HOST=N.N.N.N:5555 as http', function() {
it('should accept DOCKER_HOST=N.N.N.N:5555 as http', function () {
delete process.env.DOCKER_TLS_VERIFY;
process.env.DOCKER_HOST = '192.168.59.105:5555';

Expand All @@ -106,7 +116,7 @@ describe('Modem', function() {
assert.strictEqual(modem.protocol, 'http');
});

it('should auto encode querystring option maps as JSON', function() {
it('should auto encode querystring option maps as JSON', function () {
var modem = new Modem();

var opts = {
Expand All @@ -121,20 +131,20 @@ describe('Modem', function() {
assert.strictEqual(decodeURI(qs), control);
});

it('should parse DOCKER_CLIENT_TIMEOUT from environment', function() {
it('should parse DOCKER_CLIENT_TIMEOUT from environment', function () {
process.env.DOCKER_HOST = '192.168.59.105:5555';
process.env.DOCKER_CLIENT_TIMEOUT = 3000;

var modem = new Modem();
assert.strictEqual(modem.timeout, 3000);
});

it('should use default http agent when no agent is specified', function() {
it('should use default http agent when no agent is specified', function () {
var modem = new Modem();
assert.strictEqual(typeof modem.agent, 'undefined');
});

it('should use custom http agent', function() {
it('should use custom http agent', function () {
var httpAgent = new http.Agent({
keepAlive: true
});
Expand Down

0 comments on commit 12ceaef

Please sign in to comment.