containerized
Advanced tools
Comparing version 1.0.1 to 1.0.2
17
index.js
@@ -7,4 +7,3 @@ var containerized = null, | ||
module.exports = function(callback) { | ||
var returnVal, | ||
err, | ||
var err, | ||
cgroups = ''; | ||
@@ -21,13 +20,13 @@ | ||
try { | ||
cgroups = child_process.execSync(cmd); | ||
cgroups = child_process.execSync(cmd()); | ||
} catch (e) { | ||
err = e; | ||
} | ||
returnVal = determine(cgroups.toString('utf8')); | ||
callback(err, returnVal); | ||
return returnVal; | ||
containerized = !!determine(cgroups.toString('utf8')); | ||
callback(err, containerized); | ||
return containerized; | ||
} else { | ||
// async (node <= 0.10.x) | ||
child_process.exec(cmd, function(err, data) { | ||
containerized = determine(data); | ||
child_process.exec(cmd(), function(err, data) { | ||
containerized = !!determine(data); | ||
callback(err, containerized); | ||
@@ -40,3 +39,3 @@ }); | ||
// already determined. use cache. | ||
callback(null, containerized) | ||
callback(null, containerized); | ||
return containerized; | ||
@@ -43,0 +42,0 @@ } |
@@ -1,1 +0,3 @@ | ||
module.exports = 'cat /proc/self/cgroup'; | ||
module.exports = function() { | ||
return 'cat /proc/self/cgroup'; | ||
}; |
{ | ||
"name": "containerized", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A microlibrary to detect whether Node.js runs inside a Docker container or not", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha test" | ||
"test": "mocha test", | ||
"test-travis": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -R spec ./test/*" | ||
}, | ||
@@ -27,5 +28,9 @@ "repository": { | ||
"assert": "^1.3.0", | ||
"coveralls": "^2.11.9", | ||
"istanbul": "^0.4.3", | ||
"lodash": "^4.13.1", | ||
"mocha": "^2.3.3", | ||
"proxyquire": "^1.7.2" | ||
"proxyquire": "^1.7.2", | ||
"sinon": "^1.17.4" | ||
} | ||
} |
@@ -1,6 +0,10 @@ | ||
[![npm version](https://badge.fury.io/js/containerized.svg)](http://badge.fury.io/js/containerized) [![Build Status](https://travis-ci.org/pipedrive/containerized.svg?branch=master)](https://travis-ci.org/pipedrive/containerized) | ||
[![npm version](https://badge.fury.io/js/containerized.svg)](http://badge.fury.io/js/containerized) [![Build Status](https://travis-ci.org/pipedrive/containerized.svg?branch=master)](https://travis-ci.org/pipedrive/containerized) [![Coverage Status](https://coveralls.io/repos/github/pipedrive/containerized/badge.svg?branch=master)](https://coveralls.io/github/pipedrive/containerized?branch=master) | ||
# Containerized | ||
Detect whether your Node.js process is containerized — e.g. whether it runs inside a Docker container. | ||
* **Detect whether your Node.js process runs inside a Docker container or not** | ||
* Detection is based on existence of Docker-specific cgroups | ||
* Well tested (aiming at 100% line, function, statement and branch coverage) | ||
* Works with all Node.js versions >= 0.10.x. | ||
* Tested in 0.10.x, 4.x, 5.x, 6.x. | ||
@@ -13,4 +17,16 @@ ## Usage | ||
### In node <= v0.10.x | ||
### Node.js versions 0.12, 4.x, 5.x, 6.x, ... | ||
```javascript | ||
var containerized = require('containerized'); | ||
if (containerized()) { | ||
console.log('I am running inside a Docker container.'); | ||
} else { | ||
console.log('I am NOT running inside a Docker container.'); | ||
} | ||
``` | ||
### In Node.js 0.10.x | ||
Up until node 0.10.x, containerized offers only async way of fetching whether the process is containerized in a container or not. | ||
@@ -30,3 +46,3 @@ | ||
For achieving a synchronous interface in Node <= 0.10, use [deasync](https://www.npmjs.com/package/deasync) module: | ||
For synchronous interface in Node <= 0.10, wrap it in [deasync](https://www.npmjs.com/package/deasync) module: | ||
```javascript | ||
@@ -36,21 +52,9 @@ var deasync = require('deasync'); | ||
// then you can: | ||
if (containerized()) { | ||
... | ||
console.log('I am running inside a Docker container.'); | ||
} | ||
``` | ||
### In node >= v0.12.x | ||
Starting from node 0.12.x, where child_process.execSync is available, containerized offers synchronous interface, so it will be a lot easier to use. | ||
```javascript | ||
var containerized = require('containerized'); | ||
if (containerized()) { | ||
console.log('I am running inside a Docker container.'); | ||
} else { | ||
console.log('I am NOT running inside a Docker container.'); | ||
} | ||
``` | ||
## Licence | ||
@@ -57,0 +61,0 @@ |
var assert = require('assert'), | ||
sinon = require('sinon'), | ||
proxyquire = require('proxyquire'), | ||
child_process = require('child_process'), | ||
_ = require('lodash'), | ||
hostname = 'a9f22af02012', | ||
mockContainer = { | ||
'./lib/cmd': 'echo "10:net_prio:/docker/a9f22af020125424921a9dac4d8ab8681f7d7866da86d51e1fd97db857a51d1c\n9:perf_event:/docker/a9f22af020125424921a9dac4d8ab8681f7d7866da86d51e1fd97db857a51d1c"', | ||
'./lib/hostname': function() { return 'a9f22af02012'; } | ||
cgroups: '10:net_prio:/docker/a9f22af020125424921a9dac4d8ab8681f7d7866da86d51e1fd97db857a51d1c\n9:perf_event:/docker/a9f22af020125424921a9dac4d8ab8681f7d7866da86d51e1fd97db857a51d1c', | ||
}, | ||
mockNoContainer = { | ||
'./lib/cmd': 'echo "10:net_prio:/\n9:perf_event:/"', | ||
'./lib/hostname': function() { return 'a9f22af02012'; } | ||
cgroups: '10:net_prio:/\n9:perf_event:/' | ||
}; | ||
describe('containerized', function() { | ||
var mockContainerAsync = _.extend({}, mockContainer, { | ||
child_process: { | ||
execSync: undefined | ||
}, | ||
'./lib/cmd': function() { | ||
return 'echo "' + mockContainer.cgroups + '"'; | ||
}, | ||
'./lib/hostname': function() { | ||
return hostname; | ||
} | ||
}); | ||
if (child_process.execSync) { | ||
var mockNoContainerAsync = _.extend({}, mockNoContainer, { | ||
child_process: { | ||
execSync: undefined | ||
}, | ||
'./lib/cmd': function() { | ||
return 'echo "' + mockNoContainer.cgroups + '"'; | ||
}, | ||
'./lib/hostname': function() { | ||
return hostname; | ||
} | ||
}); | ||
it('should detect sync whether it runs inside a Docker container', function() { | ||
var containerized = proxyquire('../index.js', mockContainer); | ||
assert.equal(containerized(), true); | ||
var mockContainerSync = _.extend({}, mockContainer, { | ||
child_process: { | ||
execSync: function() { | ||
return mockContainer.cgroups; | ||
} | ||
}, | ||
'./lib/hostname': function() { | ||
return hostname; | ||
} | ||
}); | ||
var mockNoContainerSyncForError = _.extend({}, mockNoContainer, { | ||
child_process: { | ||
execSync: function() { | ||
throw new Error('xxx'); | ||
} | ||
}, | ||
'./lib/hostname': function() { | ||
return hostname; | ||
} | ||
}); | ||
var mockNoContainerSync = _.extend({}, mockNoContainer, { | ||
child_process: { | ||
execSync: function() { | ||
return mockNoContainer.cgroups; | ||
} | ||
}, | ||
'./lib/hostname': function() { | ||
return hostname; | ||
} | ||
}); | ||
var mockNoContainerSyncWithSpy = _.extend({}, mockNoContainerSync, { | ||
child_process: { | ||
execSync: sinon.spy(mockNoContainerSync.child_process.execSync) | ||
} | ||
}); | ||
var mockNoContainerAsyncWithSpy = _.extend({}, mockNoContainerAsync, { | ||
'./lib/cmd': sinon.spy(mockNoContainerAsync['./lib/cmd']) | ||
}); | ||
describe('containerized lib', function() { | ||
it('should detect sync whether it runs inside a Docker container', function() { | ||
var containerized = proxyquire('../index.js', mockContainerSync); | ||
assert.equal(containerized(), true); | ||
}); | ||
it('should detect sync whether it does not run inside a Docker container', function() { | ||
var containerized = proxyquire('../index.js', mockNoContainerSync); | ||
assert.equal(containerized(), false); | ||
}); | ||
it('should handle errors in sync mode', function() { | ||
var containerized = proxyquire('../index.js', mockNoContainerSyncForError); | ||
containerized(); | ||
}); | ||
it('should detect async whether it runs inside a Docker container', function(done) { | ||
var containerized = proxyquire('../index.js', mockContainerAsync); | ||
containerized(function(err, result) { | ||
assert.equal(err, null); | ||
assert.equal(result, true); | ||
done(); | ||
}); | ||
}); | ||
it('should detect sync whether it does not run inside a Docker container', function() { | ||
var containerized = proxyquire('../index.js', mockNoContainer); | ||
assert.equal(containerized(), false); | ||
it('should detect async whether it does not run inside a Docker container', function(done) { | ||
var containerized = proxyquire('../index.js', mockNoContainerAsync); | ||
containerized(function(err, result) { | ||
assert.equal(err, null); | ||
assert.equal(result, false); | ||
done(); | ||
}); | ||
}); | ||
} else { | ||
it('should utilize cache after first call in sync mode', function() { | ||
var containerized = proxyquire('../index.js', mockNoContainerSyncWithSpy); | ||
assert.equal(containerized(), false); | ||
assert.equal(containerized(), false); | ||
assert.equal(containerized(), false); | ||
assert.equal(mockNoContainerSyncWithSpy.child_process.execSync.callCount, 1); | ||
}); | ||
it('should detect async whether it runs inside a Docker container', function(done) { | ||
var containerized = proxyquire('../index.js', mockContainer); | ||
it('should utilize cache after first call in async mode', function(done) { | ||
var containerized = proxyquire('../index.js', mockNoContainerAsyncWithSpy); | ||
containerized(function(err, result) { | ||
assert.equal(err, null); | ||
assert.equal(result, false); | ||
containerized(function(err, result) { | ||
assert.equal(err, null); | ||
assert.equal(result, true); | ||
assert.equal(result, false); | ||
assert.equal(mockNoContainerAsyncWithSpy['./lib/cmd'].callCount, 1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should detect async whether it does not run inside a Docker container', function(done) { | ||
var containerized = proxyquire('../index.js', mockNoContainer); | ||
}); | ||
containerized(function(err, result) { | ||
assert.equal(err, null); | ||
assert.equal(result, false); | ||
done(); | ||
}); | ||
}); | ||
} | ||
}); | ||
describe('lib/cmd.js', function() { | ||
it('should cat cgroups', function() { | ||
var cmd = require(__dirname + '/../lib/cmd'); | ||
assert.equal(cmd(), 'cat /proc/self/cgroup'); | ||
}); | ||
}); | ||
describe('lib/hostname.js', function() { | ||
it('should return os.hostname()', function() { | ||
var hostnameLib = require(__dirname + '/../lib/hostname'); | ||
assert.equal(hostnameLib(), require('os').hostname()); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
10346
178
64
7
2